from libcloud.base import NodeImage, NodeSize, NodeLocation from libcloud.types import Provider from libcloud.providers import get_driver from libcloud.deployment import MultiStepDeployment, ScriptDeployment, SSHKeyDeployment import os, sys import getopt CLOUD_SERVERS_DRIVER = os.environ.get('CLOUD_SERVERS_DRIVER','rackspace') CLOUD_SERVERS_USERNAME = os.environ['CLOUD_SERVERS_USERNAME'] CLOUD_SERVERS_API_KEY = os.environ['CLOUD_SERVERS_API_KEY'] CLOUD_SERVERS_HOST = os.environ.get('CLOUD_SERVERS_HOST', None) CLOUD_SERVERS_PATH = os.environ.get('CLOUD_SERVERS_PATH', None) (option_pairs, args) = getopt.getopt(sys.argv[1:], [], ["distro="]) if len(args) == 0: print "Node Name required!" sys.exit(1) node_name = args[0] files={} for key in ("slave_private_key", "slave_gpg_key", "slave_tarmac_key"): if os.path.exists(key): with open(key, "r") as private_key: files["/root/%s" % key] = private_key.read() node_size = '3' node_type = '76' if CLOUD_SERVERS_DRIVER == 'rackspace': import clouddns for (name, value) in option_pairs: if name == "--distro": if value == "maverick": node_type = '69' if value == "natty": node_type = '76' Driver = get_driver(Provider.RACKSPACE) conn = Driver(CLOUD_SERVERS_USERNAME, CLOUD_SERVERS_API_KEY) images = conn.list_images() size = [sz for sz in conn.list_sizes() if sz.id == node_size][0] image = [img for img in conn.list_images() if img.id == node_type][0] elif CLOUD_SERVERS_DRIVER == 'eucalyptus': node_type = 'ami-0000037e' node_size = 'standard.small' Driver = get_driver(Provider.EUCALYPTUS) conn = Driver(CLOUD_SERVERS_USERNAME, CLOUD_SERVERS_API_KEY, host=CLOUD_SERVERS_HOST, path=CLOUD_SERVERS_PATH) image = NodeImage(id=node_type, name="", driver="") size = NodeSize(id=node_size, name="", ram=None, disk=None, bandwidth=None, price=None, driver="") # a simple script to install puppet post boot, can be much more complicated. launch_script = """perl -ple 's/main/main universe/' -i /etc/apt/sources.list apt-get update apt-get -y --force-yes upgrade apt-get install -y --force-yes git rubygems gem install --no-rdoc --no-ri --version=1.6.0 facter gem install --no-rdoc --no-ri --version=2.7.1 puppet cd /root git clone git://github.com/openstack/openstack-ci-puppet.git cd openstack-ci-puppet mv /root/slave_*_key modules/jenkins_slave/files/ /var/lib/gems/1.8/bin/puppet apply -l /tmp/manifest.log --modulepath=`pwd`/modules manifests/site.pp """ # a task that first installs the ssh key, and then runs the script if CLOUD_SERVERS_DRIVER == 'rackspace': # read your public key in sd = SSHKeyDeployment(open(os.path.expanduser("~/.ssh/id_rsa.pub")).read()) script = ScriptDeployment(launch_script) msd = MultiStepDeployment([sd, script]) else: private_key_path = os.path.expanduser("~/.ssh/%s.pem" % node_name) if not os.path.exists(private_key_path): resp = conn.ex_create_keypair(name=node_name) key_material = resp.get('keyMaterial') if not key_material: print "Couldn't create keypair" sys.exit(1) with open(private_key_path, 'w') as private_key: private_key.write(key_material + '\n') os.chmod(private_key_path, 0600) # deploy_node takes the same base keyword arguments as create_node. if CLOUD_SERVERS_DRIVER == 'rackspace': node = conn.deploy_node(name=node_name, image=image, size=size, deploy=msd, ex_files=files) dns_ctx = clouddns.connection.Connection(CLOUD_SERVERS_USERNAME, CLOUD_SERVERS_API_KEY) domain_name = ".".join(node_name.split(".")[-2:]) domain = dns_ctx.get_domain(name=domain_name) try: record = domain.get_record(name=node_name) except: record = None if record is None: domain.create_record(node_name, node.public_ip[0], "A") else: record.update(data=node.public_ip[0]) else: node = conn.create_node(name=node_name, image=image, size=size, ex_keyname=node_name, ex_userdata=launch_script) with open("%s.node.sh" % node_name,"w") as node_file: node_file.write("ipAddr=%s\n" % node.public_ip[0]) node_file.write("nodeId=%s\n" % node.id)