From 29fb166457de8e2d1f93c8e574eca0018357ed0e Mon Sep 17 00:00:00 2001 From: AdamHebby Date: Sun, 28 Feb 2021 01:17:50 +0000 Subject: [PATCH 1/6] 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 2/6] 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 3/6] 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 4/6] 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 5/6] 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 f65000e1fc5ab59b1a8222a847bfdd4a9ee735e6 Mon Sep 17 00:00:00 2001 From: AdamHebby Date: Sun, 28 Feb 2021 01:34:43 +0000 Subject: [PATCH 6/6] Ensuring glimesh-web build & store dirs are in vars --- ansible/roles/glimesh-web/defaults/main.yml | 5 ++- ansible/roles/glimesh-web/tasks/main.yml | 34 ++++++++++----------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/ansible/roles/glimesh-web/defaults/main.yml b/ansible/roles/glimesh-web/defaults/main.yml index a8d8b16..13b4b53 100644 --- a/ansible/roles/glimesh-web/defaults/main.yml +++ b/ansible/roles/glimesh-web/defaults/main.yml @@ -39,4 +39,7 @@ glimesh_waffle_asset_host: some_value glimesh_twitter_consumer_key: some_value glimesh_twitter_consumer_secret: some_value glimesh_twitter_access_token: some_value -glimesh_twitter_access_secret: some_value \ No newline at end of file +glimesh_twitter_access_secret: some_value + +glimesh_web_build_dir: /tmp/glimesh.tv +glimesh_web_host_dir: /opt/glimesh.tv diff --git a/ansible/roles/glimesh-web/tasks/main.yml b/ansible/roles/glimesh-web/tasks/main.yml index 1e7c22c..cc6c41b 100644 --- a/ansible/roles/glimesh-web/tasks/main.yml +++ b/ansible/roles/glimesh-web/tasks/main.yml @@ -45,13 +45,13 @@ - name: ensure web_build_dir exists file: - path: /tmp/glimesh.tv + path: "{{ glimesh_web_build_dir }}" state: directory - name: download glimesh.tv git: repo: https://github.com/Glimesh/glimesh.tv.git - dest: /tmp/glimesh.tv + dest: "{{ glimesh_web_build_dir }}" recursive: true update: true force: true @@ -61,19 +61,19 @@ - name: register short git hash version command: git rev-parse --short HEAD args: - chdir: /tmp/glimesh.tv + chdir: "{{ glimesh_web_build_dir }}" register: glimesh_git_hash - name: change version lineinfile: - path: /tmp/glimesh.tv/mix.exs + path: "{{ glimesh_web_build_dir }}/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 args: - chdir: /tmp/glimesh.tv + chdir: "{{ glimesh_web_build_dir }}" environment: MIX_ENV: prod # when: glimesh.changed @@ -81,32 +81,32 @@ - name: install npm asset deps command: npm --prefix ./assets ci --progress=false --no-audit --loglevel=error args: - chdir: /tmp/glimesh.tv + chdir: "{{ glimesh_web_build_dir }}" # when: glimesh.changed - name: build static assets command: npm run --prefix ./assets deploy args: - chdir: /tmp/glimesh.tv + chdir: "{{ glimesh_web_build_dir }}" # when: glimesh.changed - name: digest static assets command: mix phx.digest args: - chdir: /tmp/glimesh.tv + chdir: "{{ glimesh_web_build_dir }}" # when: glimesh.changed - name: build glimesh.tv command: mix do compile --force, release --force --overwrite args: - chdir: /tmp/glimesh.tv + chdir: "{{ glimesh_web_build_dir }}" environment: MIX_ENV: prod # when: glimesh.changed - name: Ensure deploy state directories exist file: - path: "/opt/glimesh.tv-{{ item }}" + path: "{{ glimesh_web_host_dir }}-{{ item }}" state: directory owner: nobody recurse: yes @@ -116,19 +116,19 @@ - name: Copy currently live files to old directory synchronize: - src: /opt/glimesh.tv - dest: /opt/glimesh.tv-old + src: "{{ glimesh_web_host_dir }}" + dest: "{{ glimesh_web_host_dir }}-old" links: yes - name: Copy newly built files to new store synchronize: - src: /tmp/glimesh.tv/_build/prod - dest: /opt/glimesh.tv-new + src: "{{ glimesh_web_build_dir }}/_build/prod" + dest: "{{ glimesh_web_host_dir }}-new" links: yes - name: permissions file: - path: /opt/glimesh.tv-new + path: "{{ glimesh_web_host_dir }}-new" owner: nobody recurse: yes @@ -150,8 +150,8 @@ - name: Change the symlink file: - src: /opt/glimesh.tv-new - dest: /opt/glimesh.tv + src: "{{ glimesh_web_host_dir }}-new" + dest: "{{ glimesh_web_host_dir }}" state: link owner: nobody force: yes