-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstaller.py
More file actions
555 lines (446 loc) · 18.4 KB
/
installer.py
File metadata and controls
555 lines (446 loc) · 18.4 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
import os
import requests
import pexpect
import sys
import subprocess
import shutil
import secrets
import base64
import getpass
import time
from colorama import Fore, Style, init
init(autoreset=True)
def create_secret_key(length: int = 64) -> str:
random_bytes = secrets.token_bytes(length)
secret_key = base64.b64encode(random_bytes).decode("utf-8").rstrip("=")
return secret_key
def get_server_ip():
try:
result = subprocess.run(
["hostname", "-I"], capture_output=True, text=True, check=True
)
ip_addresses = result.stdout.strip().split()
return ip_addresses[0] if ip_addresses else "your-server-ip"
except:
return "your-server-ip"
def display_panel_info(username, password, port, path):
subprocess.run("clear")
server_ip = get_server_ip()
url = f"http://{server_ip}:{port}/{path}" if path else f"http://{server_ip}:{port}/"
print(f"\n{Fore.CYAN}Access URL: {url}{Style.RESET_ALL}\n")
def ask_user(prompt, allow_empty=False, input_type="text"):
while True:
try:
if input_type == "password":
value = getpass.getpass(prompt)
else:
value = input(prompt)
if not allow_empty and not value.strip():
print(Fore.RED + "Input cannot be empty. Please try again...")
time.sleep(2)
subprocess.run("clear")
continue
if input_type == "port":
try:
port_num = int(value)
if not (1 <= port_num <= 65535):
print(Fore.RED + "Port must be between 1 and 65535.")
time.sleep(2)
subprocess.run("clear")
continue
except ValueError:
print(Fore.RED + "Port must be a valid number.")
time.sleep(2)
subprocess.run("clear")
continue
return value.strip()
except KeyboardInterrupt:
print(f"\n\n{Fore.GREEN}Thank you for using OV-Panel!{Style.RESET_ALL}\n")
sys.exit(0)
def ask_password(prompt):
while True:
try:
password = getpass.getpass(prompt)
if not password.strip():
print(Fore.RED + "Password cannot be empty. Please try again...")
time.sleep(2)
subprocess.run("clear")
continue
confirm_password = getpass.getpass("Confirm password: ")
if password != confirm_password:
print(Fore.RED + "Passwords do not match. Please try again...")
time.sleep(2)
subprocess.run("clear")
continue
return password
except KeyboardInterrupt:
print(f"\n\n{Fore.GREEN}Thank you for using OV-Panel!{Style.RESET_ALL}\n")
sys.exit(0)
def show_banner():
subprocess.run("clear")
banner = f"""
{Fore.CYAN} ____ _ ____ ____ _ _____ _
/ _ \\/ \\ |\\/ __\\/ _ \\/ \\ /|/ __// \\
| / \\|| | //| \\/|| / \\|| |\\ ||| \\ | |
| \\_/|| \\// | __/| |-||| | \\||| /_ | |_/\\\\
\\____/\\__/ \\_/ \\_/ \\|\\_/ \\|\\____\\\\____/
{Style.RESET_ALL}
"""
print(banner)
def show_menu():
show_banner()
print(f"{Fore.YELLOW}Please choose an option:{Style.RESET_ALL}\n")
options = [
("1", "Install", Fore.GREEN),
("2", "Update", Fore.CYAN),
("3", "Restart", Fore.BLUE),
("4", "Uninstall", Fore.RED),
("5", "Exit", Fore.YELLOW),
]
for num, desc, color in options:
print(f" {color}[{num}]{Style.RESET_ALL} {desc}")
print()
def ask_choice():
while True:
try:
choice = input(f"{Fore.YELLOW}Enter your choice: {Style.RESET_ALL}")
if choice in ["1", "2", "3", "4", "5"]:
return choice
else:
print(
f"\n{Fore.RED}Invalid choice. Please enter a number between 1-5{Style.RESET_ALL}"
)
time.sleep(2)
show_menu()
except KeyboardInterrupt:
print(f"\n\n{Fore.GREEN}Thank you for using OV-Panel!{Style.RESET_ALL}\n")
sys.exit(0)
def ask_confirmation(prompt):
while True:
try:
choice = input(f"{Fore.YELLOW}{prompt} {Style.RESET_ALL}").strip().lower()
if choice in ["y", "yes", "n", "no"]:
return choice in ["y", "yes"]
else:
print(Fore.RED + "Please enter 'y' for yes or 'n' for no")
time.sleep(2)
subprocess.run("clear")
except KeyboardInterrupt:
print(f"\n\n{Fore.GREEN}Thank you for using OV-Panel!{Style.RESET_ALL}\n")
sys.exit(0)
def setup_panel():
try:
subprocess.run("clear")
print(f"\n{Fore.YELLOW}Installing OpenVPN Core...{Style.RESET_ALL}\n")
subprocess.run(
["wget", "https://git.io/vpn", "-O", "/root/openvpn-install.sh"],
check=True, # thanks to Nyr for ovpn installation script <3 https://github.com/Nyr/openvpn-install
)
bash = pexpect.spawn(
"/usr/bin/bash", ["/root/openvpn-install.sh"], encoding="utf-8", timeout=180
)
prompts = [
(r"Which IPv4 address should be used.*:", "1"),
(r"Protocol.*:", "2"),
(r"Port.*:", "1194"),
(r"Select a DNS server for the clients.*:", "1"),
(r"Enter a name for the first client.*:", "first_client"),
(r"Press any key to continue...", ""),
]
for pattern, reply in prompts:
try:
bash.expect(pattern, timeout=10)
bash.sendline(reply)
except pexpect.TIMEOUT:
pass
bash.expect(pexpect.EOF, timeout=None)
bash.close()
shutil.copy(".env.example", ".env")
subprocess.run("clear")
print(f"\n{Fore.YELLOW}Panel Configuration{Style.RESET_ALL}\n")
panel_username = ask_user(f"{Fore.GREEN}> Panel username: {Style.RESET_ALL}")
panel_password = ask_password(f"{Fore.RED}> Panel password: {Style.RESET_ALL}")
panel_port = ask_user(
f"{Fore.GREEN}> Panel port number: {Style.RESET_ALL}", input_type="port"
)
panel_path = ask_user(
f"{Fore.GREEN}> Panel path (optional): {Style.RESET_ALL}", allow_empty=True
)
replacements = {
"ADMIN_USERNAME": panel_username,
"ADMIN_PASSWORD": panel_password,
"PORT": panel_port,
"URLPATH": panel_path,
"JWT_SECRET_KEY": create_secret_key(),
}
lines = []
with open(".env", "r") as f:
for line in f:
for key, value in replacements.items():
if line.startswith(f"{key}="):
line = f"{key}={value}\n"
lines.append(line)
with open(".env", "w") as f:
f.writelines(lines)
# Create virtual environment if not exists
venv_dir = "/opt/ov-panel/venv"
if not os.path.exists(venv_dir):
print(f"\n{Fore.YELLOW}Creating virtual environment...{Style.RESET_ALL}")
subprocess.run(["/usr/bin/python3", "-m", "venv", "venv"], check=True)
install_dependencies()
apply_migrations()
subprocess.run("clear")
print(f"\n{Fore.YELLOW}Installation Complete!{Style.RESET_ALL}")
display_panel_info(panel_username, panel_password, panel_port, panel_path)
start_service()
try:
input(f"{Fore.YELLOW}Press Enter to return to menu...{Style.RESET_ALL}")
except KeyboardInterrupt:
print(f"\n\n{Fore.GREEN}Thank you for using OV-Panel!{Style.RESET_ALL}\n")
sys.exit(0)
main_menu()
except Exception as e:
print(f"\n{Fore.RED}Installation failed: {e}{Style.RESET_ALL}")
try:
input(f"\n{Fore.YELLOW}Press Enter to return to menu...{Style.RESET_ALL}")
except KeyboardInterrupt:
print(f"\n\n{Fore.GREEN}Thank you for using OV-Panel!{Style.RESET_ALL}\n")
sys.exit(0)
main_menu()
def refresh_panel():
if not os.path.exists("/opt/ov-panel") or not os.path.exists(
"/etc/systemd/system/ov-panel.service"
):
subprocess.run("clear")
print(
f"\n{Fore.MAGENTA}OV-Panel is not installed on your system.{Style.RESET_ALL}"
)
print(
f"{Fore.MAGENTA}Please install OV-Panel first using option 1.{Style.RESET_ALL}"
)
try:
input(f"\n{Fore.MAGENTA}Press Enter to return to menu...{Style.RESET_ALL}")
except KeyboardInterrupt:
print(f"\n\n{Fore.GREEN}Thank you for using OV-Panel!{Style.RESET_ALL}\n")
sys.exit(0)
main_menu()
return
print(f"\n{Fore.YELLOW}Updating OV-Panel...{Style.RESET_ALL}\n")
try:
repo = "https://api.github.com/repos/primeZdev/ov-panel/releases/latest"
install_dir = "/opt/ov-panel"
env_file = os.path.join(install_dir, ".env")
data_dir = os.path.join(install_dir, "data")
venv_dir = os.path.join(install_dir, "venv")
backup_env = "/tmp/ovpanel_env_backup"
backup_data = "/tmp/ovpanel_data_backup"
backup_venv = "/tmp/ovpanel_venv_backup"
response = requests.get(repo)
response.raise_for_status()
release = response.json()
download_url = release["tarball_url"]
filename = "/tmp/ov-panel-latest.tar.gz"
print(f"{Fore.YELLOW}Downloading latest version...{Style.RESET_ALL}")
subprocess.run(["wget", "-O", filename, download_url], check=True)
if os.path.exists(env_file):
shutil.copy2(env_file, backup_env)
if os.path.exists(data_dir):
shutil.copytree(data_dir, backup_data, dirs_exist_ok=True)
if os.path.exists(venv_dir):
print(f"{Fore.YELLOW}Backing up virtual environment...{Style.RESET_ALL}")
shutil.copytree(venv_dir, backup_venv, dirs_exist_ok=True)
if os.path.exists(install_dir):
shutil.rmtree(install_dir)
os.makedirs(install_dir, exist_ok=True)
subprocess.run(
["tar", "-xzf", filename, "-C", install_dir, "--strip-components=1"],
check=True,
)
if os.path.exists(backup_env):
shutil.move(backup_env, env_file)
if os.path.exists(backup_data):
if os.path.exists(data_dir):
shutil.rmtree(data_dir)
shutil.move(backup_data, data_dir)
os.chdir(install_dir)
# Restore or create venv
if os.path.exists(backup_venv):
print(f"{Fore.YELLOW}Restoring virtual environment...{Style.RESET_ALL}")
shutil.move(backup_venv, venv_dir)
else:
print(f"{Fore.YELLOW}Creating virtual environment...{Style.RESET_ALL}")
subprocess.run(["/usr/bin/python3", "-m", "venv", "venv"], check=True)
install_dependencies()
apply_migrations()
subprocess.run(["systemctl", "restart", "ov-panel"], check=True)
print(f"\n{Fore.GREEN}Update Complete!{Style.RESET_ALL}\n")
try:
input(f"{Fore.YELLOW}Press Enter to return to menu...{Style.RESET_ALL}")
except KeyboardInterrupt:
print(f"\n\n{Fore.GREEN}Thank you for using OV-Panel!{Style.RESET_ALL}\n")
sys.exit(0)
main_menu()
except Exception as e:
print(f"\n{Fore.RED}Update failed: {str(e)}{Style.RESET_ALL}")
try:
input(f"\n{Fore.YELLOW}Press Enter to return to menu...{Style.RESET_ALL}")
except KeyboardInterrupt:
print(f"\n\n{Fore.GREEN}Thank you for using OV-Panel!{Style.RESET_ALL}\n")
sys.exit(0)
main_menu()
def restart_panel():
try:
if not os.path.exists("/etc/openvpn") and not os.path.exists("/opt/ov-panel"):
print(f"\n{Fore.RED}OV-Panel is not installed.{Style.RESET_ALL}")
return
print(f"\n{Fore.YELLOW}Restarting OV-Panel...{Style.RESET_ALL}")
subprocess.run(["systemctl", "restart", "ov-panel"], check=True)
time.sleep(3)
subprocess.run(["systemctl", "restart", "openvpn-server@server"], check=True)
print(f"\n{Fore.GREEN}OV-Panel restarted successfully!{Style.RESET_ALL}")
input(f"{Fore.YELLOW}Press Enter to return to menu...{Style.RESET_ALL}")
main_menu()
except Exception as e:
print(f"\n{Fore.RED}Failed to restart OV-Panel: {str(e)}{Style.RESET_ALL}")
try:
input(f"{Fore.YELLOW}Press Enter to return to menu...{Style.RESET_ALL}")
except KeyboardInterrupt:
print(f"\n\n{Fore.GREEN}Thank you for using OV-Panel!{Style.RESET_ALL}\n")
sys.exit(0)
main_menu()
def remove_panel():
try:
if not os.path.exists("/etc/openvpn") and not os.path.exists("/opt/ov-panel"):
subprocess.run("clear")
print(
f"\n{Fore.YELLOW}OV-Panel is not installed on your system.{Style.RESET_ALL}"
)
try:
input(
f"\n{Fore.YELLOW}Press Enter to return to menu...{Style.RESET_ALL}"
)
except KeyboardInterrupt:
print(
f"\n\n{Fore.GREEN}Thank you for using OV-Panel!{Style.RESET_ALL}\n"
)
sys.exit(0)
main_menu()
return
subprocess.run("clear")
print(f"\n{Fore.RED}Warning: This will remove all panel data!{Style.RESET_ALL}")
if not ask_confirmation("Do you want to proceed? (y/n): "):
print(f"\n{Fore.YELLOW}Uninstallation cancelled.{Style.RESET_ALL}")
time.sleep(1)
main_menu()
return
bash = pexpect.spawn("bash /root/openvpn-install.sh", timeout=60)
subprocess.run("clear")
print(f"\n{Fore.YELLOW}Processing removal...{Style.RESET_ALL}\n")
bash.expect("Option:")
bash.sendline("3")
bash.expect("Confirm OpenVPN removal")
bash.sendline("y")
bash.expect("OpenVPN removed!")
if os.path.exists("/etc/systemd/system/ov-panel.service"):
os.remove("/etc/systemd/system/ov-panel.service")
subprocess.run(["sudo", "systemctl", "daemon-reload"])
stop_service()
print(f"\n{Fore.GREEN}Uninstallation Complete!{Style.RESET_ALL}\n")
stop_service()
try:
input(f"{Fore.YELLOW}Press Enter to return to menu...{Style.RESET_ALL}")
except KeyboardInterrupt:
print(f"\n\n{Fore.GREEN}Thank you for using OV-Panel!{Style.RESET_ALL}\n")
sys.exit(0)
main_menu()
except Exception as e:
print(f"\n{Fore.RED}Uninstallation failed: {str(e)}{Style.RESET_ALL}")
try:
input(f"\n{Fore.YELLOW}Press Enter to return to menu...{Style.RESET_ALL}")
except KeyboardInterrupt:
print(f"\n\n{Fore.GREEN}Thank you for using OV-Panel!{Style.RESET_ALL}\n")
sys.exit(0)
main_menu()
def apply_migrations() -> None:
"""Apply database migrations using Alembic"""
backend_dir = "/opt/ov-panel/backend"
data_dir = "/opt/ov-panel/data"
current_dir = os.getcwd()
try:
os.makedirs(data_dir, exist_ok=True)
os.chdir(backend_dir)
if not os.path.exists("alembic.ini"):
print(f"{Fore.RED}Error: alembic.ini not found{Style.RESET_ALL}")
return
print(f"{Fore.YELLOW}Running database migrations...{Style.RESET_ALL}")
venv_alembic = "/opt/ov-panel/venv/bin/alembic"
subprocess.run([venv_alembic, "upgrade", "head"], check=True)
print(f"{Fore.GREEN}Database migrations completed!{Style.RESET_ALL}")
except subprocess.CalledProcessError as e:
print(f"{Fore.RED}Database migration failed!{Style.RESET_ALL}")
raise
finally:
os.chdir(current_dir)
def install_dependencies() -> None:
"""Install Python dependencies into virtual environment"""
venv_pip = "/opt/ov-panel/venv/bin/pip"
try:
print(f"{Fore.YELLOW}Installing dependencies...{Style.RESET_ALL}")
subprocess.run([venv_pip, "install", "--upgrade", "pip"], check=True)
if os.path.exists("/opt/ov-panel/pyproject.toml"):
subprocess.run([venv_pip, "install", "-e", "."], check=True, cwd="/opt/ov-panel")
print(f"{Fore.GREEN}Dependencies installed successfully!{Style.RESET_ALL}")
except subprocess.CalledProcessError as e:
print(f"{Fore.RED}Dependency installation failed: {e}{Style.RESET_ALL}")
raise
def start_service() -> None:
service_content = """
[Unit]
Description=OV-Panel App
After=network.target
[Service]
User=root
WorkingDirectory=/opt/ov-panel/
ExecStart=/opt/ov-panel/venv/bin/python main.py
Restart=always
RestartSec=5
Environment="PATH=/opt/ov-panel/venv/bin:/usr/local/bin:/usr/bin:/bin"
[Install]
WantedBy=multi-user.target
"""
with open("/etc/systemd/system/ov-panel.service", "w") as f:
f.write(service_content)
subprocess.run(["sudo", "systemctl", "daemon-reload"])
subprocess.run(["sudo", "systemctl", "enable", "ov-panel"])
subprocess.run(["sudo", "systemctl", "start", "ov-panel"])
def stop_service() -> None:
service_file = "/etc/systemd/system/ov-panel.service"
subprocess.run(["sudo", "systemctl", "stop", "ov-panel"], stderr=subprocess.DEVNULL)
if os.path.exists(service_file):
subprocess.run(["rm", "-f", service_file])
subprocess.run(["sudo", "systemctl", "daemon-reload"])
subprocess.run(["sudo", "systemctl", "reset-failed"], stderr=subprocess.DEVNULL)
def main_menu():
try:
show_menu()
choice = ask_choice()
if choice == "1":
setup_panel()
elif choice == "2":
refresh_panel()
elif choice == "3":
restart_panel()
elif choice == "4":
remove_panel()
elif choice == "5":
print(f"\n{Fore.GREEN}Thank you for using OV-Panel!{Style.RESET_ALL}\n")
sys.exit()
except KeyboardInterrupt:
print(f"\n\n{Fore.GREEN}Thank you for using OV-Panel!{Style.RESET_ALL}\n")
sys.exit(0)
if __name__ == "__main__":
try:
main_menu()
except KeyboardInterrupt:
print(f"\n\n{Fore.GREEN}Thank you for using OV-Panel!{Style.RESET_ALL}\n")
sys.exit(0)