forked from OCP-on-NERC/python-batchtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
48 lines (42 loc) · 1.45 KB
/
helpers.py
File metadata and controls
48 lines (42 loc) · 1.45 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
from imports import *
def is_logged_in() -> bool:
try:
oc.invoke("whoami")
return True
except oc.OpenShiftPythonException:
print("You are not logged in to the oc cli. Retrieve the token using 'oc login --web' or retrieving the login token from the openshift UI.")
return False
def get_cmd(command:str) -> str:
"""
Helper function to print the hostname using subprocess.
"""
try:
result = subprocess.run(
[command],
capture_output=True,
text=True,
check=True
)
output = result.stdout.strip()
return output
except subprocess.CalledProcessError as e:
print(f"Error: command failed with exit code {e.returncode}")
if e.stderr:
print(f"stderr: {e.stderr.strip()}")
sys.exit(-1)
def pretty_print(pod_name:str) -> Optional[str]:
try:
logs = oc.selector(f"pod/{pod_name}").logs()
# ⋆ ˚。⋆୨୧˚ stringify and pretty print for readibility ⋆ ˚。⋆୨୧˚
logs = str(logs).replace("\\n", "\n")
return logs
except OpenShiftPythonException as e:
print("Error occurred while retrieving logs:")
print(e)
def oc_delete(job_name:str) -> None:
try:
print(f"Deleting {job_name}")
oc.invoke("delete", ["job", job_name])
except OpenShiftPythonException as e:
print("Error occurred while retrieving logs:")
print(e)