from dockeringMongo import * c = docker.Client(base_url='unix://var/run/docker.sock', version='1.10', timeout=10) #Fire up three MongoDB containers that we'll use in a replica set #STUB is an identifier used to label the nodes #Superstition - short stub required? STUB='rs3' rsc=rs_config(c,STUB,num=3) rsc showContainers(c) #tidyAwayContainers(c,['rs3_srv0','rs3_srv1','rs3_srv2']) docker_ps(c) #Initialise the replica set from pymongo import MongoClient #We'll use the 0th server in the set as a the node mc = MongoClient('localhost', get27017tcp_port(c,STUB+'_srv0'),replicaset=STUB) #Set up connections to the other members of the replica set client1=MongoClient('localhost', get27017tcp_port(c,STUB+'_srv1'),replicaset=STUB) client2=MongoClient('localhost', get27017tcp_port(c,STUB+'_srv2'),replicaset=STUB) #In the mongo console, we would typically use the command MONGOCLIENT.config() to initialise the replica set #Here, we use the replSetInitiate admin command, applying it with the desired configuration mc.admin.command( "replSetInitiate",rsc); #We may need to wait a minute or two for the configuration to come up #If you get an error message that suggests the configuration isn't up yet, wait a few seconds then rerun the cell mc.admin.command('replSetGetStatus') #The statusReport() command lets you ask a mongoDB node what it thinks the state of the world is statusReport(mc) #If we're quick, we can watch the machines come up into the fold statusReport(client1) statusReport(client1) statusReport(client1) #The blockade library contains functions for setting up and tearing down iptable firewall rulesets from blockade import * #As we define blockade rules by IP address, we need to know those addresses rsc #Maybe worth creating an object that lists stuff like this by instance name? memberIPaddress(rsc,'rs3_srv2') #The machines in each partition are put into their own lists #partition_containers(RULESET,[ PARTITION_1_LIST, PARTITION_2_LIST ] partition_containers('test1w2s2', [ [netobj('172.17.0.4')],[netobj('172.17.0.2'),netobj('172.17.0.3')]]) statusReport(mc) statusReport(client1) statusReport(client2) clear_iptables('test1w2s2') #Wait a second or too then see how things are... statusReport(mc) #Put the primary server into a partition on it's own partition_containers('test1w2s2', [ [netobj('172.17.0.2')],[netobj('172.17.0.3'),netobj('172.17.0.4')]]) statusReport(mc) statusReport(client1) statusReport(client2) clear_iptables('test1w2s2') statusReport(mc) statusReport(client1) statusReport(client2) #In an ssh shell, the following sort of command displays a real time stream of stdio log messages from the container #!docker.io logs --follow=true {STUB}_srv1 !docker.io logs {STUB}_srv0 > {STUB}_srv0_log.txt !docker.io logs {STUB}_srv1 > {STUB}_srv1_log.txt !docker.io logs {STUB}_srv2 > {STUB}_srv2_log.txt #?we may also be able to use mongostat - http://docs.mongodb.org/manual/reference/program/mongostat/ !tail -500 {STUB}_srv0_log.txt !tail -500 {STUB}_srv1_log.txt !tail -500 {STUB}_srv2_log.txt #Tidy up #Remove the blockade rules clear_iptables('test1w2s2') #Shut down the containers tidyAwayContainers(c,['rs3_srv0','rs3_srv1','rs3_srv2'])