Skip to content

Commit

Permalink
Merge pull request hashbangcode#292 from wizonesolutions/feature-pant…
Browse files Browse the repository at this point in the history
…heon-import

Add support for importing Pantheon sites.
  • Loading branch information
philipnorton42 committed Oct 31, 2015
2 parents 571409b + c114d87 commit 43998c7
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 4 deletions.
13 changes: 13 additions & 0 deletions vlad_guts/playbooks/roles/pantheon_import_site/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
# Pantheon Import settings

vlad_pantheon_import_site: false
vlad_pantheon_import_site_email: ''
vlad_pantheon_import_site_password: ''
vlad_pantheon_import_site_site: ''
vlad_pantheon_import_site_env: ''
vlad_pantheon_import_site_include_code: false
vlad_pantheon_import_site_include_db: true
vlad_pantheon_import_site_include_files: true

# @todo: Figure out an easy way, or document an Ansible command — possibly with tags — to update the imported Pantheon site without having to rebuild the entire VM.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- name: pantheon_import_site | code | Look up Git URL
command: /usr/local/bin/terminus site connection-info --json --site='{{ vlad_pantheon_import_site_site }}' --env='{{ vlad_pantheon_import_site_env }}'
register: vlad_pantheon_import_site_connection_info_output
- set_fact:
vlad_pantheon_import_site_site_info: "{{ vlad_pantheon_import_site_connection_info_output.stdout|from_json }}"
32 changes: 32 additions & 0 deletions vlad_guts/playbooks/roles/pantheon_import_site/tasks/import_db.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
# If they halted or restarted the VM before running provisioning, Vlad itself will have overwritten vladdb.sql.gz.
# Move it to a separate location instead of deleting it outright.
- name: db | Check for existing vladdb.sql.gz
stat: path=/var/www/site/vlad_aux/db_io/halt_destroy/vladdb.sql.gz
register: vlad_pantheon_import_site_evdb
ignore_errors: true

- name: db | Back up existing vladdb.sql.gz
command: mv -f /var/www/site/vlad_aux/db_io/halt_destroy/vladdb.sql.gz /var/www/site/vlad_aux/db_io/halt_destroy/vladdb_pantheon_import_site_backup.sql.gz
when: vlad_pantheon_import_site_evdb is defined and vlad_pantheon_import_site_evdb.stat.exists == True

- name: db | Download latest database backup from Pantheon
command: terminus site backups get --site="{{ vlad_pantheon_import_site_site }}" --env="{{ vlad_pantheon_import_site_env }}" --element=db --latest --to=/var/www/site/vlad_aux/db_io/halt_destroy/pantheon_import_site.sql.gz
args:
creates: /var/www/site/vlad_aux/db_io/halt_destroy/pantheon_import_site.sql.gz

- name: db | Copy downloaded database to vladdb.sql.gz so mysql role will import it
command: cp /var/www/site/vlad_aux/db_io/halt_destroy/pantheon_import_site.sql.gz /var/www/site/vlad_aux/db_io/halt_destroy/vladdb.sql.gz
args:
creates: /var/www/site/vlad_aux/db_io/halt_destroy/vladdb.sql.gz

# If we already have MySQL from a previous run, then clear the DB so that the new backup will get imported later.
- name: db | Check if mysql works
stat: path=/usr/bin/mysql
ignore_errors: true
register: vlad_pantheon_import_site_haz_mysql

- name: db | Drop vladdb database
mysql_db: name=vladdb state=absent login_user='{{ dbuser }}' login_password='{{ dbpass }}'
sudo: yes
when: vlad_pantheon_import_site_haz_mysql.stat.exists is defined and vlad_pantheon_import_site_haz_mysql.stat.exists == True
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
- name: files | Download latest files backup from Pantheon
command: terminus site backups get --site="{{ vlad_pantheon_import_site_site }}" --env="{{ vlad_pantheon_import_site_env }}" --element=files --latest --to=/var/www/site/vlad_aux/pantheon_import_site_files.tar.gz
args:
creates: /var/www/site/vlad_aux/pantheon_import_site_files.tar.gz

- name: files | Remove old file directory
file: path=/var/www/site/docroot/sites/default/files state=absent recurse=yes
when: vlad_pantheon_import_site_files_stat is defined and vlad_pantheon_import_site_files_stat.stat.exists == True

- name: files | Expand downloaded archive
unarchive:
src: /var/www/site/vlad_aux/pantheon_import_site_files.tar.gz
dest: /var/www/site/docroot/sites/default/
copy: no # This is an in-VM unarchive operation.
creates: /var/www/site/docroot/sites/default/files_{{ vlad_pantheon_import_site_env }}

