forked from rgbkrk/bootstrap-salt-nbviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
194 lines (142 loc) · 5.51 KB
/
fabfile.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Bootstrapping salt minions on Rackspace (using fabric rather than
salt-cloud).
This is not the most effective way to do this, but it gets it done.
For instance, we could be bootstrapping servers while others are still being
built by Rackspace. In the end, it doesn't matter much.
To get up and running, use
$ fab master_up:key_name=main,credential_file=~/.rax_creds fullstrap_master
$ fab minions_up:key_name=main fullstrap_minions:master=<ip from master up>
minions_up will set env.hosts (for other fabric calls).
If you want to use other calls as a standalone, be sure
to set the hosts when calling them.
$ fab -H 67.207.156.15,67.207.156.147,67.207.155.4 restart_minion
'''
import os
from itertools import ifilter
import fabric.api
from fabric.api import parallel
from fabric.api import env, run
from fabric.contrib import files
from fabric.context_managers import settings
import pyrax
env.user = 'root'
env.key_filename = os.path.expanduser("~/.ssh/id_rsa")
################################################################################
# Master tools
################################################################################
def master_up(key_name, credential_file="~/.rackspace_cloud_credentials"):
'''
Create a salt-master on Rackspace
Alternatively create the master using nova
'''
# Authenticate with Rackspace, use credential file
pyrax.set_setting("identity_type", "rackspace")
pyrax.set_credential_file(os.path.expanduser(credential_file))
# Shorthand
cs = pyrax.cloudservers
# Building Ubuntu 12.04 boxes with 512 MB RAM
iter_flavors = ifilter(lambda flavor: flavor.ram == 512, cs.flavors.list())
flavor_512 = iter_flavors.next()
iter_os = ifilter(lambda img: "Ubuntu 12.04" in img.name, cs.images.list())
ubu_image = iter_os.next()
master = cs.servers.create("master.ipython.org", ubu_image.id, flavor_512,
key_name=key_name)
master = pyrax.utils.wait_for_build(master, verbose=True)
env.hosts = [master.accessIPv4]
print("Master IP: {}".format(master.accessIPv4))
return master.accessIPv4
def fullstrap_master():
apt_update()
install_curl()
bootstrap_salt_master()
install_pip()
install_gitpython()
restart_master()
@parallel
def install_curl():
run("apt-get -y install curl")
@parallel
def apt_update():
run('apt-get -y update')
run('apt-get -y upgrade')
def bootstrap_salt_master():
'''
Bootstraps a salt master
'''
run('curl -L http://bootstrap.saltstack.org | sudo sh -s -- -M -N git develop')
def install_pip():
# Good pip
run('wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py\
-O - | python2.7')
run('curl --show-error --retry 5\
https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python2.7')
def install_gitpython():
run('apt-get install git')
run('pip install GitPython==0.3.2.RC1 --upgrade')
def restart_master():
run('service salt-master restart')
################################################################################
# Minion tools
################################################################################
def minions_up(key_name, credential_file="~/.rackspace_cloud_credentials"):
'''
Creates a specific build of machines and bootstraps salt.
The credential file can be set via keyword credential_file, and defaults
to ~/.rackspace_cloud_credentials
'''
# Authenticate with Rackspace, use credential file
pyrax.set_setting("identity_type", "rackspace")
pyrax.set_credential_file(os.path.expanduser(credential_file))
# Shorthand
cs = pyrax.cloudservers
# Building Ubuntu 12.04 boxes with 512 MB RAM
iter_flavors = ifilter(lambda flavor: flavor.ram == 512, cs.flavors.list())
flavor_512 = iter_flavors.next()
iter_os = ifilter(lambda img: "Ubuntu 12.04" in img.name, cs.images.list())
ubu_image = iter_os.next()
domain_template = "{0}{1:02d}.minion.{2}"
server_names = [domain_template.format(release_type, idx, "ipython.org") for
release_type in ("qa", "prod") for idx in xrange(1, 3)]
# Create homogenous minions (with names as above)
# This hardcodes the key name to the one I'm using right now.
minions = map(lambda name: cs.servers.create(name, ubu_image.id,
flavor_512, key_name=key_name), server_names)
# Make sure all the minions are done before we move on to fabric runs
minions = [pyrax.utils.wait_for_build(minion, verbose=True)
for minion in minions]
env.hosts = [str(minion.accessIPv4) for minion in minions]
print(env.hosts)
@parallel
def fullstrap_minions(master):
'''
Install saltstack, set master, restart the salt-minion daemon
$ fab fullstrap_minions:master=127.0.0.1
'''
apt_update()
install_curl()
bootstrap_salt()
point_minion_at_master(master)
restart_minion()
@parallel
def bootstrap_salt():
'''
Uses the salt bootstrap script (from bootstrap.saltstack.org) to install a
salt-minion and run it as a daemon
'''
run("curl -L http://bootstrap.saltstack.org | sh -s -- git develop")
@parallel
def point_minion_at_master(master):
'''
Simply writes the master for this minion at /etc/salt/minion
$ fab point_minion_at_master:master=127.0.0.1
'''
run("echo 'master: {}' > /etc/salt/minion".format(master))
@parallel
def restart_minion():
'''
Restart a salt minion
'''
run("service salt-minion restart")