Skip to content

Commit

Permalink
Merged in ad-hoc-use-pip-to-manage-dependencies (pull request #4)
Browse files Browse the repository at this point in the history
AD-HOC refactor (Dependencies): Use pip to manage dependencies

Approved-by: David Manners <[email protected]>
  • Loading branch information
Andrew Howden committed Sep 22, 2017
2 parents c1c2345 + 86e0801 commit 2db7a37
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 58 deletions.
18 changes: 18 additions & 0 deletions tasks/dependencies.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
- name: "Install tools required to fetch/unpack content from AWS"
package:
name: "{{ item }}"
state: "present"
with_items:
- "gzip"
become: "True"
become_user: "root"

- name: "Install the required python packages"
pip:
name: "{{ item }}"
state: "latest"
become: "True"
become_user: "root"
with_items:
- "boto3"
49 changes: 49 additions & 0 deletions tasks/import.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
# Note: The AWS keys are deliberately omitted for this task. They should be set in the session:
# $ export AWS_ACCESS_KEY_ID="YOUR_ACCESS_KEY"
# $ export AWS_SECRET_ACCESS_KEY="YOUR_SECRET_KEY"
- name: "Download the copy of the DB from S3"
aws_s3:
bucket: "{{ import_db_s3_bucket }}"
object: "{{ import_db_s3_object_name }}"
dest: "/tmp/db_dump.sql.gz"
region: "{{ import_db_s3_region }}"
aws_access_key: "{{ import_db_aws_access_key_id }}"
aws_secret_key: "{{ import_db_aws_secret_access_key }}"
mode: "get"
register: "s3db"

# Note: Ansible does not support plain .gz files as part of its "unarchive" module, as they are not strictly speaking
# archives. See
# - https://github.com/ansible/ansible-modules-extras/pull/1301
# - https://github.com/ansible/ansible-modules-core/issues/1035
- name: "Unpack the DB"
command: "gzip -d /tmp/db_dump.sql.gz"
args:
creates: "/tmp/db_dump.sql"
when: "s3db.changed"

- name: "Delete the old database"
mysql_db:
name: "{{ import_db_database_name }}"
state: "absent"
login_password: "{{ import_db_root_password }}"
login_user: "root"
when: "s3db.changed"

- name: "Restore the database"
mysql_db:
name: "{{ import_db_database_name }}"
state: "import"
target: "/tmp/db_dump.sql"
login_password: "{{ import_db_root_password }}"
login_user: "root"

- name: "Clean up the db from /tmp"
file:
path: "{{ item }}"
state: "absent"
with_items:
- "/tmp/db_dump.sql"
- "/tmp/db_dump.sql.gz"
when: "s3db.changed"
60 changes: 2 additions & 58 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,59 +1,3 @@
---
- name: "Install tools required to fetch/unpack content from AWS"
package:
name: "{{ item }}"
state: "present"
with_items:
- "python3-boto"
- "gzip"
become: "True"
become_user: "root"

# Note: The AWS keys are deliberately omitted for this task. They should be set in the session:
# $ export AWS_ACCESS_KEY_ID="YOUR_ACCESS_KEY"
# $ export AWS_SECRET_ACCESS_KEY="YOUR_SECRET_KEY"
- name: "Download the copy of the DB from S3"
s3:
bucket: "{{ import_db_s3_bucket }}"
object: "{{ import_db_s3_object_name }}"
dest: "/tmp/db_dump.sql.gz"
region: "{{ import_db_s3_region }}"
aws_access_key: "{{ import_db_aws_access_key_id }}"
aws_secret_key: "{{ import_db_aws_secret_access_key }}"
mode: "get"
register: "s3db"

# Note: Ansible does not support plain .gz files as part of its "unarchive" module, as they are not strictly speaking
# archives. See
# - https://github.com/ansible/ansible-modules-extras/pull/1301
# - https://github.com/ansible/ansible-modules-core/issues/1035
- name: "Unpack the DB"
command: "gzip -d /tmp/db_dump.sql.gz"
args:
creates: "/tmp/db_dump.sql"
when: "s3db.changed"

- name: "Delete the old database"
mysql_db:
name: "{{ import_db_database_name }}"
state: "absent"
login_password: "{{ import_db_root_password }}"
login_user: "root"
when: "s3db.changed"

- name: "Restore the database"
mysql_db:
name: "{{ import_db_database_name }}"
state: "import"
target: "/tmp/db_dump.sql"
login_password: "{{ import_db_root_password }}"
login_user: "root"

- name: "Clean up the db from /tmp"
file:
path: "{{ item }}"
state: "absent"
with_items:
- "/tmp/db_dump.sql"
- "/tmp/db_dump.sql.gz"
when: "s3db.changed"
- include: "dependencies.yml"
- include: "import.yml"

0 comments on commit 2db7a37

Please sign in to comment.