/
fabfile.py
executable file
·267 lines (222 loc) · 7.35 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# XXXX
#
#
from __future__ import with_statement
from __future__ import division
import os
import pprint
import sys
# bootstrap variables
HERE = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = HERE
PYTHON = os.path.abspath(os.path.join(PROJECT_ROOT, 'bin', 'python'))
FABRIC = os.path.abspath(os.path.join(PROJECT_ROOT, 'bin', 'fab'))
FABUTILS_DIR = os.path.join(PROJECT_ROOT, 'buildout')
# This is our entry point.
if __name__ == '__main__':
import subprocess
if not os.path.exists(FABRIC):
print 'ERROR: fabric not found!'
print 'ERROR: make sure you have build XXXX before trying to run this script...'
sys.exit(1)
if len(sys.argv) > 1:
# If we got an argument then invoke fabric with it.
subprocess.call([FABRIC, '-f', os.path.abspath(__file__)] + sys.argv[1:])
sys.exit(0)
else:
# Otherwise list our targets.
subprocess.call([FABRIC, '-f', os.path.abspath(__file__), '--list'])
print
print "Run \"%s --help\" for more details" % __file__
sys.exit(0)
###################################################################################################
from fabric.api import run, env, task, parallel, serial, roles, runs_once
from fabric.context_managers import lcd, cd
from fabric.operations import put, sudo, local
from fabric.contrib.files import upload_template
from fabric.decorators import with_settings
from fabric.colors import red, green, yellow
from fabric.contrib.console import confirm
from fabric.operations import run, sudo
from fabric.context_managers import cd
from fabric.contrib.files import sed, upload_template
## load the configuration file
sys.path.append(FABUTILS_DIR)
from fabutils import load_cfg, _exists
load_cfg(env, root = PROJECT_ROOT)
###################################################################################################
@task
def XXXX_build_rpm ():
"""
Creates the RPM package distribution
"""
local('make rpm')
@task
@parallel
@roles('integration', 'deployment')
def XXXX_deploy ():
"""
Deploy the packages in the deployment machines
"""
print(green("Installing packages at %s" % str(env.host_string)))
if confirm(red('Install the packages at the %s?' % (env.host_string)), default = False):
print(yellow("... stopping XXXX"))
if _exists('/etc/init.d/XXXX'):
sudo('service XXXX stop')
sudo('rm -f /etc/init.d/XXXX')
with cd(env.admin.prefix):
print(yellow("... cleaning up old RPMs"))
if not _exists('tmp'): run('mkdir tmp')
run('rm -rf tmp/*')
directory = os.path.join(env.admin.prefix, 'tmp')
with cd(directory):
print(yellow("... uploading RPMs"))
for f in env.packages.rpms:
put(os.path.join(directory, f), '.')
print(yellow("... installing software"))
sudo('yum install -R 2 -q -y --nogpgcheck *.rpm')
print(red("... XXXX is STOPPED at %s!" % env.host_string))
@task
@parallel
@roles('integration', 'deployment')
def XXXX_start():
"""
Start the XXXX system
"""
print(green('Starting XXXX at %s' % (env.host_string)))
with settings(warn_only = True):
sudo('rm -f nohup*')
sudo('[ -f /etc/init.d/XXXX ] && nohup /sbin/service XXXX start || echo "No service script found!!"')
@task
@serial
@roles('integration', 'deployment')
def XXXX_start_i():
"""
Start the XXXX system (interactive)
"""
if confirm(red('Start XXXX at %s?' % (env.host_string)), default = False):
execute(XXXX_start)
@task
@parallel
@roles('integration', 'deployment')
def XXXX_restart():
"""
Restart the XXXX system
"""
print(green('Restarting XXXX at %s' % (env.host_string)))
with settings(warn_only = True):
sudo('rm -f nohup*')
sudo('[ -f /etc/init.d/XXXX ] && nohup /sbin/service XXXX restart || echo "No service script found!!"')
@task
@serial
@roles('integration', 'deployment')
def XXXX_restart_i():
"""
Restart the XXXX system (interactive)
"""
if confirm(red('Restart XXXX at %s?' % (env.host_string)), default = False):
execute(XXXX_restart)
@task
@parallel
@roles('integration', 'deployment')
def XXXX_stop():
"""
Stop the XXXX system
"""
print(green('Stoping XXXX at %s' % (env.host_string)))
with settings(warn_only = True):
sudo('rm -f nohup*')
sudo('[ -f /etc/init.d/XXXX ] && nohup /sbin/service XXXX stop || echo "No service script found!!"')
@task
@serial
@roles('integration', 'deployment')
def XXXX_stop_i():
"""
Stop the XXXX system (interactive)
"""
if confirm(red('Stop XXXX at %s?' % (env.host_string)), default = False):
execute(XXXX_stop)
@task
@serial
@roles('integration', 'deployment')
def XXXX_update_config():
"""
Upload the configuration file (after doing replacements)
"""
dest = os.path.join(env.admin.prefix, 'conf')
context = env.machines[env.host_string]
if confirm(red('Upload the new configuration files from %s at %s?' %
(env.templates.local_dir, env.host_string)),
default = False):
for f in env.templates.files:
print(green('... uploading %s' % (f)))
sudo('chown -R {user}:{group} {d}'.format(user = env.admin.user,
group = env.admin.group,
d = env.admin.prefix))
upload_template(f, dest,
context = context,
use_jinja = True,
template_dir = env.templates.local_dir,
use_sudo = False,
backup = True)
#
# logs
#
@task
@roles('integration', 'deployment')
def XXXX_tail_log():
"""
Prints the tail of PREFIX/logs/XXXX.log
"""
f = os.path.join(env.admin.prefix, 'logs', 'XXXX.log')
print(green('%s at %s' % (f, env.host_string)))
with settings(warn_only = True):
run('[ -f {f} ] && tail {f} || echo "file not found"'.format(f = f))
@task
@parallel
@roles('integration', 'deployment')
def XXXX_cleanup_logs ():
"""
Cleanup all the logs
"""
with cd(env.admin.prefix):
with settings(warn_only = True):
sudo('[ -d logs ] && rm -rf logs/*')
#
# auxiliary tasks
#
@task
@roles('integration', 'deployment')
def dump_os_env ():
"""
Dump the OS environment variables on all machines
"""
with settings(warn_only = True):
run('export')
@task
@runs_once
def dump_roles ():
"""
Dumps the current roles list
"""
roledefs_str = pprint.pformat(env.roledefs, indent = 2)
print(green("Currently defined roles:\n%s" % roledefs_str))
@task
@runs_once
def dump_env ():
"""
Dumps the current Fabric environment
"""
env_str = pprint.pformat(env, indent = 2)
print(green("Currently defined environment:\n%s" % env_str))
@task
@runs_once
def dump_machines ():
"""
Dumps the current machines list with their variables
"""
machines_str = pprint.pformat(env.machines, indent = 2)
print(green("Currently defined machines:\n%s" % machines_str))