-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRailsC.sh
executable file
·391 lines (338 loc) · 10.7 KB
/
RailsC.sh
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/bin/bash
############################################################################
# Script Name : RailsC
# Description : A CLI tool to help you start a new rails app with docker compose fast
# Version : 1.1.0
# Author : AhmedKamal20 ( Ahmed Kamal )
# Email : ahmed.kamal200@gmail.com
# Dependencies : docker compose readlink basename pushd popd read sudo sleep
############################################################################
set -e
sudo -v
TESTING=false
ForceDisableNode=false
RED='\e[1;31m'; GRE='\e[1;32m'; BLU='\e[1;34m'; YEL='\e[1;33m'; NCO='\e[0m';
# Help Message
if [ $# -lt 1 ]; then
echo -e "\n${RED}No/Wrong arguments supplied${NCO}\n"
echo -e "${YEL}RailsC${NCO} - A CLI tool to help you start a new rails app fast\n"
echo -e "${GRE}Format:${NCO} RailsC <PATH> <LOCAL DOMAIN> [<RAILS OPTIONS>]\n"
echo -e "${BLU}PATH:${NCO} The desired folder name for the Rails app"
echo -e "${BLU}LOCAL DOMAIN:${NCO} Domain to use with Nginx reverse proxy, Default is <AppName>.io"
echo -e "${BLU}RAILS OPTIONS:${NCO} Options to use for Rails new Apps, Default is --database=postgresql\n"
echo -e "${YEL}e.g.${NCO} RailsC ~/apps/myApp example.com \"--force --database=postgresql\"\n"
exit 1
fi
RubyVersion="3.2.2" # https://www.ruby-lang.org/en/downloads/
RailsVersion="7.1.1" # https://rubygems.org/gems/rails/versions
PostgresVersion="14.9" # https://www.postgresql.org/docs/release/
NodeVersion="20" # https://nodejs.org/en/about/releases/
DefaultOptions="--database=postgresql --skip-test --skip-system-test --skip-jbuilder -j esbuild -c tailwind"
ScriptDir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
RepoPath=$(readlink -f "${1}")
AppName=$(basename "${RepoPath}" | tr ' ' '-' | tr '[:upper:]' '[:lower:]')
if [ -z "${2}" ]; then
AppDomain=$(echo "${AppName}" | tr '.' '-' | awk '{print $1".io"}')
else
AppDomain="${2}"
fi
if [ -z "${3}" ]; then
RailsOptions="${DefaultOptions}"
else
RailsOptions="${3}"
fi
if [[ ${RailsOptions} == *api* ]] || [[ ${ForceDisableNode} = true ]]; then
NodeEnabled=false
else
NodeEnabled=true
fi
function log {
echo -e "${GRE}✔${NCO} $1"
}
function f_run {
echo -e "${RED}\$${NCO} $*"
"$@"
}
function run {
echo -e "${RED}\$${NCO} $*"
if [[ $TESTING == false ]]; then
"$@"
fi
}
echo -e "${GRE}\n\nCreating New Rails App as follow:${NCO}"
echo -e "${BLU}AppName:${NCO} ${AppName}"
echo -e "${BLU}RepoPath:${NCO} ${RepoPath}"
echo -e "${BLU}RailsOptions:${NCO} ${RailsOptions}"
echo -e "${BLU}RubyVersion:${NCO} ${RubyVersion}"
echo -e "${BLU}RailsVersion:${NCO} ${RailsVersion}"
echo -e "${BLU}PostgresVersion:${NCO} ${PostgresVersion}"
echo -e "${BLU}NodeEnabled?:${NCO} ${NodeEnabled}"
echo -e "${RED}Starting...${NCO}"
function overwrite_railsc {
run pushd "${RepoPath}"
run docker compose run --rm --entrypoint '' app rails db:drop
run docker compose down
run popd
read -p "Are you sure you want to remove ${RepoPath}? [YyNn] " yn
case $yn in
[Yy]* ) run sudo rm -rf "${RepoPath}";;
[Nn]* ) exit;;
* ) echo "Please answer [YyNn]"; exit;;
esac
}
if [[ -d ${RepoPath} && $TESTING == false ]]; then
read -p "The Path is Existed Already, Do you wish to overwrite it? [YyNn] " yn
case $yn in
[Yy]* ) overwrite_railsc;;
[Nn]* ) exit;;
* ) echo "Please answer [YyNn]"; exit;;
esac
fi
f_run mkdir -p "${RepoPath}"
f_run cd "${RepoPath}"
# Creating Initial Files
cat > ./Dockerfile.dev << ENDOFFILE
FROM ruby:${RubyVersion}
ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn
RUN curl -sL https://deb.nodesource.com/setup_${NodeVersion}.x | bash -
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \\
&& echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq \\
&& apt-get install -y --no-install-recommends \\
libpq-dev \\
postgresql-client \\
poppler-utils \\
nodejs \\
yarn
WORKDIR /usr/src/app
RUN gem update --system
RUN gem update bundler
COPY entrypoint.dev.sh /usr/bin/entrypoint.sh
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
CMD ["bin/dev"]
ENDOFFILE
log "Dockerfile Created"
cat > ./docker-compose.yml << ENDOFFILE
services:
reverse:
image: "nginxproxy/nginx-proxy"
container_name: "${AppName}-reverse"
ports:
- 80:80
- 443:443
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
app:
build:
context: .
dockerfile: Dockerfile.dev
image: "${AppName}-app"
hostname: "app"
domainname: "${AppDomain}"
container_name: "${AppName}-app"
tty: true
stdin_open: true
networks:
default:
aliases:
- "app.${AppDomain}"
volumes:
- ./:/usr/src/app
- ${AppName}-app-bundler-dir:/usr/local/bundle
$([[ $NodeEnabled = true ]] && echo "- ${AppName}-app-node-modules-dir:/usr/src/app/node_modules:delegated")
$([[ $NodeEnabled = true ]] && echo "- ${AppName}-app-yarn-cache-dir:/usr/src/yarn:delegated")
depends_on:
- "db"
environment:
VIRTUAL_HOST: "app.${AppDomain}"
RAILS_ENV: "development"
NODE_ENV: "development"
$([[ $NodeEnabled = true ]] && echo "YARN_CACHE_FOLDER: '/usr/src/yarn'")
HOST: "app.${AppDomain}"
PORT: "3000"
PGHOST: "db"
PGPORT: "5432"
PGUSER: "postgres"
PGPASSWORD: "postgres"
REDIS_URL: "redis://redis:6379/1"
sidekiq:
image: "${AppName}-app"
hostname: "sidekiq"
domainname: "${AppDomain}"
container_name: "${AppName}-sidekiq"
networks:
default:
aliases:
- "sidekiq.${AppDomain}"
volumes:
- ./:/usr/src/app
- ${AppName}-app-bundler-dir:/usr/local/bundle
$([[ $NodeEnabled = true ]] && echo "- ${AppName}-app-node-modules-dir:/usr/src/app/node_modules:delegated")
$([[ $NodeEnabled = true ]] && echo "- ${AppName}-app-yarn-cache-dir:/usr/src/yarn:delegated")
depends_on:
- "app"
- "redis"
command: "bundle exec sidekiq"
entrypoint: ''
environment:
VIRTUAL_HOST: "sidekiq.${AppDomain}"
RAILS_ENV: "development"
NODE_ENV: "development"
$([[ $NodeEnabled = true ]] && echo "YARN_CACHE_FOLDER: '/usr/src/yarn'")
HOST: "app.${AppDomain}"
PORT: "3000"
PGHOST: "db"
PGPORT: "5432"
PGUSER: "postgres"
PGPASSWORD: "postgres"
REDIS_URL: "redis://redis:6379/1"
mailcatcher:
image: "schickling/mailcatcher:latest"
hostname: "mailcatcher"
domainname: "${AppDomain}"
container_name: "${AppName}-mailcatcher"
networks:
default:
aliases:
- "mailcatcher.${AppDomain}"
depends_on:
- "app"
environment:
VIRTUAL_HOST: "mailcatcher.${AppDomain}"
VIRTUAL_PORT: "1080"
db:
image: "postgres:${PostgresVersion}-alpine"
hostname: "db"
domainname: "${AppDomain}"
container_name: "${AppName}-db"
restart: "always"
networks:
default:
aliases:
- "db.${AppDomain}"
volumes:
- ${AppName}-db-data:/var/lib/postgresql/data
- ${AppName}-db-logs:/var/log/postgresql
environment:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
adminer:
image: "adminer:latest"
hostname: "adminer"
domainname: "${AppDomain}"
container_name: "${AppName}-adminer"
restart: "always"
networks:
default:
aliases:
- "adminer.${AppDomain}"
depends_on:
- "db"
environment:
VIRTUAL_HOST: "adminer.${AppDomain}"
redis:
image: "redis:alpine"
hostname: "redis"
domainname: "${AppDomain}"
container_name: "${AppName}-redis"
restart: "always"
networks:
default:
aliases:
- "redis.${AppDomain}"
commander:
image: "rediscommander/redis-commander:latest"
hostname: "commander"
domainname: "${AppDomain}"
container_name: "${AppName}-commander"
restart: "always"
networks:
default:
aliases:
- "commander.${AppDomain}"
depends_on:
- "redis"
environment:
VIRTUAL_HOST: "commander.${AppDomain}"
REDIS_HOSTS: "local:redis:6379,local:redis:6379:15"
volumes:
${AppName}-db-data:
${AppName}-db-logs:
${AppName}-app-bundler-dir:
$([[ $NodeEnabled = true ]] && echo "${AppName}-app-node-modules-dir:")
$([[ $NodeEnabled = true ]] && echo "${AppName}-app-yarn-cache-dir:")
ENDOFFILE
log "DockerCompose file Created"
cat > ./entrypoint.dev.sh << ENDOFFILE
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f tmp/pids/server.pid
echo 'Installing Ruby Dependencies'
bundle check || bundle install
rails log:clear
rails tmp:clear
$([[ $NodeEnabled = true ]] && echo "
echo 'Installing JS Dependencies'
yarn install
")
# Do the pending migrations.
if psql -lqt | cut -d \| -f 1 | grep -qw app_development; then
rails db:migrate
elif [[ -f 'db/schema.rb' ]]; then
rails db:setup
else
rails db:create db:migrate db:seed
fi
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "\$@"
ENDOFFILE
log "Entrypoint Script Created"
log "Coping the template files to the app directory"
cp ${ScriptDir}/template.rb ${RepoPath}/template.rb
# Start Building the Project
log "Building the docker images"
run docker compose build
log "Initializing the Rails App"
log "rails new . ${RailsOptions}"
run docker compose run --rm --entrypoint '' app bash -c "gem install rails -v ${RailsVersion} && rails new . ${RailsOptions}"
run docker compose down
log "Changing the owner to current user"
run sudo chown -R "$USER":"$(id -gn "$USER")" .
log "Starting the docker services"
run docker compose up -d
if [[ $TESTING == false ]]; then
log "Wating For Rails to be UP"
spinner="/|\\-/|\\-"
while [ ! -f db/schema.rb ]; do
for i in $(seq 0 7)
do
echo -n "${spinner:$i:1}"
echo -en "\010"
sleep 0.5
done
done
fi
log "Changing the owner to current user"
run sudo chown -R "$USER":"$(id -gn "$USER")" .
# log "Creating an Initial commit"
run git commit --allow-empty -m '[ROOT] Initial commit'
run git add .
run git commit -m '[GENERATED] rails new'
echo -e "${RED}=-=-==-=-=-=-=-=-=-=${NCO}"
echo -e "${RED}Done${NCO}"
echo -e "${RED}=-=-==-=-=-=-=-=-=-=${NCO}"
echo -e "Rails App Installed In ${RepoPath}"
echo -e "Rails App Runs at http://app.${AppDomain}/"
echo -e "Adminer Runs at http://adminer.${AppDomain}/"
echo -e "Commander Runs at http://commander.${AppDomain}/"
echo -e "MailCatcher Runs at http://mailcatcher.${AppDomain}/"
echo -e "${RED}=-=-==-=-=-=-=-=-=-=${NCO}"
echo -e "Add This to your hosts file \n\
127.0.0.1 app.${AppDomain} \n\
127.0.0.1 mailcatcher.${AppDomain} \n\
127.0.0.1 commander.${AppDomain} \n\
127.0.0.1 adminer.${AppDomain}"
echo -e "${RED}=-=-==-=-=-=-=-=-=-=${NCO}"