3 #We can run a mongo database server in a container using the following command:
9 ## --noprealloc --smallfiles
11 #The -P flag is a docker flag, the rest are passed through the mongodb server)
12 #That is, the following command is run to start each mongo server using a configuration to keep it small for now...:
13 # usr/bin/mongod --replSet REPLICA_SET_NAME --noprealloc --smallfiles
14 #Add a -v flag to command to specify verbose stdio logging (increase the number of v's for more... eg -vv or -vvvv)
16 def createReplicaSetNode(c
,stub
,num
=0):
17 ''' Create and run a specified number of mongo database servers as a replica set '''
18 name
='{stub}_srv{num}'.format(stub
=stub
,num
=num
)
19 command
='--replSet {stub} --noprealloc --smallfiles'.format(stub
=stub
)
20 c
.create_container('dev24/mongodb',name
=name
,command
=command
)
21 c
.start(name
,publish_all_ports
=True)
24 #Some helper functions via https://github.com/docker/docker-py
26 #Equivalent of docker ps
28 return c
.containers(quiet
=False, all
=False, trunc
=True, latest
=False, since
=None, before
=None, limit
=-1)
31 #Find the local port bound for 27017/tcp for each server in the replica set
32 def get27017tcp_port(c
,container
):
33 cConfig
= c
.inspect_container(container
)
34 return int(cConfig
['NetworkSettings']['Ports']['27017/tcp'][0]['HostPort'])
36 def get27017tcp_ports(c
,containers
):
38 for container
in containers
:
39 ports
[container
]= get27017tcp_port(c
,container
)
43 def getContainIPaddress(c
,container
):
44 cConfig
= c
.inspect_container(container
)
45 return cConfig
['NetworkSettings']['IPAddress']
47 def getContainIPaddresses(c
,containers
):
49 for container
in containers
:
50 ipaddresses
[container
]= getContainIPaddress(c
,container
)
54 def showContainers(c
):
55 for xc
in c
.containers(quiet
=False, all
=False, trunc
=True, latest
=False, since
=None,
56 before
=None, limit
=-1):
57 print(xc
['Names'],xc
['Status'])
60 #Helper routines for shutting down and removing containers
61 def tidyAwayContainer(c
,container
):
62 container
=container
.strip('/')
64 c
.remove_container(container
)
66 def tidyAwayContainers(c
,containers
):
67 for container
in containers
:
68 tidyAwayContainer(c
,container
)
71 #Let's create a function that will create several nodes
72 def createReplicaSetNodes(c
,stub
,numNodes
):
73 ''' Create and run a specified number of mongo database servers as a replica set '''
75 for i
in range(0,numNodes
):
76 name
=createReplicaSetNode(c
,stub
,i
)
81 def rs_config(c
,rsid
,num
=3):
82 ''' Create a replica set of nodes and then define a configuation file for that replica set '''
83 createReplicaSetNodes(c
,rsid
,num
)
84 _rs_config
={"_id" : rsid
, 'members':[] }
85 #This is scrappy - should really return something better from the creation
86 for i
in range(0,num
):
87 name
='{stub}_srv{num}'.format(stub
=rsid
,num
=i
)
88 #c.inspect_container(name)
90 _rs_config
['members'].append({"_id":i
,"host":'{0}:{1}'.format(getContainIPaddress(c
,name
),27017)})
96 def statusReport(client
):
97 report
=client
.admin
.command("replSetGetStatus")
98 for m
in report
['members']:
99 if 'self' in m
and (m
['self']):
100 txt
="I am machine id {id} as {state}".format(id=m
['_id'],state
=m
['stateStr'])
102 txt
='Machine id {id} ({state}) last heard from at {time}'.format(id=m
['_id'],state
=m
['stateStr'],time
=m
['lastHeartbeat'].strftime("%Y-%m-%d %H:%M:%S"))
105 def memberIPaddress(rs
,name
):
106 for host
in rs
['members']:
107 if host
['_id']==int(name
.split('_srv')[1]):