-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-installer-multios.py
270 lines (209 loc) · 8.23 KB
/
service-installer-multios.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
import subprocess
import platform
import os
import shutil
import time
SERVICE_NAME = "ican-machine-identity"
LINUX_SERVICE_EXECUTABLE_PATH = "ican-machine-identity"
WINDOWS_SERVICE_EXECUTABLE_PATH = "ican-machine-identity.exe"
# Check if systemd is available.
def is_systemd_available():
"""Checks if systemd is available."""
return subprocess.run(["systemctl", "status"], capture_output=True, text=True).returncode == 0
# Create a systemd service file.
def create_systemd_service(service_name, description, exec_start, user="root"):
"""Creates a systemd service file."""
service_file = f"""
[Unit]
Description={description}
[Service]
Type=simple
User={user}
ExecStart={exec_start}
Restart=on-failure
[Install]
WantedBy=multi-user.target
"""
service_file_path = f"/etc/systemd/system/{service_name}.service"
with open(service_file_path, "w") as f:
f.write(service_file)
subprocess.run(["systemctl", "daemon-reload"])
subprocess.run(["systemctl", "enable", service_name])
subprocess.run(["systemctl", "start", service_name])
# Uninstall a systemd service.
def uninstall_systemd_service(service_name):
"""Uninstalls a systemd service."""
subprocess.run(["systemctl", "stop", service_name])
subprocess.run(["systemctl", "disable", service_name])
subprocess.run(["rm", f"/etc/systemd/system/{service_name}.service"])
subprocess.run(["systemctl", "daemon-reload"])
# Check if the systemd service files is present or not.
def check_linux_service_installation_status():
"""Checks the systemd service files is present or not."""
service_file_path = f"/etc/systemd/system/{SERVICE_NAME}.service"
return os.path.exists(service_file_path)
# Check status of a systemd service.
def check_systemd_service_status(service_name):
"""Checks the status of a systemd service."""
return subprocess.run(["systemctl", "status", service_name], capture_output=True, text=True).returncode
# Install a systemd service for Linux.
def install_linux_service():
"""Installs a systemd service for Linux."""
# Copy the executable to the /usr/bin directory.
subprocess.run(["sudo", "cp", LINUX_SERVICE_EXECUTABLE_PATH, "/usr/bin/"])
create_systemd_service(
service_name=SERVICE_NAME,
description=SERVICE_NAME,
exec_start=LINUX_SERVICE_EXECUTABLE_PATH,
user="root",
)
print(f"Service {SERVICE_NAME} installed successfully.")
# Uninstall a linux service.
def uninstall_linux_service():
"""Uninstalls a systemd service for Linux."""
uninstall_systemd_service(SERVICE_NAME)
subprocess.run(["sudo", "rm", f"/usr/bin/{LINUX_SERVICE_EXECUTABLE_PATH}"])
print(f"Service {SERVICE_NAME} uninstalled successfully.")
# Check status of a linux service.
def check_linux_service_status():
"""Checks the status of a systemd service."""
return check_systemd_service_status(SERVICE_NAME)
# Install a windows service.
def install_windows_service():
"""Installs a windows service."""
folder_path = f"C:\\Program Files\\{SERVICE_NAME}"
if os.path.exists(folder_path):
shutil.rmtree(folder_path)
time.sleep(0.6)
# Create a folder in the program files directory.
os.makedirs(folder_path)
# Copy the executable to the program files directory using copy.
shutil.copy2(WINDOWS_SERVICE_EXECUTABLE_PATH, f"C:\\Program Files\\{SERVICE_NAME}\\")
# Get pwd.
pwd = os.getcwd()
# Append with nssm.exe.
nssm_path = os.path.join(pwd, "nssm.exe")
# Create a service using nssm.
subprocess.run([nssm_path, "install", SERVICE_NAME, f"C:\\Program Files\\{SERVICE_NAME}\\ican-machine-identity.exe"])
# Start the service.
ret = subprocess.run([nssm_path, "start", SERVICE_NAME])
status = False
for _ in range(10):
time.sleep(0.5)
status = check_windows_service_status()
if status:
break
if status:
print(f"Service {SERVICE_NAME} started successfully.")
else:
print(f"Service {SERVICE_NAME} failed to start.")
# Uninstall a windows service.
def uninstall_windows_service():
"""Uninstalls a windows service."""
# Get pwd.
pwd = os.getcwd()
# Append with nssm.exe.
nssm_path = os.path.join(pwd, "nssm.exe")
subprocess.run([nssm_path, "stop", SERVICE_NAME])
for _ in range(10):
if not check_windows_service_status():
break
time.sleep(0.5)
subprocess.run([nssm_path, "stop", SERVICE_NAME])
subprocess.run([nssm_path, "remove", SERVICE_NAME, "confirm"])
shutil.rmtree(f"C:\\Program Files\\{SERVICE_NAME}")
print(f"Service {SERVICE_NAME} uninstalled successfully.")
# Check installation status of a windows service.
def check_windows_service_installation_status():
"""Checks the status of a windows service."""
# Get pwd.
pwd = os.getcwd()
# Append with nssm.exe.
nssm_path = os.path.join(pwd, "nssm.exe")
return subprocess.run([nssm_path, "status", SERVICE_NAME]).returncode == 0
# Check if string matches with nssm output.
def nssm_contains(nssm_output, string_value):
if string_value in nssm_output:
return True
nssm_format = ''.join([el for (idx, el) in enumerate(nssm_output) if idx % 2 == 1])
if string_value in nssm_format:
return True
nssm_format = ''.join([el for (idx, el) in enumerate(nssm_output) if idx % 2 == 0])
return string_value in nssm_format
# Check installation status of a windows service.
def check_windows_service_status():
"""Check installation status of a windows service"""
# Get pwd.
pwd = os.getcwd()
# Append with nssm.exe.
nssm_path = os.path.join(pwd, "nssm.exe")
ret = subprocess.run([nssm_path, "status", SERVICE_NAME], capture_output=True, text=True)
return nssm_contains(ret.stdout, "SERVICE_RUNNING")
# Main.
print("Welcome to the Service Installer Multi OS")
print("1. Install service")
print("2. Uninstall service")
print("3. Check service installation status")
print("4. Check service running status")
ch = input("Enter your choice: ")
if ch == "1":
print("Installing service...")
# Use platform.system() to determine the OS and install the service accordingly.
if platform.system() == "Linux":
if is_systemd_available():
install_linux_service()
else:
print("Systemd is not available. Aborting installation.")
elif platform.system() == "Windows":
install_windows_service()
else:
print("Unsupported OS.")
elif ch == "2":
print("Uninstalling service...")
if platform.system() == "Linux":
if is_systemd_available():
uninstall_linux_service()
else:
print("Systemd is not available. Aborting uninstallation.")
elif platform.system() == "Windows":
uninstall_windows_service()
else:
print("Unsupported OS.")
elif ch == "3":
print("Checking service status...")
if platform.system() == "Linux":
if is_systemd_available():
status = check_linux_service_installation_status()
if status:
print(f"Service {SERVICE_NAME} is available.")
else:
print(f"Service {SERVICE_NAME} is not available.")
else:
print("Systemd is not available. Aborting status check.")
elif platform.system() == "Windows":
status = check_windows_service_installation_status()
if status:
print(f"Service {SERVICE_NAME} is available.")
else:
print(f"Service {SERVICE_NAME} is not available.")
else:
print("Unsupported OS.")
elif ch == "4":
print("Checking service running status...")
if platform.system() == "Linux":
if is_systemd_available():
status = check_linux_service_status()
if status == 0:
print(f"Service {SERVICE_NAME} is running.")
else:
print(f"Service {SERVICE_NAME} is not running.")
else:
print("Systemd is not available. Aborting status check.")
elif platform.system() == "Windows":
status = check_windows_service_status()
if status:
print(f"Service {SERVICE_NAME} is running.")
else:
print(f"Service {SERVICE_NAME} is not running.")
else:
print("Unsupported OS.")