-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
202 lines (145 loc) · 6.05 KB
/
main.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
192
193
194
195
196
197
198
199
200
201
202
from flask import Flask, request, Response, jsonify, make_response, Blueprint
from yaml import load, dump
import json
from flask_cors import CORS
from flask_swagger_ui import get_swaggerui_blueprint
import os
app = Flask(__name__)
CORS(app)
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
REQUEST_API = Blueprint('api', __name__)
def get_blueprint():
"""Return the blueprint for the main app module"""
return REQUEST_API
### swagger specific ###
SWAGGER_URL = '/swagger'
API_URL = '/static/swagger.json'
SWAGGERUI_BLUEPRINT = get_swaggerui_blueprint(
SWAGGER_URL,
API_URL,
config={
'app_name': "ansible-playbook-json2yaml"
}
)
app.register_blueprint(SWAGGERUI_BLUEPRINT, url_prefix=SWAGGER_URL)
### end swagger specific ###
app.register_blueprint(get_blueprint())
@app.errorhandler(400)
def handle_400_error(_error):
"""Return a http 400 error to client"""
return make_response(jsonify({'error': 'Misunderstood'}), 400)
@app.errorhandler(401)
def handle_401_error(_error):
"""Return a http 401 error to client"""
return make_response(jsonify({'error': 'Unauthorised'}), 401)
@app.errorhandler(404)
def handle_404_error(_error):
"""Return a http 404 error to client"""
return make_response(jsonify({'error': 'Not found'}), 404)
@app.errorhandler(500)
def handle_500_error(_error):
"""Return a http 500 error to client"""
return make_response(jsonify({'error': 'Server error'}), 500)
@app.route('/generate-yaml', methods=['POST'])
def playbook_yaml_generator():
playbook = '---'
try:
plays = []
request_json = request.get_json()
print('Request received : \n'+str(request_json))
print('Total plays found : '+str(len(request_json)))
play_counter = 1
for a_play in request_json:
play_name = a_play['name']
print('Starting preparation of : Play_'+str(play_counter))
play_counter = play_counter + 1
tasks = []
task_counter = 1
for a_module in a_play['modules']:
print('Starting preparation of : Task_' + str(task_counter))
free_form_found = False
args = ''
for k, v in a_module['input_fields'].items():
if k == 'free_form':
free_form_found = True
if args == '' and v != '' and v is not None and v != 'null':
args = args + k + '="' + v + '"'
elif v != '' and v is not None and v != 'null':
args = args + ' ' + k + '="' + v + '"'
task = dict()
become_as = ''
become_method = 'su'
delegate_to = ''
no_log = False
register_out = ''
when_condition = ''
with_items = ''
try:
when_condition = a_module['when']
task['when'] = when_condition
except Exception as e:
print(str(e) + ' not found in task definition')
try:
with_items = a_module['with_items']
task['with_items'] = with_items
except Exception as e:
print(str(e) + ' not found in task definition')
try:
become_as = a_module['become']
task['become'] = become_as
try:
become_method = a_module['become_method']
task['become_method'] = become_method
except Exception as e:
print(str(e) + ' not found in task definition')
except Exception as e:
print(str(e) + ' not found in task definition')
try:
delegate_to = a_module['delegate_to']
task['delegate_to'] = delegate_to
except Exception as e:
print(str(e) + ' not found in task definition')
try:
no_log = a_module['no_log']
task['no_log'] = no_log
except Exception as e:
print(str(e) + ' not found in task definition')
try:
register_out = a_module['register']
task['register'] = register_out
except Exception as e:
print(str(e) + ' not found in task definition')
task_counter = task_counter + 1
if free_form_found:
module_name = a_module['module']
task[module_name] = a_module['input_fields']['free_form']
else:
task['action'] = dict(module=a_module['module'], args=args)
task['name'] = a_module['name']
tasks.append(task)
print('Constructing Play JSON')
play_json = dict()
for k, v in a_play.items():
if k != 'input_fields' and k != 'modules' and k != 'name' and v is not None and v != '' and v != {}:
play_json[k] = v
play_json['name']=play_name
play_json['tasks']=tasks
print('Intermediate Play JSON : '+str(play_json))
plays.append(play_json)
print('Constructing Consolidated Plays YAML from : ' + json.dumps(plays, indent=4))
data = load(json.dumps(plays, indent=4), Loader=Loader)
play_yaml = dump(data, Dumper=Dumper)
print('Constructing Play Book')
playbook = playbook + "\n" + play_yaml
except Exception as e:
print(str(e) + ' : Unexpected error occured.')
print('Response sent : \n'+str(playbook))
resp = Response(playbook)
resp.headers['Access-Control-Allow-Origin'] = '*'
resp.headers['content-type'] = "text/yaml"
return resp
if __name__ == '__main__':
app.run(host="0.0.0.0", port=int(os.environ.get('PORT', 5000)), debug=True)