Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
davidharrigan committed Jul 5, 2019
1 parent cd08551 commit d6e656c
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 0 deletions.
143 changes: 143 additions & 0 deletions .gitignore
@@ -0,0 +1,143 @@
# Created by https://www.gitignore.io/api/python,ansible,vagrant
# Edit at https://www.gitignore.io/?templates=python,ansible,vagrant

### Ansible ###
*.retry

### Python ###
# 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/
pip-wheel-metadata/
share/python-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/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# 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/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

### Vagrant ###
# General
.vagrant/

# Log files (if you are creating logs in debug mode, uncomment this)
# *.log

### Vagrant Patch ###
*.box

# End of https://www.gitignore.io/api/python,ansible,vagrant
21 changes: 21 additions & 0 deletions README.md
@@ -0,0 +1,21 @@
# Manifold Ansible Demo

This repo houses code used in the [Manifold Ansible Integration blog
post](link-tbd). It’s set up to provision a simple Python application which
pushes logs to LogDNA. Credentials necessary to authenticate with LogDNA are
fetched from Manifold using the [Ansible Manifold Lookup
plugin](https://docs.ansible.com/ansible/latest/plugins/lookup/manifold.html).
Running this demo on your machine requires: a Manifold.co account

Running this demo on your machine requires:
- a [Manifold.co](https://manifold.co) account
- [Ansible](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html) 2.8 or higher
- [Vagrant](https://www.vagrantup.com/downloads.html)
- [VirtualBox](https://www.virtualbox.org/wiki/Downloads)

To run the demo:
1. Edit the resource name and project name in `provision/roles/app/tasks/main.yaml` to match an existing project and resource in your Manifold account
1. Export `MANIFOLD_API_TOKEN=<your-api-token>`
1. From `./provision` run `vagrant up`

See also the [Manifold Ansible integration documentation](tbd).
55 changes: 55 additions & 0 deletions app/main.py
@@ -0,0 +1,55 @@
import logging
from logging.config import dictConfig
import os

import bottle
from bottle import request, response
from bottle import get
from logdna import LogDNAHandler

app = application = bottle.default_app()

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '[%(levelname)s] <%(asctime)s> %(pathname)s %(module)s.%(funcName)s[%(lineno)d] :: %(message)s'
}
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
'logdna': {
'level': 'DEBUG',
'class': 'logging.handlers.LogDNAHandler',
'key': os.environ.get('KEY'),
'options': {
'app': 'ansible-manifold-demo',
},
},
},
'loggers': {
'': {
'handlers': ['console', 'logdna'],
'level': 'DEBUG'
}
}
}

dictConfig(LOGGING)

logger = logging.getLogger()


@get('/health')
def health_handler():
response.status = 204
logger.info("health check")
return

if __name__ == '__main__':
bottle.run(host='0.0.0.0', port = 8000)
2 changes: 2 additions & 0 deletions app/requirements.txt
@@ -0,0 +1,2 @@
bottle==0.12.16
logdna==1.3.0
29 changes: 29 additions & 0 deletions provision/Vagrantfile
@@ -0,0 +1,29 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :
#
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "ubuntu/disco64"
config.ssh.insert_key = false

config.vm.provider :virtualbox do |v|
v.name = "vagrant_host"
v.memory = 512
v.cpus = 1
end

config.vm.hostname = "local"
config.vm.network :private_network, ip: "192.168.33.27"
config.vm.synced_folder "../app", "/usr/src/app"

config.vm.define :ansible_manifold do |vagrant|
end

# Ansible provisioner.
config.vm.provision "ansible" do |ansible|
ansible.playbook = "main.yaml"
ansible.become = true
end

end
3 changes: 3 additions & 0 deletions provision/main.yaml
@@ -0,0 +1,3 @@
- hosts: all
roles:
- app
31 changes: 31 additions & 0 deletions provision/roles/app/tasks/main.yaml
@@ -0,0 +1,31 @@
---
- name: update apt cache if needed
apt: update_cache=yes cache_valid_time=3600

- name: install pip
apt:
name: "python3-pip"
state: latest

- name: install pip deps
pip:
requirements: /usr/src/app/requirements.txt

- name: install app systemd
template:
src: app.service.j2
dest: /etc/systemd/system/app.service

- name: fetch credentials
set_fact:
manifold_secrets: "{{ lookup('manifold', 'ansible-demo-logging', project='ansible-demo') }}"

- name: configure systemd env
template:
src: app.env.j2
dest: /etc/systemd/system/app.env
with_dict:
- "{{ manifold_secrets }}"

- name: start app
systemd: state=restarted name=app daemon_reload=yes
3 changes: 3 additions & 0 deletions provision/roles/app/templates/app.env.j2
@@ -0,0 +1,3 @@
{% for key, value in manifold_secrets.items() %}
{{ key }}={{ value }}
{% endfor %}
11 changes: 11 additions & 0 deletions provision/roles/app/templates/app.service.j2
@@ -0,0 +1,11 @@
[Unit]
Description=Manifold Ansible Demo App
After=multi-user.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 /usr/src/app/main.py
EnvironmentFile=/etc/systemd/system/app.env

[Install]
WantedBy=multi-user.target

0 comments on commit d6e656c

Please sign in to comment.