This repository was archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathReqMgrWriter.py
More file actions
153 lines (131 loc) · 5.68 KB
/
ReqMgrWriter.py
File metadata and controls
153 lines (131 loc) · 5.68 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import os
from logging import Logger
from Utilities.WebTools import sendResponse
from Utilities.Logging import getLogger
from Utilities.ConfigurationHandler import ConfigurationHandler
from typing import Optional
import traceback
class ReqMgrWriter(object):
"""
_ReqMgrWriter_
General API for writing data in ReqMgr
"""
def __init__(self, logger: Optional[Logger] = None) -> None:
try:
super().__init__()
self.logger = logger or getLogger(self.__class__.__name__)
configurationHandler = ConfigurationHandler()
self.reqmgrUrl = os.getenv("REQMGR_URL", configurationHandler.get("reqmgr_url"))
self.reqmgrEndpoint = {"agentConfig": "/reqmgr2/data/wmagentconfig/", "request": "/reqmgr2/data/request/"}
except Exception as error:
raise Exception(f"Error initializing ReqMgrWriter\n{str(error)}")
def invalidateWorkflow(self, wf: str, currentStatus: str, cascade: bool = False) -> bool:
"""
The function to invalidate a workflow
:param wf: workflow name
:param currentStatus: current workflow status
:param cascade: if to cascade the info or not
:return: True if invalidation succeeded, False o/w
"""
try:
if currentStatus in ["aborted", "rejected", "aborted-completed", "aborted-archived", "rejected-archived"]:
self.logger.info("%s already %s, no action required", wf, currentStatus)
return True
param = {"RequestStatus": "aborted", "cascade": str(cascade)}
if currentStatus in ["assignment-approved", "new", "completed", "closed-out", "announced", "failed"]:
param = {"RequestStatus": "rejected", "cascade": str(cascade)}
elif currentStatus == "normal-archived":
param = {"RequestStatus": "rejected-archived"}
return self.setWorkflowParam(wf, param)
except Exception as error:
self.logger.error("Failed to invalidate %s", wf)
self.logger.error(str(error))
return False
def forceCompleteWorkflow(self, wf: str) -> bool:
"""
The function to force complete a workflow
:param wf: workflow name
:return: True if completion succeeded, False o/w
"""
return self.setWorkflowParam(wf, {"RequestStatus": "force-complete"})
def setWorkflowParam(self, wf: str, param: dict) -> bool:
"""
The function set some params to a given workflow
:param wf: workflow name
:param param: workflow param
:return: True if succeeded, False o/w
"""
try:
result = sendResponse(
method="PUT", url=self.reqmgrUrl, endpoint=self.reqmgrEndpoint["request"] + wf, param=param
)
return any(item.get(wf) == "OK" for item in result["result"])
except Exception as error:
self.logger.error("Failed to set %s for %s", param, wf)
self.logger.error(str(error))
self.logger.error(traceback.format_exc())
return False
def setAgentConfig(self, agent: str, config: dict) -> bool:
"""
The function to set the configuration for a given agent
:param agent: agent name
:param config: agent configuration params
:return: True if succeeded, False o/w
"""
try:
result = sendResponse(
method="PUT", url=self.reqmgrUrl, endpoint=self.reqmgrEndpoint["agentConfig"] + agent, param=config
)
return result["result"][0]["ok"]
except Exception as error:
self.logger.error("Failed to set configuration in reqmgr for agent %s", agent)
self.logger.error(str(error))
def submitWorkflow(self, wfSchema: dict) -> bool:
"""
The function to submit a workflow (for cloning or resubmition)
:param wfSchema: workflow schema
:return: True if succeeded, False o/w
"""
try:
result = sendResponse(
method="POST", url=self.reqmgrUrl, endpoint=self.reqmgrEndpoint["request"], param=wfSchema
)
return result["result"][0]["request"]
except Exception as error:
self.logger.error("Failed to submit workflow in reqmgr")
self.logger.error(str(error))
self.logger.error(traceback.format_exc())
def approveWorkflow(self, wf: str) -> bool:
"""
The function to approve a workflow
:param wf: workflow name
:return: True if succeeded, False o/w
"""
try:
result = sendResponse(
method="PUT",
url=self.reqmgrUrl,
endpoint=f"{self.reqmgrEndpoint['request']}/{wf}",
param={"RequestStatus": "assignment-approved"},
)
return result
except Exception as error:
self.logger.error("Failed to approve workflow in reqmgr")
self.logger.error(str(error))
def closeoutWorkflow(self, wf: str, cascade: bool = False) -> bool:
"""
The function to close out a given workflow
:param wf: workflow name
:param cascade: if cascade or not
:return: True if succeeded, False o/w
"""
try:
result = sendResponse(
url=self.reqmgrUrl,
endpoint=f"{self.reqmgrEndpoint['request']}/{wf}",
param={"RequestStatus": "closed-out", "cascade": cascade},
)
return result["result"][0][wf] == "OK"
except Exception as error:
self.logger.error("Failed to close out workflow in reqmgr")
self.logger.error(str(error))