Skip to content
This repository was archived by the owner on Oct 8, 2025. It is now read-only.

Commit 094728d

Browse files
tymofiisobchenkogregharveydrazenCEfiliprupicfilip
authored
Asg management fix and improve pr devel 1.x (#732)
* Php unix socket pr 1.x (#724) * Moving cachetool adapter setup to the installer role and adding a test. * Updating cachetool documentation. * Php unix socket pr 1.x (#726) * Moving cachetool adapter setup to the installer role and adding a test. * Updating cachetool documentation. * Making sure _cachetool_adapter is always set. * Optional-drush-cr-before-import (#728) * 73171 fixing cachetool checks fpm pr 1.x (#729) * skipping cachetool check when no fpm * remove empty line --------- Co-authored-by: filip <filip.rupic@codeenigma.com> * asg_management_fix_and_improve * asg_management_fix_and_improve * asg_management_fix_and_improve --------- Co-authored-by: Greg Harvey <greg.harvey@gmail.com> Co-authored-by: drazenCE <140631110+drazenCE@users.noreply.github.com> Co-authored-by: Filip Rupic <123341158+filiprupic@users.noreply.github.com> Co-authored-by: filip <filip.rupic@codeenigma.com>
1 parent 4b65024 commit 094728d

File tree

3 files changed

+135
-80
lines changed

3 files changed

+135
-80
lines changed

roles/asg_management/defaults/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# AWS ASG variables to allow for the suspension of autoscaling during a code deployment.
33
asg_management:
44
name: "" # if the deploy is on an ASG put the name here
5+
target_group_name: "{{ asg_management.name }}" # change if different from the ASG name (which may happen due to the 32-char limit)
6+
refresh_asg_instances: true # runs only if squashFS image unmount failed and this set to true.
57
#profile: "example" # optional, the boto profile name to use if not the system default
68
region: "eu-west-1"
79
suspend_processes: "Launch Terminate HealthCheck" # space separated string, see https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-suspend-resume-processes.html

roles/asg_management/tasks/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
region: "{{ asg_management.region }}"
1212
profile: "{{ asg_management.profile | default(omit) }}"
1313
names:
14-
- "{{ asg_management.name }}"
14+
- "{{ asg_management.target_group_name }}"
1515
register: _target_group
1616

1717
- name: Loop over target instances until they are all 'healthy'.

roles/deploy_code/tasks/cleanup.yml

Lines changed: 132 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -150,93 +150,146 @@
150150
failed_when: false
151151
register: _deploy_code_mount_check
152152

153-
- name: Get the current pts session.
154-
ansible.builtin.shell:
155-
cmd: "tty | sed 's#/dev/##'"
156-
register: deploy_pts
157-
158-
- name: "Check for active sessions in {{ deploy_base_path }}."
159-
ansible.builtin.shell:
160-
cmd: "ps -eo pid,tty | awk '{print $1}' | xargs -n 1 pwdx 2>&1 | grep -v 'No such process' | grep {{ deploy_base_path }} | cut -d: -f1 | xargs -n 1 ps -o tty= -p | sort | uniq"
161-
register: sessions_in_deploy_path
162-
become: true
163-
164-
- name: Display active sessions.
165-
ansible.builtin.debug:
166-
msg: >
167-
Deploy session: {{ deploy_pts.stdout | default('Unknown') }}.
168-
Active sessions in {{ deploy_base_path }}: {{ sessions_in_deploy_path.stdout_lines | default([]) | join(', ') | default('None') }}.
169-
170-
- name: Kill sessions except the current one.
153+
- name: Mount SquashFS image if there is no mounted one.
171154
ansible.builtin.command:
172-
cmd: "pkill -9 -t {{ item }}"
173-
loop: "{{ sessions_in_deploy_path.stdout_lines }}"
174-
when:
175-
- "item != deploy_pts.stdout"
176-
- "item is match('^pts/\\d+$')"
177-
failed_when: false
178-
register: kill_sessions_result
179-
become: true
180-
181-
- name: Display killed sessions.
182-
ansible.builtin.debug:
183-
msg: >
184-
Sessions terminated: {{ kill_sessions_result.results | selectattr('rc', 'defined') | selectattr('rc', 'equalto', 0) | map(attribute='item') | list | join(', ') | default('None') }}.
185-
186-
- name: Reload any services that might be keeping the loop device busy.
187-
ansible.builtin.service:
188-
name: "{{ www_service }}"
189-
state: reloaded
190-
with_items: "{{ deploy_code.services }}"
191-
loop_control:
192-
loop_var: www_service
193-
become: true
194-
when:
195-
- _deploy_code_mount_check.rc == 0
196-
- deploy_code.service_action == "reload"
197-
- deploy_code.services | length > 0
198-
199-
- name: Stop any services that might be keeping the loop device busy.
200-
ansible.builtin.service:
201-
name: "{{ www_service }}"
202-
state: stopped
203-
with_items: "{{ deploy_code.services }}"
204-
loop_control:
205-
loop_var: www_service
155+
cmd: "mount {{ build_base_path }}/deploy.sqsh {{ deploy_base_path }} -t squashfs -o loop"
206156
become: true
207157
when:
208-
- _deploy_code_mount_check.rc == 0
209-
- deploy_code.service_action == "stop"
210-
- deploy_code.services | length > 0
158+
- _deploy_code_mount_check.rc != 0
211159

212-
- name: Unmount existing SquashFS image.
213-
ansible.builtin.command:
214-
cmd: "umount {{ deploy_base_path }}"
215-
become: true
160+
- name: Mount new SquashFS image instead of the already mounted.
216161
when:
217162
- _deploy_code_mount_check.rc == 0
218-
register: task_result
219-
retries: "{{ deploy_code.unmount_retries }}"
220-
delay: "{{ deploy_code.unmount_delay }}"
221-
until: task_result.rc == 0
163+
block:
164+
- name: Get the current pts session.
165+
ansible.builtin.shell:
166+
cmd: "tty | sed 's#/dev/##'"
167+
register: deploy_pts
222168

223-
- name: Mount new SquashFS image.
224-
ansible.builtin.command:
225-
cmd: "mount {{ build_base_path }}/deploy.sqsh {{ deploy_base_path }} -t squashfs -o loop"
226-
become: true
169+
- name: "Check for active sessions in {{ deploy_base_path }}."
170+
ansible.builtin.shell:
171+
cmd: "ps -eo pid,tty | awk '{print $1}' | xargs -n 1 pwdx 2>&1 | grep -v 'No such process' | grep {{ deploy_base_path }} | cut -d: -f1 | xargs -n 1 ps -o tty= -p | sort | uniq"
172+
register: sessions_in_deploy_path
173+
become: true
227174

228-
- name: Start any services we stopped.
229-
ansible.builtin.service:
230-
name: "{{ www_service }}"
231-
state: started
232-
with_items: "{{ deploy_code.services }}"
233-
loop_control:
234-
loop_var: www_service
235-
become: true
236-
when:
237-
- _deploy_code_mount_check.rc == 0
238-
- deploy_code.service_action == "stop"
239-
- deploy_code.services | length > 0
175+
- name: Display active sessions.
176+
ansible.builtin.debug:
177+
msg: >
178+
Deploy session: {{ deploy_pts.stdout | default('Unknown') }}.
179+
Active sessions in {{ deploy_base_path }}: {{ sessions_in_deploy_path.stdout_lines | default([]) | join(', ') | default('None') }}.
180+
181+
- name: Kill sessions except the current one.
182+
ansible.builtin.command:
183+
cmd: "pkill -9 -t {{ item }}"
184+
loop: "{{ sessions_in_deploy_path.stdout_lines }}"
185+
when:
186+
- "item != deploy_pts.stdout"
187+
- "item is match('^pts/\\d+$')"
188+
failed_when: false
189+
register: kill_sessions_result
190+
become: true
191+
192+
- name: Display killed sessions.
193+
ansible.builtin.debug:
194+
msg: >
195+
Sessions terminated: {{ kill_sessions_result.results | selectattr('rc', 'defined') | selectattr('rc', 'equalto', 0) | map(attribute='item') | list | join(', ') | default('None') }}.
196+
197+
- name: Reload any services that might be keeping the loop device busy.
198+
ansible.builtin.service:
199+
name: "{{ www_service }}"
200+
state: reloaded
201+
with_items: "{{ deploy_code.services }}"
202+
loop_control:
203+
loop_var: www_service
204+
become: true
205+
when:
206+
- deploy_code.service_action == "reload"
207+
- deploy_code.services | length > 0
208+
209+
- name: Stop any services that might be keeping the loop device busy.
210+
ansible.builtin.service:
211+
name: "{{ www_service }}"
212+
state: stopped
213+
with_items: "{{ deploy_code.services }}"
214+
loop_control:
215+
loop_var: www_service
216+
become: true
217+
when:
218+
- deploy_code.service_action == "stop"
219+
- deploy_code.services | length > 0
220+
221+
- name: Unmount existing SquashFS image.
222+
ansible.builtin.command:
223+
cmd: "umount {{ deploy_base_path }}"
224+
become: true
225+
register: task_result
226+
retries: "{{ deploy_code.unmount_retries }}"
227+
delay: "{{ deploy_code.unmount_delay }}"
228+
until: task_result.rc == 0
229+
failed_when: false
230+
231+
- name: If current image unmount succeeded.
232+
when: task_result.rc == 0
233+
block:
234+
- name: Mount new SquashFS image.
235+
ansible.builtin.command:
236+
cmd: "mount {{ build_base_path }}/deploy.sqsh {{ deploy_base_path }} -t squashfs -o loop"
237+
become: true
238+
239+
- name: Start any services that we stopped if the image re-mounting was successful.
240+
ansible.builtin.service:
241+
name: "{{ www_service }}"
242+
state: started
243+
with_items: "{{ deploy_code.services }}"
244+
loop_control:
245+
loop_var: www_service
246+
become: true
247+
when:
248+
- deploy_code.service_action == "stop"
249+
- deploy_code.services | length > 0
250+
251+
- name: If current image unmount failed.
252+
when: task_result.rc != 0
253+
block:
254+
- name: Resume all autoscale processes on ASG.
255+
amazon.aws.autoscaling_group:
256+
name: "{{ asg_management.name }}"
257+
region: "{{ asg_management.region }}"
258+
profile: "{{ asg_management.profile | default(omit) }}"
259+
suspended_processes: []
260+
delegate_to: localhost
261+
run_once: true
262+
when:
263+
- asg_management.name | length > 0
264+
265+
- name: Run ASG instance refresh.
266+
amazon.aws.autoscaling_instance_refresh:
267+
name: "{{ asg_management.name }}"
268+
region: "{{ asg_management.region }}"
269+
profile: "{{ asg_management.profile | default(omit) }}"
270+
strategy: Rolling
271+
preferences:
272+
min_healthy_percentage: 51
273+
instance_warmup: 100
274+
skip_matching: false
275+
state: started
276+
delegate_to: localhost
277+
when:
278+
- asg_management.name | length > 0
279+
- asg_management.refresh_asg_instances
280+
281+
- name: Start any services we stopped if the image re-mounting failed and ASG management is disabled.
282+
ansible.builtin.service:
283+
name: "{{ www_service }}"
284+
state: started
285+
with_items: "{{ deploy_code.services }}"
286+
loop_control:
287+
loop_var: www_service
288+
become: true
289+
when:
290+
- deploy_code.service_action == "stop"
291+
- deploy_code.services | length > 0
292+
- not asg_management.refresh_asg_instances or (asg_management.name | length) == 0
240293
# End of the squashFS block.
241294

242295
- name: Trigger an infrastructure rebuild.

0 commit comments

Comments
 (0)