-
Notifications
You must be signed in to change notification settings - Fork 0
/
autolite_system.py
142 lines (101 loc) · 3.9 KB
/
autolite_system.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
import sys
import yaml
import common
import consts
import db
import schema
from system import System
SELF_ABS_PATH, SELF_FULL_DIR, SELF_SUB_DIR = consts.get_self_path_dir(__file__)
PACKAGE_NAME = SELF_SUB_DIR
def menu(arguments):
if arguments['list']:
if arguments['--YAML'] or arguments['--JSON']:
where = dict(name=arguments['<name>']) if arguments['<name>'] else dict()
systems = System.list(**where)
common.dump(systems, toyaml=arguments['--YAML'], tojson=arguments['--JSON'],
entry=lambda item: item.__dict__)
else:
_system_list_table(arguments)
elif system_execute(arguments):
pass
else:
try:
if arguments['create']:
System.create(**_system_create_kwargs(arguments))
arguments['--long'] = True
_system_list_table(arguments)
elif arguments['read']:
system_read(arguments)
elif arguments['set']:
system_set(arguments)
elif arguments['delete']:
System(arguments['<name>']).delete()
except NameError as exc:
print(PACKAGE_NAME, 'Error!', exc)
sys.exit(1)
def system_read(arguments):
system = System(arguments['<name>'])
toyaml, tojson = arguments['--YAML'], arguments['--JSON']
if toyaml or tojson:
common.dump([system.__dict__], toyaml, tojson, squash=True)
else:
print(system)
def _system_create_kwargs(arguments):
kwargs = schema.TABLE_SCHEMAS.systems.new()
if arguments['--fields']:
with open(arguments['--fields']) as f:
kwargs.update(yaml.safe_load(f))
kwargs.update(dict((k, arguments['--' + k] or kwargs[k]) for k in kwargs.keys() if ('--' + k) in arguments))
kwargs.update(
name=arguments['<name>'],
ip=arguments['<ip>'],
)
return kwargs
def _system_list_table(arguments):
if arguments['--long']:
col_names = 'name ip user installer cleaner config monitor comment'.split(' ')
elif arguments['--fields']:
col_names = arguments['--fields'].lower().split(',')
else:
col_names = ['name']
if not arguments['--col-1']:
col_names += ['user', 'comment']
where = dict(name=arguments['<name>']) if arguments['<name>'] else dict()
systems = db.list_table('systems', **where)
rows = ([system[col] for col in col_names] for system in systems)
col_titles = [name.upper() for name in col_names]
common.print_table(col_titles, sorted(rows, key=lambda row: row[0]))
def system_set(arguments):
kwargs = dict(
(field, arguments['<exe>'])
for field in ['installer', 'monitor', 'cleaner', 'config']
if arguments[field]
)
if not kwargs:
if arguments['ip']:
kwargs.update(ip=arguments['<ip>'])
if arguments['comment']:
kwargs.update(comment=arguments['<text>'])
db.update('systems', name=arguments['<name>'], **kwargs)
arguments['--fields'] = 'name,' + ','.join(kwargs.keys())
_system_list_table(arguments)
def system_execute(arguments) -> bool:
if arguments['set']:
return False
for cmd in 'acquire | release | install | clean | monitor | config'.split(' | '):
if arguments[cmd]:
try:
system = System(arguments['<name>'])
cmd_method = getattr(system, cmd)
kwargs = dict(force=arguments['--force']) if cmd == 'release' else dict()
cmd_method(**kwargs)
except PermissionError as exc:
print(PACKAGE_NAME, 'Error!', exc)
sys.exit(1)
except Exception as exc:
if type(exc).__name__.endswith('Warning'):
print(PACKAGE_NAME, 'Warning:', exc)
else:
raise
return True
return False