-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctrl.sh
executable file
·165 lines (137 loc) · 4.79 KB
/
ctrl.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
#!/usr/bin/env bash
function usage() {
echo "\
Manages building and running Docker containers for general development.
COMMANDS
./ctrl.sh build
Generate .env and build Docker image. It is not necessary
to run this command first, because it's implied with ./ctrl.sh start.
./ctrl.sh start [OPTIONS]
Starts the container(s).
Options:
-E, --skipEchoPrefix Test option.
-e, --echoValue=value Test option with value.
-h, --help Print this help
./ctrl.sh kill
Stop and delete running containers (started with ./ctrl.sh start). It won't delete Docker image, volumes or any mounted data.
./ctrl.sh logs
Show and follow logs of the running containers. Press Ctrl+C to exit.
./ctrl.sh runCommand [OPTIONS]
Run command.
Options:
-E, --skipEchoPrefix Test option.
-e, --echoValue=value Test option with value.
-h, --help Print this help
./ctrl.sh printForHosts
Show entry, that can be added to /etc/hosts
"
}
function build() {
# load settings
. "$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)/.devscripts/settings.sh"
_genDotEnv
echo -e "\nBuilding Docker image..."
docker compose -f .devscripts/docker-compose.yaml --project-directory=.devscripts/ build
}
function start() {
# load settings
. "$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)/.devscripts/settings.sh"
# parse options (may override settings)
while :; do
case $1 in
-E|--skipEchoPrefix)
skipEchoPrefix=true
;;
-e=?*|--echoValue=?*)
echoValue=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
-h|--help)
usage
exit 0
;;
?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1"
;;
*) # Default case: No more options, so break out of the loop.
break
esac
shift
done
_genDotEnv
_createDockerNetworkIfNotExists ${COMPOSE_PROJECT_NAME}_${NETWORK_NAME}
time docker compose -f .devscripts/docker-compose.yaml --project-directory=.devscripts/ up --force-recreate --build -d
}
function kill() {
docker compose -f .devscripts/docker-compose.yaml --project-directory=.devscripts/ down -t 0
}
function logs() {
docker compose -f .devscripts/docker-compose.yaml --project-directory=.devscripts/ logs --tail=50 -f
}
function runCommand() {
# load settings
. "$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)/.devscripts/settings.sh"
# parse options (overrides settings)
while :; do
case $1 in
-E|--skipEchoPrefix)
skipEchoPrefix=true
;;
-e=?*|--echoValue=?*)
echoValue=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
-h|--help)
usage
exit 0
;;
?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1"
;;
*) # Default case: No more options, so break out of the loop.
break
esac
shift
done
envs="export SKIP_ECHO_PREFIX=${skipEchoPrefix:-false};"
docker compose exec devbox bash -c "$envs . /build/funcs.sh; do_echo ${echoValue:-foo}"
}
function _createDockerNetworkIfNotExists() {
echo $1
if [ 1 -eq $(docker network ls -f name=$1 | wc -l) ]; then # lists title only
docker network create "$1"
fi
}
function _genDotEnv() {
dot_env=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)/.devscripts/.env
echo -n "" > "${dot_env}"
# build on the fly
echo "ARG_UID=$(id -u)" >> "${dot_env}"
echo "ARG_GID=$(id -g)" >> "${dot_env}"
composeProjectName=${COMPOSE_PROJECT_NAME:-devbox}
echo "COMPOSE_PROJECT_NAME=$composeProjectName" >> "${dot_env}"
devboxHostname=${DEVBOX_HOSTNAME:-$composeProjectName}
echo "DEVBOX_HOSTNAME=$devboxHostname" >> "${dot_env}"
echo "NETWORK_NAME=$NETWORK_NAME" >> "${dot_env}"
echo "DEVBOX_ROOT=$(realpath ./)/" >> "${dot_env}"
echo "SKIP_ECHO_PREFIX=${skipEchoPrefix:-false}" >> "${dot_env}"
echo "ECHO_VALUE=${echoValue:-foo}" >> "${dot_env}"
echo "Generated .env:"
cat "${dot_env}"
}
function printForHosts() {
# load settings
. "$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)/.devscripts/settings.sh"
_genDotEnv
contip=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${composeProjectName}-devbox-1)
echo "You can put this line to /etc/hosts"
echo "$contip $devboxHostname"
}
if ! [[ -z "$@" ]]; then
if [[ "$1" == "build" ]] || [[ "$1" == "start" ]] || [[ "$1" == "logs" ]] || [[ "$1" == "kill" ]] || [[ "$1" == "runCommand" ]] || [[ "$1" == "printForHosts" ]]; then
"$@"
else
usage
exit 0
fi
else
usage
fi