-
Notifications
You must be signed in to change notification settings - Fork 14
Add cleanup options #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add cleanup options #127
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2ef3a74
Initial commit
LeoDiazL b671548
Fixing conditionals
LeoDiazL 67410be
Increasing Ansible stats output limit
LeoDiazL b6c16b8
Reducing Ansible gathering-facts tasks
LeoDiazL 4e12a99
Adding a docker-stop to ec2_cleanup.
LeoDiazL 55072a8
Generating playbook file from scratch
LeoDiazL 6adafaa
Changing mount/umount logic
LeoDiazL 5934149
Fixing EFS Mount
LeoDiazL afd0853
Cleanup commented code
LeoDiazL 3a0221c
Adding small docker check. In case it doesn't exists
LeoDiazL 5778f0d
Added detail in README file
LeoDiazL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
echo "In generate_ansible_playbook.sh" | ||
|
||
echo -en "- name: Ensure hosts is up and running | ||
hosts: bitops_servers | ||
gather_facts: no | ||
tasks: | ||
- name: Wait for hosts to come up | ||
wait_for_connection: | ||
timeout: 300 | ||
|
||
- name: Ansible tasks | ||
hosts: bitops_servers | ||
become: true | ||
tasks: | ||
" > $GITHUB_ACTION_PATH/operations/deployment/ansible/playbook.yml | ||
|
||
# Adding docker cleanup task to playbook | ||
if [[ $DOCKER_FULL_CLEANUP = true ]]; then | ||
echo -en " | ||
- name: Docker Cleanup | ||
include_tasks: tasks/docker_cleanup.yml | ||
" >> $GITHUB_ACTION_PATH/operations/deployment/ansible/playbook.yml | ||
fi | ||
|
||
# Adding app_pore cleanup task to playbook | ||
if [[ $APP_DIRECTORY_CLEANUP = true ]]; then | ||
echo -en " | ||
- name: EC2 Cleanup | ||
include_tasks: tasks/ec2_cleanup.yml | ||
" >> $GITHUB_ACTION_PATH/operations/deployment/ansible/playbook.yml | ||
fi | ||
|
||
# Continue adding the defaults | ||
echo -en " | ||
- name: Include install | ||
include_tasks: tasks/install.yml | ||
- name: Include fetch | ||
include_tasks: tasks/fetch.yml | ||
# Notes on why unmounting is required can be found in umount.yaml | ||
- name: Unmount efs | ||
include_tasks: tasks/umount.yml | ||
" >> $GITHUB_ACTION_PATH/operations/deployment/ansible/playbook.yml | ||
if [[ $(alpha_only "$AWS_EFS_CREATE") == true ]] || [[ $(alpha_only "$AWS_EFS_CREATE_HA") == true ]] || [[ $AWS_EFS_MOUNT_ID != "" ]]; then | ||
echo -en " | ||
- name: Mount efs | ||
include_tasks: tasks/mount.yml | ||
when: mount_efs | ||
" >> $GITHUB_ACTION_PATH/operations/deployment/ansible/playbook.yml | ||
fi | ||
echo -en " | ||
- name: Include start | ||
include_tasks: tasks/start.yml | ||
" >> $GITHUB_ACTION_PATH/operations/deployment/ansible/playbook.yml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
- name: Check Docker exists | ||
ansible.builtin.command: | ||
cmd: "docker --version" | ||
register: docker_check | ||
ignore_errors: true | ||
|
||
- name: Stop and cleanup Docker | ||
docker_compose: | ||
project_src: "{{ app_install_root }}/{{ app_repo_name }}" | ||
state: absent | ||
remove_orphans: true | ||
remove_images: all | ||
remove_volumes: true | ||
register: output | ||
when: docker_check.rc == 0 | ||
|
||
- name: Prune Docker system | ||
command: docker system prune --all --force --volumes | ||
when: docker_check.rc == 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
- name: Generate timestamp | ||
set_fact: | ||
timestamp: "{{ ansible_date_time.date | regex_replace('[^0-9]','') }}-{{ ansible_date_time.hour }}{{ ansible_date_time.minute }}" | ||
|
||
- name: Check if folder exists | ||
stat: | ||
path: "{{ app_install_root }}/{{ app_repo_name }}" | ||
register: folder_stat | ||
|
||
- name: Stop Docker | ||
docker_compose: | ||
project_src: "{{ app_install_root }}/{{ app_repo_name }}" | ||
state: present | ||
stopped: true | ||
when: folder_stat.stat.exists | ||
|
||
- name: Find the NFS volume in fstab | ||
shell: "grep 'nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=612,retrans=2,noresvport' /etc/fstab | awk '{print $2}'" | ||
register: nfs_mount_path | ||
changed_when: false | ||
failed_when: false | ||
when: folder_stat.stat.exists | ||
|
||
- name: Check if mounted | ||
shell: "mount | grep {{ nfs_mount_path.stdout }}" | ||
register: volume_mounted | ||
changed_when: false | ||
failed_when: false | ||
when: folder_stat.stat.exists and nfs_mount_path.stdout != "" | ||
|
||
- name: Unmount the NFS volume | ||
shell: "timeout 5 umount {{ nfs_mount_path.stdout }} || timeout 5 umount -f {{ nfs_mount_path.stdout }} || timeout 5 umount -fl {{ nfs_mount_path.stdout }}" | ||
ignore_errors: true | ||
when: folder_stat.stat.exists and nfs_mount_path.stdout != "" and volume_mounted.stdout != "" | ||
|
||
- name: Deletes efs mount directory | ||
file: | ||
path: "{{ nfs_mount_path.stdout }}" | ||
state: absent | ||
when: folder_stat.stat.exists | ||
|
||
- name: Compress folder without mounted EFS | ||
archive: | ||
path: "{{ app_install_root }}/{{ app_repo_name }}" | ||
dest: "{{ app_install_root }}/{{ app_repo_name }}-{{ timestamp }}.tar.gz" | ||
format: gz | ||
force_archive: true | ||
remove: true | ||
when: folder_stat.stat.exists |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,39 @@ | ||
# UnMount EFS | ||
- name: Check if HOST variable is defined | ||
shell: "grep '^HOST_DIR=' {{ app_install_root }}/{{ app_repo_name }}/.env" | ||
register: host_variable | ||
changed_when: false | ||
failed_when: false | ||
|
||
# Reason for usage | ||
# There is no reliable way to know when an unmount is necessary | ||
# if you’ve ran a deployment which created an EC2 and an EFS and you’ve mounted the EFS and you then wanted to delete the EFS, how would you tell Ansible that unmounting is needed? | ||
# Terraform is unaware of potential state changes and therefor there is no reliable way to know if an unmount is neccessary from a passed toggle. | ||
# | ||
# Unmounting every time ensures that if an EFS is destroyed, the mount is removed with it. | ||
- name: Find the NFS volume in fstab | ||
shell: "grep 'nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=612,retrans=2,noresvport' /etc/fstab | awk '{print $2}'" | ||
register: nfs_mount_path | ||
changed_when: false | ||
failed_when: false | ||
when: host_variable.stdout == "" | ||
|
||
- name: Check if efs mount directory is present | ||
stat: | ||
path: "{{ app_install_root }}/{{ app_repo_name }}/{{ application_mount_target }}/" | ||
register: check_efs_mount | ||
- name: Check if mounted | ||
shell: "mount | grep {{ nfs_mount_path.stdout }}" | ||
register: volume_mounted | ||
changed_when: false | ||
failed_when: false | ||
when: host_variable.stdout == "" and nfs_mount_path.stdout != "" | ||
|
||
- name: Stat test | ||
debug: | ||
msg: "The file or directory exists" | ||
when: check_efs_mount.stat.exists | ||
- name: Unmount the NFS volume | ||
shell: "timeout 5 umount {{ nfs_mount_path.stdout }} || timeout 5 umount -f {{ nfs_mount_path.stdout }} || timeout 5 umount -fl {{ nfs_mount_path.stdout }}" | ||
ignore_errors: true | ||
when: host_variable.stdout == "" and nfs_mount_path.stdout != "" and volume_mounted.stdout != "" | ||
|
||
- name: Unmount efs volume | ||
ansible.posix.mount: | ||
path: "{{ app_install_root }}/{{ app_repo_name }}/{{ application_mount_target }}" | ||
state: unmounted | ||
when: check_efs_mount.stat.exists | ||
- name: Deletes efs mount directory | ||
file: | ||
path: "{{ nfs_mount_path.stdout }}" | ||
state: absent | ||
when: host_variable.stdout == "" | ||
|
||
- name: Remove entry from /etc/fstab | ||
lineinfile: | ||
path: /etc/fstab | ||
search_string: 'nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=612,retrans=2,noresvport' | ||
state: absent | ||
become: true | ||
when: host_variable.stdout == "" and nfs_mount_path.stdout != "" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.