Skip to content

Commit d4d352b

Browse files
committed
Rewrite all services, cleate new cli interface, fix more bugs, add root check
1 parent fd55439 commit d4d352b

File tree

18 files changed

+496
-320
lines changed

18 files changed

+496
-320
lines changed

app/interfaces/api/api.py

Whitespace-only changes.

app/interfaces/cli/cli.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
from app.interfaces.cli.menu import Menu
2+
from app.services.bbr import BBRService
3+
from app.services.docker import DockerService
4+
from app.services.fail2ban import Fail2BanService
5+
from app.services.ufw import UFWService
6+
7+
8+
def run_interactive_script():
9+
menu = Menu()
10+
11+
while True:
12+
menu.update_status()
13+
menu.display()
14+
15+
choice = input()
16+
17+
if choice == "0":
18+
break
19+
elif choice == "1":
20+
bbr_menu()
21+
elif choice == "2":
22+
docker_menu()
23+
elif choice == "3":
24+
fail2ban_menu()
25+
elif choice == "4":
26+
ufw_menu()
27+
28+
29+
def bbr_menu():
30+
while True:
31+
print("\n1 - Enable BBR")
32+
print("2 - Disable BBR")
33+
print("0 - Назад")
34+
35+
choice = input("Выбор: ")
36+
37+
if choice == "0":
38+
break
39+
40+
bbr = BBRService.create()
41+
42+
if choice == "1":
43+
bbr.enable()
44+
print("BBR включен")
45+
input("Press Enter...")
46+
elif choice == "2":
47+
bbr.disable()
48+
print("BBR выключен")
49+
input("Press Enter...")
50+
51+
52+
def docker_menu():
53+
while True:
54+
print("\n1 - Install Docker")
55+
print("2 - Uninstall Docker")
56+
print("0 - Назад")
57+
58+
choice = input("Выбор: ")
59+
60+
if choice == "0":
61+
break
62+
63+
docker = DockerService.create()
64+
65+
if choice == "1":
66+
docker.install()
67+
print("Docker установлен")
68+
input("Press Enter...")
69+
elif choice == "2":
70+
docker.uninstall()
71+
print("Docker удален")
72+
input("Press Enter...")
73+
74+
75+
def fail2ban_menu():
76+
while True:
77+
print("\n1 - Install Fail2Ban")
78+
print("2 - Uninstall Fail2Ban")
79+
print("0 - Назад")
80+
81+
choice = input("Выбор: ")
82+
83+
if choice == "0":
84+
break
85+
86+
fail2ban = Fail2BanService.create()
87+
88+
if choice == "1":
89+
fail2ban.install()
90+
print("Fail2Ban установлен")
91+
input("Press Enter...")
92+
elif choice == "2":
93+
fail2ban.uninstall()
94+
print("Fail2Ban удален")
95+
input("Press Enter...")
96+
97+
98+
def ufw_menu():
99+
while True:
100+
print("\n1 - Install UFW")
101+
print("2 - Uninstall UFW")
102+
print("0 - Назад")
103+
104+
choice = input("Выбор: ")
105+
106+
if choice == "0":
107+
break
108+
109+
ufw = UFWService.create()
110+
111+
if choice == "1":
112+
ufw.install()
113+
print("UFW установлен")
114+
input("Press Enter...")
115+
elif choice == "2":
116+
ufw.uninstall()
117+
print("UFW удален")
118+
input("Press Enter...")

