-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmanage.py
executable file
·113 lines (100 loc) · 4.22 KB
/
manage.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
#!/usr/bin/env python3
import argparse
import configparser
import json
import logging
import os
import copy
import shutil
import subprocess
logging.basicConfig(format='[%(levelname)s]\t: %(message)s',
level=logging.DEBUG)
software_name = [path for path in os.listdir() if os.path.isdir(path)]
software_name.remove('.git')
enable_options = ["enable", "disable", "default"]
def deploy(config_path, name, tag, force):
config = configparser.ConfigParser()
config.read(config_path)
logging.info("Read in config file.")
module_path = config["common"]["module_path"]
software_list = copy.deepcopy(config.sections())
software_list.remove("common")
os.makedirs(module_path, exist_ok=True)
for section in software_list:
tags = json.loads(config.get(section, "tag"))
imgs = json.loads(config.get(section, "name"))
enables = json.loads(config.get(section, "enable"))
url, path = config[section]["url"], os.path.join(module_path, config[section]["path"])
os.makedirs(path, exist_ok=True)
if name != None and name != config[section]["path"]:
continue
if tag != None and tag not in imgs:
logging.error(f" Tag not available. {tag} not in {imgs}")
exit(-1)
logging.info(f"Process [{section}].")
for pull_tag, img_name, enable in zip(tags, imgs, enables):
if enable not in enable_options:
logging.error(f" Option enable {tag} not in {enable_options}")
exit(-1)
if (tag != None and tag != img_name):
continue
src_lua = os.path.join(config[section]["path"], img_name+".lua")
dst_lua = f"{path}/{img_name}.lua"
dst_img = f"{path}/{img_name}.sif"
if enable == "disable":
if os.path.exists(dst_lua):
os.remove(dst_lua)
logging.info(f" {dst_lua} deleted.")
if os.path.exists(dst_img):
os.remove(dst_img)
logging.info(f" {dst_img} deleted.")
continue
shutil.copyfile(src_lua, dst_lua)
logging.info(f" Copy {src_lua} to {dst_lua}.")
if (not force) and os.path.exists(dst_img):
logging.info(
f" {dst_img} exsit. You can use `--force` to do a force update."
)
else:
if url == "local":
logging.info(
f" Copy image {pull_tag} into {dst_img}."
)
copy_cmd = f"cp {pull_tag} {dst_img}"
subprocess.call(copy_cmd, shell=True)
else:
logging.info(
f" Pull image from docker://{url}:{pull_tag} into {dst_img}."
)
pull_cmd = f"singularity pull --force {dst_img} docker://{url}:{pull_tag}"
subprocess.call(pull_cmd, shell=True)
if enable == "default":
logging.info(f" Make {img_name} as default module.")
default_path = os.path.join(path, "default")
if os.path.islink(default_path):
os.unlink(default_path)
os.symlink(dst_lua, default_path)
def main():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subparser')
parser_deploy = subparsers.add_parser('deploy')
parser_deploy.add_argument('-c',
'--config',
dest='config_path',
default='./config.ini')
parser_deploy.add_argument('-n',
'--name',
dest='name',
choices=software_name)
parser_deploy.add_argument('-t', '--tag', dest='tag')
parser_deploy.add_argument('-f',
'--force',
dest='force',
action='store_true')
kwargs = vars(parser.parse_args())
if not kwargs['subparser']:
parser.print_help()
exit(-1)
globals()[kwargs.pop('subparser')](**kwargs)
if __name__ == "__main__":
main()