- name: files | Rename files_<ENV> directory to just files
command: mv '/var/www/site/docroot/sites/default/files_{{ vlad_pantheon_import_site_env }}' /var/www/site/docroot/sites/default/files
args:
creates: /var/www/site/docroot/sites/default/files
8 changes: 8 additions & 0 deletions vlad_guts/playbooks/roles/pantheon_import_site/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Load variable file based on the OS type, or a default if not found.
- include_vars: "{{ item }}"
with_items:
- "all.yml"

- include: pantheon_import_site.yml
tags: pantheon_import
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
# Authenticate to Pantheon CLI. Don't do anything else if it
# fails, but do ignore errors (this doesn't need to halt provisioning).
# @todo: Run a terminus auth whoami first and don't mark anything changed if we are already logged in.
- name: Log in to Pantheon
command: /usr/local/bin/terminus auth login "{{ vlad_pantheon_import_site_email }}" --password='{{ vlad_pantheon_import_site_password }}'
register: vlad_pantheon_auth_success
ignore_errors: yes

- name: Check if docroot/sites/default folder exists
stat: path=/var/www/site/docroot/sites/default
register: vlad_pantheon_import_site_sd_stat
when: vlad_pantheon_import_site_include_files == True and vlad_pantheon_auth_success.rc == 0
ignore_errors: yes

- name: files | Check if downloaded file archive already exists
stat: path=/var/www/site/vlad_aux/pantheon_import_site_files.tar.gz
register: vlad_pantheon_import_site_files_stat
when: vlad_pantheon_import_site_sd_stat is defined and vlad_pantheon_import_site_sd_stat.stat.exists == True and vlad_pantheon_import_site_include_files == True and vlad_pantheon_auth_success.rc == 0
ignore_errors: yes

- include: import_files.yml
when: vlad_pantheon_import_site_include_files is defined and vlad_pantheon_import_site_include_files == True and vlad_pantheon_import_site_files_stat is defined and vlad_pantheon_import_site_files_stat.stat.exists == False

- name: db | Check if database backup already exists
stat: path=/var/www/site/vlad_aux/db_io/halt_destroy/pantheon_import_site.sql.gz
register: vlad_pantheon_import_site_db_exists
when: vlad_pantheon_import_site_include_db == True and vlad_pantheon_auth_success.rc == 0
ignore_errors: yes

# This command is copied from the mysql role because we run before it.
- name: db | Ensure vlad_aux/db_io directories exist
file:
path: /var/www/site/vlad_aux/{{ item }}
state: directory
mode: 0777
with_items:
- db_io
- db_io/halt_destroy
sudo: true

- include: import_db.yml
when: vlad_pantheon_import_site_include_db is defined and vlad_pantheon_import_site_include_db == True and vlad_pantheon_import_site_db_exists is defined and vlad_pantheon_import_site_db_exists.stat.exists == False
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
21 changes: 17 additions & 4 deletions vlad_guts/playbooks/site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@
fail: msg="The PHP role is required to use Memcached"
when: memcached_install and php_install == false

- name: Check Pantheon Import dependencies
fail: msg="The Pantheon CLI and MySQL roles are required to import Pantheon sites."
when: vlad_pantheon_import_site and (mysql_install == false or pantheon_cli_install == false)

- name: Check if Pantheon Import will import the database
fail:
msg: You've chosen to import your Pantheon database, but your db_import_up variable is not configured. If this was unintentional, remove the pantheon_import_site_include_db variable from your Vlad configuration. Otherwise, please configure it to import the "vladdb" database (valid values are ["vladdb"] or true). For more information, see https://vlad-docs.readthedocs.org/en/latest/usage/variables/#mysql
# vladd is deliberately hardcoded in this condition because the Pantheon Import role does not yet
# allow configuration of where it saves the chosen Pantheon DB.
when: vlad_pantheon_import_site and db_import_up is not defined

# Generate variables to be used in playbooks

- name: get local IPv4 address
Expand All @@ -89,10 +100,14 @@

- { role: apache, tags: ["apache"], when: "apache_install" }

- { role: mysql, tags: ["mysql"], when: "mysql_install" }

- { role: php, tags: ["php"], when: "php_install" }

- { role: pantheon_cli, tags: ["hosting_tools pantheon_cli"], when: "pantheon_cli_install" }

- { role: pantheon_import_site, tags: ["pantheon_import_site"], when: "vlad_pantheon_import_site" }

- { role: mysql, tags: ["mysql"], when: "mysql_install" }

- { role: composer, tags: ["composer"], when: "php_install" }

- { role: xdebug, tags: ["xdebug"], when: "php_install" }
Expand Down Expand Up @@ -134,8 +149,6 @@

- { role: aberdeencloud_cli, tags: ["hosting_tools aberdeen_cli"], when: "aberdeen_cli_install" }

- { role: pantheon_cli, tags: ["hosting_tools pantheon_cli"], when: "pantheon_cli_install" }

post_tasks:

- name: get current Vlad version
Expand Down

0 comments on commit 43998c7

Please sign in to comment.