From 29fb166457de8e2d1f93c8e574eca0018357ed0e Mon Sep 17 00:00:00 2001 From: AdamHebby Date: Sun, 28 Feb 2021 01:17:50 +0000 Subject: [PATCH 01/12] Changes glimesh-web role to symlink deploys with states --- ansible/roles/glimesh-web/tasks/main.yml | 80 +++++++++++++++++------- 1 file changed, 56 insertions(+), 24 deletions(-) diff --git a/ansible/roles/glimesh-web/tasks/main.yml b/ansible/roles/glimesh-web/tasks/main.yml index ea25699..4359195 100644 --- a/ansible/roles/glimesh-web/tasks/main.yml +++ b/ansible/roles/glimesh-web/tasks/main.yml @@ -5,15 +5,15 @@ name: "{{ inventory_hostname }}" - name: Ensure .ssh directory exists. - file: + file: dest: "/root/.ssh" - mode: 0700 - owner: root + mode: 0700 + owner: root state: directory - name: Install ssh key - copy: - src: "glimesh-deploy" + copy: + src: "glimesh-deploy" dest: "/root/.ssh/id_rsa" mode: 0600 owner: root @@ -27,12 +27,12 @@ # - name: install required packages # apt: -# pkg: -# - esl-erlang -# - elixir -# - npm +# pkg: +# - esl-erlang +# - elixir +# - npm # - git -# - openssl +# - openssl # - imagemagick # state: present # update_cache: yes @@ -43,8 +43,10 @@ - name: install rebar command: mix local.rebar --force -- name: ensure /tmp/glimesh.tv exists - file: path=/tmp/glimesh.tv state=directory +- name: ensure web_build_dir exists + file: + path: /tmp/glimesh.tv + state: directory - name: download glimesh.tv git: @@ -62,14 +64,14 @@ chdir: /tmp/glimesh.tv register: glimesh_git_hash -- name: change version +- name: change version lineinfile: path: /tmp/glimesh.tv/mix.exs regexp: '^\s+version: .*$' line: "version: \"0.1.0+{{ glimesh_git_hash.stdout }}\"," - name: install mix dependencies - command: mix do deps.get, deps.compile + command: mix do deps.get, deps.compile args: chdir: /tmp/glimesh.tv environment: @@ -95,7 +97,7 @@ # when: glimesh.changed - name: build glimesh.tv - command: mix do compile --force, release --force --overwrite + command: mix do compile --force, release --force --overwrite args: chdir: /tmp/glimesh.tv environment: @@ -103,7 +105,10 @@ # when: glimesh.changed - name: setup unit file for glimesh.tv - template: src=glimesh.service.j2 dest=/etc/systemd/system/glimesh.service mode=644 + template: + src: glimesh.service.j2 + dest: /etc/systemd/system/glimesh.service + mode: 644 tags: config - name: reload systemd immediately @@ -111,22 +116,49 @@ daemon_reload: yes - name: gracefully shut down old glimesh app - systemd: + systemd: name: glimesh state: stopped -# Copying the file will automatically restart the daemon. -- name: copy built glimesh.tv - command: cp -Rf /tmp/glimesh.tv/_build/prod /opt/glimesh.tv +- name: Ensure deploy state directories exist + file: + path: /opt/glimesh.tv-{{ item }} + state: directory + owner: nobody + recurse: yes + with_items: + - "old" + - "new" + +- name: Copy currently live files to old directory + synchronize: + src: /opt/glimesh.tv + dest: /opt/glimesh.tv-old + links: yes + +# Shouldn't we rsync? +- name: Copy newly built files to new store + synchronize: + src: /tmp/glimesh.tv/_build/prod + dest: /opt/glimesh.tv-new + links: yes - name: permissions file: - path: /opt/glimesh.tv + path: /opt/glimesh.tv-new owner: nobody recurse: yes +- name: Change the symlink + file: + src: /opt/glimesh.tv-new + dest: /opt/glimesh.tv + state: link + owner: nobody + force: yes + - name: start new glimesh app - systemd: + systemd: name: glimesh state: started @@ -136,5 +168,5 @@ delay: 5 - name: pause to make sure the other node can take over - pause: - seconds: 15 \ No newline at end of file + pause: + seconds: 15 From f494e04f25ca7ce51e6ebf27be327271fa3f2148 Mon Sep 17 00:00:00 2001 From: AdamHebby Date: Sun, 28 Feb 2021 01:20:14 +0000 Subject: [PATCH 02/12] Moves Glimesh shutdown to last possible place --- ansible/roles/glimesh-web/tasks/main.yml | 32 ++++++++++++------------ 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/ansible/roles/glimesh-web/tasks/main.yml b/ansible/roles/glimesh-web/tasks/main.yml index 4359195..b3fb67f 100644 --- a/ansible/roles/glimesh-web/tasks/main.yml +++ b/ansible/roles/glimesh-web/tasks/main.yml @@ -104,22 +104,6 @@ MIX_ENV: prod # when: glimesh.changed -- name: setup unit file for glimesh.tv - template: - src: glimesh.service.j2 - dest: /etc/systemd/system/glimesh.service - mode: 644 - tags: config - -- name: reload systemd immediately - systemd: - daemon_reload: yes - -- name: gracefully shut down old glimesh app - systemd: - name: glimesh - state: stopped - - name: Ensure deploy state directories exist file: path: /opt/glimesh.tv-{{ item }} @@ -149,6 +133,22 @@ owner: nobody recurse: yes +- name: setup unit file for glimesh.tv + template: + src: glimesh.service.j2 + dest: /etc/systemd/system/glimesh.service + mode: 644 + tags: config + +- name: reload systemd immediately + systemd: + daemon_reload: yes + +- name: gracefully shut down old glimesh app + systemd: + name: glimesh + state: stopped + - name: Change the symlink file: src: /opt/glimesh.tv-new From 90bfcbaf62dbb9fd617a0bbc28d98e1714360329 Mon Sep 17 00:00:00 2001 From: AdamHebby Date: Sun, 28 Feb 2021 01:21:16 +0000 Subject: [PATCH 03/12] Removes redundant comment in glimesh-web --- ansible/roles/glimesh-web/tasks/main.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/ansible/roles/glimesh-web/tasks/main.yml b/ansible/roles/glimesh-web/tasks/main.yml index b3fb67f..ec77c39 100644 --- a/ansible/roles/glimesh-web/tasks/main.yml +++ b/ansible/roles/glimesh-web/tasks/main.yml @@ -120,7 +120,6 @@ dest: /opt/glimesh.tv-old links: yes -# Shouldn't we rsync? - name: Copy newly built files to new store synchronize: src: /tmp/glimesh.tv/_build/prod From 411261f8d4df876c2949a62a50d6d2731da0f645 Mon Sep 17 00:00:00 2001 From: AdamHebby Date: Sun, 28 Feb 2021 01:24:23 +0000 Subject: [PATCH 04/12] Adds quick Ansible script to rollback Glimesh to prev version --- ansible/web-rollback.yml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ansible/web-rollback.yml diff --git a/ansible/web-rollback.yml b/ansible/web-rollback.yml new file mode 100644 index 0000000..061b474 --- /dev/null +++ b/ansible/web-rollback.yml @@ -0,0 +1,27 @@ +- name: web + hosts: web + serial: 1 + + tasks: + - name: Change the symlink + file: + src: /opt/glimesh.tv-old + dest: /opt/glimesh.tv + state: link + owner: nobody + force: yes + + - name: restarted glimesh + systemd: + name: glimesh + daemon_reload: yes + state: restarted + + - name: wait for service to be online + wait_for: + port: 8080 + delay: 5 + + - name: pause to make sure the other node can take over + pause: + seconds: 15 From 06a69658410bc41f6573656324034ea46c5d277f Mon Sep 17 00:00:00 2001 From: AdamHebby Date: Sun, 28 Feb 2021 01:29:07 +0000 Subject: [PATCH 05/12] Forgot to quote Ansible variable --- ansible/roles/glimesh-web/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ansible/roles/glimesh-web/tasks/main.yml b/ansible/roles/glimesh-web/tasks/main.yml index ec77c39..1e7c22c 100644 --- a/ansible/roles/glimesh-web/tasks/main.yml +++ b/ansible/roles/glimesh-web/tasks/main.yml @@ -106,7 +106,7 @@ - name: Ensure deploy state directories exist file: - path: /opt/glimesh.tv-{{ item }} + path: "/opt/glimesh.tv-{{ item }}" state: directory owner: nobody recurse: yes From 644f74f95bee26ecaa7c6551662f0e41392335de Mon Sep 17 00:00:00 2001 From: AdamHebby Date: Sun, 28 Feb 2021 17:26:59 +0000 Subject: [PATCH 06/12] Adds delegation & deletion to synchronize symlink deploys --- ansible/roles/glimesh-web/tasks/main.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ansible/roles/glimesh-web/tasks/main.yml b/ansible/roles/glimesh-web/tasks/main.yml index 1e7c22c..97f5e21 100644 --- a/ansible/roles/glimesh-web/tasks/main.yml +++ b/ansible/roles/glimesh-web/tasks/main.yml @@ -119,12 +119,16 @@ src: /opt/glimesh.tv dest: /opt/glimesh.tv-old links: yes + delete: yes + delegate_to: "{{ inventory_hostname }}" - name: Copy newly built files to new store synchronize: src: /tmp/glimesh.tv/_build/prod dest: /opt/glimesh.tv-new links: yes + delete: yes + delegate_to: "{{ inventory_hostname }}" - name: permissions file: From 1ff4649d4d8c2b7a0434e88cd87e4e959135df10 Mon Sep 17 00:00:00 2001 From: AdamHebby Date: Sun, 28 Feb 2021 17:31:51 +0000 Subject: [PATCH 07/12] Adds checking /opt/glimesh.tv dir exists --- ansible/roles/glimesh-web/tasks/main.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ansible/roles/glimesh-web/tasks/main.yml b/ansible/roles/glimesh-web/tasks/main.yml index 97f5e21..2c58707 100644 --- a/ansible/roles/glimesh-web/tasks/main.yml +++ b/ansible/roles/glimesh-web/tasks/main.yml @@ -43,7 +43,7 @@ - name: install rebar command: mix local.rebar --force -- name: ensure web_build_dir exists +- name: ensure tmp build dir exists file: path: /tmp/glimesh.tv state: directory @@ -104,6 +104,13 @@ MIX_ENV: prod # when: glimesh.changed +- name: ensure opt host exists + file: + path: /opt/glimesh.tv + state: directory + owner: nobody + mode: 0755 + - name: Ensure deploy state directories exist file: path: "/opt/glimesh.tv-{{ item }}" From bf0896c46789afa3351a5214f54bc1be5ebc6dfb Mon Sep 17 00:00:00 2001 From: AdamHebby Date: Mon, 1 Mar 2021 17:51:04 +0000 Subject: [PATCH 08/12] Changes deploys to use git hash --- ansible/roles/glimesh-web/tasks/main.yml | 90 +++++++++++++++++------- ansible/web-rollback.yml | 11 ++- 2 files changed, 73 insertions(+), 28 deletions(-) diff --git a/ansible/roles/glimesh-web/tasks/main.yml b/ansible/roles/glimesh-web/tasks/main.yml index 2c58707..51c447d 100644 --- a/ansible/roles/glimesh-web/tasks/main.yml +++ b/ansible/roles/glimesh-web/tasks/main.yml @@ -70,6 +70,29 @@ regexp: '^\s+version: .*$' line: "version: \"0.1.0+{{ glimesh_git_hash.stdout }}\"," +- set_fact: + deploy_dest: "/opt/glimesh.tv-{{ glimesh_git_hash.stdout }}" + +## Check we should deploy + +- name: Get currently deployed hash + stat: + path: /opt/glimesh.tv + follow: yes + get_checksum: no + register: current_deploy + +- name: Same version deployed? + pause: + prompt: | + WARNING: Latest version is already deployed. + Pess Enter to continue + delegate_to: localhost + when: current_deploy.stat.lnk_target == deploy_dest + register: same_deploy + +## Build the deploy + - name: install mix dependencies command: mix do deps.get, deps.compile args: @@ -104,45 +127,29 @@ MIX_ENV: prod # when: glimesh.changed -- name: ensure opt host exists +- name: Ensure new directory exists file: - path: /opt/glimesh.tv - state: directory - owner: nobody - mode: 0755 - -- name: Ensure deploy state directories exist - file: - path: "/opt/glimesh.tv-{{ item }}" + path: "{{ deploy_dest }}" state: directory owner: nobody recurse: yes - with_items: - - "old" - - "new" -- name: Copy currently live files to old directory +- name: Copy newly built files to new host synchronize: - src: /opt/glimesh.tv - dest: /opt/glimesh.tv-old + src: /tmp/glimesh.tv + dest: "{{ deploy_dest }}" links: yes delete: yes delegate_to: "{{ inventory_hostname }}" -- name: Copy newly built files to new store - synchronize: - src: /tmp/glimesh.tv/_build/prod - dest: /opt/glimesh.tv-new - links: yes - delete: yes - delegate_to: "{{ inventory_hostname }}" - -- name: permissions +- name: Check permissions file: - path: /opt/glimesh.tv-new + path: "{{ deploy_dest }}" owner: nobody recurse: yes +## Change the service + - name: setup unit file for glimesh.tv template: src: glimesh.service.j2 @@ -161,7 +168,7 @@ - name: Change the symlink file: - src: /opt/glimesh.tv-new + src: "{{ deploy_dest }}" dest: /opt/glimesh.tv state: link owner: nobody @@ -180,3 +187,34 @@ - name: pause to make sure the other node can take over pause: seconds: 15 + +- name: Cleanup old directories + block: + - name: Add a symlink to the old deploy for ease of rollback + file: + src: "{{ current_deploy.stat.lnk_target }}" + dest: /opt/glimesh.tv-old + state: link + owner: nobody + force: yes + + ## Cleanup + - name: Find all deploy folders + find: + paths: /opt + file_type: directory + use_regex: yes + patterns: "^glimesh.tv-([a-zA-Z0-9]+)$" + recurse: no + register: all_deploys + + - name: Delete old directories + file: + path: "{{ item }}" + state: absent + when: item != current_deploy.stat.lnk_target and item != deploy_dest + with_items: "{{ all_deploys.files | map(attribute='path') | list }}" + + when: same_deploy is not defined + tags: + - cleanup diff --git a/ansible/web-rollback.yml b/ansible/web-rollback.yml index 061b474..f91d303 100644 --- a/ansible/web-rollback.yml +++ b/ansible/web-rollback.yml @@ -3,15 +3,22 @@ serial: 1 tasks: + - name: Get old deployed hash + stat: + path: /opt/glimesh.tv-old + follow: yes + get_checksum: no + register: old_deploy + - name: Change the symlink file: - src: /opt/glimesh.tv-old + src: "{{ old_deploy.stat.lnk_target }}" dest: /opt/glimesh.tv state: link owner: nobody force: yes - - name: restarted glimesh + - name: restart glimesh systemd: name: glimesh daemon_reload: yes From 71e5b69e983b29d21207a483d29b797a2b250e9e Mon Sep 17 00:00:00 2001 From: AdamHebby Date: Mon, 1 Mar 2021 19:54:13 +0000 Subject: [PATCH 09/12] Fixed link target by disabling follow --- ansible/roles/glimesh-web/tasks/main.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ansible/roles/glimesh-web/tasks/main.yml b/ansible/roles/glimesh-web/tasks/main.yml index 51c447d..c77f22f 100644 --- a/ansible/roles/glimesh-web/tasks/main.yml +++ b/ansible/roles/glimesh-web/tasks/main.yml @@ -78,10 +78,14 @@ - name: Get currently deployed hash stat: path: /opt/glimesh.tv - follow: yes + follow: no get_checksum: no register: current_deploy +- name: Debug + debug: + var: current_deploy + - name: Same version deployed? pause: prompt: | From 25720aa6181ea84b44d65d0a17c87cd6505bf5d2 Mon Sep 17 00:00:00 2001 From: AdamHebby Date: Mon, 1 Mar 2021 23:15:34 +0000 Subject: [PATCH 10/12] Fixing missed prod build path to synchronize --- ansible/roles/glimesh-web/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ansible/roles/glimesh-web/tasks/main.yml b/ansible/roles/glimesh-web/tasks/main.yml index c77f22f..405b2c3 100644 --- a/ansible/roles/glimesh-web/tasks/main.yml +++ b/ansible/roles/glimesh-web/tasks/main.yml @@ -140,7 +140,7 @@ - name: Copy newly built files to new host synchronize: - src: /tmp/glimesh.tv + src: /tmp/glimesh.tv/_build/prod dest: "{{ deploy_dest }}" links: yes delete: yes From e2812d92e455fe29b30989044cb364e607d24051 Mon Sep 17 00:00:00 2001 From: AdamHebby Date: Wed, 17 Mar 2021 01:12:59 +0000 Subject: [PATCH 11/12] Fixed bad when indent on delete old dirs --- ansible/roles/glimesh-web/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ansible/roles/glimesh-web/tasks/main.yml b/ansible/roles/glimesh-web/tasks/main.yml index 405b2c3..6bf0f02 100644 --- a/ansible/roles/glimesh-web/tasks/main.yml +++ b/ansible/roles/glimesh-web/tasks/main.yml @@ -216,7 +216,7 @@ file: path: "{{ item }}" state: absent - when: item != current_deploy.stat.lnk_target and item != deploy_dest + when: item != current_deploy.stat.lnk_target and item != deploy_dest with_items: "{{ all_deploys.files | map(attribute='path') | list }}" when: same_deploy is not defined From 2ba7703ef749436fb5a38db7dca31da3d41a5b84 Mon Sep 17 00:00:00 2001 From: AdamHebby Date: Mon, 5 Apr 2021 17:09:50 +0100 Subject: [PATCH 12/12] Maybe fixes directory deleting - fully tested in docker --- ansible/roles/glimesh-web/tasks/main.yml | 45 +++++++++++++++--------- ansible/web-rollback.yml | 2 +- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/ansible/roles/glimesh-web/tasks/main.yml b/ansible/roles/glimesh-web/tasks/main.yml index 6bf0f02..7f5b0a4 100644 --- a/ansible/roles/glimesh-web/tasks/main.yml +++ b/ansible/roles/glimesh-web/tasks/main.yml @@ -82,17 +82,13 @@ get_checksum: no register: current_deploy -- name: Debug - debug: - var: current_deploy - - name: Same version deployed? pause: prompt: | WARNING: Latest version is already deployed. Pess Enter to continue delegate_to: localhost - when: current_deploy.stat.lnk_target == deploy_dest + when: current_deploy.stat.exists and current_deploy.stat.lnk_target == deploy_dest register: same_deploy ## Build the deploy @@ -103,7 +99,6 @@ chdir: /tmp/glimesh.tv environment: MIX_ENV: prod - # when: glimesh.changed - name: install npm asset deps command: npm --prefix ./assets ci --progress=false --no-audit --loglevel=error @@ -192,17 +187,31 @@ pause: seconds: 15 +- name: Add a symlink to the old deploy for ease of rollback + file: + src: "{{ current_deploy.stat.lnk_target }}" + dest: /opt/glimesh.tv-old + state: link + owner: nobody + force: yes + - name: Cleanup old directories block: - - name: Add a symlink to the old deploy for ease of rollback - file: - src: "{{ current_deploy.stat.lnk_target }}" - dest: /opt/glimesh.tv-old - state: link - owner: nobody - force: yes - - ## Cleanup + - name: Get currently deployed dest + stat: + path: /opt/glimesh.tv + follow: no + get_checksum: no + register: gtv_current + + - name: Get old deployed dest + stat: + path: /opt/glimesh.tv-old + follow: no + get_checksum: no + register: gtv_old + + # Cleanup - name: Find all deploy folders find: paths: /opt @@ -216,9 +225,11 @@ file: path: "{{ item }}" state: absent - when: item != current_deploy.stat.lnk_target and item != deploy_dest + when: + - item != '/opt/glimesh.tv-old' + - gtv_current.stat.exists == False or item != gtv_current.stat.lnk_target + - gtv_old.stat.exists == False or item != gtv_old.stat.lnk_target with_items: "{{ all_deploys.files | map(attribute='path') | list }}" - when: same_deploy is not defined tags: - cleanup diff --git a/ansible/web-rollback.yml b/ansible/web-rollback.yml index f91d303..3d4d9bd 100644 --- a/ansible/web-rollback.yml +++ b/ansible/web-rollback.yml @@ -6,7 +6,7 @@ - name: Get old deployed hash stat: path: /opt/glimesh.tv-old - follow: yes + follow: no get_checksum: no register: old_deploy