app/interfaces/cli/menu.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import platform
2+
from subprocess import run
3+
4+
from app.utils import sysinfo_utils
5+
6+
7+
class Menu:
8+
def __init__(self):
9+
self.distro = sysinfo_utils.detect_distro()
10+
self.kernel = platform.release()
11+
self.hostname = platform.node()
12+
13+
# Service statuses
14+
self.bbr_status = "Unknown"
15+
self.docker_status = "Unknown"
16+
self.fail2ban_status = "Unknown"
17+
self.ufw_status = "Unknown"
18+
19+
self.menu_text = ""
20+
self.update_status()
21+
22+
def update_status(self):
23+
"""Update all service statuses"""
24+
self.bbr_status = self._check_bbr()
25+
self.docker_status = self._check_service("docker")
26+
self.fail2ban_status = self._check_service("fail2ban")
27+
self.ufw_status = self._check_ufw()
28+
self._build_menu_text()
29+
30+
def _check_bbr(self):
31+
"""Check BBR status"""
32+
try:
33+
result = run(
34+
["sysctl", "net.ipv4.tcp_congestion_control"],
35+
capture_output=True,
36+
text=True,
37+
)
38+
if "bbr" in result.stdout:
39+
return "Enabled"
40+
return "Disabled"
41+
except Exception:
42+
return "Unknown"
43+
44+
def _check_service(self, service_name):
45+
"""Check service status (systemd)"""
46+
try:
47+
result = run(
48+
["systemctl", "is-active", service_name], capture_output=True, text=True
49+
)
50+
status = result.stdout.strip()
51+
if status == "active":
52+
return "Running"
53+
return "Stopped"
54+
except Exception:
55+
return "Not Installed"
56+
57+
def _check_ufw(self):
58+
"""Check UFW status"""
59+
try:
60+
result = run(["ufw", "status"], capture_output=True, text=True)
61+
if "Status: active" in result.stdout:
62+
return "Active"
63+
return "Inactive"
64+
except Exception:
65+
return "Not Installed"
66+
67+
def _build_menu_text(self):
68+
"""Build menu text"""
69+
self.menu_text = f"""
70+
╔════════════════════════════════════════════════════════════╗
71+
║ DevOps Automation Scripts v1.0.4 ║
72+
╚════════════════════════════════════════════════════════════╝
73+
74+
System: {self.distro} | Kernel: {self.kernel} | Host: {self.hostname}
75+
Services: BBR [{self.bbr_status}] | Docker [{self.docker_status}] | Fail2Ban [{self.fail2ban_status}] | UFW [{self.ufw_status}]
76+
77+
────────────────────────────────────────────────────────────
78+
79+
Выберите скрипт:
80+
0 - Выход
81+
1 - BBR
82+
2 - Docker
83+
3 - Fail2Ban
84+
4 - UFW
85+
86+
Выбор: """
87+
88+
def display(self):
89+
"""Display menu"""
90+
print(self.menu_text, end="")

app/interfaces/cli/run.py

Lines changed: 0 additions & 84 deletions
This file was deleted.

app/services/bbr.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
1+
from abc import ABC, abstractmethod
2+
13
from app.utils import sysinfo_utils
24

3-
__os: str | None = sysinfo_utils.detect_distro().lower()
45

5-
if __os == "debian":
6-
from app.services.distro.debian.bbr import BBRService
7-
elif __os == "arch":
8-
from app.services.distro.arch.bbr import BBRService
9-
elif __os == "openwrt":
10-
from app.services.distro.wrt.bbr import BBRService
11-
else:
12-
raise OSError("Unsupported distro!")
6+
class BBRService(ABC):
7+
"""Base class for BBR congestion control management"""
8+
9+
def __init__(self):
10+
self.path_to_sysctl_config = "/etc/sysctl.conf"
11+
self.bbr_config = """
12+
net.core.default_qdisc=fq_codel
13+
net.ipv4.tcp_congestion_control=bbr
14+
"""
15+
16+
@abstractmethod
17+
def enable(self):
18+
"""Enable BBR"""
19+
pass
20+
21+
@abstractmethod
22+
def disable(self):
23+
"""Disable BBR"""
24+
pass
25+
26+
@staticmethod
27+
def create():
28+
"""Factory method to create instance based on OS"""
29+
__os: str | None = sysinfo_utils.detect_distro().lower()
30+
31+
if __os == "debian":
32+
from app.services.distro.debian.bbr import DebianBBRService
33+
34+
return DebianBBRService()
35+
if __os == "arch":
36+
from app.services.distro.arch.bbr import ArchBBRService
37+
38+
return ArchBBRService()
39+
if __os == "openwrt":
40+
from app.services.distro.wrt.bbr import WrtBBRService
1341

14-
bbrservice = BBRService()
42+
return WrtBBRService()
43+
raise OSError(f"Unsupported distro: {__os}")

0 commit comments

Comments
 (0)