-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_rails_app.yml
265 lines (225 loc) · 6.92 KB
/
add_rails_app.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
---
- name: Add a Rails app server
hosts: rails_app_servers,vagrant*
become: yes
gather_facts: yes
tasks:
- name: add nodesource repo key
apt_key:
url: https://deb.nodesource.com/gpgkey/nodesource.gpg.key
state: present
- name: Add node_14.x repository into sources list
apt_repository:
repo: deb https://deb.nodesource.com/node_14.x focal main
state: present
- name: install prerequisite
apt:
pkg:
- git
- curl
- libssl-dev
- libreadline-dev
- zlib1g-dev
- autoconf
- bison
- build-essential
- libyaml-dev
- libreadline-dev
- libncurses5-dev
- libffi-dev
- libgdbm-dev
- libmagickwand-dev
- imagemagick
- apache2
- libmysqlclient-dev
- mysql-client
- libsqlite3-dev
- nodejs
- python-pexpect
- name: create user primary group
group:
name: "{{ app }}"
gid: "{{ puma_port }}"
state: present
- name: create user
user:
name: "{{ app }}"
group: "{{ app }}"
comment: "{{ app }}"
uid: "{{ puma_port }}"
shell: /bin/bash
createhome: yes
home: "/home/{{ app }}"
state: present
- name: set home directory owner
file:
path: "/home/{{ app }}"
owner: "{{ app }}"
group: "{{ app }}"
- name: Adding user {{ app }} to group "sudo"
user: name={{ app }}
groups=sudo
append=yes
when: rails_env == 'development'
- name: Adding user {{ app }} to group "vagrant"
user: name={{ app }}
groups=vagrant
append=yes
when: rails_env == 'development'
failed_when: false
- name: copy /etc/skel/.bash_aliases to user home directory
copy:
src: etc/skel/.bash_aliases
dest: "/home/{{ app }}"
owner: "{{ app }}"
group: "{{ app }}"
force: no
- block:
- name: create app specific "/home/{{ app }}/.{{ app }}" environment file
template:
src: templates/.env
dest: "/home/{{ app }}/.{{ app }}"
owner: "{{ app }}"
group: "{{ app }}"
- name: add SSH pubkeys
authorized_key:
user: "{{ app }}"
key: "{{ lookup('file', '{{ item }}') }}"
with_fileglob: sshkeys/*
- name: check if rbenv installed
stat:
path: /home/{{ app }}/.rbenv/bin/rbenv
register: rbenv
- name: clone rbenv-installer
git:
repo: "https://github.com/rbenv/rbenv-installer.git"
dest: /home/{{ app }}/rbenv-installer
single_branch: yes
version: main
when: rbenv.stat.exists == False
- name: install rbenv
shell: "cat /home/{{ app }}/rbenv-installer/bin/rbenv-installer | bash"
when: rbenv.stat.exists == False
- name: setup rbenv PATH for the user "{{ app }}"
lineinfile:
path: /home/{{ app }}/.profile
line: 'export PATH="$HOME/.rbenv/bin:$PATH"'
state: present
create: yes
- name: setup rbenv command for the user "{{ app }}"
lineinfile:
path: /home/{{ app }}/.profile
line: 'eval "$(rbenv init -)"'
state: present
create: yes
- name: create ~/.gemrc for user "{{ app }}"
lineinfile:
path: /home/{{ app }}/.gemrc
line: 'gem: --no-document'
state: present
create: yes
- name: create ~/.bundle
file:
path: /home/{{ app }}/.bundle
state: directory
- name: create ~/.bundle/config for user "{{ app }}"
lineinfile:
path: /home/{{ app }}/.bundle/config
line: 'BUNDLE_WITHOUT: "development:test"'
state: present
create: yes
when: rails_env == 'production'
- name: determine latest installable ruby version
shell: . /home/{{ app }}/.profile ; rbenv install -l | grep -v - | tail -1
register: rbenv_latest_available
changed_when: false
when: ruby_version is not defined
- name: choose latest installable ruby
set_fact: ruby_version="{{ rbenv_latest_available.stdout }}"
when: ruby_version is not defined
- name: install required ruby version "{{ ruby_version }}"
shell: . /home/{{ app }}/.profile ; rbenv install -s "{{ ruby_version }}"
register: rbenv_install
changed_when: rbenv_install.stderr is search('Installed ruby')
failed_when: rbenv_install.stderr is search('BUILD FAILED')
- name: get current global ruby version
shell: . /home/{{ app }}/.profile ; rbenv global
register: rbenv_version
changed_when: False
- name: set global ruby version to "{{ ruby_version }}"
shell: . /home/{{ app }}/.profile ; rbenv global "{{ ruby_version }}"
register: rbenv_global
changed_when: not rbenv_version.stdout is search(ruby_version)
failed_when: rbenv_global.stderr | length > 0
- name: unset local ruby version
file:
path: /home/{{ app }}/.ruby-version
state: absent
become: yes
become_user: "{{ app }}"
- name: create apache conf /etc/apache2/sites-available/{{ vhost }}.conf
template:
src: etc/apache2/sites-available/rails_app_template.conf
dest: "/etc/apache2/sites-available/{{ vhost }}.conf"
owner: root
group: root
force: yes
- name: enable mod_proxy_http
community.general.apache2_module:
state: present
name: proxy_http
notify: restart apache2
- name: Enable the Apache2 mod_rewrite
community.general.apache2_module:
state: present
name: rewrite
ignore_configcheck: yes
notify: restart apache2
- name: Enable the Apache2 mod_headers
community.general.apache2_module:
state: present
name: headers
ignore_configcheck: yes
notify: restart apache2
- name: create DocumentRoot for {{ vhost }}
file:
path: "/var/www/{{ vhost }}"
owner: root
group: root
state: directory
mode: '0755'
- name: enable apache site
file:
src: "/etc/apache2/sites-available/{{ vhost }}.conf"
dest: "/etc/apache2/sites-enabled/{{ vhost }}.conf"
owner: root
group: root
state: link
notify: restart apache2
- name: create puma systemd unit file
template:
src: etc/systemd/system/rails_app_template.service
dest: "/etc/systemd/system/{{ app }}.service"
owner: root
group: root
force: yes
- name: create mailcatcher systemd unit file
template:
src: etc/systemd/system/mailcatcher.service
dest: "/etc/systemd/system/mailcatcher.service"
owner: root
group: root
force: yes
when: rails_env == "development"
- name: add block to source and export variables from "/home/{{ app }}/.{{ app }}" to "/home/{{ app }}/.profile"
ansible.builtin.blockinfile:
path: "/home/{{ app }}/.profile"
block: |
set -o allexport
. /home/{{ app }}/.{{ app }}
set +o allexport
handlers:
- name: restart apache2
systemd:
state: restarted
name: apache2