Skip to content

Commit

Permalink
Merge pull request #45 from xsnippet/deployment
Browse files Browse the repository at this point in the history
Add a basic ansible playbook for deployment
  • Loading branch information
malor committed Oct 23, 2017
2 parents 2baa46e + d39a34a commit 471839d
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
68 changes: 68 additions & 0 deletions contrib/ansible/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
- hosts: xsnippet
tasks:
- name: ensure Python 2 is installed
raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
gather_facts: false

- hosts: xsnippet
vars:
xsnippet_image: xsnippet/xsnippet-api:latest
tasks:
- name: update apt cache (if needed)
apt: update_cache=yes cache_valid_time=3600

- name: install essential packages
apt: "name={{ item }} state=latest"
with_items:
- apt-transport-https
- ca-certificates

- name: add Docker gpg key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present

- name: add Docker packages repo
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
state: present
register: newrepo

- name: update apt cache (if needed)
apt: update_cache=yes
when: newrepo.changed

- name: install the latest Docker
apt: name=docker-ce state=latest

- name: initialize Docker Swarm
shell: "docker swarm init --advertise-addr 127.0.0.1 || docker stack ls"
register: init_result
changed_when: "'This node is already part of a swarm' not in init_result.stderr"

- name: create xsnippet user
user:
name: xsnippet
createhome: yes

- name: prepare the config file
template:
src: xsnippet-api.conf.j2
dest: "/home/xsnippet/xsnippet-api.conf"
mode: 0600
owner: xsnippet
group: xsnippet

- name: prepare the Docker stack config
template:
src: docker-compose.yml.j2
dest: "/home/xsnippet/docker-compose.yml"
mode: 0600
owner: xsnippet
group: xsnippet

- name: create or update a Docker Swarm stack
command: "docker stack deploy -c docker-compose.yml xsnippet"
args:
chdir: "/home/xsnippet"
38 changes: 38 additions & 0 deletions contrib/ansible/docker-compose.yml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
version: "3.3"
services:
db:
image: mongo:3
deploy:
replicas: 1
restart_policy:
condition: on-failure
volumes:
- db_state:/data/db
networks:
- default

api:
image: {{ xsnippet_image }}
configs:
- source: api_config
target: /etc/xsnippet-api.conf
mode: 0600
deploy:
replicas: 2
restart_policy:
condition: on-failure
depends_on:
- db
ports:
- "80:8000"
networks:
- default
networks:
default:

volumes:
db_state:

configs:
api_config:
file: ./xsnippet-api.conf
47 changes: 47 additions & 0 deletions contrib/ansible/xsnippet-api.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[server]

; IP ADDRESS TO LISTEN ON
;
; By default, only localhost interface is used for listening. That's
; why no requests from outer world will be handled. If you want to
; accept any incoming request on any interface, please change that
; value to '0.0.0.0'.
host = 0.0.0.0

; PORT TO LISTEN ON
;
; In production you probably will choose a default HTTP port - '80'.
; If you want to pick any random free port, just pass '0'.
port = 8000


[database]

; DATABASE CONNECTION URI
;
; Supported backends: MongoDB only
connection = mongodb://db:27017/xsnippet


[snippet]

; LIST OF ALLOWED SYNTAXES
;
; Set constraint on 'snippet.syntax' property. Each syntax should come
; on separate line. E.g:
;
; syntaxes =
; cpp
; python
; javascript
;
; By default, constraint is not defined so any syntax is accepted.


[auth]

; SECRET KEY TO BE USED FOR VALIDATION OF SIGNED TOKENS
;
; If not set, it will be generated automatically for this run of application.
; Set the value explicitly, so that it's preserved between runs.
secret =

0 comments on commit 471839d

Please sign in to comment.