From 20774f65a3cc14ea86b3bd810e32913771f4f55a Mon Sep 17 00:00:00 2001 From: gregharvey Date: Mon, 13 Jun 2022 13:36:26 +0200 Subject: [PATCH 01/24] Adding SquashFS option to syncing. --- roles/deploy_code/defaults/main.yml | 4 +++- roles/deploy_code/tasks/cleanup.yml | 26 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/roles/deploy_code/defaults/main.yml b/roles/deploy_code/defaults/main.yml index d616bf08..bff3cd01 100644 --- a/roles/deploy_code/defaults/main.yml +++ b/roles/deploy_code/defaults/main.yml @@ -15,8 +15,10 @@ deploy_code: templates: [] # Number of builds to keep. Note this is independant of databases/dump. keep: 10 - # Wether to sync the local deploy base to a shared destination, after successful build. + # Whether to sync the local deploy base to a shared destination, after successful build. mount_sync: "" + # Type of file to use for sync - 'squashfs' or 'tarball' + mount_type: "" # mount_sync: "/home/{{ deploy_user }}/shared/{{ project_name }}_{{ build_type }}/deploy" # Path that you want to make sure has 755 permissions. Make sure to include the webroot WITHOUT the slash. perms_fix_path: "" diff --git a/roles/deploy_code/tasks/cleanup.yml b/roles/deploy_code/tasks/cleanup.yml index 03530f22..e9e58146 100644 --- a/roles/deploy_code/tasks/cleanup.yml +++ b/roles/deploy_code/tasks/cleanup.yml @@ -26,6 +26,18 @@ when: - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type is defined + - deploy_code.mount_type == "tarball" + run_once: true + +- name: Create a SquashFS image of the deployed codebases. + ansible.builtin.command: + cmd: "mksquashfs {{ deploy_base_path }} /tmp/{{ project_name }}_{{ build_type }}.sqsh" + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type is defined + - deploy_code.mount_type == "squashfs" run_once: true - name: Create destination folder. @@ -38,12 +50,24 @@ - deploy_code.mount_sync | length > 1 run_once: true -- name: Move to final destination. +- name: Move tar file to final destination. ansible.builtin.command: cmd: "mv /tmp/{{ project_name }}_{{ build_type }}.tar {{ deploy_code.mount_sync }}/{{ project_name }}_{{ build_type }}.tar" when: - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type is defined + - deploy_code.mount_type == "tarball" + run_once: true + +- name: Move SquashFS image to final destination. + ansible.builtin.command: + cmd: "mv /tmp/{{ project_name }}_{{ build_type }}.sqsh {{ deploy_code.mount_sync }}/{{ project_name }}_{{ build_type }}.sqsh" + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type is defined + - deploy_code.mount_type == "squashfs" run_once: true - name: Trigger an infrastructure rebuild. From 7f3e7503dadc57d52b71764ee64a43c061ebc1ef Mon Sep 17 00:00:00 2001 From: gregharvey Date: Mon, 13 Jun 2022 16:12:00 +0200 Subject: [PATCH 02/24] Make 'tarball' the default mount type so nothing existing breaks. --- roles/deploy_code/defaults/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roles/deploy_code/defaults/main.yml b/roles/deploy_code/defaults/main.yml index bff3cd01..be4cbceb 100644 --- a/roles/deploy_code/defaults/main.yml +++ b/roles/deploy_code/defaults/main.yml @@ -18,7 +18,7 @@ deploy_code: # Whether to sync the local deploy base to a shared destination, after successful build. mount_sync: "" # Type of file to use for sync - 'squashfs' or 'tarball' - mount_type: "" + mount_type: "tarball" # mount_sync: "/home/{{ deploy_user }}/shared/{{ project_name }}_{{ build_type }}/deploy" # Path that you want to make sure has 755 permissions. Make sure to include the webroot WITHOUT the slash. perms_fix_path: "" From 0b17bb5605fb666d4d83fe6997785d6fcad3703b Mon Sep 17 00:00:00 2001 From: gregharvey Date: Tue, 14 Jun 2022 12:50:14 +0200 Subject: [PATCH 03/24] Altering deploy_path so we can build in another location for SquashFS builds. --- roles/_init/tasks/main.yml | 41 ++++++++++++++++++++--------- roles/deploy_code/defaults/main.yml | 3 ++- roles/deploy_code/tasks/cleanup.yml | 30 +++++++++++++++++---- 3 files changed, 55 insertions(+), 19 deletions(-) diff --git a/roles/_init/tasks/main.yml b/roles/_init/tasks/main.yml index a1b6e204..384eebe7 100644 --- a/roles/_init/tasks/main.yml +++ b/roles/_init/tasks/main.yml @@ -1,27 +1,42 @@ --- # Ensure default values for common variables. - name: Define deploy user. - set_fact: + ansible.builtin.set_fact: deploy_user: "{{ deploy_user | default('deploy') }}" - name: Define deploy base path. - set_fact: + ansible.builtin.set_fact: deploy_base_path: "{{ deploy_base_path | default('/home/{{ deploy_user }}/deploy/{{ project_name }}_{{ build_type }}') }}" - name: Define mounted directory for assets. - set_fact: + ansible.builtin.set_fact: deploy_assets_base_path: "{{ deploy_assets_base_path | default('/home/{{ deploy_user }}/shared/{{ project_name }}_{{ build_type }}/assets') }}" - name: Define webroot. - set_fact: + ansible.builtin.set_fact: webroot: "{{ webroot | default('web') }}" - name: Define build deploy path prefix. - set_fact: + ansible.builtin.set_fact: deploy_path_prefix: "{{ deploy_base_path }}/{{ project_name }}_{{ build_type }}_build_" - name: Define build deploy path. - set_fact: + ansible.builtin.set_fact: deploy_path: "{{ deploy_path | default('{{ deploy_path_prefix }}{{ build_number }}') }}" - name: Define live_symlink dest. - set_fact: + ansible.builtin.set_fact: live_symlink_dest: "{{ live_symlink_dest | default('{{ deploy_base_path }}/live.{{ project_name }}_{{ build_type }}') }}" +# Manipulate variables for SquashFS builds. +- name: Define image builds base path. + ansible.builtin.set_fact: + build_base_path: "/home/{{ deploy_user }}/builds/{{ project_name }}_{{ build_type }}" +- name: Define image builds build path prefix. + ansible.builtin.set_fact: + build_path_prefix: "{{ build_base_path }}/{{ project_name }}_{{ build_type }}_build_" +- name: Overwrite deploy path if SquashFS deploy. + ansible.builtin.set_fact: + deploy_path: "{{ build_path | default('{{ build_path_prefix }}{{ build_number }}') }}" + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + # Gather last known good build directly from symlink. # This can happen: # - when the first builds failed, @@ -42,7 +57,7 @@ # This is passed from caller. - name: Gather last known good build number. - set_fact: + ansible.builtin.set_fact: previous_build_number: "{{ previous_known_build_number }}" # - set_fact: @@ -53,27 +68,27 @@ # Make sure the deploy target exists. - name: Ensure deploy target directory exists. - file: + ansible.builtin.file: path: "{{ deploy_base_path }}" state: directory # Check for project specific init tasks. - name: Check that {{ project_type }}.yml exists. - stat: + ansible.builtin.stat: path: "{{ role_path }}/tasks/{{ project_type }}.yml" register: _project_type_task_result delegate_to: "localhost" # Project specific init tasks. - name: Include project init tasks. - include_tasks: "{{ project_type }}.yml" + ansible.builtin.include_tasks: "{{ project_type }}.yml" when: - _project_type_task_result.stat.exists - name: Define opcache cachetool path. - set_fact: + ansible.builtin.set_fact: cachetool_bin: "{{ cachetool_bin | default('/home/{{ deploy_user }}/.bin/cachetool.phar') }}" - name: Ensure we have a cachetool binary. - import_role: + ansible.builtin.import_role: name: cli/cachetool diff --git a/roles/deploy_code/defaults/main.yml b/roles/deploy_code/defaults/main.yml index be4cbceb..c0e29363 100644 --- a/roles/deploy_code/defaults/main.yml +++ b/roles/deploy_code/defaults/main.yml @@ -3,7 +3,7 @@ deploy_code: # Specify any additional symlink to create, with src (target) and dest (link). # src: can be either absolute or relative to the dest (eg. '/var/my_data', '/home/deploy/simplesaml', '../../../myconfig') # dest: can only be relative to the root of your repository (eg. 'www/themes/myassets', 'var/cache') - # create: wether to create the target if it does not exists. + # create: whether to create the target if it does not exists. # - src: '/home/{{ deploy_user }}//{{ project_name }}_{{ build_type }}/simplesaml' # dest: 'vendor/simplesamlphp/simplesamlphp/config' # - src: '/var/simplesaml/etc' @@ -18,6 +18,7 @@ deploy_code: # Whether to sync the local deploy base to a shared destination, after successful build. mount_sync: "" # Type of file to use for sync - 'squashfs' or 'tarball' + # @see the _init role for SquashFS build dir paths mount_type: "tarball" # mount_sync: "/home/{{ deploy_user }}/shared/{{ project_name }}_{{ build_type }}/deploy" # Path that you want to make sure has 755 permissions. Make sure to include the webroot WITHOUT the slash. diff --git a/roles/deploy_code/tasks/cleanup.yml b/roles/deploy_code/tasks/cleanup.yml index e9e58146..87441926 100644 --- a/roles/deploy_code/tasks/cleanup.yml +++ b/roles/deploy_code/tasks/cleanup.yml @@ -26,17 +26,15 @@ when: - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 - - deploy_code.mount_type is defined - deploy_code.mount_type == "tarball" run_once: true - name: Create a SquashFS image of the deployed codebases. ansible.builtin.command: - cmd: "mksquashfs {{ deploy_base_path }} /tmp/{{ project_name }}_{{ build_type }}.sqsh" + cmd: "mksquashfs {{ build_base_path }} /tmp/{{ project_name }}_{{ build_type }}.sqsh" when: - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 - - deploy_code.mount_type is defined - deploy_code.mount_type == "squashfs" run_once: true @@ -56,7 +54,6 @@ when: - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 - - deploy_code.mount_type is defined - deploy_code.mount_type == "tarball" run_once: true @@ -66,10 +63,33 @@ when: - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 - - deploy_code.mount_type is defined - deploy_code.mount_type == "squashfs" run_once: true +- name: Copy SquashFS image to local server. + ansible.builtin.command: + cmd: "cp {{ deploy_code.mount_sync }}/{{ project_name }}_{{ build_type }}.sqsh {{ build_base_path }}/deploy.sqsh" + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + +- name: Unmount existing SquashFS image. + ansible.builtin.command: + cmd: "umount --force {{ deploy_base_path }}" + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + +- name: Mount new SquashFS image. + ansible.builtin.command: + cmd: "mount {{ build_base_path }}/deploy.sqsh {{ deploy_base_path }} -t squashfs -o loop" + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + - name: Trigger an infrastructure rebuild. ansible.builtin.include_role: name: api_call From 0b380de3b788442d4c0fb0b4039ac76d213089a3 Mon Sep 17 00:00:00 2001 From: gregharvey Date: Tue, 14 Jun 2022 13:04:03 +0200 Subject: [PATCH 04/24] Ensuring the build dir exists if doing a SquashFS build. --- roles/_init/tasks/main.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/roles/_init/tasks/main.yml b/roles/_init/tasks/main.yml index 384eebe7..a546364a 100644 --- a/roles/_init/tasks/main.yml +++ b/roles/_init/tasks/main.yml @@ -72,6 +72,16 @@ path: "{{ deploy_base_path }}" state: directory +# Make sure the build target exists. +- name: Ensure build target directory exists. + ansible.builtin.file: + path: "{{ build_base_path }}" + state: directory + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + # Check for project specific init tasks. - name: Check that {{ project_type }}.yml exists. ansible.builtin.stat: From 7d8a9ac4b20ea91934a9e110fea55cc4ef983104 Mon Sep 17 00:00:00 2001 From: gregharvey Date: Tue, 14 Jun 2022 14:49:28 +0200 Subject: [PATCH 05/24] Force symlink creation as the deploy dir may not exist yet. --- roles/live_symlink/tasks/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/roles/live_symlink/tasks/main.yml b/roles/live_symlink/tasks/main.yml index 460ced9a..246f55c7 100644 --- a/roles/live_symlink/tasks/main.yml +++ b/roles/live_symlink/tasks/main.yml @@ -11,6 +11,7 @@ src: "{{ _live_symlink_build_target }}" dest: "{{ live_symlink_dest }}" state: link + force: true - name: Generate additional templates. template: @@ -28,6 +29,7 @@ src: "{{ link.src }}" dest: "{{ deploy_path }}/{{ link.dest }}" state: link + force: true with_items: "{{ live_symlink.symlinks }}" loop_control: loop_var: link From 307ac67e18d69f47c343a50acc750ae03ca7620b Mon Sep 17 00:00:00 2001 From: gregharvey Date: Wed, 15 Jun 2022 13:33:14 +0200 Subject: [PATCH 06/24] Running mount commands with sudo. --- roles/deploy_code/tasks/cleanup.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/roles/deploy_code/tasks/cleanup.yml b/roles/deploy_code/tasks/cleanup.yml index 87441926..5ca473bf 100644 --- a/roles/deploy_code/tasks/cleanup.yml +++ b/roles/deploy_code/tasks/cleanup.yml @@ -77,6 +77,8 @@ - name: Unmount existing SquashFS image. ansible.builtin.command: cmd: "umount --force {{ deploy_base_path }}" + become: true + become_user: "{{ deploy_user }}" when: - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 @@ -85,6 +87,8 @@ - name: Mount new SquashFS image. ansible.builtin.command: cmd: "mount {{ build_base_path }}/deploy.sqsh {{ deploy_base_path }} -t squashfs -o loop" + become: true + become_user: "{{ deploy_user }}" when: - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 From 4424413e23b43677b3d22ef1b8b572ecf2c668d5 Mon Sep 17 00:00:00 2001 From: gregharvey Date: Wed, 15 Jun 2022 17:26:35 +0200 Subject: [PATCH 07/24] Tweaking link command and destination for live links. --- roles/_init/tasks/main.yml | 3 ++- roles/live_symlink/tasks/main.yml | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/roles/_init/tasks/main.yml b/roles/_init/tasks/main.yml index a546364a..b09ac7dc 100644 --- a/roles/_init/tasks/main.yml +++ b/roles/_init/tasks/main.yml @@ -29,9 +29,10 @@ - name: Define image builds build path prefix. ansible.builtin.set_fact: build_path_prefix: "{{ build_base_path }}/{{ project_name }}_{{ build_type }}_build_" -- name: Overwrite deploy path if SquashFS deploy. +- name: Overwrite deploy and live_symlink paths if SquashFS deploy. ansible.builtin.set_fact: deploy_path: "{{ build_path | default('{{ build_path_prefix }}{{ build_number }}') }}" + live_symlink_dest: "{{ live_symlink_dest | default('{{ build_base_path }}/live.{{ project_name }}_{{ build_type }}') }}" when: - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 diff --git a/roles/live_symlink/tasks/main.yml b/roles/live_symlink/tasks/main.yml index 246f55c7..0bb1bd70 100644 --- a/roles/live_symlink/tasks/main.yml +++ b/roles/live_symlink/tasks/main.yml @@ -1,20 +1,21 @@ --- -- set_fact: +- ansible.builtin.set_fact: _live_symlink_build_target: "{{ deploy_base_path }}/{{ project_name }}_{{ build_type }}_build_{{ build_number }}" -- set_fact: +- ansible.builtin.set_fact: _live_symlink_build_target: "{{ deploy_base_path }}/{{ project_name }}_{{ build_type }}_build_{{ previous_build_number }}" when: deploy_operation == 'revert' - name: Symlink build. - file: + ansible.builtin.file: src: "{{ _live_symlink_build_target }}" dest: "{{ live_symlink_dest }}" state: link + follow: false force: true - name: Generate additional templates. - template: + ansible.builtin.template: src: "{{ template.src }}" dest: "{{ deploy_path }}/{{ template.dest }}" with_items: "{{ live_symlink.templates }}" @@ -25,10 +26,11 @@ - deploy_operation == 'deploy' - name: Create additional symlinks. - file: + ansible.builtin.file: src: "{{ link.src }}" dest: "{{ deploy_path }}/{{ link.dest }}" state: link + follow: false force: true with_items: "{{ live_symlink.symlinks }}" loop_control: From 7e396411ca225e4ac115457dd90c32f2ec74cdfc Mon Sep 17 00:00:00 2001 From: gregharvey Date: Wed, 15 Jun 2022 17:42:18 +0200 Subject: [PATCH 08/24] Slight bug in link path handling for SquashFS. --- roles/_init/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roles/_init/tasks/main.yml b/roles/_init/tasks/main.yml index b09ac7dc..44aa2092 100644 --- a/roles/_init/tasks/main.yml +++ b/roles/_init/tasks/main.yml @@ -32,7 +32,7 @@ - name: Overwrite deploy and live_symlink paths if SquashFS deploy. ansible.builtin.set_fact: deploy_path: "{{ build_path | default('{{ build_path_prefix }}{{ build_number }}') }}" - live_symlink_dest: "{{ live_symlink_dest | default('{{ build_base_path }}/live.{{ project_name }}_{{ build_type }}') }}" + live_symlink_dest: "{{ live_symlink_build_dest | default('{{ build_base_path }}/live.{{ project_name }}_{{ build_type }}') }}" when: - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 From 2bb64595735ca8475e3859871582f36d5a9a6e31 Mon Sep 17 00:00:00 2001 From: gregharvey Date: Wed, 15 Jun 2022 18:31:24 +0200 Subject: [PATCH 09/24] Checking for existing mount and using remount operation. --- roles/deploy_code/tasks/cleanup.yml | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/roles/deploy_code/tasks/cleanup.yml b/roles/deploy_code/tasks/cleanup.yml index 5ca473bf..3336b22f 100644 --- a/roles/deploy_code/tasks/cleanup.yml +++ b/roles/deploy_code/tasks/cleanup.yml @@ -29,6 +29,16 @@ - deploy_code.mount_type == "tarball" run_once: true +- name: Ensure older SquashFS images are deleted. + ansible.builtin.file: + path: "{{ build_base_path }}/deploy.sqsh" + state: absent + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + run_once: true + - name: Create a SquashFS image of the deployed codebases. ansible.builtin.command: cmd: "mksquashfs {{ build_base_path }} /tmp/{{ project_name }}_{{ build_type }}.sqsh" @@ -74,15 +84,26 @@ - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" -- name: Unmount existing SquashFS image. +- name: Check if we have a mount already. + ansible.builtin.command: + cmd: "mount | grep {{ deploy_base_path }}" + ignore_errors: true + register: _mount_check + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + +- name: Remount existing SquashFS image. ansible.builtin.command: - cmd: "umount --force {{ deploy_base_path }}" + cmd: "mount {{ build_base_path }}/deploy.sqsh {{ deploy_base_path }} -t squashfs -o remount" become: true become_user: "{{ deploy_user }}" when: - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" + - _mount_check | success - name: Mount new SquashFS image. ansible.builtin.command: @@ -93,6 +114,7 @@ - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" + - _mount_check | failed - name: Trigger an infrastructure rebuild. ansible.builtin.include_role: From f076147fa6df28691db97b3aa2cc2e1f9f96a47f Mon Sep 17 00:00:00 2001 From: gregharvey Date: Wed, 15 Jun 2022 18:45:13 +0200 Subject: [PATCH 10/24] Stop ce-deploy trying to delete from read-only SquashFS mount. --- roles/deploy_code/tasks/cleanup.yml | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/roles/deploy_code/tasks/cleanup.yml b/roles/deploy_code/tasks/cleanup.yml index 3336b22f..85c67c24 100644 --- a/roles/deploy_code/tasks/cleanup.yml +++ b/roles/deploy_code/tasks/cleanup.yml @@ -6,19 +6,39 @@ become: true when: "www_user != deploy_user" -- name: Ensure permissions are set on directory. +- name: Ensure permissions are set on deploy directory. ansible.builtin.shell: cmd: "if [ -d {{ deploy_path_prefix }}{{ item }}/{{ deploy_code.perms_fix_path }} ]; then chmod 755 {{ deploy_path_prefix }}{{ item }}/{{ deploy_code.perms_fix_path }}; fi" with_sequence: start={{ [previous_build_number | int - 50, 0] | max }} end={{ [previous_build_number | int - deploy_code.keep, 0] | max }} when: - deploy_code.perms_fix_path is defined - deploy_code.perms_fix_path | length > 1 + - deploy_code.mount_type != "squashfs" -- name: Delete codebases. +- name: Ensure permissions are set on builds directory. + ansible.builtin.shell: + cmd: "if [ -d {{ build_path_prefix }}{{ item }}/{{ deploy_code.perms_fix_path }} ]; then chmod 755 {{ build_path_prefix }}{{ item }}/{{ deploy_code.perms_fix_path }}; fi" + with_sequence: start={{ [previous_build_number | int - 50, 0] | max }} end={{ [previous_build_number | int - deploy_code.keep, 0] | max }} + when: + - deploy_code.perms_fix_path is defined + - deploy_code.perms_fix_path | length > 1 + - deploy_code.mount_type == "squashfs" + +- name: Delete codebases from deploy directory. ansible.builtin.file: name: "{{ deploy_path_prefix }}{{ item }}" state: absent with_sequence: start={{ [previous_build_number | int - 50, 0] | max }} end={{ [previous_build_number | int - deploy_code.keep, 0] | max }} + when: + - deploy_code.mount_type != "squashfs" + +- name: Delete codebases from builds directory. + ansible.builtin.file: + name: "{{ build_path_prefix }}{{ item }}" + state: absent + with_sequence: start={{ [previous_build_number | int - 50, 0] | max }} end={{ [previous_build_number | int - deploy_code.keep, 0] | max }} + when: + - deploy_code.mount_type == "squashfs" - name: Create a tarball of the deployed codebases. ansible.builtin.command: From 6c7d2c97b9e99b8e48902a5603b9f4e63df440a2 Mon Sep 17 00:00:00 2001 From: gregharvey Date: Wed, 15 Jun 2022 19:09:11 +0200 Subject: [PATCH 11/24] Formatting error, these special jinja2 things are not filters - no spaces. --- roles/deploy_code/tasks/cleanup.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/roles/deploy_code/tasks/cleanup.yml b/roles/deploy_code/tasks/cleanup.yml index 85c67c24..e7146438 100644 --- a/roles/deploy_code/tasks/cleanup.yml +++ b/roles/deploy_code/tasks/cleanup.yml @@ -123,7 +123,7 @@ - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" - - _mount_check | success + - _mount_check|success - name: Mount new SquashFS image. ansible.builtin.command: @@ -134,7 +134,7 @@ - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" - - _mount_check | failed + - _mount_check|failed - name: Trigger an infrastructure rebuild. ansible.builtin.include_role: From 5e145d4aabe7ea006dbd63716c6e40f9fbbb7525 Mon Sep 17 00:00:00 2001 From: gregharvey Date: Thu, 16 Jun 2022 10:10:44 +0200 Subject: [PATCH 12/24] Reloading services to make sure mounting doesn't fail. --- roles/deploy_code/defaults/main.yml | 7 ++++++- roles/deploy_code/tasks/cleanup.yml | 21 ++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/roles/deploy_code/defaults/main.yml b/roles/deploy_code/defaults/main.yml index c0e29363..2c496e6b 100644 --- a/roles/deploy_code/defaults/main.yml +++ b/roles/deploy_code/defaults/main.yml @@ -17,13 +17,18 @@ deploy_code: keep: 10 # Whether to sync the local deploy base to a shared destination, after successful build. mount_sync: "" + # mount_sync: "/home/{{ deploy_user }}/shared/{{ project_name }}_{{ build_type }}/deploy" # Type of file to use for sync - 'squashfs' or 'tarball' # @see the _init role for SquashFS build dir paths mount_type: "tarball" - # mount_sync: "/home/{{ deploy_user }}/shared/{{ project_name }}_{{ build_type }}/deploy" # Path that you want to make sure has 755 permissions. Make sure to include the webroot WITHOUT the slash. perms_fix_path: "" # perms_fix_path: "www/sites/default" + # List of services to reload to free the loop device for 'squashfs' builds, post lazy umount. + # @see the squashfs role in ce-provision where special permissions for deploy user to manipulate services get granted. + services: [] + # services: + # - php8.0-fpm # Trigger an API call to rebuild infra after a deploy, e.g. if you need to repack an AMI. rebuild_infra: false # Details of API call to trigger. See api_call role. diff --git a/roles/deploy_code/tasks/cleanup.yml b/roles/deploy_code/tasks/cleanup.yml index e7146438..1f94b1e4 100644 --- a/roles/deploy_code/tasks/cleanup.yml +++ b/roles/deploy_code/tasks/cleanup.yml @@ -114,9 +114,9 @@ - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" -- name: Remount existing SquashFS image. +- name: Unmount existing SquashFS image. ansible.builtin.command: - cmd: "mount {{ build_base_path }}/deploy.sqsh {{ deploy_base_path }} -t squashfs -o remount" + cmd: "umount -l {{ deploy_base_path }}" become: true become_user: "{{ deploy_user }}" when: @@ -125,6 +125,22 @@ - deploy_code.mount_type == "squashfs" - _mount_check|success +- name: Reload any services that might be keeping the loop device busy. + ansible.builtin.service: + name: "{{ ssl_service }}" + state: reloaded + with_items: "{{ deploy_code.services }}" + loop_control: + loop_var: ssl_service + become: true + become_user: "{{ deploy_user }}" + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + - _mount_check|success + - deploy_code.services | length > 0 + - name: Mount new SquashFS image. ansible.builtin.command: cmd: "mount {{ build_base_path }}/deploy.sqsh {{ deploy_base_path }} -t squashfs -o loop" @@ -134,7 +150,6 @@ - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" - - _mount_check|failed - name: Trigger an infrastructure rebuild. ansible.builtin.include_role: From 57ed6e37da6aa6cf054673375780b6cf1c4b9327 Mon Sep 17 00:00:00 2001 From: gregharvey Date: Thu, 16 Jun 2022 10:12:24 +0200 Subject: [PATCH 13/24] Picking more sensible loop var name. --- roles/deploy_code/tasks/cleanup.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/roles/deploy_code/tasks/cleanup.yml b/roles/deploy_code/tasks/cleanup.yml index 1f94b1e4..e2b1cc48 100644 --- a/roles/deploy_code/tasks/cleanup.yml +++ b/roles/deploy_code/tasks/cleanup.yml @@ -127,11 +127,11 @@ - name: Reload any services that might be keeping the loop device busy. ansible.builtin.service: - name: "{{ ssl_service }}" + name: "{{ www_service }}" state: reloaded with_items: "{{ deploy_code.services }}" loop_control: - loop_var: ssl_service + loop_var: www_service become: true become_user: "{{ deploy_user }}" when: From 1b66af207eee92f4178a50c5c895abe09a58687d Mon Sep 17 00:00:00 2001 From: gregharvey Date: Thu, 16 Jun 2022 10:28:37 +0200 Subject: [PATCH 14/24] Moving to shell for mount check and fixing jinja2 filter names. --- roles/deploy_code/tasks/cleanup.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/roles/deploy_code/tasks/cleanup.yml b/roles/deploy_code/tasks/cleanup.yml index e2b1cc48..f901679a 100644 --- a/roles/deploy_code/tasks/cleanup.yml +++ b/roles/deploy_code/tasks/cleanup.yml @@ -105,7 +105,7 @@ - deploy_code.mount_type == "squashfs" - name: Check if we have a mount already. - ansible.builtin.command: + ansible.builtin.shell: cmd: "mount | grep {{ deploy_base_path }}" ignore_errors: true register: _mount_check @@ -123,7 +123,7 @@ - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" - - _mount_check|success + - _mount_check is succeeded - name: Reload any services that might be keeping the loop device busy. ansible.builtin.service: @@ -138,7 +138,7 @@ - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" - - _mount_check|success + - _mount_check is succeeded - deploy_code.services | length > 0 - name: Mount new SquashFS image. From 9db2899239f746eb3e84bde400417e88cddfb96d Mon Sep 17 00:00:00 2001 From: gregharvey Date: Thu, 16 Jun 2022 10:49:41 +0200 Subject: [PATCH 15/24] Trying with the posix mount module instead of command. --- roles/deploy_code/tasks/cleanup.yml | 67 ++++++++++++++++------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/roles/deploy_code/tasks/cleanup.yml b/roles/deploy_code/tasks/cleanup.yml index f901679a..5ed7b4ad 100644 --- a/roles/deploy_code/tasks/cleanup.yml +++ b/roles/deploy_code/tasks/cleanup.yml @@ -104,46 +104,53 @@ - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" -- name: Check if we have a mount already. - ansible.builtin.shell: - cmd: "mount | grep {{ deploy_base_path }}" - ignore_errors: true - register: _mount_check - when: - - deploy_code.mount_sync is defined - - deploy_code.mount_sync | length > 1 - - deploy_code.mount_type == "squashfs" +#- name: Check if we have a mount already. +# ansible.builtin.shell: +# cmd: "mount | grep {{ deploy_base_path }}" +# ignore_errors: true +# register: _mount_check +# when: +# - deploy_code.mount_sync is defined +# - deploy_code.mount_sync | length > 1 +# - deploy_code.mount_type == "squashfs" - name: Unmount existing SquashFS image. - ansible.builtin.command: - cmd: "umount -l {{ deploy_base_path }}" + ansible.posix.mount: + path: "{{ deploy_base_path }}" + state: unmounted become: true become_user: "{{ deploy_user }}" when: - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" - - _mount_check is succeeded - -- name: Reload any services that might be keeping the loop device busy. - ansible.builtin.service: - name: "{{ www_service }}" - state: reloaded - with_items: "{{ deploy_code.services }}" - loop_control: - loop_var: www_service - become: true - become_user: "{{ deploy_user }}" - when: - - deploy_code.mount_sync is defined - - deploy_code.mount_sync | length > 1 - - deploy_code.mount_type == "squashfs" - - _mount_check is succeeded - - deploy_code.services | length > 0 +# - _mount_check is succeeded + +#- name: Reload any services that might be keeping the loop device busy. +# ansible.builtin.service: +# name: "{{ www_service }}" +# state: reloaded +# with_items: "{{ deploy_code.services }}" +# loop_control: +# loop_var: www_service +# become: true +# become_user: "{{ deploy_user }}" +# when: +# - deploy_code.mount_sync is defined +# - deploy_code.mount_sync | length > 1 +# - deploy_code.mount_type == "squashfs" +# - _mount_check is succeeded +# - deploy_code.services | length > 0 - name: Mount new SquashFS image. - ansible.builtin.command: - cmd: "mount {{ build_base_path }}/deploy.sqsh {{ deploy_base_path }} -t squashfs -o loop" + ansible.posix.mount: + #cmd: "mount {{ build_base_path }}/deploy.sqsh {{ deploy_base_path }} -t squashfs -o loop" + src: "{{ build_base_path }}/deploy.sqsh" + path: "{{ deploy_base_path }}" + state: mounted + opts: loop + fstype: squashfs + boot: false become: true become_user: "{{ deploy_user }}" when: From 169a5f705e5058e8a1e6c16a776a42c6aad989c5 Mon Sep 17 00:00:00 2001 From: gregharvey Date: Thu, 16 Jun 2022 12:21:14 +0200 Subject: [PATCH 16/24] Working through user/sudo issues. --- roles/deploy_code/tasks/cleanup.yml | 68 ++++++++++++----------------- 1 file changed, 29 insertions(+), 39 deletions(-) diff --git a/roles/deploy_code/tasks/cleanup.yml b/roles/deploy_code/tasks/cleanup.yml index 5ed7b4ad..c722b049 100644 --- a/roles/deploy_code/tasks/cleanup.yml +++ b/roles/deploy_code/tasks/cleanup.yml @@ -104,55 +104,45 @@ - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" -#- name: Check if we have a mount already. -# ansible.builtin.shell: -# cmd: "mount | grep {{ deploy_base_path }}" -# ignore_errors: true -# register: _mount_check -# when: -# - deploy_code.mount_sync is defined -# - deploy_code.mount_sync | length > 1 -# - deploy_code.mount_type == "squashfs" +- name: Check if we have a mount already. + ansible.builtin.shell: + cmd: "mount | grep {{ deploy_base_path }}" + ignore_errors: true + register: _mount_check + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" - name: Unmount existing SquashFS image. - ansible.posix.mount: - path: "{{ deploy_base_path }}" - state: unmounted + ansible.builtin.command: + cmd: "umount -l {{ deploy_base_path }}" become: true - become_user: "{{ deploy_user }}" when: - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" -# - _mount_check is succeeded - -#- name: Reload any services that might be keeping the loop device busy. -# ansible.builtin.service: -# name: "{{ www_service }}" -# state: reloaded -# with_items: "{{ deploy_code.services }}" -# loop_control: -# loop_var: www_service -# become: true -# become_user: "{{ deploy_user }}" -# when: -# - deploy_code.mount_sync is defined -# - deploy_code.mount_sync | length > 1 -# - deploy_code.mount_type == "squashfs" -# - _mount_check is succeeded -# - deploy_code.services | length > 0 + - _mount_check is succeeded + +- name: Reload any services that might be keeping the loop device busy. + ansible.builtin.service: + name: "{{ www_service }}" + state: reloaded + with_items: "{{ deploy_code.services }}" + loop_control: + loop_var: www_service + become: true + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + - _mount_check is succeeded + - deploy_code.services | length > 0 - name: Mount new SquashFS image. - ansible.posix.mount: - #cmd: "mount {{ build_base_path }}/deploy.sqsh {{ deploy_base_path }} -t squashfs -o loop" - src: "{{ build_base_path }}/deploy.sqsh" - path: "{{ deploy_base_path }}" - state: mounted - opts: loop - fstype: squashfs - boot: false + ansible.builtin.command: + cmd: "mount {{ build_base_path }}/deploy.sqsh {{ deploy_base_path }} -t squashfs -o loop" become: true - become_user: "{{ deploy_user }}" when: - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 From e878aab89c7413a2d9cca95912abcb2ba06dab97 Mon Sep 17 00:00:00 2001 From: gregharvey Date: Thu, 16 Jun 2022 16:06:19 +0200 Subject: [PATCH 17/24] Altering symlink handling slightly so we have the deploy directory always set. --- roles/_init/tasks/main.yml | 12 +++++++----- roles/deploy_code/defaults/main.yml | 1 + roles/live_symlink/tasks/main.yml | 9 ++++++++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/roles/_init/tasks/main.yml b/roles/_init/tasks/main.yml index 44aa2092..b4392b8c 100644 --- a/roles/_init/tasks/main.yml +++ b/roles/_init/tasks/main.yml @@ -26,17 +26,19 @@ - name: Define image builds base path. ansible.builtin.set_fact: build_base_path: "/home/{{ deploy_user }}/builds/{{ project_name }}_{{ build_type }}" + when: deploy_code.mount_type == "squashfs" - name: Define image builds build path prefix. ansible.builtin.set_fact: build_path_prefix: "{{ build_base_path }}/{{ project_name }}_{{ build_type }}_build_" + when: deploy_code.mount_type == "squashfs" +- name: Define live_symlink dest for image builds. + ansible.builtin.set_fact: + live_symlink_build_dest: "{{ live_symlink_build_dest | default('{{ build_base_path }}/live.{{ project_name }}_{{ build_type }}') }}" + when: deploy_code.mount_type == "squashfs" - name: Overwrite deploy and live_symlink paths if SquashFS deploy. ansible.builtin.set_fact: deploy_path: "{{ build_path | default('{{ build_path_prefix }}{{ build_number }}') }}" - live_symlink_dest: "{{ live_symlink_build_dest | default('{{ build_base_path }}/live.{{ project_name }}_{{ build_type }}') }}" - when: - - deploy_code.mount_sync is defined - - deploy_code.mount_sync | length > 1 - - deploy_code.mount_type == "squashfs" + when: deploy_code.mount_type == "squashfs" # Gather last known good build directly from symlink. # This can happen: diff --git a/roles/deploy_code/defaults/main.yml b/roles/deploy_code/defaults/main.yml index 2c496e6b..f1f7cdb4 100644 --- a/roles/deploy_code/defaults/main.yml +++ b/roles/deploy_code/defaults/main.yml @@ -20,6 +20,7 @@ deploy_code: # mount_sync: "/home/{{ deploy_user }}/shared/{{ project_name }}_{{ build_type }}/deploy" # Type of file to use for sync - 'squashfs' or 'tarball' # @see the _init role for SquashFS build dir paths + # @see the squashfs role in ce-provision which installs the special conditions required by the deploy user to use this behaviour mount_type: "tarball" # Path that you want to make sure has 755 permissions. Make sure to include the webroot WITHOUT the slash. perms_fix_path: "" diff --git a/roles/live_symlink/tasks/main.yml b/roles/live_symlink/tasks/main.yml index 0bb1bd70..fa1d9e97 100644 --- a/roles/live_symlink/tasks/main.yml +++ b/roles/live_symlink/tasks/main.yml @@ -6,10 +6,17 @@ _live_symlink_build_target: "{{ deploy_base_path }}/{{ project_name }}_{{ build_type }}_build_{{ previous_build_number }}" when: deploy_operation == 'revert' +- ansible.builtin.set_fact: + _live_symlink_dest_target: "{{ live_symlink_dest }}" + +- ansible.builtin.set_fact: + _live_symlink_dest_target: "{{ live_symlink_build_dest }}" + when: deploy_code.mount_type == "squashfs" + - name: Symlink build. ansible.builtin.file: src: "{{ _live_symlink_build_target }}" - dest: "{{ live_symlink_dest }}" + dest: "{{ _live_symlink_dest_target }}" state: link follow: false force: true From c45db1da3d8940ff5cf1558544d43cadabe60a01 Mon Sep 17 00:00:00 2001 From: gregharvey Date: Thu, 16 Jun 2022 16:08:25 +0200 Subject: [PATCH 18/24] Simplifying 'when' checks. --- roles/_init/tasks/main.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/roles/_init/tasks/main.yml b/roles/_init/tasks/main.yml index b4392b8c..53be27d3 100644 --- a/roles/_init/tasks/main.yml +++ b/roles/_init/tasks/main.yml @@ -80,10 +80,7 @@ ansible.builtin.file: path: "{{ build_base_path }}" state: directory - when: - - deploy_code.mount_sync is defined - - deploy_code.mount_sync | length > 1 - - deploy_code.mount_type == "squashfs" + when: deploy_code.mount_type == "squashfs" # Check for project specific init tasks. - name: Check that {{ project_type }}.yml exists. From b9306522361388ced24206cc3b17a4f8159b8452 Mon Sep 17 00:00:00 2001 From: gregharvey Date: Thu, 23 Jun 2022 18:02:03 +0200 Subject: [PATCH 19/24] Adding a revert behaviour for SquashFS. --- roles/deploy_code/tasks/cleanup.yml | 42 +++++++++++++------ roles/deploy_code/tasks/revert.yml | 64 ++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 14 deletions(-) diff --git a/roles/deploy_code/tasks/cleanup.yml b/roles/deploy_code/tasks/cleanup.yml index c722b049..c38ef57f 100644 --- a/roles/deploy_code/tasks/cleanup.yml +++ b/roles/deploy_code/tasks/cleanup.yml @@ -49,16 +49,6 @@ - deploy_code.mount_type == "tarball" run_once: true -- name: Ensure older SquashFS images are deleted. - ansible.builtin.file: - path: "{{ build_base_path }}/deploy.sqsh" - state: absent - when: - - deploy_code.mount_sync is defined - - deploy_code.mount_sync | length > 1 - - deploy_code.mount_type == "squashfs" - run_once: true - - name: Create a SquashFS image of the deployed codebases. ansible.builtin.command: cmd: "mksquashfs {{ build_base_path }} /tmp/{{ project_name }}_{{ build_type }}.sqsh" @@ -96,6 +86,32 @@ - deploy_code.mount_type == "squashfs" run_once: true +- name: Check if we have a SquashFS image already there. + ansible.builtin.stat: + path: "{{ build_base_path }}/deploy.sqsh" + register: _deploy_code_mount_image + +- name: Copy previous SquashFS image in case of rollback. + ansible.builtin.copy: + remote_src: true + force: true + src: "{{ build_base_path }}/deploy.sqsh" + dest: "{{ build_base_path }}/deploy_previous.sqsh" + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + - _deploy_code_mount_image.stat.islnk is defined + +- name: Ensure mounted SquashFS image is deleted. + ansible.builtin.file: + path: "{{ build_base_path }}/deploy.sqsh" + state: absent + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + - name: Copy SquashFS image to local server. ansible.builtin.command: cmd: "cp {{ deploy_code.mount_sync }}/{{ project_name }}_{{ build_type }}.sqsh {{ build_base_path }}/deploy.sqsh" @@ -108,7 +124,7 @@ ansible.builtin.shell: cmd: "mount | grep {{ deploy_base_path }}" ignore_errors: true - register: _mount_check + register: _deploy_code_mount_check when: - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 @@ -122,7 +138,7 @@ - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" - - _mount_check is succeeded + - _deploy_code_mount_check is succeeded - name: Reload any services that might be keeping the loop device busy. ansible.builtin.service: @@ -136,7 +152,7 @@ - deploy_code.mount_sync is defined - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" - - _mount_check is succeeded + - _deploy_code_mount_check is succeeded - deploy_code.services | length > 0 - name: Mount new SquashFS image. diff --git a/roles/deploy_code/tasks/revert.yml b/roles/deploy_code/tasks/revert.yml index 03c03856..bc913450 100644 --- a/roles/deploy_code/tasks/revert.yml +++ b/roles/deploy_code/tasks/revert.yml @@ -1 +1,63 @@ -# Nothing to do. \ No newline at end of file +- name: Check if we have a SquashFS image from a previous successful build. + ansible.builtin.stat: + path: "{{ build_base_path }}/deploy_previous.sqsh" + register: _deploy_code_revert_image + +- name: Move previous SquashFS image back to be re-mounted. + ansible.builtin.copy: + remote_src: true + force: true + src: "{{ build_base_path }}/deploy_previous.sqsh" + dest: "{{ build_base_path }}/deploy.sqsh" + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + - _deploy_code_revert_image.stat.islnk is defined + +- name: Check if we have a mount already. + ansible.builtin.shell: + cmd: "mount | grep {{ deploy_base_path }}" + ignore_errors: true + register: _deploy_code_mount_check + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + +- name: Unmount existing SquashFS image. + ansible.builtin.command: + cmd: "umount -l {{ deploy_base_path }}" + become: true + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + - _deploy_code_mount_check is succeeded + - _deploy_code_revert_image.stat.islnk is defined + +- name: Reload any services that might be keeping the loop device busy. + ansible.builtin.service: + name: "{{ www_service }}" + state: reloaded + with_items: "{{ deploy_code.services }}" + loop_control: + loop_var: www_service + become: true + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + - _deploy_code_mount_check is succeeded + - deploy_code.services | length > 0 + - _deploy_code_revert_image.stat.islnk is defined + +- name: Mount new SquashFS image. + ansible.builtin.command: + cmd: "mount {{ build_base_path }}/deploy.sqsh {{ deploy_base_path }} -t squashfs -o loop" + become: true + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + - _deploy_code_revert_image.stat.islnk is defined From 4f7aa2584be2ef9f19b13c0f9cf88488796f4a94 Mon Sep 17 00:00:00 2001 From: gregharvey Date: Thu, 23 Jun 2022 18:23:44 +0200 Subject: [PATCH 20/24] Making some wording a little less ambiguous. --- roles/deploy_code/tasks/revert.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/roles/deploy_code/tasks/revert.yml b/roles/deploy_code/tasks/revert.yml index bc913450..7256e766 100644 --- a/roles/deploy_code/tasks/revert.yml +++ b/roles/deploy_code/tasks/revert.yml @@ -25,7 +25,7 @@ - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" -- name: Unmount existing SquashFS image. +- name: Unmount existing and presumed bad SquashFS image. ansible.builtin.command: cmd: "umount -l {{ deploy_base_path }}" become: true @@ -52,7 +52,7 @@ - deploy_code.services | length > 0 - _deploy_code_revert_image.stat.islnk is defined -- name: Mount new SquashFS image. +- name: Mount SquashFS image from previous successful build. ansible.builtin.command: cmd: "mount {{ build_base_path }}/deploy.sqsh {{ deploy_base_path }} -t squashfs -o loop" become: true From 7be01a2a4b7a5fdcf9d3c6647f463bb9ca2fee26 Mon Sep 17 00:00:00 2001 From: gregharvey Date: Wed, 29 Jun 2022 09:46:54 +0200 Subject: [PATCH 21/24] Fixing bad image path and adding clauses to stat check. --- roles/deploy_code/tasks/revert.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/roles/deploy_code/tasks/revert.yml b/roles/deploy_code/tasks/revert.yml index 7256e766..21743cb1 100644 --- a/roles/deploy_code/tasks/revert.yml +++ b/roles/deploy_code/tasks/revert.yml @@ -2,6 +2,10 @@ ansible.builtin.stat: path: "{{ build_base_path }}/deploy_previous.sqsh" register: _deploy_code_revert_image + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" - name: Move previous SquashFS image back to be re-mounted. ansible.builtin.copy: @@ -54,7 +58,7 @@ - name: Mount SquashFS image from previous successful build. ansible.builtin.command: - cmd: "mount {{ build_base_path }}/deploy.sqsh {{ deploy_base_path }} -t squashfs -o loop" + cmd: "mount {{ _deploy_code_revert_image }} {{ deploy_base_path }} -t squashfs -o loop" become: true when: - deploy_code.mount_sync is defined From 1f12a90f6d5b58e8aee130989b08f61d44dc227f Mon Sep 17 00:00:00 2001 From: gregharvey Date: Wed, 29 Jun 2022 12:31:42 +0200 Subject: [PATCH 22/24] Trying to make remounting SquashFS images a bit safer. --- roles/deploy_code/tasks/cleanup.yml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/roles/deploy_code/tasks/cleanup.yml b/roles/deploy_code/tasks/cleanup.yml index c38ef57f..02076f2d 100644 --- a/roles/deploy_code/tasks/cleanup.yml +++ b/roles/deploy_code/tasks/cleanup.yml @@ -130,16 +130,6 @@ - deploy_code.mount_sync | length > 1 - deploy_code.mount_type == "squashfs" -- name: Unmount existing SquashFS image. - ansible.builtin.command: - cmd: "umount -l {{ deploy_base_path }}" - become: true - when: - - deploy_code.mount_sync is defined - - deploy_code.mount_sync | length > 1 - - deploy_code.mount_type == "squashfs" - - _deploy_code_mount_check is succeeded - - name: Reload any services that might be keeping the loop device busy. ansible.builtin.service: name: "{{ www_service }}" @@ -155,6 +145,16 @@ - _deploy_code_mount_check is succeeded - deploy_code.services | length > 0 +- name: Unmount existing SquashFS image. + ansible.builtin.command: + cmd: "umount {{ deploy_base_path }}" + become: true + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" + - _deploy_code_mount_check is succeeded + - name: Mount new SquashFS image. ansible.builtin.command: cmd: "mount {{ build_base_path }}/deploy.sqsh {{ deploy_base_path }} -t squashfs -o loop" From edf02be7c532e3cb0ecc7474a1db71f129ba2e65 Mon Sep 17 00:00:00 2001 From: gregharvey Date: Wed, 29 Jun 2022 12:33:39 +0200 Subject: [PATCH 23/24] Adding when clauses to SquashFS image path check. --- roles/deploy_code/tasks/cleanup.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/roles/deploy_code/tasks/cleanup.yml b/roles/deploy_code/tasks/cleanup.yml index 02076f2d..e1412749 100644 --- a/roles/deploy_code/tasks/cleanup.yml +++ b/roles/deploy_code/tasks/cleanup.yml @@ -90,6 +90,10 @@ ansible.builtin.stat: path: "{{ build_base_path }}/deploy.sqsh" register: _deploy_code_mount_image + when: + - deploy_code.mount_sync is defined + - deploy_code.mount_sync | length > 1 + - deploy_code.mount_type == "squashfs" - name: Copy previous SquashFS image in case of rollback. ansible.builtin.copy: From abe7fe43c07ebe88eecdebd522628519cd2aed56 Mon Sep 17 00:00:00 2001 From: gregharvey Date: Wed, 29 Jun 2022 12:38:09 +0200 Subject: [PATCH 24/24] Removing revert code, as it cannot work. --- roles/deploy_code/tasks/revert.yml | 68 +----------------------------- 1 file changed, 1 insertion(+), 67 deletions(-) diff --git a/roles/deploy_code/tasks/revert.yml b/roles/deploy_code/tasks/revert.yml index 21743cb1..03c03856 100644 --- a/roles/deploy_code/tasks/revert.yml +++ b/roles/deploy_code/tasks/revert.yml @@ -1,67 +1 @@ -- name: Check if we have a SquashFS image from a previous successful build. - ansible.builtin.stat: - path: "{{ build_base_path }}/deploy_previous.sqsh" - register: _deploy_code_revert_image - when: - - deploy_code.mount_sync is defined - - deploy_code.mount_sync | length > 1 - - deploy_code.mount_type == "squashfs" - -- name: Move previous SquashFS image back to be re-mounted. - ansible.builtin.copy: - remote_src: true - force: true - src: "{{ build_base_path }}/deploy_previous.sqsh" - dest: "{{ build_base_path }}/deploy.sqsh" - when: - - deploy_code.mount_sync is defined - - deploy_code.mount_sync | length > 1 - - deploy_code.mount_type == "squashfs" - - _deploy_code_revert_image.stat.islnk is defined - -- name: Check if we have a mount already. - ansible.builtin.shell: - cmd: "mount | grep {{ deploy_base_path }}" - ignore_errors: true - register: _deploy_code_mount_check - when: - - deploy_code.mount_sync is defined - - deploy_code.mount_sync | length > 1 - - deploy_code.mount_type == "squashfs" - -- name: Unmount existing and presumed bad SquashFS image. - ansible.builtin.command: - cmd: "umount -l {{ deploy_base_path }}" - become: true - when: - - deploy_code.mount_sync is defined - - deploy_code.mount_sync | length > 1 - - deploy_code.mount_type == "squashfs" - - _deploy_code_mount_check is succeeded - - _deploy_code_revert_image.stat.islnk is defined - -- name: Reload any services that might be keeping the loop device busy. - ansible.builtin.service: - name: "{{ www_service }}" - state: reloaded - with_items: "{{ deploy_code.services }}" - loop_control: - loop_var: www_service - become: true - when: - - deploy_code.mount_sync is defined - - deploy_code.mount_sync | length > 1 - - deploy_code.mount_type == "squashfs" - - _deploy_code_mount_check is succeeded - - deploy_code.services | length > 0 - - _deploy_code_revert_image.stat.islnk is defined - -- name: Mount SquashFS image from previous successful build. - ansible.builtin.command: - cmd: "mount {{ _deploy_code_revert_image }} {{ deploy_base_path }} -t squashfs -o loop" - become: true - when: - - deploy_code.mount_sync is defined - - deploy_code.mount_sync | length > 1 - - deploy_code.mount_type == "squashfs" - - _deploy_code_revert_image.stat.islnk is defined +# Nothing to do. \ No newline at end of file