Skip to content

Instantly share code, notes, and snippets.

@mpobrien
Created June 27, 2012 14:44
Show Gist options
  • Select an option

  • Save mpobrien/3004512 to your computer and use it in GitHub Desktop.

Select an option

Save mpobrien/3004512 to your computer and use it in GitHub Desktop.
mongo cluster spinner-upper
import sys
import subprocess
import clint
import argparse
import random
import string
import os
CLUSTER_TYPES = ["standalone", "replset"]
PATH_TO_WHIRR = "/Users/mike/projects/Whirr-MongoDB/bin/whirr"
PATH_TO_CONF_FILE = "/Users/mike/projects/Whirr-MongoDB/provision.properties"
VERSION_URLS = {"1.8.0" : "linux/mongodb-linux-x86_64-1.8.0.tgz",#{{{
"1.8.0-rc0" : "linux/mongodb-linux-x86_64-1.8.0-rc0.tgz",
"1.8.0-rc1" : "linux/mongodb-linux-x86_64-1.8.0-rc1.tgz",
"1.8.0-rc2" : "linux/mongodb-linux-x86_64-1.8.0-rc2.tgz",
"1.8.1" : "linux/mongodb-linux-x86_64-1.8.1.tgz",
"1.8.1-rc0" : "linux/mongodb-linux-x86_64-1.8.1-rc0.tgz",
"1.8.1-rc1" : "linux/mongodb-linux-x86_64-1.8.1-rc1.tgz",
"1.8.2" : "linux/mongodb-linux-x86_64-1.8.2.tgz",
"1.8.2-rc0" : "linux/mongodb-linux-x86_64-1.8.2-rc0.tgz",
"1.8.2-rc1" : "linux/mongodb-linux-x86_64-1.8.2-rc1.tgz",
"1.8.2-rc2" : "linux/mongodb-linux-x86_64-1.8.2-rc2.tgz",
"1.8.2-rc3" : "linux/mongodb-linux-x86_64-1.8.2-rc3.tgz",
"1.8.3" : "linux/mongodb-linux-x86_64-1.8.3.tgz",
"1.8.3-rc0" : "linux/mongodb-linux-x86_64-1.8.3-rc0.tgz",
"1.8.3-rc1" : "linux/mongodb-linux-x86_64-1.8.3-rc1.tgz",
"1.8.4" : "linux/mongodb-linux-x86_64-1.8.4.tgz",
"1.8.4-rc0" : "linux/mongodb-linux-x86_64-1.8.4-rc0.tgz",
"1.8.5" : "linux/mongodb-linux-x86_64-1.8.5.tgz",
"1.8.5-rc0" : "linux/mongodb-linux-x86_64-1.8.5-rc0.tgz",
"1.9.0" : "linux/mongodb-linux-x86_64-1.9.0.tgz",
"1.9.1" : "linux/mongodb-linux-x86_64-1.9.1.tgz",
"1.9.2" : "linux/mongodb-linux-x86_64-1.9.2.tgz",
"2.0.0" : "linux/mongodb-linux-x86_64-2.0.0.tgz",
"2.0.0-rc0" : "linux/mongodb-linux-x86_64-2.0.0-rc0.tgz",
"2.0.0-rc1" : "linux/mongodb-linux-x86_64-2.0.0-rc1.tgz",
"2.0.0-rc2" : "linux/mongodb-linux-x86_64-2.0.0-rc2.tgz",
"2.0.1" : "linux/mongodb-linux-x86_64-2.0.1.tgz",
"2.0.1-rc0" : "linux/mongodb-linux-x86_64-2.0.1-rc0.tgz",
"2.0.1-rc1" : "linux/mongodb-linux-x86_64-2.0.1-rc1.tgz",
"2.0.2" : "linux/mongodb-linux-x86_64-2.0.2.tgz",
"2.0.2-rc0" : "linux/mongodb-linux-x86_64-2.0.2-rc0.tgz",
"2.0.2-rc1" : "linux/mongodb-linux-x86_64-2.0.2-rc1.tgz",
"2.0.2-rc2" : "linux/mongodb-linux-x86_64-2.0.2-rc2.tgz",
"2.0.3" : "linux/mongodb-linux-x86_64-2.0.3.tgz",
"2.0.3-rc0" : "linux/mongodb-linux-x86_64-2.0.3-rc0.tgz",
"2.0.3-rc1" : "linux/mongodb-linux-x86_64-2.0.3-rc1.tgz",
"2.0.4" : "linux/mongodb-linux-x86_64-2.0.4.tgz",
"2.0.4-rc0" : "linux/mongodb-linux-x86_64-2.0.4-rc0.tgz",
"2.0.4-rc1" : "linux/mongodb-linux-x86_64-2.0.4-rc1.tgz",
"2.0.5" : "linux/mongodb-linux-x86_64-2.0.5.tgz",
"2.0.5-rc0" : "linux/mongodb-linux-x86_64-2.0.5-rc0.tgz",
"2.0.5-rc1" : "linux/mongodb-linux-x86_64-2.0.5-rc1.tgz",
"2.0.6" : "linux/mongodb-linux-x86_64-2.0.6.tgz",
"2.0.6-rc0" : "linux/mongodb-linux-x86_64-2.0.6-rc0.tgz",
"2.1.0" : "linux/mongodb-linux-x86_64-2.1.0.tgz",
"2.1.1" : "linux/mongodb-linux-x86_64-2.1.1.tgz",
"2.1.2" : "linux/mongodb-linux-x86_64-2.1.2.tgz"}
#}}}
def getclusterinfo(instancename):
instancefilepath = os.path.join(os.path.expanduser("~"),
".whirr",
instancename,
"instances")
clusterfile = open(instancefilepath, 'r')
clusterinfolines = clusterfile.readlines()
clusterfile.close()
clusterinfo = {}
for line in clusterinfolines:
line = line.strip()
instance_id, roles, hostname, private_ip = line.split("\t")
clusterinfo[instance_id] = dict(roles=roles, hostname=hostname, private_ip=private_ip)
return clusterinfo
def generate_random_name(n):
return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(n))
def replicaset_sanity_check(numdatamembers, numarbs):
# this is pretty naive right now, add some more checking logic
totalvotes = numdatamembers + numarbs
if totalvotes < 3:
return "Replica set should have at least 3 votes."
if totalvotes % 2 == 0:
return "Replica set config has even number of votes, should be odd"
def main():
LAUNCH = "launch-cluster"
args = [PATH_TO_WHIRR, LAUNCH, "--conf", PATH_TO_CONF_FILE]
parser = argparse.ArgumentParser(description="Helper script to start MongoDB clusters")
parser.add_argument("-n", dest="clustername", help="Name of the Cluster")
parser.add_argument("-t", dest="clustertype", default="standalone", help="Type of cluster: 'standalone' or 'replset'")
parser.add_argument("--version", dest="version", default=None, help="MongoDB Version to use")
parser.add_argument("--image", dest="image", default=None, help="AMI image id")
parser.add_argument("--arbiters", dest="numarbiters", default=0, help="With type=replset: number of arbiters to use in the replica set", type=int)
parser.add_argument("--data-members", dest="numdatamembers", default=3, help="With type=replset: number of data-storing members (non-arbiters)", type=int)
results = parser.parse_args()
if results.clustertype not in CLUSTER_TYPES:
print "Cluster type must be one of:", ','.join(CLUSTER_TYPES)
if results.clustertype == "replset":
sanity_check = replicaset_sanity_check(results.numdatamembers, results.numarbiters)
if sanity_check: #if set, something is bad
print sanity_check
sys.exit(0)
else:
templates = [str(results.numdatamembers) + " mongodb-replsetmember"]
if results.numarbiters > 0:
templates.append(str(results.numarbiters) + " mongodb-arbiter")
args += ["--instance-templates", ','.join(templates)]
elif results.clustertype == "standalone":
args += ["--instance-templates", '1 mongodb-standalone']
if not results.clustername:
results.clustername = generate_random_name(5)
args += ["--cluster-name", results.clustername]
if results.image:
args += ["--image-id", results.image]
if results.version:
if not results.version in VERSION_URLS:
print "version must be one of", ",".join(VERSION_URLS.keys())
else:
pass
print "name", results.clustername
print "type", results.clustertype
print "image", results.image
subprocess.call(args)
print "done"
print getclusterinfo(results.clustername)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment