From d0a73a3db1754e7ac8eaebd3872c011fdb58f15b Mon Sep 17 00:00:00 2001
From: Bombar Maxime <bombar@crans.org>
Date: Sun, 3 May 2020 05:15:16 +0200
Subject: [PATCH] Add role to manage thunes.

---
 host_vars/thunes-dev.yml                 |  5 ++
 host_vars/thunes.yml                     |  5 ++
 roles/thunes/handlers/main.yml           | 10 +++
 roles/thunes/tasks/main.yml              | 77 ++++++++++++++++++++++++
 roles/thunes/templates/settings_local.j2 | 25 ++++++++
 thunes.yml                               |  5 ++
 6 files changed, 127 insertions(+)
 create mode 100644 host_vars/thunes-dev.yml
 create mode 100644 host_vars/thunes.yml
 create mode 100644 roles/thunes/handlers/main.yml
 create mode 100644 roles/thunes/tasks/main.yml
 create mode 100644 roles/thunes/templates/settings_local.j2
 create mode 100755 thunes.yml

diff --git a/host_vars/thunes-dev.yml b/host_vars/thunes-dev.yml
new file mode 100644
index 0000000..b5b610a
--- /dev/null
+++ b/host_vars/thunes-dev.yml
@@ -0,0 +1,5 @@
+thunes_debug: True
+thunes_db_name: "thunes_dev"
+thunes_db_user: "thunes_dev"
+thunes_db_ip: "172.16.0.2"
+thunes_db_password: "l0gHdg8AhZOtg"
diff --git a/host_vars/thunes.yml b/host_vars/thunes.yml
new file mode 100644
index 0000000..73bf98b
--- /dev/null
+++ b/host_vars/thunes.yml
@@ -0,0 +1,5 @@
+thunes_debug: False
+thunes_db_name: "thunes"
+thunes_db_user: "thunes"
+thunes_db_ip: "172.16.0.2"
+thunes_password_db: "TFacRAvx3FHcM"
diff --git a/roles/thunes/handlers/main.yml b/roles/thunes/handlers/main.yml
new file mode 100644
index 0000000..296cac6
--- /dev/null
+++ b/roles/thunes/handlers/main.yml
@@ -0,0 +1,10 @@
+---
+- name: Reload nginx
+  systemd:
+    name: nginx
+    state: reloaded
+
+- name: Reload uwsgi
+  systemd:
+    name: uwsgi
+    state: reloaded
diff --git a/roles/thunes/tasks/main.yml b/roles/thunes/tasks/main.yml
new file mode 100644
index 0000000..0a2978a
--- /dev/null
+++ b/roles/thunes/tasks/main.yml
@@ -0,0 +1,77 @@
+---
+- name: Install dependencies
+  apt:
+    update_cache: true
+    install_recommends: false
+    name:
+      - python3-venv
+      - nginx
+      - uwsgi
+      - uwsgi-plugin-python3
+  register: apt_result
+  retries: 3
+  until: apt_result is succeeded
+
+- name: Create thunes directory
+  file:
+    path: /var/www/MathoosHouse
+    state: directory
+    mode: "2775"
+    owner: www-data
+
+- name: Clone Thunes repository
+  git:
+    repo: https://gitea.servens.org/bombar/MathoosHouse.git
+    dest: /var/www/MathoosHouse
+    track_submodules: yes
+    version: master
+    umask: "002"
+  when: not ansible_check_mode
+
+- name: Generate django SECRET_KEY
+  shell: python3 -c 'import random; print("".join([random.choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$%&*(-_=+)") for i in range(50)]))'
+  register: django_secret_key
+  check_mode: false
+  changed_when: true
+
+- name: Deploy settings_local
+  template:
+    src: settings_local.j2
+    dest: /var/www/MathoosHouse/MathoosHouse/settings_local.py
+  notify: Reload uwsgi
+
+- name: Install pip requirements
+  pip:
+    requirements: /var/www/MathoosHouse/requirements.txt
+    virtualenv: /var/www/MathoosHouse/.env
+    virtualenv_command: pyvenv
+    umask: "0002"
+
+- name: Install migrations
+  django_manage:
+    app_path: /var/www/MathoosHouse
+    command: migrate
+    virtualenv: /var/www/MathoosHouse/.env
+
+- name: Configure nginx
+  file:
+    src: /var/www/MathoosHouse/WebUtils/MathoosHouse.nginx
+    dest: /etc/nginx/sites-enabled/MathoosHouse.nginx
+    state: link
+  when: not ansible_check_mode
+  notify: Reload nginx
+
+- name: Remove default nginx config
+  file:
+    path: /etc/nginx/sites-enabled/default
+    state: absent
+  when: not ansible_check_mode
+  notify: Reload nginx
+
+- name: Configure uwsgi
+  file:
+    src: /var/www/MathoosHouse/WebUtils/MathoosHouse.uwsgi
+    dest: /etc/uwsgi/apps-enabled/MathoosHouse.ini
+    state: link
+  when: not ansible_check_mode
+  notify: Reload uwsgi
diff --git a/roles/thunes/templates/settings_local.j2 b/roles/thunes/templates/settings_local.j2
new file mode 100644
index 0000000..050ff3f
--- /dev/null
+++ b/roles/thunes/templates/settings_local.j2
@@ -0,0 +1,25 @@
+{{ ansible_managed | comment }}
+
+SECRET_KEY = "{{ django_secret_key.stdout_lines | first }}"
+
+DEBUG = {{ thunes_debug }}
+
+INTERNAL_IPS = []
+
+EMAIL_HOST = ''
+SERVER_EMAIL = ''
+DEFAULT_FROM_EMAIL = ''
+EMAIL_PORT = 25
+
+OPTIONAL_APPS = []
+
+DATABASES = {
+    'default': {
+        'ENGINE': 'django.db.backends.postgresql',
+        'NAME': '{{ thunes_db_name }}',
+        'USER': '{{ thunes_db_user }}',
+	'PASSWORD': '{{ thunes_db_password }}',
+        'HOST': '{{ thunes_db_ip }}',
+        'PORT': '5432',
+    }
+}
diff --git a/thunes.yml b/thunes.yml
new file mode 100755
index 0000000..feb48f6
--- /dev/null
+++ b/thunes.yml
@@ -0,0 +1,5 @@
+#!/usr/bin/env ansible-playbook
+---
+- hosts: thunes thunes-dev
+  roles:
+    - thunes
-- 
GitLab