|
150 | 150 | failed_when: false |
151 | 151 | register: _deploy_code_mount_check |
152 | 152 |
|
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. |
171 | 154 | 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" |
206 | 156 | become: true |
207 | 157 | 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 |
211 | 159 |
|
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. |
216 | 161 | when: |
217 | 162 | - _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 |
222 | 168 |
|
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 |
227 | 174 |
|
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 |
240 | 293 | # End of the squashFS block. |
241 | 294 |
|
242 | 295 | - name: Trigger an infrastructure rebuild. |
|
0 commit comments