This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathflowinvoke.py
192 lines (142 loc) · 5.64 KB
/
flowinvoke.py
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/python
import json
import logging
import sys
import argparse
import requests
import requests.packages.urllib3
import time
from base64 import encodestring, b64encode
def main () :
levels = {
'debug': logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
'critical': logging.CRITICAL
}
inputs = {}
parser = argparse.ArgumentParser(description = 'Run HP OO 10 flow from the command line')
parser.add_argument('--user', default = 'admin', help='username (default: admin)')
parser.add_argument('--password', default = 'admin', help='password for the user (default: admin)')
parser.add_argument('--host', default = '16.57.70.238:8443', help='The hostname of OO server. Should include port also')
parser.add_argument('--uuid', help='The UUID of the flow you want to run')
parser.add_argument('--encode', help='Encodes username and password for use with OO api. Should be in form of username:password string.')
parser.add_argument('--loglevel', default = 'INFO', help='FATAL, ERROR, WARNING, INFO, DEBUG')
parser.add_argument('--logfile', default = 'flowinvoke.log', help='Logfile to store messages (Default: flowinvoke.log)')
parser.add_argument('--input', action='append', help='''Key=value pair of inputs for the flow
(repeat for more inputs e.g. --input key1=value1 --input key2=value2)''')
parser.add_argument('--timeout', default = 3600, type = int, help='The time to wait for flow completion in seconds (Default: 3600 - 1hour)')
parser.add_argument('--heartbeat', default = 120, type = int, help='Operation Orchestration polling interval (Default: 120 secs)')
parser.add_argument('--async', action = 'store_true', help='''Run the flow in asynchronous mode (don't wait for the end result Default: synchronous)''')
parser.add_argument('--verbose', action='store_true', help='''By default only the flow Result is printed. Verbose will print json object that contains
also the flow execution summary and all bound inputs''')
parser.add_argument('--credentials', help='Use the encoded output of --encode to connect to OO instead of using the --user and --password option.')
args = parser.parse_args()
loglevel = levels.get(args.loglevel, logging.NOTSET)
logging.basicConfig(
level= args.loglevel,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filename= args.logfile,
filemode='a')
root = logging.getLogger()
if (args.encode is not None) :
print b64encode(args.encode)
if (args.credentials is not None) :
authorization = 'Basic '+ args.credentials
else :
authorization = "Basic "+ b64encode(args.user+ ":" +args.password)
if (args.uuid is None) :
parser.print_help()
return
requests.packages.urllib3.disable_warnings()
s = requests.Session()
s.headers.update({'Authorization': authorization})
if args.input is not None :
for i in args.input :
a,b = i.split('=')
inputs[a] = b
run_id = run_flow(s, args.host, args.uuid, inputs)
if (args.async is True) :
return
status = track_flow( s, args.host, run_id, args.timeout, args.heartbeat)
flow_result = collect_result( s, args.host, run_id)
if (args.verbose is True) :
print json.dumps(flow_result, indent=4)
else :
if (flow_result['flowOutput']) :
for i in flow_result['flowOutput']:
print i + '=' + flow_result['flowOutput'][i]
if status :
print 'Status='+status
if status == "RESOLVED" :
sys.exit(0)
raise Exception("Something went wrong\n"+json.dumps(flow_result['executionSummary'], indent=4))
def run_flow (s, host, uuid, input) :
log = logging.getLogger()
log.info('Entering run_flow')
#collect flow information
url = 'https://'+ host +'/oo/rest/v1/flows/'+ uuid
r = s.get(url,verify=False)
if (r.reason != 'OK') :
raise Exception(r.reason)
log.debug(r.text)
flow_info = json.loads(r.text)
#check mandatory flow inputs
url = 'https://' + host + '/oo/rest/v1/flows/' + uuid + '/inputs'
r = s.get(url,verify=False)
if (r.reason != 'OK') :
raise Exception (r.reason)
log.debug(r.text)
if (r.text) :
flow_input = json.loads(r.text)
for i in flow_input:
if (i['mandatory'] is True and i['name'] not in input) :
raise Exception('Missing required flow input: ' + i['name'])
#construct json post object
post_data = {}
post_data['uuid'] = uuid
post_data['runName'] = flow_info['name']
post_data['logLevel'] = 'DEBUG'
if (input is not None) :
post_data['inputs'] = input
json_post = json.dumps(post_data)
#run the flow
url = 'https://' + host + '/oo/rest/v1/executions'
s.headers = {'Content-type':'application/json'}
r = s.post(url, data=json_post, verify=False)
if (r.reason != 'Created') :
log.debug(r.reason)
raise Exception (r.reason)
else :
response = json.loads(r.text)
log.debug(response)
return response['executionId']
def track_flow (s, host , run_id, timeout, heartbeat) :
log = logging.getLogger()
log.info('Entering track_flow')
poll_iterations = round(timeout/heartbeat)
url = 'https://' + host + '/oo/rest/v1/executions/' + run_id + '/summary'
while(poll_iterations) :
r = s.get(url, verify=False)
if (r.reason != 'OK'):
raise Exception(r.reason)
log.debug(r.text)
response = json.loads(r.text)
if (response[0]['status'] == "RUNNING"):
time.sleep(heartbeat)
poll_iterations = poll_iterations - 1
else :
return response[0]['resultStatusType']
def collect_result (s, host, run_id) :
log = logging.getLogger()
log.info('Entering collect_result')
url = 'https://'+ host + '/oo/rest/v1/executions/' + run_id + '/execution-log'
r = s.get(url, verify=False)
if (r.reason != 'OK') :
raise Exception(r.reason)
log.debug(r.text)
return json.loads(r.text)
if __name__ == "__main__":
main()