Skip to content

Commit 9dc8049

Browse files
committed
Refactor release cleanup and unarchive flow
1 parent 8f8fe11 commit 9dc8049

3 files changed

Lines changed: 73 additions & 21 deletions

File tree

tasks/cleanup.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
---
2-
# Clean up releases
2+
- name: ANSISTRANO | Gather releases
3+
find:
4+
paths: "{{ ansistrano_releases_path }}"
5+
file_type: directory
6+
recurse: false
7+
register: ansistrano_releases
8+
when: ansistrano_keep_releases | int > 0
9+
310
- name: ANSISTRANO | Clean up releases
4-
shell: ls -1dt {{ ansistrano_releases_path }}/* | tail -n +{{ ansistrano_keep_releases | int + 1 }} | xargs rm -rf
11+
file:
12+
path: "{{ item.path }}"
13+
state: absent
14+
loop: "{{ (ansistrano_releases.files | sort(attribute='mtime', reverse=true))[ansistrano_keep_releases | int:] }}"
515
when: ansistrano_keep_releases | int > 0

tasks/update-code/unarchive.yml

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
---
22
- name: ANSISTRANO | Unarchive | Unarchive source
3-
unarchive:
4-
copy: no
5-
src: "{{ ansistrano_archived_file }}"
6-
dest: "{{ ansistrano_release_path.stdout }}"
7-
exclude: "{{ ansistrano_unarchive_exclude }}"
8-
extra_opts: "{{ ansistrano_unarchive_extra_opts }}"
9-
when: ansistrano_unarchive_owner == "default" and ansistrano_unarchive_group == "default"
10-
11-
- name: ANSISTRANO | Unarchive | Unarchive source
12-
fail:
13-
msg: "must set ansistrano_unarchive_group and ansistrano_unarchive_owner"
3+
assert:
4+
that:
5+
- (ansistrano_unarchive_owner == "default") == (ansistrano_unarchive_group == "default")
6+
fail_msg: "must set ansistrano_unarchive_group and ansistrano_unarchive_owner"
147
when: >
15-
(ansistrano_unarchive_owner == "default" and ansistrano_unarchive_group != "default") or
16-
(ansistrano_unarchive_owner != "default" and ansistrano_unarchive_group == "default")
8+
ansistrano_unarchive_owner == "default" or
9+
ansistrano_unarchive_group == "default"
1710
18-
- name: ANSISTRANO | Unarchive | Unarchive source with owner and group
11+
- name: ANSISTRANO | Unarchive | Unarchive source
1912
unarchive:
2013
copy: no
2114
src: "{{ ansistrano_archived_file }}"
2215
dest: "{{ ansistrano_release_path.stdout }}"
23-
owner: "{{ ansistrano_unarchive_owner }}"
24-
group: "{{ ansistrano_unarchive_group }}"
16+
owner: "{{ ansistrano_unarchive_owner if ansistrano_unarchive_owner != 'default' else omit }}"
17+
group: "{{ ansistrano_unarchive_group if ansistrano_unarchive_group != 'default' else omit }}"
2518
exclude: "{{ ansistrano_unarchive_exclude }}"
2619
extra_opts: "{{ ansistrano_unarchive_extra_opts }}"
27-
when: >
28-
ansistrano_unarchive_owner != "default" and
29-
ansistrano_unarchive_group != "default"
3020

3121
- name: ANSISTRANO | Unarchive | Delete archived file
3222
file:

test/rsync-test.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,58 @@
6969
roles:
7070
- { role: local-ansistrano }
7171

72+
- name: When deploying with release cleanup enabled
73+
hosts: all
74+
vars:
75+
ansistrano_deploy_to: "/tmp/cleanup-my-app.com"
76+
pre_tasks:
77+
- name: Set the first fixed release version
78+
set_fact:
79+
ansistrano_release_version: "20000101000000Z"
80+
ansistrano_keep_releases: 1
81+
roles:
82+
- { role: local-ansistrano }
83+
84+
- name: And deploying a newer release with release cleanup enabled
85+
hosts: all
86+
vars:
87+
ansistrano_deploy_to: "/tmp/cleanup-my-app.com"
88+
pre_tasks:
89+
- name: Set the second fixed release version
90+
set_fact:
91+
ansistrano_release_version: "20000101000001Z"
92+
ansistrano_keep_releases: 1
93+
roles:
94+
- { role: local-ansistrano }
95+
96+
- name: Then only the newest release should remain
97+
hosts: all
98+
vars:
99+
ansistrano_deploy_to: "/tmp/cleanup-my-app.com"
100+
tasks:
101+
- name: Assert the oldest release was removed
102+
stat:
103+
path: "{{ ansistrano_deploy_to }}/releases/20000101000000Z"
104+
register: old_release
105+
106+
- name: Assert the newest release still exists
107+
stat:
108+
path: "{{ ansistrano_deploy_to }}/releases/20000101000001Z"
109+
register: new_release
110+
111+
- name: Assert current points to the newest release
112+
stat:
113+
path: "{{ ansistrano_deploy_to }}/current"
114+
register: current_release
115+
116+
- name: Assert old release was removed and newest release kept
117+
assert:
118+
that:
119+
- old_release.stat.exists is defined and not old_release.stat.exists
120+
- new_release.stat.exists is defined and new_release.stat.exists
121+
- current_release.stat.islnk is defined and current_release.stat.islnk
122+
- current_release.stat.lnk_target == "./releases/20000101000001Z"
123+
72124
- name: When deploying fails before symlink
73125
hosts: all
74126
vars:

0 commit comments

Comments
 (0)