-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.yml
235 lines (196 loc) · 4.94 KB
/
main.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
---
- name: set a hostname
hostname:
name: "{{ inventory_hostname }}"
- name: Ensure .ssh directory exists.
file:
dest: "/root/.ssh"
mode: 0700
owner: root
state: directory
- name: Install ssh key
copy:
src: "glimesh-deploy"
dest: "/root/.ssh/id_rsa"
mode: 0600
owner: root
- name: Trust GitHub
shell: ssh-keyscan github.com >> ~/.ssh/known_hosts
# - name: install elixir
# apt:
# deb: https://packages.erlang-solutions.com/erlang-solutions_2.0_all.deb
# - name: install required packages
# apt:
# pkg:
# - esl-erlang
# - elixir
# - npm
# - git
# - openssl
# - imagemagick
# state: present
# update_cache: yes
- name: install hex
command: mix local.hex --force
- name: install rebar
command: mix local.rebar --force
- name: ensure tmp build dir exists
file:
path: /tmp/glimesh.tv
state: directory
- name: download glimesh.tv
git:
repo: https://github.com/Glimesh/glimesh.tv.git
dest: /tmp/glimesh.tv
recursive: true
update: true
force: true
version: "{{ glimesh_version }}"
register: glimesh
- name: register short git hash version
command: git rev-parse --short HEAD
args:
chdir: /tmp/glimesh.tv
register: glimesh_git_hash
- name: change version
lineinfile:
path: /tmp/glimesh.tv/mix.exs
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: no
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.exists and 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:
chdir: /tmp/glimesh.tv
environment:
MIX_ENV: prod
- name: install npm asset deps
command: npm --prefix ./assets ci --progress=false --no-audit --loglevel=error
args:
chdir: /tmp/glimesh.tv
# when: glimesh.changed
- name: build static assets
command: npm run --prefix ./assets deploy
args:
chdir: /tmp/glimesh.tv
# when: glimesh.changed
- name: digest static assets
command: mix phx.digest
args:
chdir: /tmp/glimesh.tv
# when: glimesh.changed
- name: build glimesh.tv
command: mix do compile --force, release --force --overwrite
args:
chdir: /tmp/glimesh.tv
environment:
MIX_ENV: prod
# when: glimesh.changed
- name: Ensure new directory exists
file:
path: "{{ deploy_dest }}"
state: directory
owner: nobody
recurse: yes
- name: Copy newly built files to new host
synchronize:
src: /tmp/glimesh.tv/_build/prod
dest: "{{ deploy_dest }}"
links: yes
delete: yes
delegate_to: "{{ inventory_hostname }}"
- name: Check permissions
file:
path: "{{ deploy_dest }}"
owner: nobody
recurse: yes
## Change the service
- 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: "{{ deploy_dest }}"
dest: /opt/glimesh.tv
state: link
owner: nobody
force: yes
- name: start new glimesh app
systemd:
name: glimesh
state: started
- 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
- 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: 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
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 != '/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 }}"
tags:
- cleanup