-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
347 lines (254 loc) · 9.55 KB
/
main.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
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
#!/usr/bin/python3.9
from pathlib import Path
from typing import Mapping
# from typing import Optional
from utils import util_yaml_reader
from utils import util_verif_bool
from utils import util_verif_str
from utils import util_subproc
from func_custom_script import main as runjob_custom_script
from func_custom_script import _JOBNAME as _SYMBOL_JOBNAME_CUSTOM_SCRIPT
from func_write_file import main as runjob_write_file
from func_write_file import _JOBNAME as _SYMBOL_JOBNAME_WRITE_FILE
from func_create_symlink import main as runjob_create_symlink
from func_create_symlink import _JOBNAME as _SYMBOL_JOBNAME_CREATE_SYMLINK
from func_mount_block import main as runjob_mount_block
from func_mount_block import _JOBNAME as _SYMBOL_JOBNAME_MOUNT_BLOCK
from func_mount_directory import main as runjob_mount_directory
from func_mount_directory import _JOBNAME as _SYMBOL_JOBNAME_MOUNT_DIR
from func_mount_volume import main as runjob_mount_volume
from func_mount_volume import _JOBNAME as _SYMBOL_JOBNAME_MOUNT_VOLUME
from func_apt_install import main as runjob_apt_install
from func_apt_install import _JOBNAME as _SYMBOL_JOBNAME_APTINSTALL
from func_new_application import main as runjob_new_application
from func_new_application import _JOBNAME as _SYMBOL_JOBNAME_NEW_APP
_SYMBOL_TIME_SETTINGS="time-settings"
_SYMBOL_JOBLIST="joblist"
_KNOWN_JOBS=(
_SYMBOL_JOBNAME_CUSTOM_SCRIPT,
_SYMBOL_JOBNAME_WRITE_FILE,
_SYMBOL_JOBNAME_CREATE_SYMLINK,
_SYMBOL_JOBNAME_MOUNT_BLOCK,
_SYMBOL_JOBNAME_MOUNT_DIR,
_SYMBOL_JOBNAME_MOUNT_VOLUME,
_SYMBOL_JOBNAME_APTINSTALL,
_SYMBOL_JOBNAME_NEW_APP,
)
def runner_timesetup(data_time:Mapping):
print("\n[ Time Settings ]")
arg_timezone=util_verif_str(data_time.get("timezone"))
if isinstance(arg_timezone,str):
util_subproc([
"timedatectl","set-timezone",arg_timezone
])
def runner_joblist(
data_joblist:list,
path_basedir:Path,
):
print("\n[ Jobs List ]\n")
#spec_tag=False
#if isinstance(target_tag,str):
# spec_tag=True
for step in data_joblist:
if not isinstance(step,Mapping):
continue
if not len(step)==1:
continue
mainkey=list(step.keys())[0]
if mainkey not in _KNOWN_JOBS:
continue
ok=False
tag=util_verif_str(
step.get(mainkey,{}).get("tag")
)
crucial=util_verif_bool(
step.get(mainkey,{}).get("crucial",False)
)
hidden=util_verif_bool(
step.get(mainkey,{}).get("hidden",False)
)
headline=(
f"Job {mainkey}" "\n"
"\t" f"crucial: {crucial}" "\n"
"\t" f"hidden: {hidden}"
)
if isinstance(tag,str):
headline=(
f"{headline}" "\n"
"\t" f"tag: {tag}"
)
print(
f"{headline}\n"
)
if hidden:
print(
"\t" "(Job skipped)"
)
continue
#print(headline)
#if hidden and (not spec_tag):
# continue
#if spec_tag:
# if not target_tag==tag:
# continue
if mainkey==_SYMBOL_JOBNAME_CUSTOM_SCRIPT:
ok=runjob_custom_script(step.get(mainkey),path_basedir)
if mainkey==_SYMBOL_JOBNAME_WRITE_FILE:
ok=runjob_write_file(step.get(mainkey),path_basedir)
if mainkey==_SYMBOL_JOBNAME_CREATE_SYMLINK:
ok=runjob_create_symlink(step.get(mainkey),path_basedir)
if mainkey==_SYMBOL_JOBNAME_MOUNT_BLOCK:
ok=runjob_mount_block(step.get(mainkey),path_basedir)
if mainkey==_SYMBOL_JOBNAME_MOUNT_DIR:
ok=runjob_mount_directory(step.get(mainkey),path_basedir)
if mainkey==_SYMBOL_JOBNAME_MOUNT_VOLUME:
ok=runjob_mount_volume(step.get(mainkey),path_basedir)
if mainkey==_SYMBOL_JOBNAME_APTINSTALL:
ok=runjob_apt_install(step.get(mainkey),path_basedir)
if mainkey==_SYMBOL_JOBNAME_NEW_APP:
ok=runjob_new_application(step.get(mainkey),path_basedir)
if not ok:
if crucial:
print(
"\nA crucial job failed! Cannot go any further\n"
)
break
print("\nJob failed!\n")
continue
print("\nJob Finished!\n")
def runner(
data:Mapping,
path_basedir:Path
):
# print(data)
if _SYMBOL_TIME_SETTINGS in data.keys():
if not isinstance(data[_SYMBOL_TIME_SETTINGS],Mapping):
print(f"YAML Mapping expected in '{_SYMBOL_TIME_SETTINGS}'")
return
runner_timesetup(data[_SYMBOL_TIME_SETTINGS])
if _SYMBOL_JOBLIST in data.keys():
if not isinstance(data[_SYMBOL_JOBLIST],list):
print(f"YAML List expected in '{_SYMBOL_JOBLIST}'")
return
runner_joblist(
data[_SYMBOL_JOBLIST],
path_basedir
)
def command_run(path_config:Path)->int:
the_custom_config=util_yaml_reader(path_config)
if the_custom_config is None:
print("Failed to read the custom config")
return 1
if len(the_custom_config)==0:
print("Custom config not valid")
return 1
runner(
the_custom_config,
path_config.parent,
)
return 0
def command_help()->int:
print(
"\n"
"# Config file structure (in YAML btw):" "\n\n"
f"{_SYMBOL_TIME_SETTINGS}:" "\n"
"\t" "timezone:" "\n"
"\t\t" "# Any valid timezone listed in timedatectl" "\n"
"\t\t" "# timedatectl (and systemd) must be installed in order for this to work" "\n"
"\n"
f"{_SYMBOL_JOBLIST}:" "\n"
"\t" "# List of jobs to perform sequentially" "\n"
"\t" f"# There are {len(_KNOWN_JOBS)} types of jobs:" "\n"
"\n"
"\t" f"- {_SYMBOL_JOBNAME_WRITE_FILE}: # Writes content to a text file" "\n"
"\t\t" "dest: # A path to a file" "\n"
"\t\t" "content: # Text that you want to write" "\n"
"\n"
"\t" f"- {_SYMBOL_JOBNAME_CREATE_SYMLINK}: # Create symbolic link. All links created are absolute" "\n"
"\t\t" "orig: # (Mandatory) A path to a real file or directory" "\n"
"\t\t" "dest: # (Mandatory) A path to the destination path for the symlink" "\n"
"\t\t" "indir: # Wether the destination path is a directory and the symlink has to be created as a child of that directory" "\n"
"\n"
"\t" f"- {_SYMBOL_JOBNAME_CUSTOM_SCRIPT}: # Run a custom bash script" "\n"
"\t\t" "content:" "\n"
"\t\t\t" "# A bash script (multiline recommended), nonzero exit status will be trated as a failure" "\n"
"\t\t\t" "# The Config file's working directory path (CFWD) is inyected into the bash script as a variable called 'CONFIG_FILE_WORKING_DIRECTORY'" "\n"
"\t\t" "cfwd-as: # Rename the variable holding the inyected CFWD to a different name" "\n"
"\n"
"\t" f"- {_SYMBOL_JOBNAME_MOUNT_BLOCK}: # Mount a file or block device" "\n"
"\t\t" "file: # (Mandatory) A path to the block device file" "\n"
"\t\t" "dest: # (Mandatory) A path to the destination directory" "\n"
"\t\t" "mode: # Mount mode (rw, ro). Leaving it blank means auto" "\n"
"\n"
"\t" f"- {_SYMBOL_JOBNAME_MOUNT_DIR}: # Bind mount of a directory (non-recursive)" "\n"
"\t\t" "orig: # A path to the directory that you want to mount" "\n"
"\t\t" "dest: # A path to the destination directory" "\n"
"\n"
"\t" f"- {_SYMBOL_JOBNAME_MOUNT_VOLUME}: # Mount a volume by its UUID" "\n"
"\t\t" "uuid: # (Mandatory) UUID of the volume (partition) that you want to mount" "\n"
"\t\t" "dest: # (Mandatory) A path to the destination directory" "\n"
"\t\t" "mode: # Mount mode (rw, ro). Leaving it blank means auto" "\n"
"\n"
"\t" f"- {_SYMBOL_JOBNAME_APTINSTALL}:" "\n"
"\t\t" "# Install packages from remote sources OR local .DEB packages using APT" "\n"
"\t\t" "# NOTE: You cannot do both things in the same job, you have to choose what to do" "\n"
"\t\t" "path: # A path to a package or a directory (non-recursive)" "\n"
"\t\t" "names: # Names of packages separated by a single space" "\n"
"\n"
"\t" f"- {_SYMBOL_JOBNAME_NEW_APP}: # Creates (or overwrites) a .DESKTOP file in /usr/share/applications" "\n"
"\t\t" "stem: # Filename without the extension" "\n"
"\t\t" "name: # (Mandatory) .DESKTOP Name" "\n"
"\t\t" "exec: # (Mandatory) .DESKTOP Exec" "\n"
"\t\t" "categories: # .DESKTOP Categories (Mandatory)" "\n"
"\t\t" "comment: # .DESKTOP Comment" "\n"
"\t\t" "terminal: # .DESKTOP Terminal (Bool, defaults to false)" "\n"
"\t\t" "icon: # .DESKTOP Icon" "\n"
"\t\t" "mimetype: # .DESKTOP MymeType" "\n"
"\t\t" "snotify: # .DESKTOP StartupNotify (Bool, defaults to false)" "\n"
"\t\t" "path-icon:" "\n"
"\t\t\t" "# Image file path that will be copied to /usr/share/icons/" "\n"
"\t\t\t" "# The filename will be the icon name + the file extension from the given path" "\n"
"\t\t" "path-link:" "\n"
"\t\t\t" "# A path to a new symlink" "\n"
"\t\t\t" "# The path has to be a directory" "\n"
"\n"
"\t" "# Think of the jobs as functions: You can reuse any of the available jobs with different parameters as much as you want to match your specific needs" "\n\n"
"\t" "# Additional fields for jobs:" "\n\n"
"\t" "# 'tag' field: You can add the 'tag' field to any job to identify it at runtime, this is good for debugging and testing" "\n"
"\t" "# 'crucial' field: Jobs marked as crucial will cancel the entire workflow in case of failure" "\n"
"\t" "# 'hidden' field: Hidden jobs will be skipped when running the joblist" "\n\n"
"\t" "# About path parameters:" "\n\n"
"\t" "# Paths can be absolute or non-absolute (relative)" "\n"
"\t" "# Non-absolute paths will be taken as relative to the config file's parent directory" "\n"
)
return 0
if __name__=="__main__":
from sys import argv as sys_argv
from sys import exit as sys_exit
if len(sys_argv)==1:
print(
"\n"
"Usage" "\n"
"-----" "\n"
f"$ {Path(sys_argv[0]).name} [Command] (Args)" "\n\n"
"Commands" "\n"
"--------" "\n"
"help → Shows the config file reference (it has no args)" "\n"
"run → Runs a config file" "\n"
)
sys_exit(0)
if sys_argv[1]=="help":
sys_exit(
command_help()
)
if sys_argv[1]=="run":
if not len(sys_argv)>2:
print("Missing: Config file path")
sys_exit(1)
sys_exit(
command_run(
Path(sys_argv[2])
)
)
print("Unknown command")
sys_exit(1)