From b85b617c82f35924483db96c2777c9f1f31aaea4 Mon Sep 17 00:00:00 2001
From: Jim Rigsbee <jrigsbee@redhat.com>
Date: Wed, 19 Feb 2020 20:41:25 +0100
Subject: [PATCH] Moved to Red Hat Training repo

---
 playbooks/install.yml      |   48 ++++++++++++
 playbooks/vars/RedHat.yml  |    3 
 requirements.txt           |    3 
 .gitignore                 |  105 ++++++++++++++++++++++++++
 test/test.yml              |   14 +++
 wsgi.py                    |    4 +
 playbooks/.gitignore       |    1 
 playbooks/inventory.ini    |    2 
 playbooks/requirements.yml |    8 ++
 server.py                  |   31 +++++++
 README.md                  |   11 ++
 11 files changed, 228 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..7bff731
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,105 @@
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+wheels/
+*.egg-info/
+.installed.cfg
+*.egg
+MANIFEST
+
+# PyInstaller
+#  Usually these files are written by a python script from a template
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*.cover
+.hypothesis/
+.pytest_cache/
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+local_settings.py
+db.sqlite3
+
+# Flask stuff:
+instance/
+.webassets-cache
+
+# Scrapy stuff:
+.scrapy
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+target/
+
+# Jupyter Notebook
+.ipynb_checkpoints
+
+# pyenv
+.python-version
+
+# celery beat schedule file
+celerybeat-schedule
+
+# SageMath parsed files
+*.sage.py
+
+# Environments
+.env
+.venv
+env/
+venv/
+ENV/
+env.bak/
+venv.bak/
+
+# Spyder project settings
+.spyderproject
+.spyproject
+
+# Rope project settings
+.ropeproject
+
+# mkdocs documentation
+/site
+
+# mypy
+.mypy_cache/
+
diff --git a/README.md b/README.md
index 0d8556b..262c1b7 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,9 @@
-# provisioner-api
-Status reporting API for provisioning classrooms
+# Sample API for report application status via JSON document 
+
+This API has two endpoints:
+
+* healthz
+  returns 200 and json string indicating health of API
+* status
+  returns 200 + json doc if it can find a given JSON document
+  returns 404 if it cannot find the document
diff --git a/playbooks/.gitignore b/playbooks/.gitignore
new file mode 100644
index 0000000..f9da32b
--- /dev/null
+++ b/playbooks/.gitignore
@@ -0,0 +1 @@
+roles
diff --git a/playbooks/install.yml b/playbooks/install.yml
new file mode 100644
index 0000000..8f18253
--- /dev/null
+++ b/playbooks/install.yml
@@ -0,0 +1,48 @@
+---
+- hosts: bastions
+  become: yes
+  tasks:
+    - name: Create application folder
+      file:
+        name: "/opt/status/src"
+        state: directory
+    - name: Copy application files
+      copy:
+        dest: "/opt/status/src/{{ item }}"
+        src: "../{{ item }}"
+        owner: "{{ ansible_user }}"
+        mode: 0644
+      with_items:
+        - requirements.txt
+        - server.py
+        - wsgi.py
+    - name: Create folder
+      file:
+        name: "{{item}}"
+        state: directory
+        owner: "{{ansible_user}}"
+        group: nginx
+      with_items:
+        - /opt/status/run
+        - /opt/status/log
+    - name: Fix selinux context for nginx access
+      shell: |
+        semanage fcontext -a -t httpd_sys_rw_content_t "{{item}}(/.*)?"
+        restorecon -R -v "{{item}}"
+      with_items:
+        - /opt/status/run
+        - /opt/status/log
+    - name: Install python virtualenv
+      pip:
+        name: virtualenv
+    - name: Install python application as a service
+      include_role:
+        name: Stouts.wsgi
+      vars:
+        python_enabled: false
+        wsgi_group: nginx
+        wsgi_applications:
+        - name: status
+          server: gunicorn
+          module: wsgi
+          pip_requirements: requirements.txt
diff --git a/playbooks/inventory.ini b/playbooks/inventory.ini
new file mode 100644
index 0000000..dba1a71
--- /dev/null
+++ b/playbooks/inventory.ini
@@ -0,0 +1,2 @@
+[bastions]
+bastion.33cc33cc.do417.dev.nextcle.com ansible_ssh_private_key_file=~/.ssh/glsdev ansible_user=ec2-user
diff --git a/playbooks/requirements.yml b/playbooks/requirements.yml
new file mode 100644
index 0000000..0137080
--- /dev/null
+++ b/playbooks/requirements.yml
@@ -0,0 +1,8 @@
+# This is the Ansible Galaxy requirements file to pull in the correct roles
+# to support the operation of CASL provisioning/runs.
+# and also support any role dependency while using the repository in Ansible Tower
+
+# From 'Stouts.wsgi'
+- src: https://github.com/Stouts/Stouts.wsgi
+  version: 2.1.4
+
diff --git a/playbooks/vars/RedHat.yml b/playbooks/vars/RedHat.yml
new file mode 100644
index 0000000..97f0fe4
--- /dev/null
+++ b/playbooks/vars/RedHat.yml
@@ -0,0 +1,3 @@
+---
+
+wsgi_service: systemd
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..d799682
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,3 @@
+Flask==1.0.2
+gunicorn==19.9.0
+requests==2.6.0
diff --git a/server.py b/server.py
new file mode 100644
index 0000000..caff277
--- /dev/null
+++ b/server.py
@@ -0,0 +1,31 @@
+#!/usr/bin/python
+from flask import Flask, request, jsonify, make_response
+import requests
+import os.path
+import json
+
+application = Flask(__name__)
+
+@application.route('/api/provision/v1/healthz')
+def healthz():
+    return make_response(jsonify({"health": "ok"}), 200)
+
+@application.route('/api/provision/v1/status')
+def status():
+
+    if os.path.exists('/tmp/provision_report.json'):
+        with open('/tmp/provision_report.json') as json_file:
+            data = json.load(json_file)
+            return make_response(jsonify(data), 200)
+
+    return make_response(jsonify({"status": "unknown"}), 404)
+
+@application.route('/api/provision/v1/report', methods=['POST'])
+def report():
+    with open('/tmp/provision_report.json', 'w') as json_file:
+        json.dump(request.json, json_file)
+
+    return make_response("", 200)
+
+if __name__ == '__main__':
+     application.run(host='127.0.0.1')
diff --git a/test/test.yml b/test/test.yml
new file mode 100644
index 0000000..30b9504
--- /dev/null
+++ b/test/test.yml
@@ -0,0 +1,14 @@
+---
+- hosts: localhost
+  connection: local
+  become: false
+  tasks:
+    - name: Send start status
+      uri:
+        url: http://bastion.33cc33cc.do417.dev.nextcle.com/api/provision/v1/report
+        method: POST
+        body_format: json
+        body:
+          status: "allocating infrastructure"
+        status_code:
+          - 200
diff --git a/wsgi.py b/wsgi.py
new file mode 100644
index 0000000..84aff31
--- /dev/null
+++ b/wsgi.py
@@ -0,0 +1,4 @@
+from server import application
+
+if __name__ == "__main__":
+    application.run()

--
Gitblit v1.9.3