This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conductor
executable file
·317 lines (269 loc) · 9.31 KB
/
conductor
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
#!/usr/bin/env bash
set -o allexport
source ./laralgeria.env
set +o allexport
# This is a general-purpose function to ask Yes/No questions in Bash, either
# with or without a default answer. It keeps repeating the question until it
# gets a valid answer.
ask() {
local prompt default reply
while true; do
if [[ "${2:-}" = "Y" ]]; then
prompt="Y/n"
default=Y
elif [[ "${2:-}" = "N" ]]; then
prompt="y/N"
default=N
else
prompt="y/n"
default=
fi
# Ask the question (not using "read -p" as it uses stderr not stdout)
echo -n "$1 [$prompt] "
read reply
# Default?
if [[ -z "$reply" ]]; then
reply=${default}
fi
# Check if the reply is valid
case "$reply" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
#
# Run the initial installer of Docker and laralgeria.
# Usage: ./conductor install
#
install() {
if [[ $(which docker) && $(docker --version) ]]; then
echo "Docker is already installed! Continuing..."
else
if ask "Docker does not appear to be installed. Install Docker now?" Y; then
curl -fsSL get.docker.com -o get-docker.sh
sh get-docker.sh
rm get-docker.sh
if [[ $EUID -ne 0 ]]; then
sudo usermod -aG docker `whoami`
echo "You must log out or restart to apply necessary Docker permissions changes."
echo "Restart, then continue installing using this script."
exit
fi
fi
fi
if [[ $(which docker-compose) && $(docker-compose --version) ]]; then
echo "Docker Compose is already installed! Continuing..."
else
if ask "Docker Compose does not appear to be installed. Install Docker Compose now?" Y; then
if [[ ! $(which git) ]]; then
echo "Git does not appear to be installed."
echo "Install git using your host's package manager,"
echo "then continue installing using this script."
exit 1
fi
if [[ ! $(which curl) ]]; then
echo "cURL does not appear to be installed."
echo "Install curl using your host's package manager,"
echo "then continue installing using this script."
exit 1
fi
COMPOSE_VERSION=`git ls-remote https://github.com/docker/compose | grep refs/tags | grep -oP "[0-9]+\.[0-9][0-9]+\.[0-9]+$" | tail -n 1`
sudo sh -c "curl -L https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose"
sudo chmod +x /usr/local/bin/docker-compose
sudo sh -c "curl -L https://raw.githubusercontent.com/docker/compose/${COMPOSE_VERSION}/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose"
fi
fi
check_composer_is_installed
generate_db
[[ -d ${APP_FOLDER_NAME} ]] || mkdir ${APP_FOLDER_NAME}
docker-compose pull
docker-compose up -d
composer_install
cleanup
}
#
# Build the Docker containers from your local copies.
# Usage: ./conductor build
#
build() {
generate_db
docker-compose build
docker-compose up -d
composer_install
cleanup
}
rebuild() {
printf "Rebuilding your docker environment...\n"
printf "Killing all the containers...\n"
docker-compose kill
printf "Rebuilding and restarting all the containers...\n"
build
}
#
# Enter the bash terminal of the running web container.
# Usage: ./conductor bash
#
bash() {
printf "Opening a bash connection into your dev container...\n"
docker exec -it ${1:-} bash
}
#
# Enter the ssh terminal of the running web container.
# Usage: ./conductor ssh
#
ssh() {
printf "Opening a bash connection into your dev container...\n"
docker exec -e COLUMNS="`tput cols`" -e LINES="`tput lines`" -ti laralgeria_web_v1 bash
}
#
# The log command will show you the output a container is generating when you don't run it interactively.
# This is likely to include the error message
# Usage: ./conductor log <container_name>
#
log() {
if [[ $1 == "web" ]] ; then
printf "Opening a bash connection into your web dev container...\n"
docker logs --tail 50 --follow --timestamps laralgeria_web_v1
exit 1
fi
if [[ $1 == "db" ]] ; then
printf "Opening a bash connection into your db dev container...\n"
docker logs --tail 50 --follow --timestamps laralgeria_db_v1
exit 1
fi
printf "Opening a bash connection into your dev container...\n"
docker logs --tail 50 --follow --timestamps ${1:-}
}
#
# Stop all Docker containers and remove related volumes.
# Usage: ./conductor uninstall
#
uninstall() {
if ask "This operation is destructive and will wipe your existing Docker containers. Continue?" N; then
stop
docker-compose down -v
docker-compose rm -f
docker volume prune -f
docker rmi -f laralgeria-radio_web
docker rmi -f laralgeria-radio_stations
docker rmi -f laralgeria-radio_db
docker rmi -f mysql:8.0.16
docker rmi -f $(docker images | grep "^<none>" | awk "{print $3}")
echo "All laralgeria Docker containers, volumes and images were removed."
echo "To remove *all* Docker containers and volumes, run:"
echo "docker stop \$(docker ps -a -q)"
echo "docker rm \$(docker ps -a -q)"
echo "docker volume prune -f"
echo ""
fi
}
if [[ $1 == "setup" ]] ; then
printf "Setting up an initial environment for you...\n"
docker exec -i -t laralgeria_web_v1 /bin/bash
exit 1
fi
if [[ $1 == "cp-stations-conf" ]] ; then
printf "Copy files or folders from a container to the host ...\n"
docker cp laralgeria_web_v1:${2:-} ${3:-}
exit 1
fi
#
# Create and link a LetsEncrypt SSL certificate.
# Usage: ./conductor letsencrypt-create domainname.example.com
#
letsencrypt-create() {
docker-compose exec --user="laralgeria" web letsencrypt_connect $*
}
#
# Renew an existing LetsEncrypt SSL certificate
# Usage: ./conductor letsencrypt-renew
#
letsencrypt-renew() {
docker-compose exec --user="laralgeria" web letsencrypt_renew $*
}
#
# Start all Docker containers
# Usage: ./conductor start
#
start() {
printf "Starting containers...\n"
docker-compose up -d
}
#
# Stop all Docker containers.
# Usage: ./conductor stop
#
stop() {
printf "Shutting down containers...\n"
docker-compose kill
}
#
# Restart all Docker containers.
# Usage: ./conductor restart
#
restart() {
printf "Restarting containers...\n"
docker-compose kill
docker-compose up -d
}
unitest() {
dockerexec laralgeria_web_v1 "vendor/bin/phpunit $1 --order-by=defects --stop-on-defect --colors=always"
}
#
# Migrate all the migration files.
# Usage: ./conductor migrate
#
migrate() {
echo "🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹"
echo -e "⌛ \033[34mDevelopment environment: \033[0m\033[93m \033[93mStart migrating all migrations...\033[0m"
echo "🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹🎹"
docker exec -it laralgeria_web_v1 php artisan migrate
}
function generate_db() {
printf "Generating database file...\n"
./docker-containers/scripts/generate_db
}
function composer_install() {
printf "Installing Laravel...\n"
composer install --working-dir=$PWD/${APP_FOLDER_NAME}
}
function cleanup() {
printf "Cleanup: deleting unnecessary files used for installation...\n"
rm -rf ./docker-containers/laralgeria-db/db.sql
alert_success
}
function check_composer_is_installed() {
# Check if PHP is installed
php -v > /dev/null 2>&1
PHP_IS_INSTALLED=$?
[[ ${PHP_IS_INSTALLED} -ne 0 ]] && { printf "!!! PHP is not installed.\n Installing Composer aborted!\n"; exit 0; }
# Check if Composer is installed
composer -v > /dev/null 2>&1
COMPOSER_IS_INSTALLED=$?
if [[ ${COMPOSER_IS_INSTALLED} -ne 0 ]]; then
if ask "Composer does not appear to be installed. Install Composer now?" Y; then
if [[ ! $(which curl) ]]; then
echo "cURL does not appear to be installed."
echo "Install curl using your host's package manager,"
echo "then continue installing using this script."
exit 1
fi
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
fi
else
echo "Composer is already installed! Continuing..."
fi
}
function alert_success() {
echo "🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉"
echo -e "🎉 \033[34mWell done!\033[0m\033[93m \033[93mYour environment is ready to run!\033[0m 🎉"
echo "🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉"
echo -e "⚠️ ⚠️ ⚠️ \033[93mPlease don't forget to add \033[31mlaralgeria.local\033[0m\033[93m \033[93min your local hosts.\033[0m️ ⚠️ ⚠️ ⚠️"
}
function dockerexec {
: ${1? First parameter required}
: ${2? Second parameter required}
docker exec $1 bash -c "TERM=xterm-256color $2"
}
$*