Skip to content

Commit

Permalink
ADHOC feat (cleaup): auto-cleanup
Browse files Browse the repository at this point in the history
automatic cleanup of older clones to keep the disk usage under control. Those
clones/releases are usually around 1GB in size, having 30 of them on the
machine is easy within a week if project is atively developed. That means that
most of this space is pretty much wasted, cause all those clones are anyways
stored in AWS/Azure we don't need to keep them on the machine for backup
purposes or whatsoever. We only need few recent ones to ensure fast roll-back
in case of failed deploy.

The roll back is expected to be performed manually as of now. There is no
automation prepared or planned for it at the moment.

This is yet another implementation for the problem explained above.
This implementaion is also backported from another project, so it is also
battle-prven so to say, at the same time it ansible native which makes it a bit
more readable. Author leaves both implementation stay in the history and not
squash/rebase to show possible alternative if later on additional constainst
will be discovered.

Using ctime as the actual file creation date and also non-fakeable attribute.

    http://lists.gnu.org/archive/html/coreutils/2010-08/msg00010.html
    ctime cannot be faked (at least it's not intended to be fakeable):

    POSIX says that atime and mtime are user-settable to arbitrary times via
    the utimensat() family of syscalls, but that ctime must unfakeably track
    the current time of any action that changes a file's metadata or contents.
  • Loading branch information
Anton Boritskiy committed Aug 22, 2019
1 parent a718b18 commit cb60e4c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
5 changes: 3 additions & 2 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ magento_run_message_queue_consumers_separately: false
magento_queue_consumers_cron_template: "/usr/bin/php {{ magento_app_root }}/bin/magento queue:consumers:start {{ item.name }}"

## Deletes all installed deployable magento packages except the
## %magento_clones_to_keep% amount of latest ones + the one that is being
## deployed right now.
## %magento_clones_to_keep% amount of latest ones + all that are newer than
## %magento_clones_cleanup_min_age%
magento_cleanup_old_clones: False
magento_clones_to_keep: 5
magento_clones_cleanup_min_age: "1d"
20 changes: 14 additions & 6 deletions tasks/cleanup-old-clones.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
---
- name: "Cleanup old clones"
become: "yes"
become_user: "{{ magento_user }}"
shell: "find {{ magento_release_folder }} -maxdepth 1 -mindepth 1 ! \\( -name \"{{ magento_release_version }}\" \\) -type d -printf '%T@:%p\n' | sort -r | tail --lines=+{{ magento_clones_to_keep }} | awk -F: '{ print $2 }' | xargs -t rm -rf ;"
when:
- magento_cleanup_old_clones == True
- name: "Discover old clone/release directories which need to be cleaned"
find:
paths: "{{ magento_release_folder }}"
age: "{{ magento_clones_cleanup_min_age }}"
file_type: directory
age_stamp: "ctime"
register: magento_clones_for_cleanup

- name: "Remove old clones/releases"
file:
path: "{{ item.path }}"
state: absent
when: magento_cleanup_old_clones == True
with_items: "{{ (magento_clones_for_cleanup.files | sort(attribute='ctime', reverse=True))[magento_clones_to_keep:] | list }}"

0 comments on commit cb60e4c

Please sign in to comment.