forked from brutasse/states
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
92 lines (77 loc) · 2.82 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
import sys
from fabric.api import run, sudo, task, cd, env
from fabric.colors import red, blue
from fabric.contrib.files import exists, upload_template, contains, append
from fabric.utils import abort
config = {}
with open('salt.conf', 'r') as conf:
for line in conf.readlines():
key, config[key] = line.strip().split(' = ')
if key.startswith('env.'):
env[key[4:]] = config[key]
env.forward_agent = True
if not 'package' in env:
env.package = 'salt'
SALT_MASTER = config['SALT_MASTER']
def die(msg):
abort(red(msg, bold=True))
def btw(msg):
print >>sys.stderr, blue(msg)
@task
def enable_salt(mode):
if not mode in ('master', 'minion'):
die("`mode` can only be 'master' or 'minion'")
sudo('apt-get update')
sudo('apt-get -y install libzmq-dev python-virtualenv supervisor swig '
'python-m2crypto build-essential python-dev')
run('mkdir -p salt')
with cd('salt'):
if not exists('env/bin/pip'):
version = run('virtualenv --version')
opts = '--system-site-packages' if version >= "1.7" else ''
run('virtualenv %s env' % opts)
run('./env/bin/pip install -U pip')
btw('Installing salt. This may take a while.')
cmd = './env/bin/pip install {opts} {package}'
opts = ''
if 'index_url' in env:
opts = '-i {index_url}'.format(index_url=env.index_url)
cmd = cmd.format(opts=opts, package=env.package)
run(cmd)
sudo('mkdir -p /etc/salt')
user = run('whoami')
context = {
'pillar': {
'salt': {
'master': SALT_MASTER,
},
'user': user,
},
}
upload_template('salt/%s.template' % mode, '/etc/salt/%s' % mode,
context=context, use_sudo=True, use_jinja=True)
context = {
'run_mode': mode,
'pillar': {
'user': user,
},
}
upload_template('salt/salt.conf',
'/etc/supervisor/conf.d/salt-%s.conf' % mode,
context=context, use_sudo=True, use_jinja=True)
out = sudo('supervisorctl update')
if not out:
sudo('supervisorctl restart salt-%s' % mode)
if mode == 'minion':
btw("Now go to the salt master and accept the client key")
btw(" (run salt-key -L then salt-key -a <the name>)")
else:
bashrc = '/home/%s/.bashrc' % user
if not exists(bashrc):
run('touch %s' % bashrc)
# Alias to make salt easier to use
items = [i[4:] for i in run('ls -x salt/env/bin').split() if i.startswith('salt')]
for item in items:
alias = 'alias salt%(item)s="sudo /home/%(user)s/salt/env/bin/salt%(item)s"' % {'item': item, 'user': user}
if not contains(bashrc, alias):
append(bashrc, alias)