This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
updater.py
executable file
·133 lines (109 loc) · 4.06 KB
/
updater.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
#!/usr/bin/env python
'''
This provides a scripted update of a core service of the 3D Printer
It is meant to be executed from the command line as "update-redeem"
By doing so, the command will transfer into the appropriate virtual environment
and perform its actions there.
*** Attempting to run the command by other methods such as executing `python updater.py` ***
*** is highly discouraged. Doing so, without following proper virtual environment protocols ***
*** will not produce the desired result and is VERY LIKELY to CREATE AN NON-WORKING SYSTEM ***
'''
import os
import subprocess
from six import PY3
# the_service_name = 'redeem'
def switch_to_source_directory():
# We assume that this file is located in the top level of the repository
# and located within ${VIRTUAL_ENV}/src/${service_name}/
# virtual env is two directories up
this_dir = os.path.dirname(os.path.realpath(os.path.abspath(__file__)))
venv_base = os.path.normpath(os.path.join(this_dir, "..", '..'))
bin_dir = os.path.join(venv_base, 'bin')
os.environ["PATH"] = os.pathsep.join([bin_dir] + os.environ.get("PATH", "").split(os.pathsep))
os.environ["VIRTUAL_ENV"] = venv_base
os.chdir(this_dir)
the_service_name = os.path.basename(this_dir)
print("Working in virtual environment {}".format(os.path.basename(venv_base)))
return the_service_name
def is_service_running(name):
with open(os.devnull, 'wb') as hide_output:
exit_code = subprocess.Popen(['sudo', 'service', name, 'status'],
stdout=hide_output,
stderr=hide_output).wait()
return exit_code == 0
def start_service(name):
print("Start the {} service".format(name))
command = ["sudo", "/bin/systemctl", "restart", name]
with open(os.devnull, 'wb') as hide_output:
try:
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=hide_output)
except:
print("Error restarting")
stdout = p.communicate()[0].strip()
if PY3:
stdout = stdout.decode()
exit_code = p.returncode
if exit_code != 0:
print("Restart of %s failed with return code %d" % (name, p.returncode))
print(stdout)
return exit_code == 0
def stop_service(name):
print("Stop the {} service".format(name))
command = ["sudo", "/bin/systemctl", "stop", name]
with open(os.devnull, 'wb') as hide_output:
try:
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=hide_output)
except:
print("Error stopping")
stdout = p.communicate()[0].strip()
if PY3:
stdout = stdout.decode()
exit_code = p.returncode
if exit_code != 0:
print("Stop of %s failed with return code %d" % (name, p.returncode))
print(stdout)
return exit_code == 0
def perform_git_update():
print("Perform git update")
command = ["git", "pull"]
with open(os.devnull, 'wb') as hide_output:
try:
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=hide_output)
except:
print("Error running \"git pull\"")
stdout = p.communicate()[0].strip()
if PY3:
stdout = stdout.decode()
exit_code = p.returncode
if exit_code != 0:
print("\"git pull\" failed with return code %d" % (p.returncode))
print(stdout)
return exit_code == 0
def reinstall(name):
print("Run setup")
command = ['python', 'setup.py', 'develop']
with open(os.devnull, 'wb') as hide_output:
try:
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=hide_output)
except:
print("Error running \"setup\"")
stdout = p.communicate()[0].strip()
if PY3:
stdout = stdout.decode()
exit_code = p.returncode
if exit_code != 0:
print("Installation of %s failed with return code %d" % (name, p.returncode))
print(stdout)
return exit_code == 0
def perform_update():
service_name = switch_to_source_directory()
service_was_running = is_service_running(service_name)
if service_was_running:
stop_service(service_name)
perform_git_update()
reinstall(service_name)
if service_was_running:
start_service(service_name)
print("Update of %s complete" % (service_name))
if __name__ == '__main__':
perform_update()