-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayersbox
executable file
·995 lines (837 loc) · 34.2 KB
/
layersbox
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
#!/usr/bin/env python
import subprocess
import os
import sys
import argparse
import shutil
import re
import yaml
import json
import urllib2
import ssl
import six
import signal
import copy
from zipfile import ZipFile
import tarfile
join = os.path.join
programs = ["docker", "docker-compose"]
base = "base.yml"
conf = "box.yml"
minimal = 'minimal.yml'
layersbox_src = os.path.dirname(os.path.realpath(__file__))
minimal_data = '''adapterdata:
container_name: adapter-data
image: learninglayers/adapter-data:0.0.3
env_file:
- common.env
adapter:
container_name: adapter
image: learninglayers/adapter:0.0.1
volumes_from:
- adapterdata
volumes:
- ./ssl/:/usr/local/openresty/conf/ssl/:ro
- ./logs/:/usr/local/openresty/logs/
- ./html/:/usr/local/openresty/nginx/html/:ro
env_file:
- common.env
ports:
- "80:80"
- "443:443"
mysqldata:
container_name: mysql-data
image: learninglayers/mysql-data:0.0.1
env_file:
- common.env
- services/mysql/MYSQL.env
volumes:
- ./mysql-data/backup/:/backup
mysql:
container_name: mysql
image: learninglayers/mysql:0.0.1
env_file:
- common.env
- services/mysql/MYSQL.env
volumes_from:
- mysqldata
# volumes:
# - ./mysql/:/var/lib/mysql/
ports:
- "3306:3306"
mysqlcreate:
container_name: mysql-create
image: learninglayers/mysql-create:0.0.1
links:
- mysql
env_file:
- common.env
- services/mysql/MYSQL.env
- tmp/secret.env
volumes:
- ./tmp/sqlfile/:/sqlfile/
'''
def which(program):
"""Emulates the 'which' utility.
Checks wether a program exists in PATH and is executable
:program: String containing the name of the program
:returns: True if the program exists and is executable, False otherwise
"""
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = join(path, program)
if is_exe(exe_file):
return exe_file
return None
def check_programs(programs):
"""Checks if given programs are in PATH
:program: List of program names
:returns: True if all programs exist, False otherwise
"""
for p in programs:
if not which(p):
print("%s is not installed! Exiting." % p)
return False
return True
def read_url(dir):
"""Reads the LayersBox URL from the common.env file
:dest: The directory of the LayersBox
:returns: the LayersBox URL
"""
dir_env = join(dir, 'common.env')
try:
os.stat(dir_env)
with open(dir_env, 'r') as f:
lines = f.readlines()
p = re.compile('^LAYERS_API_URI=https://(.*)/$')
for line in lines:
line = line.strip()
m = p.match(line)
if m:
return m.group(1)
raise
except:
raise
def read_admin(dir):
"""Reads the LayersBox admin from the common.env file
:dest: The directory of the LayersBox
:returns: the LayersBox admin
"""
dir_env = join(dir, 'common.env')
try:
os.stat(dir_env)
with open(dir_env, 'r') as f:
lines = f.readlines()
p = re.compile('^LAYERS_API_ADMIN=(.*)$')
for line in lines:
line = line.strip()
m = p.match(line)
if m:
return m.group(1)
raise
except:
raise
def read_config(dir):
"""Reads the configuration from the config file
:dir: directory of the layers box
:returns: the configuration
"""
conf_yaml = None
with open(join(dir, conf), 'r') as f:
conf_yaml = yaml.load(f.read())
if conf_yaml:
return conf_yaml
raise
def write_config(dir, config):
"""Writes the configuration to the config file
:dir: directory of the layers box
"""
with open(join(dir, conf), 'w') as f:
f.write(yaml.dump(config))
def check_tls(dir, url):
"""Checks if the TLS certificate and key are where they should be
:dest: folder of the LayersBox
:returns: True if everything is OK, False otherwise
"""
ssl_path = join(dir, 'ssl/')
try:
os.stat(join(ssl_path, url + '.pem'))
os.stat(join(ssl_path, url + '.key'))
return True
except:
print("Please put your TLS certificate {0}.pem and key {0}.key in {1}"
.format(url, join(dir, 'ssl/')))
return False
def generate_cert(dir, domain):
"""Generates a self-signed TLS certificate
:dir: folder where the certificate and key should be placed
:domain: domain for that the certificate will be valid
:returns: True if successful, False otherwise
"""
if not which("openssl"):
print("openssl is not installed! Can't generate a certificate.")
return False
privkey_file = join(dir, domain + ".key")
csr_file = join(dir, domain + ".csr")
cert_file = join(dir, domain + ".pem")
subj = "/C=DE/ST=NRW/O=RWTH/localityName=Aachen/" +\
"commonName={}/organizationalUnitName=ACIS/" +\
if not os.path.isfile(privkey_file):
print("Generating key file {}".format(privkey_file))
subprocess.check_output(["openssl", "genrsa",
"-out", privkey_file,
"4096"])
print("Generating CSR file {}".format(csr_file))
subprocess.check_output(["openssl", "req",
"-new",
"-batch",
"-subj", subj.format(domain),
"-key", '{}'.format(privkey_file),
"-out", '{}'.format(csr_file)])
print("Generating certificate file {}".format(cert_file))
subprocess.check_output(["openssl", "x509",
"-req",
"-days", "3650",
"-signkey", '{}'.format(privkey_file),
"-in", '{}'.format(csr_file),
"-out", '{}'.format(cert_file)])
# CSR file is not needed anymore
os.remove(csr_file)
else:
print("Certificae or key already exists, refusing to overwrite")
return False
return True
def merge_ymls(base, addon, databases=[], oidcclients=[]):
"""Merges two yml files into one
:base: base yml data
:addon: additional component data
:returns: merged yml data
"""
# top level containers, that is names like "mysql" and "mysql-data".
# "name" is the container name, "obj" contains the settings
for name, obj in addon.copy().iteritems():
# if there is a key called "actions" then return its value, otherwise return empty list
actions = 'actions' in obj and obj.pop('actions') or []
# merges each container of the new service's docker-compose.yml into the base.yml
merged = data_merge(base, {name: obj})
# the following code makes sure that databases are created
for action in actions:
if type(action) == dict:
for k, v in action.iteritems():
if k == "create_databases":
print("Added database {}".format(v))
databases.append(v)
elif k == "create_oidcclients":
print("Added OIDC client {}".format(v))
oidcclients.append(v)
else:
merged, databases, oidcclients = merge_ymls(merged, {k: v}, databases, oidcclients)
else:
print("Action is {}".format(action))
raise
# print(merged)
return merged, databases, oidcclients
class YamlReaderError(Exception):
pass
def data_merge(a, b):
"""merges b into a and return merged result
based on
http://stackoverflow.com/questions/7204805/python-dictionaries-of-dictionaries-merge
and extended to also merge arrays and to replace the content of keys with
the same name
NOTE: tuples and arbitrary objects are not handled as it is totally
ambiguous what should happen"""
key = None
# ## debug output
# sys.stderr.write("DEBUG: %s to %s\n" %(b,a))
try:
if a is None or isinstance(a, (six.string_types, float,
six.integer_types)):
# border case for first run or if a is a primitive
a = b
elif isinstance(a, list):
# lists can be only appended
if isinstance(b, list):
# merge lists
for i in b:
if i not in a:
a.append(i)
else:
# append to list
if b not in a:
a.append(b)
elif isinstance(a, dict):
# dicts must be merged
if isinstance(b, dict):
for key in b:
if key in a:
a[key] = data_merge(a[key], b[key])
else:
a[key] = b[key]
else:
raise YamlReaderError(
'Cannot merge non-dict "{}" into dict "{}"'.format(b, a))
else:
raise YamlReaderError(
'NOT IMPLEMENTED "{}" into "{}"'.format(b, a))
except TypeError as e:
raise YamlReaderError(
'TypeError "{}" in key "{}" when merging "{}" into "{}"'
.format(e, key, b, a))
return a
def generate_yml(dir):
"""Generates a yml file for docker-compose with the base services
and all installed ones.
:dir: directory of the layersbox
"""
base_config = None
with open(join(dir, base), 'r') as f:
base_config = yaml.load(f.read())
merged = copy.deepcopy(base_config)
# reads in config file that contains all installed services
config = read_config(dir)
services = config['services']
databases = []
oidcclients = []
# for with service_name e.g. being "documentation" and service the value of the
# service called "documentation"
for service_name, service in services.iteritems():
# read in the yml file of the service (docker-compose.yml)
service_yml = service['yml']
# open docker-compose.yml file for each service
with open(join(dir, service_yml), 'r') as addon_file:
addon = yaml.load(addon_file.read())
# fix env_files definitions in addon to refer to path where service's files were copied
for subservice_values in addon.values():
if 'env_file' in subservice_values:
new_envs = []
for env in subservice_values['env_file']:
# common.env shouldn't redirect to services' definition, it is *common*
if env == 'common.env' or env.startswith('services'):
new_envs.append(env)
else:
new_envs.append('services/%s/%s' % (service_name, env))
subservice_values['env_file'] = new_envs
# merged is the base one
merged, more_databases, more_oidcclients = merge_ymls(merged, addon)
# "extend" extends list by appending elements from the iterable
databases.extend(more_databases)
oidcclients.extend(more_oidcclients)
# print(yaml.dump(merged))
with open(join(dir, "docker-compose.yml"), 'w') as f:
f.write(yaml.dump(merged))
if databases:
with open(join(dir, "databases"), 'w') as f:
for dbs in databases:
for db in dbs:
f.write("{}\n".format(db))
def box_install(args):
"""Adds a new component to a LayersBox
:args: parsed arguments
:returns: shell return code
"""
# the following two lines come from the command line
dir = args.directory
repo = args.repository
#version = args.version
#tag = args.tag
if "#" in repo:
repo, version = repo.split("#")
##If tag specified instead of a version, treat the tag as the new version.
#elif version == None and tag != None:
# version = tag
else:
version = "master"
owner, service_name = repo.split("/")
url = "https://github.com/{}-Dockerfiles/archive/{}.tar.gz".format(repo, version)
servicedir = join(dir, "services")
tmp_servicedir = join(dir, "tmp", "services")
release_tar = join(tmp_servicedir,
'{}-{}.tar.gz'.format(repo, version).replace("/", "-"))
addon_yml = join(servicedir, service_name, "docker-compose.yml")
try:
check_yml(dir)
except:
print("This directory does not seem to contain a LayersBox")
return 1
# only download release if it was not yet downloaded
if version is "master" or not os.path.isfile(addon_yml):
# check if "services" directory is already existing, if not, create it
if not os.path.exists(tmp_servicedir):
os.makedirs(tmp_servicedir)
if not os.path.exists(servicedir):
os.makedirs(servicedir)
try:
response = urllib2.urlopen(url)
except urllib2.HTTPError:
print "Failed to download service package from %s" % url
return 1
with open(release_tar, 'w') as f:
f.write(response.read())
with tarfile.open(release_tar) as t:
if os.path.exists(join(dir, "services", service_name)):
shutil.rmtree(join(dir, "services", service_name))
os.makedirs(join(dir, "services", service_name))
dirname = list(x.name for x in t)[0]
t.extractall(path=servicedir)
os.rename(join(servicedir, dirname),
join(servicedir, service_name))
# read base.yml which contains the minimal Layers Box
config = read_config(dir)
services = config['services']
services[service_name] = {
'owner': owner,
'version': version,
'yml': addon_yml,
}
write_config(dir, config)
generate_yml(dir)
database_env_files = []
oidcclient_env_files = []
# read out service's docker-compose.yml file, find create_databases there
# since databases only need to be created at install time.
with open(addon_yml, 'r') as f:
addon = yaml.load(f.read())
for name, obj in addon.copy().iteritems():
# if there is a key called "actions" then return its value, otherwise return empty list
actions = 'actions' in obj and obj.pop('actions') or []
# the following code makes sure that databases to be created are identified
for action in actions:
if type(action) == dict:
for k, v in action.iteritems():
if k == "create_databases":
database_env_files.append(v)
elif k == "create_oidcclients":
oidcclient_env_files.append(v)
# create databases with the list of environment files to check for database names
create_databases(dir, service_name, database_env_files)
# create oidcclients with the list of environment files to check for oidc settings
create_oidcclients(dir, service_name, oidcclient_env_files)
# start box with updated docker-compose.yml
subprocess.check_output(["docker-compose", "up", "-d"])
# now, update the adapter to get the internal IP of the newly added service
update_adapter(config, servicedir, service_name);
return 0
def box_uninstall(args):
"""Removes a service from a LayersBox
:args: parsed arguments
:returns: shell return code
"""
# the following two lines come from the command line
dir = args.directory
repo = args.repository
#version = args.version
#tag = args.tag
if "#" in repo:
repo, version = repo.split("#")
organization, repo = repo.split("/")
images = []
# read out docker-compose.yml of service
compose_yaml = None
with open(join(dir, "services", repo, 'docker-compose.yml'), 'r') as f:
compose_yaml = yaml.load(f.read())
for container, obj in compose_yaml.iteritems():
image = 'image' in obj and obj.pop('image') or None
images.append(image)
#TODO: remove nginx.adapted.conf from adapter and send HUP signal
for image in images:
# get the container numbers
container_numbers = subprocess.check_output(["docker", "ps", "-aq", "--filter", "ancestor={}".format(image)])
if container_numbers == "":
break
# stop containers in docker-compose.yml
subprocess.check_output(["docker", "stop",
"{}".format(container_numbers).replace('\n', ' ').rstrip()])
# remove containers
subprocess.check_output(["docker", "rm",
"{}".format(container_numbers).replace('\n', ' ').rstrip()])
# remove images
subprocess.check_output(["docker", "rmi", "{}".format(image)])
# delete from box.yml
config = read_config(dir)
services = config.get('services')
if repo in services.keys():
del services[repo]
config['services'] = services
write_config(dir, config)
# regenerate main docker-compose.yml
generate_yml(dir)
#TODO: delete service subfolder in services
print("Successfully removed service {}.".format(args.repository))
return 0
def update_adapter(config, service_directory, service_name):
"""Informs the adapter of a new service or updates new service configuration in the adapter.
:config: parsed box.yml
:service_directory: the directory where the services are saved
:service_name: the name of the newly added service, or None for updating all services
:returns: shell return code
"""
print("Updating adapter...")
# a list of all service names to update
servicesToUpdate = []
if service_name is None:
# update IPs of all services
services = config['services']
for service_name, service in services.iteritems():
if os.path.isfile(join(service_directory, service_name, "nginx.conf")):
servicesToUpdate.append(service_name)
else:
# parse nginx.conf of the service to get a list of all its proxied containers
if os.path.isfile(join(service_directory, service_name, "nginx.conf")):
servicesToUpdate.append(service_name)
proxied_servers = []
proxied_ips = {}
for service_name in servicesToUpdate:
# parse nginx.conf of all services to get a list of all proxied containers
#TODO: make sure to only read URLs after a "proxy_pass"
with open(join(service_directory, service_name, "nginx.conf")) as f:
for w in re.findall('(https?:\/\/[A-Za-z0-9.-]+)',f.read()):
proxied_servers.append(w.replace('http://', ''))
# now get the IPs of the proxied containers
for proxy in proxied_servers:
container_ip = subprocess.check_output(["docker", "inspect",
"-f", "'{{ .NetworkSettings.IPAddress }}'", proxy]).replace("'", "").strip()
proxied_ips[proxy] = container_ip
# finally, replace the values
for service_name in servicesToUpdate:
with open(join(service_directory, service_name, "nginx.conf")) as infile, open(join(service_directory, service_name, "nginx.adapted.conf"), 'w') as outfile:
for line in infile:
for src, target in proxied_ips.iteritems():
line = line.replace("http://" + src, "http://" + target)
outfile.write(line)
# copy nginx.adapted.conf into container
#TODO: run the whole script directly from python
subprocess.check_output([join(layersbox_src,"add_nginx_conf.sh"), service_name])
# send the adapter the HUP kill signal
subprocess.check_output(["docker", "kill", "--signal=HUP", "adapter"])
return 0
def box_start(args):
"""Starts a LayersBox"""
dir = args.directory
try:
url = read_url(args.directory)
if not check_tls(dir, url):
return 1
except:
print("This directory does not seem to contain a Layers Box configuration.")
return 1
subprocess.check_output(["docker-compose", "up", "-d"])
# regenerate nginx.adapted.conf files for all proxied services
config = read_config(dir)
servicedir = join(dir, "services")
update_adapter(config, servicedir, None);
print("You can now reach your Layers Box at https://{}/".format(url))
if os.path.isfile(".rest"):
f = open(".rest","r")
pid = f.read()
p = int(pid)
if p==0:
proc = subprocess.Popen(['python', 'layersbox-studio.py'],shell=False)
f = open('.rest', 'w+')
f.write(str(proc.pid))
return 0
def box_stop(args):
"""Stops a LayersBox"""
try:
read_url(args.directory)
except:
print("This directory does not seem to contain a Layers Box configuration.")
return 1
subprocess.check_output(["docker-compose", "stop"])
if os.path.isfile(".rest"):
f = open(".rest","r")
pid = f.read()
p = int(pid)
if p>0:
os.kill(p, signal.SIGTERM)
f = open(".rest","w")
f.write("0")
return 0
def box_logs(args):
"""Shows logs of a LayersBox"""
subprocess.check_output(["docker-compose", "logs"])
return 0
def box_ps(args):
"""Shows the states of the services inside the LayersBox"""
print(subprocess.check_output(["docker-compose", "ps"]))
return 0
def box_init(args):
"""Initializes a LayersBox"""
print("Initializing LayersBox...")
installCore = False
dir = args.directory
if not os.path.exists(dir):
os.makedirs(dir)
admin = "THIS SHOULD HAVE BEEN REPLACED"
url = "THIS SHOULD HAVE BEEN REPLACED"
try:
admin = read_admin(dir)
print("Found existing admin, using admin:{}"
.format(admin))
except:
admin = raw_input("Enter admin username: ")
with open(join(dir, "common.env"), 'w') as f:
f.write("LAYERS_API_ADMIN={}\n".format(admin))
try:
url = read_url(dir)
print("Found existing configuration, using URL https://{}/"
.format(url))
except:
url = raw_input(
"Enter the URL the box should be reachable at. \
(Don't put a / at the end)\nhttps://").strip("/")
with open(join(dir, "common.env"), 'a') as f:
f.write("LAYERS_API_URI=https://{}/\n".format(url))
f.write("LAYERS_APP_URI=https://{}/\n".format(url))
os.makedirs(join(dir, "tmp"))
with open(join(dir, "tmp", "secret.env"), 'w') as f:
f.write("\n".format(url))
os.makedirs(join(dir, "services", "mysql"))
with open(join(dir, "services", "mysql", "MYSQL.env"), 'w') as f:
f.write("MYSQL_ROOT_PASSWORD=pass")
# Todo install only if you enter something
fed = raw_input("Enter Layersbox-bootstrap ip and port: ")
with open(join(dir, "common.env"), 'a') as f:
f.write("FED_BOOTSTRAP="+fed+"\n")
# Todo install only if you enter something
rest = raw_input("Do you want to install core services? (y/n): ")
if rest=="y" or rest=="Y":
with open(join(dir, ".rest"), 'w+') as f:
f.write("0")
installCore = True
dir_yml = join(dir, base)
try:
os.stat(dir_yml)
print("Found docker-compose configuration")
except:
print("No docker configuration found, using minimal configuration")
with open(dir_yml, 'w') as f:
f.write(minimal_data)
shutil.copy(dir_yml, join(dir, "docker-compose.yml"))
ssl_dir = join(dir, 'ssl/')
if not os.path.exists(ssl_dir):
os.makedirs(ssl_dir)
if not check_tls(dir, url):
print("No TLS certificate found, generating a self signed certificate")
generate_cert(join(dir, "ssl/"), url)
# Downloading and unzippind the landing page
response = urllib2.urlopen(
'https://github.com/learning-layers/LayersAPI-LandingPage/releases/' +
'download/v1.0.0/dist.zip')
page_zip = join(dir, 'LandingPage.zip')
with open(page_zip, 'w') as f:
f.write(response.read())
with ZipFile(page_zip, 'r') as zip:
html_dir = join(dir, 'html/')
if not os.path.exists(html_dir):
os.makedirs(html_dir)
zip.extractall(html_dir)
os.remove(page_zip)
config = {'services': {}}
write_config(dir, config)
print("Initializion complete.")
print("Make sure port 80 and 443 are not in use on this machine")
# Workaround for logs dir not being available in the adapterdata container
log_dir = join(dir, 'logs/')
if not os.path.exists(log_dir):
os.makedirs(log_dir)
if(installCore):
print("Installing openldap:")
os.system("python layersbox install learning-layers/openldap")
print("Installing openidconnect:")
os.system("python layersbox install learning-layers/openidconnect")
print("Installing openldapaccount:")
os.system("python layersbox install learning-layers/openldapaccount")
print("Installing fed:")
os.system("python layersbox install learning-layers/fed")
print("Installing layersbox-studio:")
os.system("python layersbox install learning-layers/layersbox-studio")
if os.path.isfile(".rest"):
f = open(".rest","r")
pid = f.read()
p = int(pid)
if p>0:
os.kill(p, signal.SIGTERM)
proc = subprocess.Popen(['python', 'layersbox-studio.py'],shell=False)
f = open('.rest', 'w+')
f.write(str(proc.pid))
else:
print("You can start your LayersBox with 'layersbox start'")
return 0
def create_databases(dir, service_name, database_env_files):
servicedir = join(dir, "services")
for env_file in database_env_files:
# we support one database per env file currently
db_key = ""
db_name = ""
db_username = ""
db_file = ""
db_exists = False
if isinstance(env_file, (tuple, list)):
env_file = env_file[0]
with open(join(servicedir, service_name, env_file)) as infile:
for line in infile:
if "_DB_NAME" in line:
db_key = line.split("_DB_NAME=")[0]
db_name = line.split("=")[1]
elif "_DB_USER" in line:
db_username = line.split("=")[1]
elif "_DB_FILE" in line:
db_file = line.split("=")[1].replace('\n', '').replace('\r', '')
db_exists = True
shutil.copyfile(join(servicedir, service_name, db_file), join(dir, "tmp", "sqlfile", "db.sql"))
# save name and username to secret.env that mysql-create is using
with open(join(dir, "tmp", "secret.env"), 'w') as f:
#f.writelines(["SERVICE_DB_NAME=%s" % db_name, "SERVICE_DB_USER=%s" % db_username])
f.write('SERVICE_DB_NAME={}SERVICE_DB_USER={}'.format(db_name,db_username))
if not db_exists:
f.write("SERVICE_DB_EXISTS=True")
# generate database and parse output
mysqlcreate_output = subprocess.check_output(["docker-compose", "run", "mysqlcreate"])
db_password = mysqlcreate_output.split(" -p")[1].split(" -hmysql")[0]
# write password to env file
with open(join(servicedir, service_name, env_file), "a") as outfile:
outfile.write("\n{}_DB_PASS={}\n".format(db_key, db_password))
# clean up and delete secret.env content, otherwise db is created again
with open(join(dir, "tmp", "secret.env"), "w"):
pass
# clean up and delete sqlfile
try:
os.remove(join(dir, "tmp", "sqlfile", "db.sql"))
except OSError:
pass
return 0
def create_oidcclients(dir, service_name, oidcclient_env_files):
servicedir = join(dir, "services")
layers_api_uri = None
with open(join(dir, 'common.env')) as common_file:
for line in common_file:
if "LAYERS_API_URI" in line:
layers_api_uri = line.split("=")[1].replace('\n', '')
# For Python >2.7.9, give context to override local unsigned https warnings
# evil workaround from http://stackoverflow.com/questions/19268548/python-ignore-certicate-validation-urllib2
if hasattr(ssl, 'create_default_context'):
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
else:
# For older pythons the warnings do not stop the show and can be ignored
# (anyways, they don't have the ssl.create_default_context.)
ctx = None
for env_file in oidcclient_env_files:
# we support one OIDC client per env file currently
oidc_key = None
oidc_json = None
with open(join(servicedir, service_name, env_file[0])) as infile:
for line in infile:
if "_OIDC_FILE" in line:
oidc_key = line.split("_OIDC_FILE=")[0]
oidc_json_file = line.split("=")[1].replace('\n', '')
with open (join(servicedir, service_name, oidc_json_file), "r") as myfile:
oidc_json = myfile.read().replace('\n', '')
oidc_json = oidc_json.replace('LAYERS_API_URI', layers_api_uri)
oidc_json = oidc_json.replace('LAYERS_APP_URI', layers_api_uri)
# run HTTP POST against OIDC endpoint and then save the results into the env file
req = urllib2.Request(layers_api_uri + 'o/oauth2/register')
req.add_header('Content-Type', 'application/json')
if ctx:
response = urllib2.urlopen(req, oidc_json, context=ctx).read()
else:
response = urllib2.urlopen(req, oidc_json).read()
oidc_config = json.loads(response)
#print(oidc_config)
# save values to env file
with open(join(servicedir, service_name, env_file[0]), "a") as outfile:
outfile.write("{}_OIDC_CLIENT_ID={}\n".format(oidc_key, oidc_config['client_id']))
outfile.write("{}_OIDC_CLIENT_SECRET={}\n".format(oidc_key, oidc_config['client_secret']))
return 0
def parse_args():
"""Parses the command line arguments to choose which action should be taken
"""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(
help='commands (use "layersbox -h command" to find out about their \
parameters)',
dest='command')
# debug_parser = subparsers.add_parser('debug', help='Generates detailed information for debugging')
# debug_parser.add_argument('--debug', action='store_true', default='False')
start_parser = subparsers.add_parser('start', help='Starts the LayersBox')
start_parser.add_argument('--directory', '-d', action='store',
help='Directory of the box', default='.')
start_parser.set_defaults(func=box_start)
stop_parser = subparsers.add_parser("stop", help="Stops the LayersBox")
stop_parser.add_argument('--directory', '-d', action='store',
help='Directory of the box', default='.')
stop_parser.set_defaults(func=box_stop)
init_parser = subparsers.add_parser(
"init",
help="Initializes a LayersBox in the current directory")
init_parser.add_argument(
'--directory', '-d', action='store',
help='The directory to use', default='.')
init_parser.set_defaults(func=box_init)
# logs_parser = subparsers.add_parser(
# 'logs', help='Shows logs of the LayersBox')
# logs_parser.add_argument('--directory', '-d', action='store',
# help='Directory of the box', default='.')
# logs_parser.set_defaults(func=box_logs)
ps_parser = subparsers.add_parser(
'status',
help='Shows the states of the services inside the LayersBox')
ps_parser.add_argument('--directory', '-d', action='store',
help='Directory of the box', default='.')
ps_parser.set_defaults(func=box_ps)
install_parser = subparsers.add_parser(
'install', help='Adds a component to the LayersBox')
install_parser.add_argument(
'repository', action='store',
help='Github repository of the service. Example: "owner/repo#version"')
install_parser.add_argument('--directory', '-d', action='store',
help='Directory of the box', default='.')
# install_parser.add_argument(
# '--version', '-v', action='store', nargs=1,
# help='The version to use; incompatible with the "-t" option.')
# install_parser.add_argument(
# '--tag', '-t', action='store', nargs=1,
# help='The tag to use; incompatible with the "-v" option.', default='latest')
# gr = parser.add_mutually_exclusive_group()
# gr.add_argument("-v", "--version")
# gr.add_argument("-t", "--tag")
install_parser.set_defaults(func=box_install)
uninstall_parser = subparsers.add_parser(
'uninstall', help='Removes a component from the LayersBox')
uninstall_parser.add_argument(
'repository', action='store',
help='Github repository of the service. Example: "owner/repo#version"')
uninstall_parser.add_argument('--directory', '-d', action='store',
help='Directory of the box', default='.')
uninstall_parser.set_defaults(func=box_uninstall)
return parser.parse_args()
def check_yml(dir):
"""Check if base.yml exists
:returns: True if it exists, False otherwise
"""
try:
os.stat(join(dir, base))
return True
except:
print("{} is missing in directory {}.".format(base, dir))
sys.exit(1)
return False
def main():
"""Entrypoint
"""
if not check_programs(programs):
sys.exit(1)
parsed = parse_args()
sys.exit(parsed.func(parsed))
if __name__ == "__main__":
main()