-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcli.py
More file actions
122 lines (114 loc) · 4.49 KB
/
cli.py
File metadata and controls
122 lines (114 loc) · 4.49 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
import asyncio
import logging
import sys
from pathlib import Path
from pydoc import ttypager
import os
from datetime import datetime
from ceph_devstack import config, logger, parse_args, VERBOSE
from ceph_devstack.requirements import check_requirements
from ceph_devstack.resources.ceph import CephDevStack
def main():
args = parse_args(sys.argv[1:])
config.load(args.config_file)
if args.verbose:
for handler in logging.getLogger("root").handlers:
if not isinstance(handler, logging.FileHandler):
handler.setLevel(VERBOSE)
if args.command == "config":
if args.config_op == "dump":
print(config.dump())
if args.config_op == "get":
print(config.get_value(args.name))
elif args.config_op == "set":
config.set_value(args.name, args.value)
return
config["args"] = vars(args)
data_path = Path(config["data_dir"]).expanduser()
data_path.mkdir(parents=True, exist_ok=True)
obj = CephDevStack()
async def run():
if not await asyncio.gather(
check_requirements(),
obj.check_requirements(),
):
logger.error("Requirements not met!")
sys.exit(1)
if args.command == "doctor":
return
elif args.command == "wait":
return await obj.wait(container_name=args.container)
elif args.command == "logs":
ret=teuthology_logs(str(data_path))
return ret
else:
await obj.apply(args.command)
return 0
try:
sys.exit(asyncio.run(run()))
except KeyboardInterrupt:
logger.debug("Exiting!")
def teuthology_logs(data_path:str) -> int:
"""
This function is used to get the teuthology logs of the latest job run.
Args:
data_path (str): Path to the data directory.
"""
list_runs = [f.path for f in os.scandir(data_path + "/archive") if f.is_dir()]
if len(list_runs) == 0:
logger.error("No runs found!")
return 1
# Atleast one run directory exist, due to above condition
try:
list_runs.sort(key=lambda x: datetime.strptime(x.split('/')[-1].split('root-')[1].split('-teuthology')[0], '%Y-%m-%d_%H:%M:%S'))
except ValueError as e:
# if the run directory is not in the format root-YYYY-MM-DD_HH:MM:SS-teuthology
logger.error(f"Error parsing date: {e}")
return 1
latest_run = list_runs[-1]
latest_run_subdir = [f.path for f in os.scandir(latest_run) if f.is_dir()]
if len(latest_run_subdir) == 0:
logger.error("No jobs found!")
return 1
# check if only one job present, then display logs. Also check if the teuthology.log file exists in the latest run directory
if len(latest_run_subdir) == 1 :
if os.path.exists(latest_run_subdir[0] + "/teuthology.log") and os.path.isfile(latest_run_subdir[0] + "/teuthology.log"):
try:
if config["args"].get("log_file", False):
print(f"Log file path: {latest_run_subdir[0]}/teuthology.log")
return 0
with open(latest_run_subdir[0] + "/teuthology.log", 'r') as f:
ttypager(f.read())
return 0
except :
logger.error("Error while reading teuthology.log!")
return 1
else:
logger.error("teuthology.log file not found!")
return 1
# Multiple jobs present, then display the job ids
print("Jobs present in latest run:")
job_ids=[]
for job in latest_run_subdir:
job_ids.append(job.split('/')[-1])
print(f"Job id: {job.split('/')[-1]}")
job_id=input("Enter any of the above job id to get logs: ")
# check if the job id is valid
if job_id not in job_ids:
logger.error("Invalid job id!")
return 1
# check if the teuthology.log file exists in the job directory
if os.path.exists(latest_run +'/'+ job_id +'/teuthology.log') and os.path.isfile(latest_run +'/'+ job_id +'/teuthology.log'):
try:
if config["args"].get("log_file", False):
print(f"Log file path: {latest_run +'/'+ job_id +'/teuthology.log'}")
return 0
with open(latest_run +"/"+ job_id +"/teuthology.log", 'r') as f:
ttypager(f.read())
except :
logger.error("Error while reading teuthology.log!!")
return 1
else:
logger.error("teuthology.log file not found!")
return 1
return 0