forked from qiime/qiime-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqiime-deploy.py
400 lines (351 loc) · 14.5 KB
/
qiime-deploy.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
from lib import application
from lib import config
from lib import custom
from lib import data
from lib import dependencies
from lib import deploy
from lib import environment
from lib import logconfig
from lib import util
from optparse import OptionParser
import commands
import logging
import os
import shutil
import sys
import time
SLEEP_SECS = 5
def generate_activate_file(print_env,
deploy_dir,
append_to_bashrc=False,
append_to_bashprofile=False):
log.info('Generating activate.sh file')
lines = []
for env_name in print_env:
old_var = print_env[env_name]
if (env_name.upper() == 'PATH') or \
(env_name.upper() == 'PYTHONPATH'):
new_var = old_var + ':$%s' % env_name.upper()
else:
new_var = old_var
var_string = 'export %s=%s' % (env_name, new_var)
var_string += '\n'
lines.append(var_string)
output_file = os.path.join(deploy_dir, 'activate.sh')
old_contents = []
if os.path.exists(output_file):
oldFile = open(output_file, 'r')
old_contents = oldFile.readlines()
oldFile.close()
if lines != old_contents:
util.backup_file(output_file)
if os.path.exists(output_file):
os.remove(output_file)
util.write_new_file(output_file, lines)
appendStr = 'echo "source %s" >> ~/.bashrc' % output_file
appendFile = '~/.bashrc'
compStr = 'source %s\n' % output_file
if (append_to_bashrc) and (not util.line_in_file(compStr, appendFile)):
log.debug('Appending environment settings to ~/.bashrc')
(appendStatus, appendOut) = commands.getstatusoutput(appendStr)
if appendStatus != 0:
log.error('Error appending to ~/.bashrc')
log.error('Output:')
log.error(appendOut)
else:
log.info('Successfully appended to ~/.bashrc')
appendStr = 'echo "source %s" >> ~/.bash_profile' % output_file
appendFile = '~/.bash_profile'
compStr = 'source %s\n' % output_file
if (append_to_bashprofile) and (not util.line_in_file(compStr, appendFile)):
log.debug('Appending environment settings to ~/.bash_profile')
(appendStatus, appendOut) = commands.getstatusoutput(appendStr)
if appendStatus != 0:
log.error('Error appending to ~/.bash_profile')
log.error('Output:')
log.error(appendOut)
else:
log.info('Successfully appended to ~/.bash_profile')
def is_app_successful(app_name, app_list):
for app in app_list:
if app.name == app_name:
if app.success:
return True
return False
def is_app_complete(app_name, app_list):
for app in app_list:
if app.name == app_name:
if app.complete:
return True
return False
"""
This is the main thread responsible for deploying all applications and data
specified in the config file in the appropriate order and making sure
everything completes.
"""
def deploy_apps(deploy_config, force_remove=False, remove_repos=False):
env = environment.BashEnvironment()
# launch status thread
threads = []
log.info('Starting status thread.')
t = deploy.StatusThread()
t.start()
threads.append(t)
# launch worker threads
try:
num_threads = int(deploy_config.get('global', 'max-deploy-threads'))
except:
log.debug('Missing max-deploy-threads section. Setting to 1.')
num_threads = 1
log.info('Starting %s deploy threads.' % num_threads)
for i in range(num_threads):
t = deploy.WorkerThread()
t.start()
threads.append(t)
# create final deploy directory location if it doesn't exist
deploy_dir = deploy_config.get('global', 'final-deploy-directory')
deploy_dir = os.path.expanduser(deploy_dir)
deploy_dir = os.path.abspath(deploy_dir)
if os.path.exists(deploy_dir):
log.debug('Final deploy directory already exists: %s' % deploy_dir)
else:
log.debug('Creating final deploy directory: %s' % deploy_dir)
os.makedirs(deploy_dir)
# identify data and application sections in the config file
all_sect = deploy_config.sections()
req_sect = ['dependencies', 'global']
data_sect = [x for x in all_sect if 'data-' in x]
app_sect = list(set(all_sect) - (set(req_sect) | set(data_sect)))
log.debug('All config sections: %s' % all_sect)
log.debug('Required config sections: %s' % req_sect)
log.debug('Data config sections: %s' % data_sect)
log.debug('Application config sections: %s' % app_sect)
log.info('Running deploy for data and apps without dependencies.')
# locate the python exe that we should use for the deploy process (if
# required)
log.info('Searching for python...')
custom_py_exe = None
try:
custom_py_exe = deploy_config.get('global', 'python-exe')
if custom_py_exe == 'None':
custom_py_exe = None
else:
custom_py_exe = os.path.expanduser(custom_py_exe)
log.info('python-exe specified, using: %s' % custom_py_exe)
except:
log.info('python-exe is not specified.')
custom_py_exe = None
if not custom_py_exe:
if 'python' in app_sect:
log.info('Looks like python is being deployed, using that version')
base_dir = deploy_config.get('global', 'final-deploy-directory')
python_version = deploy_config.get('python', 'version')
try:
release_location = deploy_config.get('python', 'release-location')
deploy_type = 'release'
except:
log.debug('%s has no release-location' % 'python')
deploy_type = 'repository'
python_dir = 'python-' + python_version + '-' + deploy_type
custom_py_exe = os.path.join(base_dir, python_dir)
custom_py_exe = os.path.join(custom_py_exe, 'bin/python')
log.info('Setting python: %s' % custom_py_exe)
else:
log.info('Could not find a python, setting \'python\'')
custom_py_exe = 'python'
# create application objects for all apps to deploy
all_apps_to_deploy = []
remaining_apps_to_deploy = []
for app_name in app_sect:
a = application.Application(app_name,
env,
deploy_config,
custom_py_exe,
remove_repos)
remaining_apps_to_deploy.append(a)
all_apps_to_deploy.append(a)
# create and schedule all data objects
for data_name in data_sect:
d = data.Data(data_name, env, deploy_config)
deploy.WORK_Q.put(d)
# immediately schedule all applications that do not contain dependencies
for app in list(remaining_apps_to_deploy):
if not app.deps:
log.debug('Putting %s in the queue to deploy.' % app.name)
deploy.WORK_Q.put(app)
remaining_apps_to_deploy.remove(app)
remaining_names = [app.name for app in remaining_apps_to_deploy]
log.info('Launched initial deploy, remaining apps: %s' % remaining_names)
# schedule applications with dependencies once all of an applications
# dependencies have completed successfully
log.info('Deploying remaining apps.')
while remaining_apps_to_deploy:
for app in list(remaining_apps_to_deploy):
all_deps_complete = True
all_deps_successful = True
for dep in app.deps:
if not is_app_complete(dep, all_apps_to_deploy):
log.debug('%s dependencies remaining:%s' % (app.name, app.deps))
all_deps_complete = False
if not is_app_successful(dep, all_apps_to_deploy):
log.debug('%s dependencies not yet successful:%s' % (app.name, app.deps))
all_deps_successful = False
if dep not in app_sect:
log.error('Application will never complete, dependency' + \
'does not exist: %s' % dep)
app.success = False
app.complete = True
deploy.FAILED_Q.put(app)
remaining_apps_to_deploy.remove(app)
if not app.complete:
if all_deps_complete and all_deps_successful:
log.debug('Putting %s in the queue to deploy.' % app.name)
deploy.WORK_Q.put(app)
remaining_apps_to_deploy.remove(app)
if all_deps_complete and (not all_deps_successful):
app.success = False
app.complete = True
log.error('Dependencies failed for %s.' % app.name)
deploy.FAILED_Q.put(app)
remaining_apps_to_deploy.remove(app)
remaining_names = [app.name for app in remaining_apps_to_deploy]
log.debug('Applications waiting on dependencies: %s' % remaining_names)
log.debug('Control thread sleeping: %s seconds' % SLEEP_SECS)
time.sleep(SLEEP_SECS)
# wait for everything to complete
deploy.WORK_Q.join()
deploy.DONE = True
for t in threads:
t.join()
summary = '\n\n'
summary += 'DEPLOYMENT SUMMARY\n\n'
# print out successful deployments
first = True
summary += 'Packages deployed successfully:\n'
while not deploy.COMPLETE_Q.empty():
item = deploy.COMPLETE_Q.get(False)
if item:
if not first:
summary += ', %s' % item.name
else:
summary += '%s' % item.name
first = False
summary += '\n'
# print out skipped deployments
first = True
summary += '\nPackages skipped (assumed successful):\n'
while not deploy.SKIPPED_Q.empty():
item = deploy.SKIPPED_Q.get(False)
if item:
if not first:
summary += ', %s' % item.name
else:
summary += '%s' % item.name
first = False
summary += '\n'
# print out failed applications at the end and perform cleanup
failed_items = False
first = True
failed_list = []
summary += '\nPackages failed to deploy:\n'
while not deploy.FAILED_Q.empty():
item = deploy.FAILED_Q.get(False)
if item:
failed_dir = util.remove_directory(item.deploy_dir, force_remove)
if failed_dir:
failed_list.append(failed_dir)
if not first:
summary += ', %s' % item.name
else:
summary += '%s' % item.name
first = False
failed_items = True
summary += '\n\n'
if failed_list:
summary += 'Directories of failed applications must be removed ' + \
'manually or --force-remove-failed-dirs must be specified:'
summary += '\n'
for failed_dir in failed_list:
summary += failed_dir
summary += '\n'
summary += '\n'
# build and write out activate.sh
log.debug('Writing new environment variables to activate.sh')
log.debug('Print environment: %s' % env.get_print_env())
try:
append_bashrc = deploy_config.get('global', 'append-environment-to-bashrc')
if append_bashrc == 'yes':
append_bashrc = True
else:
append_bashrc = False
except:
log.debug('Did not find append-environment-to-bashrc option, skipping.')
append_bashrc = False
try:
append_bashprofile = deploy_config.get('global', 'append-environment-to-bashprofile')
if append_bashprofile == 'yes':
append_bashprofile = True
else:
append_bashprofile = False
except:
log.debug('Did not find append-environment-to-bashprofile option, skipping.')
append_bashprofile = False
generate_activate_file(env.get_print_env(),
deploy_dir,
append_bashrc,
append_bashprofile)
# run any custom finalization code, in QIIME's case, this is where
# the QIIME config file is generated and written
rc = custom.custom_finalize(custom_py_exe, deploy_dir, all_apps_to_deploy, log)
sys.stdout.write(summary)
if failed_items:
rc = 1
return rc
def get_options():
usage = "usage: %prog [options] [deploy_path]"
parser = OptionParser(usage=usage)
parser.add_option('-f', '--config-file', action = 'store', \
dest = 'configFile', help = 'Custom location for ' + \
'app-deploy configuration file ' + \
'[default: %default]')
parser.add_option('--force-remove-failed-dirs', action = 'store_true', \
dest = 'forceRemove', help = 'Force remove ' + \
'directories of applications that failed ' + \
'to deploy successfully ' + \
'[default: %default]')
parser.add_option('--force-remove-previous-repos', action = 'store_true', \
dest = 'removePreviousRepos', help = 'Force remove ' + \
'directories of previous repository deployments ' + \
'(assuming a later version succeeded) ' + \
'[default: %default]')
parser.set_defaults(configFile='./qiime.conf',
forceRemove=False,
removePreviousRepos=False)
opts, args = parser.parse_args()
if opts.configFile != None:
opts.configFile = os.path.expanduser(opts.configFile)
opts.configFile = os.path.abspath(opts.configFile)
return opts, args
def main():
opts, args = get_options()
deploy_config = config.read_config(opts.configFile)
if args:
deploy_dir = args[0]
deploy_config.set('global', 'final-deploy-directory', deploy_dir)
try:
if deploy_config.get('global', 'log-level') == 'DEBUG':
logging.getLogger('').setLevel(logging.DEBUG)
except:
log.debug('Missing log-level option. Setting to INFO.')
if not dependencies.dependencies_ok(deploy_config):
log.error('Dependency check failed.')
return 1
else:
log.info('Dependencies look good.')
return deploy_apps(deploy_config,
opts.forceRemove,
opts.removePreviousRepos)
if __name__ == '__main__':
logconfig.configure_log()
log = logging.getLogger(__name__)
sys.exit(main())