-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlaunch_slave.py
66 lines (51 loc) · 2.14 KB
/
launch_slave.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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_USERNAME = os.environ['CLOUD_SERVERS_USERNAME']
CLOUD_SERVERS_API_KEY = os.environ['CLOUD_SERVERS_API_KEY']
(option_pairs, args) = getopt.getopt(sys.argv[1:], [], ["distro="])
if len(args) == 0:
print "Node Name required!"
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)
# 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
apt-get update
apt-get -y --force-yes upgrade
apt-get install -y --force-yes git rubygems
gem install --no-rdoc --no-ri puppet
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])
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)
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)