forked from ajth-in/codax-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodax-cli.py
More file actions
95 lines (81 loc) · 3.52 KB
/
Copy pathcodax-cli.py
File metadata and controls
95 lines (81 loc) · 3.52 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
import os
import subprocess
from typing import Optional
# third party
import typer
from halo import Halo
# local
from exceptions import ServiceNotFoundException, ExceedsLengthException
from utils import is_service_running, plot_table, scripts, plot_graph
VERSION = '0.0.1'
app = typer.Typer()
@app.command()
def version():
typer.echo(VERSION)
@app.command()
def install():
command = ['sudo', 'echo', 'CODAX CLI']
subprocess.run(command)
for prompt, script in scripts:
spinner = Halo(text=f"CODAX CLI {VERSION}", spinner='dots')
spinner.start()
spinner.text = prompt
typer.echo(script)
if not subprocess.run(["bash", f"scripts/install/{script}"]).returncode: # nosec
spinner.stop_and_persist(symbol='🦄 '.encode(
'utf-8'), text=f'\033[1;32mCompleted:{prompt}\n')
else:
spinner.fail(f"Failed:{prompt}")
spinner.stop()
typer.echo("\nProcedure Completed! \n")
@app.command()
def get_container_info(pid: Optional[str] = None, n: Optional[int] = 50):
if not is_service_running('codax.service'):
raise ServiceNotFoundException(
f"codax.service not found!! install and configure with `codax-cli install` ")
directory = os.path.join(os.path.expanduser("~"), ".codax", "data")
threshold_info = dict()
files = os.listdir(directory)
for file_ in files:
name, extension = os.path.splitext(file_)
if extension == ".thresh":
with open(os.path.join(directory, file_), 'r') as thresh_file:
threshold_info[name] = {"data": thresh_file.read().strip()}
try:
with open(os.path.join(directory, name), 'r') as thresh_file:
threshold_info[name]["series"] = [
int(x) for x in thresh_file.read().split(" ")]
except Exception as exception_:
typer.echo(exception_)
typer.echo(f"Failed to fetch the cpu-time sequence for {name}")
# typer.echo(threshold_info)
table_data = [
['Container ID', 'Threshold Value', 'info'], ]
if pid:
if pid in threshold_info:
# nosec
os.system(f'ps -p {pid} -o pid,ppid,user,stat,cmd,%cpu,%mem,etime')
if n:
if n >= len(threshold_info[pid]["series"]):
raise ExceedsLengthException(
f"N exceeds the size of cpu-sequence({n}>={len(threshold_info[pid]['series'])})")
return
typer.echo(f"\033[33mThe graph shows the last {n} cpu-time sequence")
plot_graph(threshold_info, pid, n)
typer.echo(
"\033[31mcodax-cli get-container-info --pid <container_pid> --log <n> : ")
typer.echo("\t Log the past n cpu-time sequence")
input("\033[34mPress Enter to see the past cpu-times...")
typer.echo(f"cpu-time sequence: {threshold_info[pid]['series']}")
return
for key, value in threshold_info.items():
table_data.append([key, value["data"], ""])
typer.echo(
f"\nCODAX:{VERSION}\nFollowing table shows the list of containers with the present threshold value")
plot_table(table_data)
typer.echo("\033[31mcodax-cli get-container-info --pid <container_pid>: ")
typer.echo("\t To get the detailed view of individual containers")
typer.echo("\033[31mcodax-cli get-container-info --pid <container_pid> --log <n> : ")
typer.echo("\t Log the past n cpu-time sequence")
if __name__ == "__main__":
app()