-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdefault.rb
More file actions
250 lines (211 loc) · 5.63 KB
/
default.rb
File metadata and controls
250 lines (211 loc) · 5.63 KB
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
# frozen_string_literal: true
# Code With Rails
# (c) Copyright 2022
#
# https://github.com/Code-With-Rails/rails-template
#
# v1.1.0
#
# Intro: A simple, opinionated Rails template to set up your Rails project with a
# Docker development environment.
#
# Installation:
# bin/rails app:template LOCATION=
#
# Usage:
# To run the app - `docker-compose up`
# To go into app's dev shell - 'docker-compose run app bash'
#
# What this template adds:
# - Adds Dockerfile
# - Adds docker-compose.yml with PostgreSQL and Redis services, as well the app service
#
# For more information about Rails template, please refer to the official Rails guide at
# https://guides.rubyonrails.org/rails_application_templates.html.
git :init
git add: '.'
git commit: %( -m 'Initial commit' )
file 'Dockerfile', <<~CODE
FROM ruby:3.2.0-bullseye
# Install apt based dependencies required to run Rails as
# well as RubyGems. As the Ruby image itself is based on a
# Debian image, we use apt-get to install those.
RUN apt-get update && apt-get install -y \
build-essential \
nano \
nodejs
# Configure the main working directory. This is the base
# directory used in any further RUN, COPY, and ENTRYPOINT
# commands.
RUN mkdir -p /app
WORKDIR /app
# Copy the Gemfile as well as the Gemfile.lock and install
# the RubyGems. This is a separate step so the dependencies
# will be cached unless changes to one of those two files
# are made.
COPY Gemfile* ./
RUN gem install bundler -v 2.3.22 && bundle install --jobs 20 --retry 5
RUN gem install foreman
COPY . /app
RUN rm -rf tmp/*
ADD . /app
CODE
file 'script/wait-for-tcp.sh', <<~CODE
#!/usr/bin/env bash
if [ -z "$2" ]; then cat <<'HELP'; exit; fi
Usage: script/wait-for-tcp ADDRESS PORT
Wait for TCP connection at ADDRESS:PORT, retrying every 3 seconds, maximum 20 times.
HELP
wait_for_tcp() {
local addr="$1"
local port="$2"
local status="down"
local counter=0
local wait_time=3
local max_retries=20
while [ "$status" = 'down' -a ${counter} -lt ${max_retries} ]; do
status="$( (echo > "/dev/tcp/${addr}/${port}") >/dev/null 2>&1 && echo 'up' || echo 'down' )"
if [ "$status" = 'up' ]; then
echo "Connection to ${addr}:${port} up"
else
echo "Waiting ${wait_time}s for connection to ${addr}:${port}..."
sleep "$wait_time"
let counter++
fi
done
if [ ${status} = 'down' ]; then
echo "Could not connect to ${addr}:${port} after ${max_retries} retries"
exit 1
fi
}
wait_for_tcp "$1" "$2"
CODE
inside('script') do
run 'chmod +x wait-for-tcp.sh'
end
file 'script/docker-dev-start-web.sh', <<~CODE
#!/usr/bin/env bash
set -xeuo pipefail
./script/wait-for-tcp.sh db 5432
./script/wait-for-tcp.sh redis 6379
if [[ -f ./tmp/pids/server.pid ]]; then
rm ./tmp/pids/server.pid
fi
bundle
if ! [[ -f .db-created ]]; then
bin/rails db:drop db:create
touch .db-created
fi
bin/rails db:create
bin/rails db:migrate
bin/rails db:fixtures:load
if ! [[ -f .db-seeded ]]; then
bin/rails db:seed
touch .db-seeded
fi
foreman start -f Procfile.dev
CODE
inside('script') do
run 'chmod +x docker-dev-start-web.sh'
end
file 'docker-compose.yml', <<~CODE
version: "3.8"
networks:
backend:
frontend:
selenium:
services:
db:
image: postgres:14.2-alpine
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
networks:
- backend
redis:
image: redis:7-alpine
ports:
- "6379:6379"
networks:
- backend
chrome_server:
image: seleniarm/standalone-chromium
volumes:
- /dev/shm:/dev/shm
networks:
- selenium
app:
build: .
tty: true
volumes:
- .:/app
working_dir: /app
environment:
DB: postgresql
DB_HOST: db
DB_PORT: 5432
DB_USERNAME: postgres
DB_PASSWORD: postgres
BUNDLE_GEMFILE: /app/Gemfile
SELENIUM_REMOTE_URL: http://chrome_server:4444/wd/hub
command: script/docker-dev-start-web.sh
networks:
- backend
- frontend
- selenium
ports:
- "3000:3000"
depends_on:
- db
- redis
- chrome_server
CODE
git add: '.'
git commit: '-a -m \'Add Docker config to app\''
# Add .railsrc
file '.railsrc', <<~CODE
rails: --skip-bundle --database=postgresql
CODE
# Update config/database.yml development and test configs
gsub_file 'config/database.yml', /^development:\n <<: \*default/, <<-CODE
development:
<<: *default
username: postgres
password: postgres
host: db
CODE
gsub_file 'config/database.yml', /^test:\n <<: \*default/, <<-CODE
test:
<<: *default
username: postgres
password: postgres
host: db
CODE
# Update Ruby version in the Gemfile
gsub_file 'Gemfile', /^ruby .*$/, 'ruby \'3.2.0\''
git add: '.'
git commit: '-a -m \'Use Ruby 3.2.0 in the Gemfile\''
# Add Procfile.dev
file 'Procfile.dev', <<~CODE
web: bin/rails server --port 3000 --binding 0.0.0.0
CODE
# Update .gitignore
append_file '.gitignore', <<~CODE
.db-seeded
.db-created
.DS_Store
CODE
puts"""
**********************************
And we're done!
If you have any questions, issue, or suggestions, please go to https://github.com/Code-With-Rails/rails-template to submit an issue.
Next Steps:
1. `cd` into your app directory
2. Run `docker-compose build` to ensure that the container environment builds correctly
3. Run `docker-compose up` to boot up the app
4. To enter into the shell, run `docker-compose run app bash`
Enjoy!
**********************************
"""