forked from Graham277/NewDozer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·879 lines (758 loc) · 31 KB
/
Copy pathsetup.py
File metadata and controls
executable file
·879 lines (758 loc) · 31 KB
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
#!/usr/bin/env python3
#
# ========= Setup =========
# Standalone script to install files and to set up dependencies.
# This script makes the following changes:
# 1. Clones all project files into the selected directory. If appropriate,
# creates symlinks to `bin` directories.
# 2. Creates a virtualenv in the installation directory.
# 3. `pip install`s dependencies in the virtualenv.
# 4. Creates a `systemd` service (if available) to run the bot.
# Created as a user service if installed into the home directory, system
# otherwise.
# 5. Configures rsyslog if using system-wide service. Configures logrotate if
# using user service.
# Requires sudo privileges.
import os
import pathlib
import stat
from os.path import sep
import shutil
import subprocess
import sys
import textwrap
## Helper functions
def choose_option(message: str, *options: str, default: int | None = None) -> int:
"""
Present a list for the user to choose various options from.
The default option is chosen when the user enters '0' or nothing, and is
marked with an asterisk.
:param message: a header message to inform the user of the message's context
:param options: the set of options the user has
:param default: the index of the option that is chosen by default, or None if none
:return: the index of the option that was chosen
"""
if options is None or len(options) == 0:
raise ValueError("choose_option needs a list of options. This is a bug.")
wrap_col = 80
# regularize to non-None int
# first print out the options
# all printed messages are wrapped in textwrap.wrap() and have sep='\n'
# this is so extra-long messages are put onto multiple lines.
print(*textwrap.wrap(message, wrap_col), sep='\n')
print()
# either:
# Choose an option [1-N, or 0 for default]: | if default not None
# Choose an option [1-N]: | if default None
print(*textwrap.wrap(f"Choose an option [1-{len(options)}"
f"{", or 0 for default" if default is not None else ""}]:", wrap_col),
sep='\n')
# offset = how long the last index is
offset = len(str(len(options)))
number = 1
# print each option. each number is space-buffered from the left if it is
# not max length.
# default is marked with an asterisk
for option in options:
buffer = " " * (offset - len(str(number)))
if default is not None and default + 1 == number:
# either (default selected):
# | * NNN. option placeholder
# | NNN. option placeholder
# or (no default):
# | NNN. option placeholder
# | NNN. option placeholder
print(*textwrap.wrap(f"{" *" if default is not None else ""} {buffer}"
f"{number}. {option}", wrap_col), sep='\n')
else:
print(*textwrap.wrap(f"{" " if default is not None else ""} {buffer}"
f"{number}. {option}", wrap_col), sep='\n')
number += 1
print()
# get choice
# keep doing it until a valid input is reached
# choice_start: lowest valid integer for choice
choice_start = 0 if default is not None else 1
while True:
# wait for input
# either:
# Choice [0-N]:
# or:
# Choice [1-N]:
val = input(f"Choice [{choice_start}-{len(options)}]: ")
# validate input
# first, if the string is empty, that means the default was chosen
if not val:
val = "0"
# check: value is an integer
try:
val_index: int = int(val) - 1
except ValueError:
print("Invalid input (not a number), please try again.")
continue
# from now on, index starts at 0 and default == -1
# check: value is not default if default is unset
if default is None and val_index == -1:
print("Invalid input (no default option), please try again.")
continue
# collapse default value
# default is not None already implied here
if val_index == -1 and default is not None:
val_index = default
# check: value in range
if val_index < 0 or val_index >= len(options):
print(f"Input out of range ({choice_start}-{len(options)}), please try again.")
continue
# all good
return val_index
assert False, "Unreachable state"
def confirm(message: str, *, default_state: bool | None = None) -> bool:
"""
Presents the user with a confirmation message, which they can respond to in
various ways.
The values "y", "yes" and "true" yield `True`, while "n", "no" and "false"
yield `False`, an empty string results in the default value, and anything
else makes the user try again.
:param message:
:param default_state: the option to return if the user inputs nothing, or
None if this is not a valid option
:return: a `bool` representing the user's choice
"""
# essentially:
# default is False -> y/N
# default is True -> Y/n
# default is None -> y/n
possible_inputs = (f"[{'y' if not default_state or default_state is None else 'Y'}/"
f"{'n' if default_state or default_state is None else 'N'}]")
val = input(message + " " + possible_inputs + "? ")
while True:
# if nothing was entered, use default
if not val or len(val.strip()) == 0:
if default_state is not None:
return default_state
# if default is None, reject it and try again
else:
val = input(f"Invalid input. {possible_inputs}? ")
continue
else:
# if it is yes, true
if val.strip().lower() in ("y", "yes", "true"):
return True
# if no, false
elif val.strip().lower() in ("n", "no", "false"):
return False
# if invalid, go again
else:
val = input(f"Invalid input. {possible_inputs}? ")
continue
assert False, "Unreachable state"
def wait():
"""
Waits for the user to press enter.
:return: nothing
"""
input("Press enter to continue...")
def is_subdir(parent: str | os.PathLike, child: str | os.PathLike) -> bool:
"""
Checks if the child is a subdirectory of or equal to the parent.
:param parent: the parent directory
:param child: the child, which might be a child of the parent
:return: bool: whether the child is a subdirectory of the parent
"""
parent_real = os.path.realpath(os.path.abspath(os.path.expanduser(parent)))
child_real = os.path.realpath(os.path.abspath(os.path.expanduser(child)))
return parent_real == os.path.commonpath([parent_real, child_real])
def setup_help():
print()
print("Usage: python setup.py <command>")
print()
print("Available commands:")
print(" - install: install the bot")
print(" - uninstall: uninstall the bot")
print(" - import: import credentials from a secrets file")
print(" - help: show this help")
def setup_install():
"""
Main routine that handles all functions.
:return: nothing
"""
subdir_name = "dozerbot"
wrap_width = 80
print(" === Dozer Setup === ")
print("Version 1.0.0")
print("Task: Install")
print()
print("Step 1 - gathering information...")
print()
# get information
# only supports Linux atm
if not sys.platform.startswith("linux"):
print("ERROR: This install script is only supported on Linux (and similar) systems.")
print("Abort. ---")
sys.exit(1)
# check deps (virtualenv)
if shutil.which("virtualenv") is None:
print(*textwrap.wrap("ERROR: no installed copy of virtualenv was found, which is"
" required for installation. Please check $PATH and install it if"
" necessary.", wrap_width), sep='\n')
print("Abort. ---")
sys.exit(1)
# get the install directory
install_targets = ["/usr/local/share", "/opt", "~/.local/share"]
bin_path_targets = ["/usr/local/bin", "/opt", "~/.local/bin"]
etc_path_targets = ["/usr/local/etc", "/opt", "~/.local/etc"]
annotated_install_targets = [
"Into /usr/local/share, linked into /usr/local/bin and etc",
"Into /opt, in its own subdirectory",
"Into ~/.local/share, linked into ~/.local/bin and etc",]
option = choose_option(
"Which directory should the bot be installed into?\n",
*annotated_install_targets, default=1) # default is /opt
install_parent_target_abs = os.path.realpath(os.path.abspath(
os.path.expanduser(install_targets[option])
))
bin_path_target_abs = os.path.realpath(os.path.abspath(
os.path.expanduser(bin_path_targets[option])
))
etc_path_target_abs = os.path.realpath(os.path.abspath(
os.path.expanduser(etc_path_targets[option])
))
# check $PATH
path = os.getenv("PATH")
path_entries = path.split(os.pathsep)
has_found_current = False
# this awful mess just gets the real path of a file
# (i.e. no symlinks, tildes or relative components)
# this loop checks if the selected directory is in PATH
for entry in path_entries:
entry_test_dir = os.path.realpath(os.path.abspath(os.path.expanduser(entry)))
if entry_test_dir == bin_path_target_abs:
has_found_current = True
break
# warn user if their installation dir is not in PATH
if not has_found_current:
print()
print(*textwrap.wrap("WARNING: Cannot find " + bin_path_target_abs +
" in $PATH. This will not prevent installation, but may cause"
" issues if running directly from the command line.",
wrap_width), sep='\n')
print(*textwrap.wrap("It is recommended to add the directory to PATH if"
" you plan on using it directly from the command"
" line.", wrap_width), sep='\n')
print()
wait()
# install!
print()
print("Step 2 - installing files...")
print()
install_dir = install_parent_target_abs + sep + subdir_name
print("Starting copy...")
print("The installer will now ask for superuser permissions.")
# copy files
try:
# make sure directories exist
subprocess.run(["sudo", "mkdir", "-p", install_dir])
subprocess.run(["sudo", "cp", "-r", ".", install_dir])
# and make sure anyone can execute
subprocess.run(["sudo", "chmod", "+x", install_dir + sep + "main.py"])
subprocess.run(["sudo", "chmod", "+x", install_dir + sep + "start.sh"])
# make sure .env exists
subprocess.run(["sudo", "touch", install_dir + sep + ".env"])
print("Success!")
print()
except subprocess.CalledProcessError as e:
print(f"ERROR: Failed to copy files! (exit code {e.returncode},"
f" message: {str(e)})")
print("Abort. ---")
sys.exit(1)
# symlink bin and etc directories
if install_parent_target_abs != bin_path_target_abs:
print("Linking installation...")
# make links
# each link points from the base directory to the appropriate folder
# (just as an alias)
#
# secrets.json is specifically excluded because it shouldn't exist most
# of the time
# .env can also point somewhere else if required
# make sure symlinking directories exist
subprocess.run(["sudo", "mkdir", "-p", bin_path_target_abs])
subprocess.run(["sudo", "mkdir", "-p", etc_path_target_abs])
# bin
subprocess.run(["sudo", "-E", "ln", "-s",
install_dir + sep + "main.py",
bin_path_target_abs + sep + "dozermain"])
subprocess.run(["sudo", "-E", "ln", "-s",
install_dir + sep + "start.sh",
bin_path_target_abs + sep + "dozerstart"])
# etc
subprocess.run(["sudo", "-E", "ln", "-s",
install_dir + sep + ".env",
etc_path_target_abs + sep + "dozer.env"])
print()
print(*textwrap.wrap(
f"Success! Executables and configuration may be also found at "
f"{bin_path_target_abs + sep + "dozermain"} (for main.py), "
f"{bin_path_target_abs + sep + "dozerstart"} (for start.sh), and "
f"{(etc_path_target_abs + sep + "dozer.env")} (for .env).",
wrap_width), sep='\n')
# done
# make venv
print()
print("Step 3 - creating virtual environment...")
print()
venv_folder = install_dir + sep + ".venv"
try:
print("Creating...")
print()
subprocess.run(["sudo", "-E", "virtualenv", venv_folder])
print()
print("Success!")
# cannot source it so it has to be explicitly called every time
except subprocess.CalledProcessError as e:
print(f"ERROR: Failed to create virtual environment! (exit code"
f" {e.returncode}, message: {str(e)})")
print("Abort. ---")
sys.exit(1)
# done
# get pip deps
print()
print("Step 4 - installing dependencies...")
print()
try:
print("Installing dependencies from requirements.txt...")
print()
print(" -------- pip output begins -------- ")
print()
subprocess.run(["sudo", "-E", venv_folder + sep + "bin" + sep + "pip",
"install", "-r", install_dir + sep + "requirements.txt"])
print()
print(" -------- pip output ends -------- ")
print()
print("Success!")
print()
except subprocess.CalledProcessError as e:
print(f"ERROR: Failed to install dependencies! (exit code"
f" {e.returncode}, message: {str(e)})")
print("Abort. ---")
sys.exit(1)
print()
print("Step 5 - creating system service...")
print()
is_system = not is_subdir(os.path.expanduser("~"), install_dir)
log_options_annotated = ["Sink (/dev/null)", "To ~/.cache/dozer.log",
"To /var/log/dozer.log", "To syslog"]
if not is_system:
log_options_annotated.remove("To /var/log/dozer.log")
log_options = ["null", "cache", "varlog", "syslog"]
if not is_system:
log_options.remove("varlog")
log_option = log_options[choose_option("Where should log messages be sent?",
*log_options_annotated,
default=3 if is_system else 2)]
# create a systemd file
if is_system:
print("Creating as a system-wide systemd unit")
print("...")
# create a new unit file
start_sh_path = install_dir + sep + "start.sh"
service_tmp_path = "/tmp/dozer.service"
service_file_path = "/etc/systemd/system/dozer.service"
log_data = ""
match log_option:
case "null":
log_data = \
"""
StandardOutput=/dev/null
StandardError=/dev/null
"""
print("WARNING: The `null` log output was selected. This can"
" make troubleshooting much more difficult.")
print()
case "cache":
log_data = \
f"""
StandardOutput={os.path.expanduser("~/.cache/dozer.log")}
StandardError={os.path.expanduser("~/.cache/dozer.log")}
"""
print("Note: It is recommended to set up a log rotation service"
" (like logrotate) to avoid having the log grow"
" uncontrollably.")
print()
case "varlog":
log_data = \
f"""
StandardOutput=/var/log/dozer.log
StandardError=/var/log/dozer.log
"""
print("Note: It is recommended to set up a log rotation service"
" (like logrotate) to avoid having the log grow"
" uncontrollably.")
print()
case "syslog":
pass # default, no action needed
# taken from nodejs version
# bad formatting here but looks much nicer in situ
service_file_contents = \
f"""
[Unit]
Description=Dozer discord bot
After=network.target
[Service]
WorkingDirectory={install_dir}
ExecStart={start_sh_path}
Restart=always
RestartSec=10
{log_data}
[Install]
WantedBy=multi-user.target
"""
# cat into a temp file then move with sudo
with open(service_tmp_path, "w") as f:
f.write(service_file_contents)
subprocess.run(["sudo", "mv", service_tmp_path, service_file_path])
print("Enabling...")
subprocess.run(["sudo", "systemctl", "enable", "dozer.service"])
print("Success! Try systemctl start dozer.service")
else:
print("Creating as a user systemd unit")
print("...")
start_sh_path = install_dir + sep + "start.sh"
service_tmp_path = "/tmp/dozer.service"
service_dir_path = os.path.expanduser("~/.config/systemd/user/")
service_file_path = service_dir_path + "dozer.service"
log_data = ""
match log_option:
case "null":
log_data = \
"""
StandardOutput=/dev/null
StandardError=/dev/null
"""
print("WARNING: The `null` log output was selected. This can"
"make troubleshooting much more difficult.")
print()
case "cache":
log_data = \
f"""
StandardOutput={os.path.expanduser("~/.cache/dozer.log")}
StandardError={os.path.expanduser("~/.cache/dozer.log")}
"""
print("Note: It is recommended to set up a log rotation service"
" (like logrotate) to avoid having the log grow"
" uncontrollably.")
print()
case "syslog":
pass # default, no action needed
# bad formatting here but looks much nicer in situ
service_file_contents = \
f"""
[Unit]
Description=Dozer discord bot
After=network.target
[Service]
WorkingDirectory={install_dir}
ExecStart={start_sh_path}
Restart=always
RestartSec=10
{log_data}
[Install]
WantedBy=default.target
"""
with open(service_tmp_path, "w") as f:
f.write(service_file_contents)
subprocess.run(["mkdir", "-p", service_dir_path])
subprocess.run(["mv", service_tmp_path, service_file_path])
print("Enabling...")
subprocess.run(["systemctl", "enable", "--user", "dozer.service"])
print("Success! Try systemctl --user start dozer.service")
print()
print("Successfully installed Dozer bot.")
print()
print(*textwrap.wrap("Note: A keyring (a DBus Secret Service provider) is"
" required to run the bot with attendance features."
" Ensure one is installed before running the bot for"
" the first time.", wrap_width), sep='\n')
print()
print("You will probably also need to manually import credentials.")
print("For that, run setup.py with the subcommand 'import'.")
def setup_import():
print()
print(" === Dozer Setup ===")
print("Version 1.0.0")
print("Task: Import secrets")
print()
# import systemd secrets
print("Where is secrets.json located?")
print()
while True:
secrets_path = os.path.expanduser(input("Enter a path: "))
if not os.path.exists(secrets_path):
print(f"Couldn't find secrets file at {secrets_path}, try again.")
print()
continue
if not os.path.isfile(secrets_path):
print(f"{secrets_path} path is not a file, try again.")
print()
continue
break
cred_locations = ["Into keyring", "Into the unit file (encrypted)", "Cancel"]
cred_location = cred_locations[choose_option("How should the"
" credentials be imported?",
*cred_locations, default=1)]
if cred_location == "Cancel":
print("Abort. --- ")
sys.exit(0)
is_systemd = False if cred_location == "Into keyring" else True
is_system = False # early assignment for the print
if is_systemd:
while True:
install_paths = ["/usr/local/share/dozerbot", "/opt/dozerbot", "~/.local/share/dozerbot"]
install_path = install_paths[choose_option("Where is the bot installed? ", *install_paths)]
if not os.path.exists(os.path.expanduser(install_path)):
print(f"{install_path} does not exist, try again.")
continue
break
is_system = not install_path.startswith("~")
unit_file_conf_path = os.path.expanduser("~/.config/systemd/user/dozer.service.d/")\
if not is_system else "/etc/systemd/system/dozer.service.d/"
unit_file_path = os.path.expanduser("~/.config/systemd/user/dozer.service")\
if not is_system else "/etc/systemd/system/dozer.service"
if not os.path.exists(unit_file_path):
print(f"ERROR: {unit_file_path} does not exist.")
print("Abort. --- ")
sys.exit(1)
print(f"Using {"system" if is_system else "user"} service's unit file")
# encrypt
print("The script will now ask for superuser (required for encryption).")
out = subprocess.check_output(["sudo", "systemd-creds", "encrypt", "-p",
"--name=service_auth", secrets_path, "-"], text=True)
# write to conf
if not is_system:
# can use native functions
pathlib.Path(unit_file_conf_path).mkdir(parents=True, exist_ok=True)
with open(unit_file_conf_path + "10-creds.conf", "w") as f2:
f2.write(f"[Service]\n{out}\n")
else:
subprocess.run(["sudo", "mkdir", "-p", unit_file_conf_path])
with open("/tmp/10-creds.conf", "w") as f2:
f2.write(f"[Service]\n{out}\n")
subprocess.run(["sudo", "mv", "/tmp/10-creds.conf",
unit_file_conf_path + "10-creds.conf"])
print()
if is_systemd:
print(f"Successfully imported secrets! Try systemctl"
f" {"--user" if not is_system else ""} restart dozer.service")
else:
print("Successfully imported secrets!")
def setup_uninstall():
print()
print(" === Dozer Setup ===")
print("Version 1.0.0")
print("Task: Uninstall")
print()
print(*textwrap.wrap("See README.md for manual instructions if"
" uninstallation does not work for whatever reason.",
80), sep='\n')
print()
print("Finding installations...")
# installation locations
# dict to associate each "value" with other constant lists
possible_locations = ["/usr/local/share/dozerbot", "/opt/dozerbot",
os.path.expanduser("~/.local/share/dozerbot")]
# some lookup tables to keep all of the constants in one place
unlink_locations = [
["/usr/local/bin/dozermain", "/usr/local/bin/dozerstart",
"/usr/local/etc/dozer.env"],
[],
[os.path.expanduser("~/.local/bin/dozermain"),
os.path.expanduser("~/.local/bin/dozerstart"),
os.path.expanduser("~/.local/etc/dozer.env")],
]
unit_locations = ["/etc/systemd/system/dozer.service",
"/etc/systemd/system/dozer.service",
"~/.config/systemd/user/dozer.service"]
# valid installations that were found
locations = []
for location in possible_locations:
if os.path.exists(location) and os.path.isdir(location) and\
os.path.exists(f"{location}{sep}main.py"):
locations.append(location)
if len(locations) == 0:
print("No installations found.")
print("See README.md for manual uninstallation instructions if this is incorrect.")
print()
exit(0)
print(f"Found installation{'s' if len(locations) > 1 else ''}:")
for location in locations:
print(f" * {location}")
print()
confirmation = confirm("Uninstall everything")
if not confirmation:
print("Abort. --- ")
sys.exit(1)
# uninstall everything
for location in locations:
print(f"Uninstalling {location}...")
# give option to back up .env
if os.path.exists(f"{location}{sep}.env"):
back_option = confirm("Back up .env file", default_state=True)
if back_option:
# keep trying until it works
while True:
back_path = input("Choose a target path (default = ~/.env): ")
if not back_path:
back_path = os.path.expanduser("~/.env")
# manually move file to dodge permissions errors
try:
with open(f"{location}{sep}.env", "r") as i:
with open(back_path, "w") as o:
o.write(i.read())
except FileExistsError:
print("File already exists at target")
continue
except OSError as e:
print("Could not move file: " + str(e))
continue
print("Successfully backed up .env")
break
is_user = is_subdir(os.path.expanduser("~"), location)
# find extra details
index = possible_locations.index(location)
files_to_unlink: list[str] = unlink_locations[index]
unit = unit_locations[index]
if is_user:
# stop target
found_unit = os.path.exists(unit)
if found_unit:
subprocess.run(["systemctl", "--user", "stop", "dozer.service"])
subprocess.run(["systemctl", "--user", "disable", "dozer.service"])
print("Stopped and disabled service")
# reconfirm
print("Found the following targets with appropriate action:")
print(f" * directory {location}: remove tree")
for target in files_to_unlink:
print(f" * symlink {target}: unlink")
if found_unit:
print(f" * systemd unit file {unit}: delete")
if os.path.exists(unit + ".d"):
print(f" * systemd unit config {unit + ".d"}: remove tree")
confirm_option = confirm("Do you want to continue")
if not confirm_option:
print("Abort. --- ")
continue
# unlink
for target in files_to_unlink:
os.unlink(target)
print("Unlinked " + target)
# remove tree (main)
shutil.rmtree(location)
print("Removed main installation files at " + location)
# delete unit
if found_unit:
os.remove(unit)
print("Removed systemd unit file at " + unit)
if os.path.exists(unit + ".d"):
shutil.rmtree(unit + ".d")
print("Removed systemd unit config at " + unit + ".d")
else:
# stop target
found_unit = os.path.exists(unit)
if found_unit:
subprocess.run(["sudo", "systemctl", "stop", "dozer.service"])
subprocess.run(["sudo", "systemctl", "disable", "dozer.service"])
print("Stopped and disabled service")
# reconfirm
print("Found the following targets with appropriate actions:")
print(f" * directory {location}: remove tree")
for target in files_to_unlink:
print(f" * symlink {target}: unlink")
if found_unit:
print(f" * systemd unit file {unit}: delete")
if os.path.exists(unit + ".d"):
print(f" * systemd unit config {unit + ".d"}: remove tree")
print("(remove tree = delete all subdirectories, and then the"
" directory itself)")
print()
confirm_option = confirm("Do you want to continue")
if not confirm_option:
print("Abort. --- ")
continue
# unlink
for target in files_to_unlink:
subprocess.run(["sudo", "unlink", target])
print("Unlinked " + target)
# remove tree (main)
subprocess.run(["sudo", "rm", "-rf", location])
print("Removed main installation files at " + location)
# delete unit
if found_unit:
subprocess.run(["sudo", "rm", "-f", unit])
print("Removed systemd unit file at " + unit)
if os.path.exists(unit + ".d"):
subprocess.run(["sudo", "rm", "-rf", unit + ".d"])
print("Removed systemd unit config at " + unit + ".d")
# remove logs if applicable
print()
# find logs
varlog_location = "/var/log/dozer.log"
cache_location = os.path.expanduser("~/.cache/dozer.log")
if not os.path.exists(varlog_location):
varlog_location = None
if not os.path.exists(cache_location):
cache_location = None
if varlog_location is not None or cache_location is not None:
# first give the user the list of files
print(f"Found log file{"s" if varlog_location is not None
and cache_location is not None else ""}:")
if varlog_location is not None:
print(f" * {varlog_location}")
if cache_location is not None:
print(f" * {cache_location}")
# then ask if they want them gone
confirm_remove_logs = confirm("Remove log files")
if confirm_remove_logs:
# i.e.
# no logs: <none>
# one log: Found log file:
# two logs: Found log files:
cont = confirm("Delete these logs")
if cont:
if varlog_location is not None:
subprocess.run(["sudo", "rm", "-f", varlog_location])
print("Removed " + varlog_location)
if cache_location is not None:
os.remove(cache_location)
print("Removed " + cache_location)
print()
print("Done. Check for any logrotate leftovers (if applicable).")
print()
print("Successfully uninstalled")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Missing subcommand.")
setup_help()
sys.exit(1)
if len(sys.argv) > 2:
print("Too many arguments.")
setup_help()
sys.exit(1)
match sys.argv[1]:
case "import":
setup_import()
sys.exit(0)
case "install":
setup_install()
sys.exit(0)
case "uninstall":
setup_uninstall()
sys.exit(0)
case "help":
print(" === Dozer Setup === ")
print("Version 1.0.0")
print("Task: Help")
setup_help()
case _:
print("Unknown command.")
setup_help()
sys.exit(1)