-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevops_tool.py
More file actions
49 lines (42 loc) · 1.36 KB
/
devops_tool.py
File metadata and controls
49 lines (42 loc) · 1.36 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
import subprocess
from pathlib import Path
import typer, psutil
from rich import print
app = typer.Typer(help='DevOps utility CLI')
@app.command()
def health():
print({
'cpu_percent': psutil.cpu_percent(interval=1),
'memory_percent': psutil.virtual_memory().percent,
'disk_percent': psutil.disk_usage('/').percent,
'boot_time': psutil.boot_time(),
})
@app.command()
def ports(port: int = typer.Option(..., help='TCP port to inspect')):
out = subprocess.check_output(['sh', '-lc', f'ss -lntp | grep :{port} || true'], text=True)
print(out or f'No listening process found on port {port}')
@app.command()
def docker_clean(dry_run: bool = True):
cmd = 'docker system prune -af --volumes'
if dry_run:
print(f'[yellow]Dry run.[/yellow] Would run: {cmd}')
else:
subprocess.run(['sh', '-lc', cmd], check=False)
@app.command()
def nginx_config(server_name: str, upstream: str, out: Path = Path('./nginx.conf')):
content = f"""
server {{
listen 80;
server_name {server_name};
location / {{
proxy_pass http://{upstream};
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}}
}}
"""
out.write_text(content.strip() + '\n')
print(f'Wrote {out}')
if __name__ == '__main__':
app()