Merge "Add ec2 api support to launch_slave."
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
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="])
|
||||
|
||||
@@ -14,52 +18,81 @@ if len(args) == 0:
|
||||
sys.exit(1)
|
||||
node_name = args[0]
|
||||
|
||||
node_type = '76'
|
||||
|
||||
for (name, value) in option_pairs:
|
||||
if name == "--distro":
|
||||
if value == "maverick":
|
||||
node_type = '69'
|
||||
if value == "natty":
|
||||
node_type = '76'
|
||||
|
||||
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()
|
||||
|
||||
Driver = get_driver(Provider.RACKSPACE)
|
||||
conn = Driver(CLOUD_SERVERS_USERNAME, CLOUD_SERVERS_API_KEY)
|
||||
node_size = '3'
|
||||
node_type = '76'
|
||||
if CLOUD_SERVERS_DRIVER == 'rackspace':
|
||||
|
||||
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="")
|
||||
|
||||
# read your public key in
|
||||
sd = SSHKeyDeployment(open(os.path.expanduser("~/.ssh/id_rsa.pub")).read())
|
||||
# a simple script to install puppet post boot, can be much more complicated.
|
||||
script = ScriptDeployment("""
|
||||
perl -ple 's/main/main universe/' -i /etc/apt/sources.list
|
||||
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 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
|
||||
msd = MultiStepDeployment([sd, 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)
|
||||
|
||||
|
||||
images = conn.list_images()
|
||||
|
||||
size = [sz for sz in conn.list_sizes() if sz.id == '3'][0]
|
||||
image = [img for img in conn.list_images() if img.id == node_type][0]
|
||||
|
||||
|
||||
# deploy_node takes the same base keyword arguments as create_node.
|
||||
node = conn.deploy_node(name=node_name, image=image, size=size, deploy=msd,
|
||||
ex_files=files)
|
||||
|
||||
if CLOUD_SERVERS_DRIVER == 'rackspace':
|
||||
node = conn.deploy_node(name=node_name, image=image, size=size, deploy=msd,
|
||||
ex_files=files)
|
||||
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])
|
||||
|
||||
Reference in New Issue
Block a user