-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcati_portal_ctl
executable file
·160 lines (133 loc) · 5.57 KB
/
cati_portal_ctl
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
#! /usr/bin/env python3
import glob
import os
import os.path as osp
import sys
import shutil
import subprocess
import tempfile
import click
class Singularity:
def __init__(self):
self.directory = osp.dirname(osp.dirname(osp.abspath(__file__)))
self.sif = osp.join(self.directory, 'cati_portal.sif')
self.tmp = osp.join(self.directory, 'tmp')
self.env = os.environ.copy()
self.env['SINGULARITY_TMPDIR'] = self.tmp
def __call__(self, cmd, **kwargs):
check_call(['singularity', 'run', '-B', '%s:/cati_portal' % self.directory, self.sif] + cmd,
env=self.env, **kwargs)
def output(self, cmd, **kwargs):
return check_output(['singularity', 'run', '-B', '%s:/cati_portal' % self.directory, self.sif] + cmd,
env=self.env, **kwargs)
def start(self, name):
check_call(['singularity', 'instance', 'start', '-B', '%s:/cati_portal' % self.directory, self.sif, name],
env=self.env)
def stop(self, name):
check_call(['singularity', 'instance', 'stop', name],
env=self.env)
for f in glob.glob(osp.join(self.tmp, 'cati_portal.sif.%s.*' % name)):
os.remove(f)
def shell(self, name):
instance_name = 'instance://%s' % name
# Check if the Singularity instance is running
running = False
process = subprocess.Popen(['singularity', 'instance','list'], stdout=subprocess.PIPE)
output, unused_err = process.communicate()
output=output.decode()
instances = set(line.split()[0] for line in output.split('\n')[1:] if line)
if instance_name in instances:
check_call(['singularity', 'shell', instance_name],
env=self.env)
else:
check_call(['singularity', 'shell', '-B', '%s:/cati_portal' % self.directory, self.sif],
env=self.env)
def build(self, output_image, singularity_dir, recipe_file):
# Try to write in output directory with root. If not possible,
# image must be created in a temporary directory and copied.
try:
test_file = output_image + '.test'
subprocess.check_call(['sudo', 'touch', test_file])
except subprocess.CalledProcessError:
use_tmp = True
else:
use_tmp = False
subprocess.check_call(['sudo', 'rm', test_file])
if use_tmp:
tmp = tempfile.NamedTemporaryFile(suffix='.sif')
os.remove(tmp.name)
check_call(['sudo', 'singularity', 'build', tmp.name, osp.join(singularity_dir, recipe_file)], cwd=singularity_dir)
subprocess.check_call(['sudo', 'chown', '%s:%s' % (os.environ['USER'],os.environ['USER']), tmp.name])
check_call(['cp', tmp.name, output_image])
else:
check_call(['sudo', 'singularity', 'build', output_image, osp.join(singularity_dir, recipe_file)], cwd=singularity_dir)
@click.group()
def cli():
pass
def check_call(cmd, **kwargs):
print('-'*60)
print(' '.join("'{0}'".format(i) for i in cmd))
print('-'*60)
if input is not None and 'universal_newlines' not in kwargs:
kwargs['universal_newlines'] = True
subprocess.run(cmd, check=True, **kwargs)
def check_output(cmd, input=None, **kwargs):
print('-'*60)
print(' '.join("'{0}'".format(i) for i in cmd))
print('-'*60)
if input is not None and 'universal_newlines' not in kwargs:
kwargs['universal_newlines'] = True
return subprocess.run(cmd, check=True, stdout=subprocess.PIPE, **kwargs).stdout
@cli.command()
@click.option('--delete-existing', is_flag=True)
@click.option('--pg_port', default='54321') # instead of PostgreSQL default port 5432
@click.option('--http_port', default='8080') # instead of HTTP default port 80
def install(delete_existing, pg_port, http_port):
directory = osp.dirname(osp.dirname(osp.abspath(__file__)))
print('Creating new cati_portal instance in', directory)
base_sif = osp.join(directory, 'base.sif')
cati_portal_sif = osp.join(directory, 'cati_portal.sif')
git = osp.join(directory, 'git')
venv = osp.join(directory, 'venv')
singularity = Singularity()
if delete_existing:
if osp.exists(venv):
print('Delete', venv)
shutil.rmtree(venv)
singularity_dir = osp.join(git, 'singularity')
if not osp.exists(base_sif):
singularity.build(base_sif, singularity_dir, 'base.recipe')
if not osp.exists(cati_portal_sif):
singularity.build(cati_portal_sif, singularity_dir, 'cati_portal.recipe')
if not osp.exists(venv):
singularity(['python3', '-m', 'venv', '--system-site-packages', '/cati_portal/venv'])
pip = '/cati_portal/venv/bin/pip'
singularity([pip, 'install', '--upgrade', 'pip'])
singularity([pip, 'install', '--editable', '/cati_portal/git'])
singularity(['/cati_portal/venv/bin/python', '-m', 'cati_portal.install', str(delete_existing), pg_port, http_port])
@cli.command()
def start():
singularity = Singularity()
singularity.start('cati_portal')
@cli.command()
def stop():
singularity = Singularity()
singularity.stop('cati_portal')
@cli.command()
def restart():
singularity = Singularity()
singularity.stop('cati_portal')
singularity.start('cati_portal')
@cli.command()
def shell():
singularity = Singularity()
singularity.shell('cati_portal')
if __name__ == '__main__':
try:
cli()
except Exception as e:
print('-'*60)
print('ERROR:', e, file=sys.stderr)
raise
sys.exit(1)
sys.exit(0)