From 11dfb6b5d746634f31fb561a7402765f9c5c2fdc Mon Sep 17 00:00:00 2001
From: Chris Croome <chris@webarchitects.co.uk>
Date: Tue, 23 May 2017 19:03:04 +0100
Subject: [PATCH] Playbook for updating the dev with the live data

---
 README.md                             |  9 ++++++
 live2dev.yml                          | 13 ++++++++
 roles/live2dev/files/htaccess         | 35 +++++++++++++++++++++
 roles/live2dev/tasks/main.yml         | 44 +++++++++++++++++++++++++++
 roles/live2dev/templates/gitconfig.j2 |  3 ++
 roles/live2dev/templates/update.j2    | 20 ++++++++++++
 6 files changed, 124 insertions(+)
 create mode 100644 live2dev.yml
 create mode 100644 roles/live2dev/files/htaccess
 create mode 100644 roles/live2dev/tasks/main.yml
 create mode 100644 roles/live2dev/templates/gitconfig.j2
 create mode 100644 roles/live2dev/templates/update.j2

diff --git a/README.md b/README.md
index 924ab0c..8307823 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,15 @@
 
 These Playbooks are designed to be used on Debian Stretch virtual servers.
 
+## live2dev
+
+Ansible playbook to update the [dev site](https://dev.coops.tech/) from the [live site](https://www.coops.tech/).
+
+```bash
+export SERVERNAME="webarch1.co.uk"
+ansible-playbook live2dev.yml -i "${SERVERNAME}," -e "hostname=${SERVERNAME}"
+``` 
+
 ## Discourse
 
 Ansible Playbooks to install
diff --git a/live2dev.yml b/live2dev.yml
new file mode 100644
index 0000000..93cd45c
--- /dev/null
+++ b/live2dev.yml
@@ -0,0 +1,13 @@
+---
+- name: Populate the dev site with the live data
+  hosts: "{{ hostname }}"
+  become: yes
+
+  roles:
+    - live2dev
+
+  vars:
+    distro: stretch
+    users:
+      - cotech
+      - cotechdev
diff --git a/roles/live2dev/files/htaccess b/roles/live2dev/files/htaccess
new file mode 100644
index 0000000..02e6846
--- /dev/null
+++ b/roles/live2dev/files/htaccess
@@ -0,0 +1,35 @@
+# https://stackoverflow.com/a/10034368
+<FilesMatch "\.(ico|gif|jpg|png)$">
+          ExpiresActive On
+  ExpiresDefault "access plus 11 month"
+  Header append Cache-Control "public"
+</FilesMatch>
+<FilesMatch "\.(css|js)$">
+  ExpiresActive On
+  ExpiresDefault "access plus 1 week"
+  Header append Cache-Control "public"
+</FilesMatch>
+
+# Redirect HTTP to HTTPS
+# https://wiki.apache.org/httpd/RewriteHTTPToHTTPS
+<IfModule mod_rewrite.c>
+  RewriteEngine on
+  RewriteCond %{HTTPS} !=on
+  RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
+</IfModule>
+
+# STS Header
+# https://stackoverflow.com/questions/24144552/how-to-set-hsts-header-from-htaccess-only-on-https
+Header set Strict-Transport-Security "max-age=31536000" env=HTTPS
+
+# BEGIN WordPress
+<IfModule mod_rewrite.c>
+RewriteEngine On
+RewriteBase /
+RewriteRule ^index\.php$ - [L]
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteCond %{REQUEST_FILENAME} !-d
+RewriteRule . /index.php [L]
+</IfModule>
+
+# END WordPress
diff --git a/roles/live2dev/tasks/main.yml b/roles/live2dev/tasks/main.yml
new file mode 100644
index 0000000..f3354b4
--- /dev/null
+++ b/roles/live2dev/tasks/main.yml
@@ -0,0 +1,44 @@
+---
+- name: Update script in place
+  template:
+    src: templates/update.j2
+    dest: "/home/{{ item }}/bin/update"
+    mode: 0755
+    owner: "{{ item }}" 
+    group: "{{ item }}" 
+  with_items: "{{ users }}"
+
+- name: Git config in place
+  template:
+    src: templates/gitconfig.j2
+    dest: "/home/{{ item }}/.gitconfig"
+    mode: 0644
+    owner: "{{ item }}" 
+    group: "{{ item }}"
+  with_items: "{{ users }}"
+
+- name: .htaccess file in place
+  copy:
+    src: files/htaccess
+    dest: "/home/{{ item }}/sites/web/.htaccess"
+    mode: 0644
+    owner: "{{ item }}" 
+    group: "{{ item }}"
+  with_items: "{{ users }}"
+
+- name: ~/bin in PATH
+  lineinfile:
+    regexp: '^export\ PATH='
+    line: 'export PATH="$PATH:$HOME/bin"'
+    dest: "/home/{{ item }}/.bashrc"
+    state: present
+  with_items: "{{ users }}"
+
+- name: Live database dumped
+  shell: "mysqldump cotech > /home/cotech/private/cotech.sql"
+
+- name: Dev database overwritten with live data
+  shell: "cat /home/cotech/private/cotech.sql | mysql cotechdev"
+
+- name: Live files rsynced to dev
+  command: "rsync -aq --chown=cotechdev:cotechdev --chmod=D2775,F0664 /home/cotech/sites/web/app/uploads/ /home/cotechdev/sites/web/app/uploads/"
diff --git a/roles/live2dev/templates/gitconfig.j2 b/roles/live2dev/templates/gitconfig.j2
new file mode 100644
index 0000000..6a6c8ea
--- /dev/null
+++ b/roles/live2dev/templates/gitconfig.j2
@@ -0,0 +1,3 @@
+[user]
+        name = CoTech 
+        email = {{ item }}@{{ hostname }}
diff --git a/roles/live2dev/templates/update.j2 b/roles/live2dev/templates/update.j2
new file mode 100644
index 0000000..491ab03
--- /dev/null
+++ b/roles/live2dev/templates/update.j2
@@ -0,0 +1,20 @@
+#!/bin/bash
+
+if [[ "${USER}" != "{{ item }}" ]]; then
+  echo "This script should be run as {{ item }}, please sudo first"
+  echo "sudo -u {{ item }} -s /bin/bash"
+  echo "${0}"
+  exit
+fi
+
+cd /home/{{ item }}/sites
+git stash
+git pull
+rm -rf web/wp
+composer update
+cd web/app/themes/coop-tech-oowp-theme
+composer update
+npm install
+nodejs ./node_modules/gulp/bin/gulp.js
+cd ../../..
+wp core update-db
-- 
GitLab