Created
April 16, 2020 20:34
-
-
Save markito/580fed46190930420e3cfd740759be87 to your computer and use it in GitHub Desktop.
Ansible Runner as Knative Service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import flask | |
from flask import request, jsonify | |
import logging | |
import ansible_runner | |
import urllib | |
import requests | |
import json | |
import os | |
logger = logging.getLogger(__name__) | |
app = flask.Flask(__name__) | |
app.config["DEBUG"] = True | |
PLAYBOOK_PATH="/tmp/playbook.yaml" | |
BROKER_URL=os.environ["BROKER_URL"] | |
@app.route('/', methods=['GET']) | |
def run(): | |
query_parameters = request.args | |
playbook = query_parameters.get('playbook') | |
logger.debug(f"Reading... {playbook}") | |
save_from_url(playbook) | |
logger.debug("Saved...") | |
r = ansible_runner.run(playbook=PLAYBOOK_PATH, private_data_dir='/tmp', json_mode=True, event_handler=publishCloudEvent) | |
logger.error(r) | |
return f"Playbook status: {r.status}" | |
@app.route('/', methods=['POST']) | |
def post(): | |
print(request.data) | |
print(request.headers) | |
return request.args | |
def save_from_url(url): | |
try: | |
urllib.request.urlretrieve(url, PLAYBOOK_PATH) | |
except Exception as e: | |
logger.error(f"Could not access file or URL: {url} \n due to: {e}") | |
def publishCloudEvent(message, type="customer-type"): | |
data = json.dumps(message) | |
headers = { "Ce-Id": "ansible-runner", | |
"Ce-Specversion": "0.3", | |
"Ce-Type": f"{type}", | |
"Ce-Source": "Red Hat", | |
"Content-Type": "application/json"} | |
requests.post(BROKER_URL, data=data, headers=headers) | |
@app.errorhandler(404) | |
def page_not_found(e): | |
return "<h1>404</h1><p>The resource could not be found.</p>", 404 | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=8080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment