forked from rih3d/cirr-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.py
210 lines (152 loc) · 6.54 KB
/
bootstrap.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
'''
Derek Merck <[email protected]>
Vanessa Sochat <[email protected]>
Spring 2016
The Medical Image Informatics Platform Bootstrapper
Utility scripts for cleaning and setting up config for docker-compose defined MIIP services.
'''
import argparse
import logging
from jinja2 import Environment, FileSystemLoader
import os
import pprint
import re
import subprocess
import yaml
__version__ = "0.2"
__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 parse_template(template_file, out_file, env):
'''parse_template will take a template file and an output
file, and parse variables from the environment into it
'''
jinja = Environment(loader=FileSystemLoader('.'))
template = jinja.get_template(template_file)
env['globals'] = global_env
parsed_template = template.render(env)
# logging.debug(output_from_parsed_template)
with open(out_file, "w") as fh:
fh.write(parsed_template)
def get_container_id(name):
'''get_container_id will return the docker container id of a running container,
based on the name in the compose file
:param name: the name of the docker-compose container (eg, postgres)
'''
cmd = subprocess.Popen(['docker-compose','ps','-q',name],
stdout=subprocess.PIPE)
container_id = cmd.stdout.read().decode('utf-8').strip('\n')
logging.debug("Container id found for %s as %s", name, container_id)
return container_id
def exec_sql(sql,postgres_container):
p = ['docker', 'exec', postgres_container, 'psql', '-c', sql, '-U', 'postgres']
subprocess.call(p)
def add_postgres_database(user,database_name,postgres_container):
'''add_postgres_database will take a database name, username
and send a command to docker-compose to parse
:param user: the user to create the database for
:param database_name: the name of the database to create
'''
sql = "CREATE DATABASE {DB_NAME} WITH OWNER {DB_USER}".format(
DB_USER=user,
DB_NAME=database_name)
exec_sql(sql,postgres_container)
exec_sql("\l",postgres_container)
def drop_postgres_database(database_name,postgres_container):
'''drop_postgres_database will drop the database
:param database_name: the name of the database
:param postgres_container: the postgres_container to use
'''
sql = "DROP DATABASE {DB_NAME}".format(
DB_NAME=database_name)
exec_sql(sql,postgres_container)
exec_sql("\l",postgres_container)
def drop_postgres_user(user,postgres_container):
# # List roles in DB
# exec_sql("\du")
#
sql = "DROP USER {DB_USER}".format(
DB_USER=user)
exec_sql(sql,postgres_container)
#
# # List revised roles in DB
# exec_sql("\du")
def add_postgres_user(user,password,postgres_container):
# # List roles in DB
# exec_sql("\du")
sql = "CREATE USER {DB_USER} WITH PASSWORD '{DB_PASSWORD}'".format(
DB_USER=user,
DB_PASSWORD=password)
exec_sql(sql,postgres_container)
sql = "ALTER USER {DB_USER} WITH CREATEDB".format(
DB_USER=user)
exec_sql(sql,postgres_container)
# List revised roles in DB
exec_sql("\du",postgres_container)
def setup_orthanc(env, postgres_container, **kwargs):
add_postgres_user(user=env['environment']['DB_USER'],
password=env['environment']['DB_PASSWORD'],
postgres_container=postgres_container)
add_postgres_database(user=env['environment']['DB_USER'],
database_name=env['environment']['DB_NAME'],
postgres_container=postgres_container)
# Create the config file
# Figure out where the data dir belongs
for f in env['volumes']:
if re.search("^data:",f):
env['DATA_DIR'] = f.split(":")[1]
# Figure out where to output the config file
for f in env['volumes']:
if re.search('shadow',f):
out_file = f.split(":")[0]
# Create it
parse_template('orthanc.template.json', out_file, env)
def setup_xnat(env, postgres_container, **kwargs):
add_postgres_user(user=env['environment']['DB_USER'],
password=env['environment']['DB_PASSWORD'],
postgres_container=postgres_container)
# 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,postgres_container):
drop_postgres_database(env,postgres_container)
drop_postgres_user(env,postgres_container)
if __name__ == "__main__":
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)
logging.info(pprint.pformat(env))
# Get the running container id
postgres_container = get_container_id(name='postgres')
# Get the DB host and port
global_env = {}
global_env['DB_HOST'] = 'postgres'
global_env['DB_PORT'] = 5432
# Do we want to clean database?
if opts.clean:
if 'orthanc' in opts.services:
clean_db(env['orthanc'],postgres_container)
if 'orthanc-receiver' in opts.services:
clean_db(env['orthanc-receiver'],postgres_container)
if 'xnat' in opts.services:
clean_db(env['xnat'],postgres_container)
if 'orthanc' in opts.services:
setup_orthanc(env['orthanc'],postgres_container)
if 'orthanc-receiver' in opts.services:
setup_orthanc(env['orthanc-receiver'],postgres_container)
if 'xnat' in opts.services:
setup_xnat(env['xnat'],postgres_container)