-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
98 lines (71 loc) · 3.01 KB
/
utils.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
import argparse
import yaml
def argparse_setup():
# type: () -> (argparse.ArgumentParser, argparse.ArgumentParser)
parser = argparse.ArgumentParser(description='Bungee Manager')
parser.add_argument('name', help='Name you want to call the VPS')
parser.add_argument('config_definition_path',
help='Path to the config set up in digitalocean_config.yml.'
'Check digitalocean_config.yml.example for examples')
parser.add_argument('-s', '--sleep-time', type=int,
help="Amount of time between creating the droplet "
"and uploading the files "
"(Allows the droplet to start up)",
default=60)
parser.add_argument('-b', '--bungee_name', type=str,
help="Name of the bungee connection as set up in "
"bungee_info.yml ")
parser.add_argument('-t', '--token',
help="For commands that may only need a token. Unused")
parser.add_argument('-y', '--yes', action='store_true',
help="Skips all checks in case of missing arguments")
args, unknown = parser.parse_known_args()
wild_parser = argparse.ArgumentParser(description='Bungee Manager/Placeholder arg viewer')
for arg in unknown:
if arg.startswith(("-", "--")):
wild_parser.add_argument(arg)
wild_args = wild_parser.parse_args(unknown)
return args, wild_args
def missing(_arg, force=False):
# type: (str, bool) -> None
message = f"You are missing the argument {_arg}."
if force:
print(f"{message} This argument is required for this operation."
" Use help for more info.")
exit(1)
consent = ''
while consent.lower() not in ('y', 'yes'):
consent = input(
f"{message} Do you want to continue without setting it?\n(yes/no)")
if consent.lower() in ('n', 'no'):
exit(0)
def get_do_config():
# type: () -> dict
with open('digitalocean_config.yml', 'r') as cfg:
config = yaml.safe_load(cfg) or {}
return config
def update_do_config(new_cfg):
# type: (dict) -> None
with open('digitalocean_config.yml', 'w') as cfg:
yaml.dump(new_cfg, cfg)
def get_upload_files_for(name):
# type: (str) -> list
config = get_do_config()
server_config = config.get(name) or {}
setup = server_config.get('setup') or {}
upload_list = setup.get('uploads') or []
return upload_list
def get_commands_to_run_for(name):
# type: (str) -> list
config = get_do_config()
server_config = config.get(name) or {}
setup = server_config.get('setup') or {}
command_list = setup.get('pre_commands') or []
return command_list
def get_script_to_run_for(name):
# type: (str) -> str
config = get_do_config()
server_config = config.get(name) or {}
setup = server_config.get('setup') or {}
script = setup.get('script') or ""
return script