-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfabfile.py
246 lines (197 loc) · 6.56 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from fabric.api import sudo, task, env, execute, local, put
from pipes import quote as shellQuote
import yaml
import json
# We assume we are running on a fedora 20 AWS image.
# These are setup with an `fedora` user, so hard code that.
env.user = 'fedora'
def cmd(*args):
"""
Quote the supplied ``list`` of ``args`` and return a command line
string.
XXX: This is duplicated in ``slaves/redhat-openstack/fabfile.py``. It
should be shared.
:param list args: The componants of the command line.
:return: The quoted command line string.
"""
return ' '.join(map(shellQuote, args))
def get_lastpass_config(key):
output = local(cmd('lpass', 'show', '--notes', key),
capture=True)
config = yaml.safe_load(output.stdout)
return config
def loadConfig(configFile, use_acceptance_config=True):
"""
Load config.
Sets env.hosts, if not already set.
"""
if configFile is not None:
config = yaml.safe_load(open(configFile))
else:
config = get_lastpass_config("[email protected]")
if not env.hosts:
env.hosts = [config['buildmaster']['host']]
if use_acceptance_config:
acceptance_config = get_lastpass_config(
config['acceptance'] = {
'ssh-key': acceptance_config['ssh-key'],
'config': yaml.safe_dump(acceptance_config['config']),
}
else:
config['acceptance'] = {}
return config
def pull(image):
sudo(cmd('docker', 'pull', image))
def containerExists(name):
return sudo(cmd('docker', 'inspect',
'-f', 'test', name), quiet=True).succeeded
def removeContainer(name):
if containerExists(name):
sudo(cmd('docker', 'stop', name))
sudo(cmd('docker', 'rm', '-f', name))
def imageFromConfig(config, baseImage='clusterhq/build.clusterhq.com'):
"""
Get the image to use from the configuration.
Uses buildmaster.docker_tag (with default 'latest').
"""
docker_tag = config['buildmaster'].get('docker_tag', 'latest')
return '%s:%s' % (baseImage, docker_tag)
def startBuildmaster(config, shouldPull=True):
image = imageFromConfig(config)
if shouldPull:
pull(image)
removeContainer('buildmaster')
sudo(cmd(
'docker', 'run', '-d',
'--name', 'buildmaster',
'-p', '80:80', '-p', '9989:9989',
'-e', 'BUILDBOT_CONFIG=%s' % (json.dumps(config),),
'-v', '/dev/log:/dev/log',
'--volumes-from', 'buildmaster-data',
image))
removeUntaggedImages()
def removeUntaggedImages():
"""
Cleanup untagged docker images.
When pulling a new version of an image, docker keeps around the old layers,
which consume diskspace. This deletes all untagged leaf layers and their
unreferenced parents.
"""
images = [line.split()
for line in sudo(cmd("docker", "images")).splitlines()]
untagged = [image[2] for image in images if image[0] == '<none>']
if untagged:
sudo(cmd('docker', 'rmi', *untagged))
def bootstrap():
"""
Install docker, and setup data volume.
"""
sudo('yum update -y')
sudo('yum install -y docker-io')
sudo('systemctl enable docker')
sudo('systemctl start docker')
if not containerExists('buildmaster-data'):
sudo(cmd('docker', 'run',
'--name', 'buildmaster-data',
'-v', '/srv/buildmaster/data',
'busybox', '/bin/true'))
@task
def start(configFile=None):
"""
Start buildmaster on fresh host.
"""
config = loadConfig(configFile)
execute(bootstrap)
execute(startBuildmaster, config)
@task
def update(configFile=None):
"""
Update buildmaster to latest image.
"""
config = loadConfig(configFile)
execute(startBuildmaster, config)
@task
def restart(configFile=None):
"""
Restart buildmaster with current image.
"""
config = loadConfig(configFile)
execute(startBuildmaster, config, shouldPull=False)
@task
def logs(configFile=None, follow=True):
"""
Show logs.
"""
loadConfig(configFile)
if follow:
execute(sudo, cmd('journalctl', '--follow',
'SYSLOG_IDENTIFIER=buildmaster'))
else:
execute(sudo, cmd('journalctl', 'SYSLOG_IDENTIFIER=buildmaster'))
@task
def getConfig():
"""
Get credentials from lastpass.
"""
local(cmd('cp', 'config.yml', 'config.yml.bak'))
local('lpass show --notes "[email protected]" >config.yml')
@task
def saveConfig():
"""
Put credentials in lastpass.
"""
local('lpass show --notes "[email protected]" >config.yml.old')
local('lpass edit --non-interactive '
'--notes "[email protected]" <config.yml')
local('lpass sync')
@task
def diff_config(configFile="config.yml"):
"""
Show the differences between the production config and a local
configuration.
"""
local('lpass show --notes "[email protected]" | diff -u - %s'
% shellQuote(configFile))
@task
def check_config(configFile="config.yml.sample"):
"""
Check that buildbot can load the configuration.
"""
from os import environ
config = loadConfig(configFile, use_acceptance_config=False)
environ['BUILDBOT_CONFIG'] = json.dumps(config)
local(cmd(
'buildbot', 'checkconfig', 'config.py'
))
@task
def startPrometheus():
PROMETHEUS_IMAGE = 'prom/prometheus:21da4f1821b3'
removeContainer('prometheus')
if not containerExists('prometheus-data'):
sudo(cmd(
'docker', 'run',
'--name', 'prometheus-data',
'--entrypoint', '/bin/true',
PROMETHEUS_IMAGE))
sudo(cmd('mkdir', '-p', '/srv/prometheus'))
put('prometheus.yml', '/srv/prometheus/prometheus.yml', use_sudo=True)
sudo(cmd('chcon', '-t', 'svirt_sandbox_file_t',
'/srv/prometheus/prometheus.yml'))
sudo(cmd(
'docker', 'run', '-d',
'--name', 'prometheus',
'-p', '9090:9090',
'-v', ':'.join(['/srv/prometheus/prometheus.yml',
'/etc/prometheus/prometheus.yml',
'ro']),
'--volumes-from', 'prometheus-data',
PROMETHEUS_IMAGE,
# Store metrics for two months
'-storage.local.retention=720h0m0s',
# Options from `CMD`.
'-config.file=/etc/prometheus/prometheus.yml',
"-storage.local.path=/prometheus",
"-web.console.libraries=/etc/prometheus/console_libraries",
"-web.console.templates=/etc/prometheus/consoles",
))