Done unit 14 d2
[tm351-notebooks.git] / notebooks / dockeringMongo.py
1 import docker
2
3 #We can run a mongo database server in a container using the following command:
4 #
5 ##sudo docker run \
6 ## -P -name rs1_srv1 \
7 ## -d dev24/mongodb \
8 ## --replSet rs1 \
9 ## --noprealloc --smallfiles
10 #
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)
15
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)
22 return name
23
24 #Some helper functions via https://github.com/docker/docker-py
25
26 #Equivalent of docker ps
27 def docker_ps(c):
28 return c.containers(quiet=False, all=False, trunc=True, latest=False, since=None, before=None, limit=-1)
29
30
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'])
35
36 def get27017tcp_ports(c,containers):
37 ports={}
38 for container in containers:
39 ports[container]= get27017tcp_port(c,container)
40 return ports
41
42
43 def getContainIPaddress(c,container):
44 cConfig = c.inspect_container(container)
45 return cConfig['NetworkSettings']['IPAddress']
46
47 def getContainIPaddresses(c,containers):
48 ipaddresses={}
49 for container in containers:
50 ipaddresses[container]= getContainIPaddress(c,container)
51 return ipaddresses
52
53
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'])
58
59
60 #Helper routines for shutting down and removing containers
61 def tidyAwayContainer(c,container):
62 container=container.strip('/')
63 c.stop(container)
64 c.remove_container(container)
65
66 def tidyAwayContainers(c,containers):
67 for container in containers:
68 tidyAwayContainer(c,container)
69
70
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 '''
74 names=[]
75 for i in range(0,numNodes):
76 name=createReplicaSetNode(c,stub,i)
77 names.append(name)
78 return names
79
80
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)
89 #get IP and port
90 _rs_config['members'].append({"_id":i,"host":'{0}:{1}'.format(getContainIPaddress(c,name),27017)})
91 return _rs_config
92
93
94
95 #Mongo commands
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'])
101 else:
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"))
103 print(txt)
104
105 def memberIPaddress(rs,name):
106 for host in rs['members']:
107 if host['_id']==int(name.split('_srv')[1]):
108 return host['host']