This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_rails_app.sh
executable file
·202 lines (174 loc) · 5.38 KB
/
docker_rails_app.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
#!/bin/bash
user=$(whoami)
directory=$(pwd -P)
app=$(expr match $directory "$DATA/\([^/]*\)")
db_dump_directory="$META/$app/tmp"
export COMPOSE_FILE='docker-compose.yml'
if [ -e Gemfile ]; then
if grep -q "gem 'rails', '5" Gemfile; then
rake_command='rails'
else
rake_command='rake'
fi
fi
if [ -e Dockerfile ]; then
if grep -q alpine Dockerfile; then
shell_command='sh'
else
shell_command='bash'
fi
fi
docker_do() { echo "+ docker $@" ; docker "$@" ; }
compose_do() {
echo "+ docker-compose $@"
if [ -e .docker_overrides.env ]; then
cp .docker_overrides.env "$META/$app/docker_overrides.env"
elif [ -e "$META/$app/docker_overrides.env" ]; then
cp "$META/$app/docker_overrides.env" .docker_overrides.env
fi
docker-compose "$@"
}
fix_file_permissions() {
find . \! -user $user -print0 | xargs -0 -I % sh -c "sudo chmod g+w \"%\"; sudo chown $user:$user \"%\""
}
stop_container_matching() {
running_server_id=`docker_do ps | grep "$1" | awk '{print $1}'`
if [ -n "$running_server_id" ]; then
docker_do stop $running_server_id
fi
}
command="$1"
shift
if [ $command = "run" ]; then # run
compose_do run $@
fix_file_permissions
fi
if [ $command = "b" ]; then # build
compose_do build
fi
if [ $command = "deploy" ]; then # deploy
branch=$(git symbolic-ref --short HEAD)
if [ $branch = "master" ]; then
environment="production"
else
environment="staging"
fi
tag="${environment}_$(date +"%F-%H%M")"
commit_range="$(git tag -l | grep $environment | tail -n 1)..$branch"
# Extract issue numbers from the branch names in the merge commits
message=$(git log --merges --abbrev-commit --pretty=oneline "$commit_range" | grep -Po "'[0-9.]+" | tr "'" "#")
printf -v message "$environment $(date +"%m/%d/%Y %I:%M%p")\n\n$message"
hub release create $tag -m "$message"
fi
if [ $command = "bundle" ]; then # bundle
compose_do run --rm web bundle $@
fix_file_permissions
fi
if [ $command = "r" ]; then # rails
compose_do run --rm web bundle exec rails $@
fix_file_permissions
fi
if [ $command = "s" ]; then # rails server
sudo rm -f $directory/tmp/pids/server.pid
sudo rm -f $directory/passenger.*
compose_do up --no-build
fi
if [ $command = "k" ]; then # rake
if [ -e Gemfile ]; then
compose_do run --rm web bundle exec $rake_command $@
else
compose_do run --rm web rake $@
fi
fix_file_permissions
fi
if [ $command = "bash" ] || [ $command = "sh" ]; then # bash
compose_do run --rm web $command
fi
if [ $command = "rs" ]; then # restart (docker clean, rake db:setup, rails server)
docker ps --quiet --no-trunc | xargs --no-run-if-empty docker stop
docker ps --all --quiet --no-trunc | xargs --no-run-if-empty docker rm -v
compose_do run --rm web bundle exec rake db:setup
sudo rm -f $directory/tmp/pids/server.pid
sudo rm -f $directory/passenger.*
compose_do up --no-build
fi
if [ $command = "dbfetch" ]; then # dbfetch
mkdir -p $db_dump_directory
if grep -q heroku .git/config; then
if [ $1 ]; then
remote="$1"
else
remote='staging'
fi
heroku pgbackups:capture -r $remote
curl -o $db_dump_directory/db.dump `heroku pgbackups:url -r $remote`
else
if [ $1 ]; then
server=$1
else
server=$app
fi
scp $server:~/db.sql.gz $db_dump_directory/
fi
if [ -e $db_dump_directory/db.sql.gz ]; then
cp $db_dump_directory/db.sql.gz $db_dump_directory/db.$(date +"%Y.%m.%d").sql.gz
gunzip -f $db_dump_directory/db.sql.gz
fi
if [ -e $db_dump_directory/db.dump ]; then
cp $db_dump_directory/db.dump $db_dump_directory/db.$(date +"%Y.%m.%d").dump
fi
fi
if [ $command = "rm" ]; then # rm docker containers interactively
data_container_id=`docker inspect --format={{.Id}} /data`
for container_id in $(docker ps --all --quiet --no-trunc)
do
if [ $container_id = $data_container_id ]; then
continue
fi
docker inspect --format='{{if .State.ExitCode}} Exited {{end}} {{.Name}} {{.Path}} {{range $i, $arg := .Args}}{{$arg}} {{end}}' $container_id
echo -n "delete? [y/N]:"
read response
if [ "$response" = "q" ]; then
break
elif [ "$response" = "y" ]; then
docker_do rm $container_id
fi
done
fi
if [ $command = "rmi" ]; then # remove untagged images
docker images -q --filter "dangling=true" | xargs docker rmi
fi
if [ $command = "c" ]; then # Docker clean
docker ps --quiet --no-trunc | xargs --no-run-if-empty docker stop
docker ps --all --quiet --no-trunc | xargs --no-run-if-empty docker rm -v
docker images --quiet --no-trunc --filter "dangling=true" | xargs --no-run-if-empty docker rmi
fi
if [ $command = "t" ]; then # Tail development logs
tail -f log/development.log
fi
if [ $command = "g" ]; then # Tail development logs and grep for '###'
tail -f log/development.log | grep '###'
fi
if [ $command = "n" ]; then # cat notes
tail -n300 notes.md
fi
if [ $command = "d" ]; then # rake db:setup
if [ -e "bin/setup" ]; then
./bin/setup
else
compose_do run --rm web bundle exec $rake_command db:setup
fi
fi
if [ $command = "a" ]; then # approve
entrypoint="bundle exec $rake_command test"
if grep -q "rubocop" Gemfile; then
entrypoint="$entrypoint && rubocop"
fi
if grep -q "slim_lint" Gemfile; then
entrypoint="$entrypoint && slim-lint app/views"
fi
compose_do run --rm web sh -c "$entrypoint"
fi
if [ $command = "p" ]; then # ruboco[p]
compose_do run --rm web rubocop --auto-correct
fi