Add command to deploy batabase
authorymh <ymh.work@gmail.com>
Fri, 14 Jun 2019 14:36:17 +0200
changeset 14 30f2ac9a7656
parent 13 abe4451542dd
child 15 1921d064475c
Add command to deploy batabase
deploy/README.md
deploy/deploy_db.sh
deploy/deploy_db.yml
deploy/host_vars/base.yml.tmpl
--- a/deploy/README.md	Wed Jun 12 17:57:01 2019 +0200
+++ b/deploy/README.md	Fri Jun 14 14:36:17 2019 +0200
@@ -13,4 +13,21 @@
 - `hosts/hosts.<profile>`
 - `group_vars/<profile>.yml`
 - `host_vars/<hostname>/base.yml` base `host_vars/base.yml.tmpl`
-`
\ No newline at end of file
+
+## Database deploy
+
+- `deploy_db.sh [CONFIG] [DB_FILE_PATH] [ORIGIN_URL]`
+
+With:
+- `CONFIG`: test or prod
+- `DB_FILE_PATH`: The database definition file (SQL)
+- `ORIGIN_URL`: The site base URL matching the database file
+
+This script call the `deploy_db.yml` ansible script and perform the following operations
+- copy the sql file to the server
+- apply the sql file with wp-cli `db` command
+- search and replace the origin url (wp-cli search-replace)
+- set `template_root` option (wp-cli option)
+- delete _site_transient_update_themes and _site_transient_theme_roots options (wp-cli option)
+- update admin password (wp-cli user update)
+- flush cache (wp-cli cache flush)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/deploy/deploy_db.sh	Fri Jun 14 14:36:17 2019 +0200
@@ -0,0 +1,38 @@
+#!/usr/bin/env bash
+
+SCRIPTPATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+SCRIPTNAME=`basename "$0"`
+
+usage() {
+  echo -n "${SCRIPTNAME} [CONFIG] [DB_FILE_PATH] [ORIGIN_URL]
+
+Deploy the database <DB_FILE_PATH> using ansible.
+Replacing the <ORIGIN_URL> by the destination url in the ansible config (rc_wp_home).
+"
+}
+
+if [[ "$#" -ne 3 ]]; then
+    usage
+    exit 1
+fi
+
+config=${1}
+db_file_path=${2}
+origin_url=${3}
+
+case $config in
+    test) configOK=true;;
+    prod) configOK=true;;
+    *)    configOK=false;;
+esac
+
+if [[ "$configOK" = false ]]; then
+    usage
+    exit 1
+fi
+
+pushd "$SCRIPTPATH"
+
+ANSIBLE_SSH_PIPELINING=1 ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook -v -i "./hosts/hosts.$config" -l "$config" ./deploy_db.yml --extra-vars "db_file_path='${db_file_path}' origin_url='${origin_url}'" --ask-vault-pass
+
+popd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/deploy/deploy_db.yml	Fri Jun 14 14:36:17 2019 +0200
@@ -0,0 +1,96 @@
+# vendor/bin/wp option delete _site_transient_update_themes
+# vendor/bin/wp option delete _site_transient_theme_roots
+
+- hosts: remote
+  vars:
+    src_dir: "{{playbook_dir}}/build/tmp/rc/src/"
+  tasks:
+
+    - name: create temporary file
+      tempfile:
+        state: file
+        suffix: sql
+      register: sql_tmp_path
+
+    - name: transfer sql file
+      copy:
+        src: "{{db_file_path}}"
+        dest: "{{sql_tmp_path.path}}"
+
+    - name: load database
+      command:
+        argv:
+          - "{{remote_path}}/vendor/bin/wp"
+          - db
+          - import
+          - "{{sql_tmp_path.path}}"
+      args:
+        chdir: "{{remote_path}}"
+
+    - name: delete temporary file
+      file:
+        path: "{{sql_tmp_path.path}}"
+        state: absent
+
+    - name: search and replace url
+      command:
+        argv:
+          - "{{remote_path}}/vendor/bin/wp"
+          - search-replace
+          - "{{origin_url}}"
+          - "{{rc_wp_home}}"
+      args:
+        chdir: "{{remote_path}}"
+
+    - name: set template_root
+      command:
+        argv:
+          - "{{remote_path}}/vendor/bin/wp"
+          - option
+          - set
+          - template_root
+          - "{{remote_path}}/web/wp/wp-content/themes"
+      args:
+        chdir: "{{remote_path}}"
+
+    - name: delete _site_transient_update_themes option
+      command:
+        argv:
+          - "{{remote_path}}/vendor/bin/wp"
+          - option
+          - delete
+          - _site_transient_update_themes
+      args:
+        chdir: "{{remote_path}}"
+
+    - name: delete _site_transient_theme_roots option
+      command:
+        argv:
+          - "{{remote_path}}/vendor/bin/wp"
+          - option
+          - delete
+          - _site_transient_theme_roots
+      args:
+        chdir: "{{remote_path}}"
+
+    - name: update admin password
+      command:
+        argv:
+          - "{{remote_path}}/vendor/bin/wp"
+          - user
+          - update
+          - "{{rc_admin_user_id}}"
+          - "--user_pass={{rc_admin_user_pswd}}"
+          - --skip-email
+      args:
+        chdir: "{{remote_path}}"
+
+    - name: flush cache
+      command:
+        argv:
+          - "{{remote_path}}/vendor/bin/wp"
+          - cache
+          - flush
+      args:
+        chdir: "{{remote_path}}"
+    
--- a/deploy/host_vars/base.yml.tmpl	Wed Jun 12 17:57:01 2019 +0200
+++ b/deploy/host_vars/base.yml.tmpl	Fri Jun 14 14:36:17 2019 +0200
@@ -7,6 +7,9 @@
 rc_wp_siteurl: ${WP_HOME}/wp
 rc_wp_env: development
 
+rc_admin_user_id: admin
+rc_admin_user_pswd: 
+
 rc_auth_key:
 rc_secure_auth_key:
 rc_logged_in_key: