forked from OCP-on-NERC/python-batchtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbp.py
More file actions
73 lines (57 loc) · 2.09 KB
/
bp.py
File metadata and controls
73 lines (57 loc) · 2.09 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
from typing import cast
from typing_extensions import override
import argparse
import sys
import openshift_client as oc
from .basecommand import Command
from .basecommand import SubParserFactory
class PrintJobsCommandArgs(argparse.Namespace):
job_names: list[str] | None = None
class PrintJobsCommand(Command):
"""
Display the pod names of the specified batch jobs. If no jobs are
specified then the pods of all current batch jobs will
be displayed.
See also:
See repository README.md for more documentation and examples.
"""
name: str = "bp"
help: str = "Display the pod names of the specified batch jobs"
@classmethod
@override
def build_parser(cls, subparsers: SubParserFactory):
p = super().build_parser(subparsers)
p.add_argument(
"job_names", nargs="*", help="Optional list of job names to display"
)
return p
@staticmethod
@override
def run(args: argparse.Namespace):
args = cast(PrintJobsCommandArgs, args)
try:
jobs = oc.selector("jobs").objects()
if not jobs:
print("No jobs found.")
return
job_dict = {job.model.metadata.name: job for job in jobs}
if args.job_names:
for name in args.job_names:
if name not in job_dict:
print(f"{name} does not exist; cannot fetch pod name.")
continue
print_pods_for(name)
else:
print("Displaying pods for all current batch jobs:\n")
for name in job_dict.keys():
print_pods_for(name)
except oc.OpenShiftPythonException as e:
sys.exit(f"Error occurred while retrieving pods: {e}")
def print_pods_for(job_name: str):
pods = oc.selector("pods", labels={"job-name": job_name}).objects()
if not pods:
print(f"No pods found for job {job_name}.")
return
print(f"\nPods for {job_name}:\n{'-' * 40}")
for pod in pods:
print(f"- {pod.model.metadata.name}")