-
Notifications
You must be signed in to change notification settings - Fork 17
/
deploy.py
executable file
·404 lines (337 loc) · 12.2 KB
/
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
401
402
403
404
#!/usr/bin/env python
import os
import sys
import tempfile
import argparse
import subprocess as sp
import datetime
import json
import fnmatch
import logging
import hashlib
from pathlib import Path
from distutils import filelist
# Determine default staging area, used in help
default_staging = "/tmp/{0}-lcdb-wf-staging".format(os.getenv('USER'))
usage = f"""
This script assists in the deployment of relevant code from the lcdb-wf
repository to a new deployment directory for running an analysis. It is
intended to be run in a standalone fashion such that with just the script you
can download and deploy a specified version of the workflows.
For example, the following command will clone the GitHub repo to {default_staging},
check out the v9.999 branch, copy the files needed for RNA-seq over to the
"my_analysis_dir" directory, store a read-only file .lcdb-wf-deployment.yaml
with the metadata of the repo used for cloning, and build the conda
environments within "my_analysis_dir":
./deploy.py \\
--clone \\
--dest my_analysis_dir \\
--flavor rnaseq \\
--build-envs \\
--branch v9.999
Compared to directly cloning the repo, this results in a cleaner deployment
directory that does not have various test infrastructure or workflows not
relevant to the project.
"""
logging.basicConfig(
format="%(asctime)s [%(module)s] %(message)s",
level=logging.DEBUG,
datefmt="%Y-%m-%dT%H:%M:%S",
)
# ANSI color escape codes
DEFAULT = "\x1b[39m"
RED = "\x1b[31m"
GREEN = "\x1b[32m"
YELLOW = "\x1b[33m"
GRAY = "\x1b[37m"
WHITE = "\x1b[97m"
BLUE = "\x1b[34m"
RESET = "\x1b[0m"
def debug(s):
logging.debug(GRAY + s + RESET)
def info(s):
logging.info(GREEN + s + RESET)
def warning(s):
logging.warning(YELLOW + s + RESET)
def error(s):
logging.error(RED + s + RESET)
def write_include_file(source, flavor='all'):
# Patterns follow that of MANIFEST.in
# (https://packaging.python.org/en/latest/guides/using-manifest-in/),
# and distutils.filelist is used below to parse them.
PATTERN_DICT = {
'rnaseq': [
'include workflows/rnaseq/Snakefile',
'recursive-include workflows/rnaseq/config *',
'include workflows/rnaseq/rnaseq_trackhub.py',
'recursive-include workflows/rnaseq/downstream *.Rmd',
'recursive-include workflows/rnaseq/downstream *.yaml',
],
'chipseq': [
'include workflows/chipseq/Snakefile',
'recursive-include workflows/chipseq/config *',
'include workflows/chipseq/chipseq_trackhub.py',
],
'all': [
'recursive-include wrappers *',
'recursive-include include *',
'recursive-include lib *',
'include env.yml env-r.yml .gitignore',
'include workflows/references/Snakefile',
'recursive-include workflows/references/config *',
'global-exclude __pycache__',
],
'full': [
'include workflows/colocalization/Snakefile',
'recursive-include workflows/colocalization/config *',
'recursive-include workflows/colocalization/scripts *',
'recursive-include workflows/figures *',
'recursive-include workflows/external *',
]
}
patterns = []
if flavor in ('full', 'rnaseq'):
patterns.extend(PATTERN_DICT['rnaseq'])
if flavor in ('full', 'chipseq'):
patterns.extend(PATTERN_DICT['chipseq'])
if flavor == 'full':
patterns.extend(PATTERN_DICT['full'])
patterns.extend(PATTERN_DICT['all'])
def fastwalk(path):
"""
Find all files recursively, but short-circuit if we get to a conda env to
avoid traversing those many files.
"""
path = str(path)
for root, dirs, files in os.walk(path, topdown=True):
if 'conda-meta' in dirs:
dirs[:] = []
files[:] = []
for d in dirs:
yield os.path.join(root, d).replace(path + '/', '')
for f in files:
yield os.path.join(root, f).replace(path + '/', '')
f = filelist.FileList()
f.allfiles = list(fastwalk(source))
for pattern in patterns:
f.process_template_line(pattern)
f.sort()
f.remove_duplicates()
under_version_control = sorted(
sp.check_output(
["git", "ls-tree", "-r", "HEAD", "--name-only"],
universal_newlines=True,
cwd=source,
).splitlines(False),
)
to_transfer = list(set(under_version_control).intersection(f.files))
include = tempfile.NamedTemporaryFile(delete=False).name
with open(include, 'w') as fout:
fout.write('\n\n')
fout.write('\n'.join(to_transfer))
return include
def clone_repo(dest, branch="master", mismatch_ok=False):
if Path(dest).exists():
error("Path {dest} already exists, aborting!".format(**locals()))
sys.exit(1)
URL = "https://github.com/lcdb/lcdb-wf.git"
info("cloning {URL} to {dest}".format(**locals()))
cmds = ["git", "clone", URL, dest]
sp.check_call(cmds)
sp.check_call(["git", "checkout", branch], cwd=dest)
info("Cloned to {dest} and will deploy using branch '{branch}'".format(**locals()))
# check to see if this very file that is running is the same as the one
# that was just cloned -- otherwise it's out of date.
def check_md5(f):
contents = open(f).read()
contents = str(contents).encode("utf-8")
return hashlib.md5(contents).hexdigest()
this_md5 = check_md5(__file__)
that_md5 = check_md5(os.path.join(dest, "deploy.py"))
if (this_md5 != that_md5) and not mismatch_ok:
full_here = Path(__file__).resolve()
full_there = Path(dest) / "deploy.py"
error(
"Files {full_here} and {full_there} do not match! ".format(**locals()) +
"The deploy script you are running appears to be out of date. "
"Please get an updated copy from https://github.com/lcdb/lcdb-wf, perhaps "
"with 'wget https://raw.githubusercontent.com/lcdb/lcdb-wf/master/deploy.py'"
)
sys.exit(1)
def rsync(include, source, dest, rsync_args):
rsync = [
"rsync",
"--relative",
rsync_args,
"--files-from={}".format(include),
source,
dest,
]
sp.check_call(rsync)
def deployment_json(source, dest):
"""
Build the .lcdb-wf-deployment.json file
"""
# First, the last commit:
commit, message = (
sp.check_output(
["git", "log", "--oneline", "-1"], universal_newlines=True, cwd=source
)
.strip()
.split(" ", 1)
)
# When we're deploying:
now = datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d%H%M")
# Where the remote was:
remotes = sp.run(
["git", "remote", "-v"],
universal_newlines=True,
cwd=source,
check=True,
stdout=sp.PIPE,
stderr=sp.STDOUT,
)
remotes = [i.strip() for i in remotes.stdout.splitlines()]
# The branch we're deploying from:
branch = sp.run(
["git", "branch"],
universal_newlines=True,
cwd=source,
check=True,
stdout=sp.PIPE,
stderr=sp.STDOUT,
)
branch = [i for i in branch.stdout.splitlines() if i.startswith("*")]
assert len(branch) == 1
branch = branch[0]
branch = branch.split("* ")[1]
d = {
"git": {
"commit": commit,
"message": message,
"remotes": remotes,
"branch": branch,
},
"timestamp": now,
}
log = Path(dest) / ".lcdb-wf-deployment.json"
with open(log, "w") as fout:
fout.write(json.dumps(d) + "\n")
os.chmod(log, 0o440)
info("Wrote details of deployment to {log}".format(**locals()))
def build_envs(dest, conda_frontend="mamba"):
"""
Build conda environments.
Parameters
----------
dest : str
Destination path. This is the project directory (likely as specified on
the command line with --dest) in which the env and env-r yaml files
should already exist. Envs will be created in here.
conda_frontend : 'mamba' | 'conda'
Which front-end to use (terminology borrowed from Snakemake)
"""
mapping = [
("./env", "env.yml"),
("./env-r", "env-r.yml"),
]
for env, yml in mapping:
info("Building environment " + os.path.join(dest, env))
try:
# conda and mamba can be hard to kill, possibly because they're
# doing threaded things. So we use Popen explicitly to capture the
# process ID so it can be killed if the user hits ^C.
#
# Here we use Popen directly in order to get the process ID so that
# it can be explictly kill upon KeyboardInterrupt.
cmds = [
conda_frontend,
"env",
"create",
"-p",
env,
"--file",
yml,
]
p = sp.Popen(cmds, universal_newlines=True, cwd=dest)
p.wait()
except KeyboardInterrupt:
print("")
error("Killing running {conda_frontend} job, '".format(**locals()) + " ".join(cmds))
p.kill()
sys.exit(1)
if p.returncode:
error("Error running {conda_frontend}, '".format(**locals()) + " ".join(cmds))
sys.exit(1)
full_env = Path(dest) / env
info("Created env {full_env}".format(**locals()))
if __name__ == "__main__":
ap = argparse.ArgumentParser(usage=usage)
ap.add_argument(
"--flavor",
default="full",
help="""Options are {0}. Default is full.""".format(['full', 'rnaseq', 'chipseq']),
)
ap.add_argument(
"--dest", help="""Destination directory in which to copy files""", required=True
)
ap.add_argument(
"--clone",
action="store_true",
help=f"""Make a new clone to a staging area (at the location specified
by --staging which defaults to {default_staging}) and deploy from
there. Useful if using this script as a standalone tool. You can also
use --branch to configure which branch to deploy from that clone."""
)
ap.add_argument(
"--staging",
help="""Only used when --clone is specified. Clone the main git repo to
this directory and do a diff on the deploy.py script found there to
ensure this one is up-to-date, and if so then proceed using the new clone as the source.
""",
)
ap.add_argument(
"--branch",
help="Branch to checkout if using --staging to clone a temporary copy. Default is %(default)s.",
default="master",
)
ap.add_argument(
"--build-envs",
action="store_true",
help="""If specified, conda environments with all dependencies will be
installed into directories called "env" and "env-r" within the directory
provided for --dest.""",
)
ap.add_argument(
"--conda-frontend",
help="Set program (conda or mamba) to use when creating environments. Default is %(default)s.",
default="mamba",
)
ap.add_argument(
"--rsync-args",
help="Options for rsync when deploying to a new directory. Default is %(default)s.",
default="-rlt"
)
ap.add_argument(
"--mismatch-ok",
action="store_true",
help="Used for testing")
args = ap.parse_args()
dest = args.dest
flavor = args.flavor
if args.staging and not args.clone:
print("ERROR: --staging was specified but --clone was not. Did you want to use --clone?", file=sys.stderr)
sys.exit(1)
if args.clone:
if args.staging is None:
args.staging = default_staging
source = os.path.abspath(args.staging)
clone_repo(args.staging, args.branch, mismatch_ok=args.mismatch_ok)
else:
source = Path(__file__).parent.resolve()
include = write_include_file(source, flavor)
rsync(include, source, dest, args.rsync_args)
deployment_json(source, dest)
if args.build_envs:
build_envs(dest, conda_frontend=args.conda_frontend)
warning("Deployment complete in {args.dest}".format(**locals()))