-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuninstall.py
executable file
·108 lines (79 loc) · 2.62 KB
/
uninstall.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
#!/usr/bin/env python3
# deploy_it uninstall script
# Copyright 2019, Aswin Babu Karuvally
# import serious stuff
import os
import logging
import json
import sys
import subprocess
import pdb
import shutil
def run_cleanup_script(config):
if not config["cleanup_script"]["enable"]:
return
script_file = config["cleanup_script"]["script_file"]
if not os.path.isfile(script_file):
logging.critical(script_file + "does not exist, exiting...")
execute_command(script_file)
def remove_virtualenv(config):
install_path = config["basics"]["install_path"]
shutil.rmtree(install_path)
logging.info("removed virtualenv at " + install_path)
# execute command
def execute_command(command):
return_code = subprocess.call(command, shell=True)
if return_code != 0:
logging.warning(command + " cannot be executed, exiting")
sys.exit(1)
# remove symlink
def remove_symlink(config):
if not config["symlink"]["enable"]:
return
link_path = os.path.join(config["symlink"]["link_path"])
execute_command("rm " + link_path)
logging.info("removed symlink at " + link_path)
# setup logging
def setup_logging():
format_string = "[%(asctime)s] %(message)s"
date_format = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(
filename = os.path.join("build.log"),
level = logging.DEBUG,
format = format_string,
datefmt = date_format
)
# print logs to stderr
logging.getLogger().addHandler(logging.StreamHandler())
# stop and remove service
def remove_service(config):
if not config["systemd_service"]["enable"]:
return
unit_file = config["systemd_service"]["unit_file"]
service_name = os.path.basename(unit_file)
execute_command("systemctl stop " + os.path.basename(service_name))
execute_command("systemctl disable " + os.path.basename(service_name))
execute_command("rm /etc/systemd/system/" + service_name)
logging.info("removed service")
# the main function
def main():
# check if user is root
if os.getuid() != 0:
print("Script cannot be run as normal user, exiting...")
sys.exit(1)
# setup logging
setup_logging()
# check if config file exists
if not os.path.isfile("config.json"):
logging.critical("config.json does not exist, exiting...")
sys.exit(1)
with open("config.json") as config_file:
config = config_file.read()
config = json.loads(config)
remove_symlink(config)
remove_service(config)
remove_virtualenv(config)
run_cleanup_script(config)
# call the main function
if __name__ == "__main__":
main()