-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootstrap.py
More file actions
212 lines (153 loc) · 5.94 KB
/
Copy pathbootstrap.py
File metadata and controls
212 lines (153 loc) · 5.94 KB
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
"""
Derek Merck <derek_merck@brown.edu>
Spring 2016
The Medical Image Informatics Platform Bootstrapper
Utility scripts for cleaning and setting up config for docker-compose defined MIIP services.
"""
import logging
import argparse
import yaml
from jinja2 import Environment, FileSystemLoader
import pprint
import subprocess
import os
__version__ = "0.1"
__description__ = "Utility scripts for cleaning and setting up config for docker-compose defined MIIP services."
# TODO: If forward in orthanc env, modify and copy the autorouter lua
# TODO: Better setup of Docker env vars or use Docker python API
def parse_args():
parser = argparse.ArgumentParser(prog='MIIP Bootstrapper', description=__description__)
parser.add_argument('-V', '--version',
action='version',
version='%(prog)s (version ' + __version__ + ')')
parser.add_argument('--clean', action='store_true')
parser.add_argument('services', nargs="+",
choices=['orthanc', 'orthanc-receiver', 'xnat'])
_opts = parser.parse_args()
return _opts
def merge(a, b, path=None):
"merges b into a"
if path is None: path = []
for key in b:
if key in a:
if isinstance(a[key], dict) and isinstance(b[key], dict):
merge(a[key], b[key], path + [str(key)])
elif a[key] == b[key]:
pass # same leaf value
else:
pass
# raise Exception('Conflict at %s' % '.'.join(path + [str(key)]))
else:
a[key] = b[key]
return a
def parse_template(template_file, out_file, env):
jinja = Environment(loader=FileSystemLoader('.'))
template = jinja.get_template(template_file)
env['globals'] = global_env
output_from_parsed_template = template.render(env)
# logging.debug(output_from_parsed_template)
with open(out_file, "wb") as fh:
fh.write(output_from_parsed_template)
def exec_sql(sql):
p = ['docker-compose', '-f', 'docker-compose.yml', '-f', 'docker-compose.shadow.yml', 'exec', 'postgres', 'psql', '-c', sql, '-U', 'postgres']
subprocess.call(p)
def add_postgres_database(env):
sql = "CREATE DATABASE {DB_NAME} WITH OWNER {DB_USER}".format(
DB_USER=env['environment']['DB_USER'],
DB_NAME=env['environment']['DB_NAME'])
exec_sql(sql)
exec_sql("\l")
def drop_postgres_database(env):
sql = "DROP DATABASE {DB_NAME}".format(
DB_NAME=env['environment']['DB_NAME'])
exec_sql(sql)
exec_sql("\l")
def drop_postgres_user(env):
# # List roles in DB
# exec_sql("\du")
#
sql = "DROP USER {DB_USER}".format(
DB_USER=env['environment']['DB_USER'])
exec_sql(sql)
#
# # List revised roles in DB
# exec_sql("\du")
def add_postgres_user(env):
# # List roles in DB
# exec_sql("\du")
sql = "CREATE USER {DB_USER} WITH PASSWORD '{DB_PASSWORD}'".format(
DB_USER=env['environment']['DB_USER'],
DB_PASSWORD=env['environment']['DB_PASSWORD'])
exec_sql(sql)
sql = "ALTER USER {DB_USER} WITH CREATEDB".format(
DB_USER=env['environment']['DB_USER'])
exec_sql(sql)
# List revised roles in DB
exec_sql("\du")
def setup_orthanc(env, **kwargs):
add_postgres_user(env)
add_postgres_database(env)
# Create the config file
# Figure out where the data dir belongs
for f in env['volumes']:
if 'db' in f:
env['DATA_DIR'] = f.split(":")[1]
# Figure out where to output the config file
for f in env['volumes']:
if 'shadow' in f:
out_file = f.split(":")[0]
# Create it
parse_template('orthanc.template.json', out_file, env)
def setup_xnat(env, **kwargs):
add_postgres_user(env)
# No need to create the database itself; the xnat builder insists on creating it
# Create the config file
parse_template('xnat-docker/xnat.config.template', 'xnat-docker/xnat.shadow.config', env)
# TODO: Build and tag the xnat template image for docker-compose if it doesn't exist
# TODO: Allow build even if the database already exists <https://github.com/chaselgrove/xnat-docker/issues/2>
def clean_db(env):
drop_postgres_database(env)
drop_postgres_user(env)
if __name__ == "__main__":
print "hi"
logging.basicConfig(level=logging.DEBUG)
logging.debug("MIIP Bootstrapper v{version}".format(version=__version__))
opts = parse_args()
with open("docker-compose.yml", 'r') as f:
env = yaml.load(f)
# Deal with shadow overrides
try:
with open("docker-compose.shadow.yml", 'r') as f:
shadow = yaml.load(f)
env = merge(shadow, env)
except:
logging.debug("No shadow file to merge")
pass
# Deal with extensions
for s, d in env['services'].iteritems():
base_service = d.get('extends', None)
if base_service:
env['services'][s] = merge(d, env['services'][base_service])
logging.info(pprint.pformat(env))
# Get the DB host and port
global_env = {}
global_env['DB_HOST'] = 'postgres'
global_env['DB_PORT'] = env['services']['postgres']['ports'][0].split(":")[0]
# Improve your Docker env
# os.environ['DOCKER_TLS_VERIFY'] = "1"
# os.environ['DOCKER_HOST'] = "tcp://192.168.99.100:2376"
# os.environ['DOCKER_CERT_PATH'] = "/Users/derek/.docker/machine/machines/default"
# os.environ['DOCKER_MACHINE_NAME'] = "default"
if opts.clean:
if 'orthanc' in opts.services:
clean_db(env['services']['orthanc'])
if 'orthanc-receiver' in opts.services:
clean_db(env['services']['orthanc-receiver'])
if 'xnat' in opts.services:
clean_db(env['services']['xnat'])
if 'orthanc' in opts.services:
setup_orthanc(env['services']['orthanc'])
if 'orthanc-receiver' in opts.services:
setup_orthanc(env['services']['orthanc-receiver'])
if 'xnat' in opts.services:
setup_xnat(env['services']['xnat'])