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 ea25699..cc6c41b 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,13 +43,15 @@ - 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: "{{ 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 @@ -59,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 +- 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 + command: mix do deps.get, deps.compile args: - chdir: /tmp/glimesh.tv + chdir: "{{ glimesh_web_build_dir }}" environment: MIX_ENV: prod # when: glimesh.changed @@ -79,31 +81,62 @@ - 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 + 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: "{{ glimesh_web_host_dir }}-{{ item }}" + state: directory + owner: nobody + recurse: yes + with_items: + - "old" + - "new" + +- name: Copy currently live files to old directory + synchronize: + src: "{{ glimesh_web_host_dir }}" + dest: "{{ glimesh_web_host_dir }}-old" + links: yes + +- name: Copy newly built files to new store + synchronize: + src: "{{ glimesh_web_build_dir }}/_build/prod" + dest: "{{ glimesh_web_host_dir }}-new" + links: yes + +- name: permissions + file: + path: "{{ glimesh_web_host_dir }}-new" + owner: nobody + recurse: yes + - 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 +144,20 @@ 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: permissions +- name: Change the symlink file: - path: /opt/glimesh.tv + src: "{{ glimesh_web_host_dir }}-new" + dest: "{{ glimesh_web_host_dir }}" + state: link owner: nobody - recurse: yes + force: yes - name: start new glimesh app - systemd: + systemd: name: glimesh state: started @@ -136,5 +167,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 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