-
Notifications
You must be signed in to change notification settings - Fork 9
/
cli.py
126 lines (107 loc) · 3.77 KB
/
cli.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
import click
import os
import six
import subprocess
@click.group(help='ocdeploy.',
context_settings=dict(help_option_names=['-h', '--help']))
def main():
pass
REPO_DIR = os.path.dirname(__file__)
# These need to be set in the environment so they can be used in the docker-compose
# files.
DOCKER_BASE_DIRS = {
'JUPYTERHUB_DOCKER_DIR': os.path.join(REPO_DIR, 'docker', 'jupyterhub/'),
'DEV_DOCKER_DIR': os.path.join(REPO_DIR, 'docker', 'dev/'),
'GIRDER_DOCKER_DIR': os.path.join(REPO_DIR, 'docker', 'girder/'),
'JENA_DOCKER_DIR': os.path.join(REPO_DIR, 'docker', 'jena/'),
'OPENBABEL_DOCKER_DIR': os.path.join(REPO_DIR, 'docker', 'openbabel/'),
'AVOGADRO_DOCKER_DIR': os.path.join(REPO_DIR, 'docker', 'avogadro/')
}
def _load_dev_env():
env_path = os.path.join(REPO_DIR, 'dev', 'dev.env')
vars = {}
with open(env_path) as f:
for l in f:
l = l.rstrip('\n')
if len(l) == 0 or l.startswith('#'):
continue
(name, value) = l.split('=')
vars[name.strip()] = value.strip();
return vars
BASE_COMPOSE_FILES = [
'docker-compose.yml',
'../girder/docker-compose.yml',
'../jupyterhub/docker-compose.yml',
'../jena/docker-compose.yml',
'../openbabel/docker-compose.yml',
'../avogadro/docker-compose.yml'
]
def _override_files(vars):
files = []
for n in vars:
file = 'docker-compose.%s.yml' % n.lower()
if os.path.exists(os.path.join(REPO_DIR, 'docker', 'dev', file)):
files.append(file)
return files
def _generate_file_switches():
vars = _load_dev_env()
overrides = _override_files(vars)
files = BASE_COMPOSE_FILES + overrides
switches = []
for f in files:
switches.append('-f')
switches.append(f)
return switches
def _generate_command(subcommand):
cmd = _generate_file_switches()
cmd.insert(0, 'docker-compose')
cmd.append(subcommand)
return cmd
def _execute_command(subcommand, args):
env = os.environ.copy()
env.update(_load_dev_env())
env.update(DOCKER_BASE_DIRS)
cwd = os.path.join(REPO_DIR, 'docker', 'dev')
cmd = _generate_command(subcommand)
cmd += args
p = None
try:
print('Executing: %s' % ' '.join(cmd))
p = subprocess.Popen(cmd, cwd=cwd, env=env)
p.communicate()
except KeyboardInterrupt:
p.communicate()
def _pull_extra_images():
"""Pull extra images that docker-compose will not pull."""
cmd = ["docker", "pull"]
extra_images = ["openchemistry/jupyterlab"]
cmd += extra_images
p = None
try:
print('Pulling JupyterLab docker image')
print('Executing: %s' % ' '.join(cmd))
p = subprocess.Popen(cmd)
p.communicate()
except KeyboardInterrupt:
p.communicate()
@main.command('up', short_help='Bring up stack.', help='Bring up stack.',
context_settings=dict(allow_extra_args=True, ignore_unknown_options=True))
@click.pass_context
def up(ctx):
_execute_command('up', ctx.args)
@main.command('down', short_help='Bring down stack.', help='Bring down stack.',
context_settings=dict(allow_extra_args=True, ignore_unknown_options=True))
@click.pass_context
def down(ctx):
_execute_command('down', ctx.args)
@main.command('pull', short_help='Pull images for stack.', help='Pull images for stack.',
context_settings=dict(allow_extra_args=True, ignore_unknown_options=True))
@click.pass_context
def pull(ctx):
_execute_command('pull', ctx.args)
_pull_extra_images()
@main.command('build', short_help='Build images for stack.', help='Build images for stack.',
context_settings=dict(allow_extra_args=True, ignore_unknown_options=True))
@click.pass_context
def build(ctx):
_execute_command('build', ctx.args)