-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·210 lines (186 loc) · 5.92 KB
/
dev.sh
File metadata and controls
executable file
·210 lines (186 loc) · 5.92 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
#!/usr/bin/env bash
set -e
PROJECT=git-wip
TARGET=
HOSTNAME="$(hostname)"
USERNAME="$(id -u -n)"
USER_UID="$(id -u)"
USER_GID="$(id -g)"
# terminal colors
BLACK='\033[0;30m' RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' BLUE='\033[0;34m' MAGENTA='\033[0;35m' CYAN='\033[0;36m' WHITE='\033[0;37m'
BRIGHT_BLACK='\033[1;30m' BRIGHT_RED='\033[1;31m' BRIGHT_GREEN='\033[1;32m' BRIGHT_YELLOW='\033[1;33m' BRIGHT_BLUE='\033[1;34m' BRIGHT_MAGENTA='\033[1;35m' BRIGHT_CYAN='\033[1;36m' BRIGHT_WHITE='\033[1;37m'
RESET='\033[0m'
function die {
echo >&2 "ERROR: $*"
exit 1
}
function available_targets {
ls Dockerfile-* 2>/dev/null | sed -n 's/^Dockerfile-\([A-Za-z0-9]\+\)$/\1/p'
}
declare TARGET= DOCKERFILE= NAME=
function set_target {
TARGET="$1"
[ -z "$TARGET" ] && die "Target not specified"
DOCKERFILE="Dockerfile-$TARGET"
[ -f "$DOCKERFILE" ] || die "$DOCKERFILE: no such file"
[ -r "$DOCKERFILE" ] || die "$DOCKERFILE: cannot be read"
NAME="$PROJECT-builder-$USERNAME-$TARGET"
}
declare -a BUILD_ARGS=()
function set_build_args {
local value
# transfer host user.email to docker
value="$(git config user.email)"
[ -z "$value" ] && value="$USERNAME@$HOSTNAME"
[ -n "$value" ] && BUILD_ARGS+=( "--build-arg=GIT_EMAIL=$value" )
# transfer host user.name to docker
value="$(git config user.name)"
[ -z "$value" ] && value="$USERNAME"
[ -n "$value" ] && BUILD_ARGS+=( "--build-arg=GIT_NAME=$value" )
# Add UID, GID, and USERNAME as build arguments to match host user
BUILD_ARGS+=( "--build-arg=UID=$USER_UID" )
BUILD_ARGS+=( "--build-arg=GID=$USER_GID" )
BUILD_ARGS+=( "--build-arg=USERNAME=$USERNAME" )
}
function show_status {
local images=( $( docker images | awk "\$1 ~ /\<${NAME}\>/ { print \$3 }" ) )
if [ "${#images[@]}" -gt 0 ] ; then
echo -e "${NAME}: image is ${BRIGHT_GREEN}built${RESET}: ${BRIGHT_BLUE}${images[*]}${RESET}"
else
echo -e "${NAME}: image is ${BRIGHT_RED}not built${RESET}."
fi
# Check if container is running
local running=( $( docker ps | awk "\$2 ~ /\<${NAME}\>/ { print \$1 }" ) )
if [ "${#running[@]}" -gt 0 ] ; then
echo -e "${NAME}: cntnr is ${BRIGHT_GREEN}running${RESET}: ${BRIGHT_BLUE}${running[*]}${RESET}"
fi
local stopped=( $( docker ps -a | awk "\$2 ~ /\<${NAME}\>/ && /Exited / { print \$1 }" ) )
if [ "${#stopped[@]}" -gt 0 ] ; then
echo -e "${NAME}: cntnr is ${BRIGHT_YELLOW}stopped${RESET}: ${BRIGHT_BLUE}${stopped[*]}${RESET}"
fi
if [ "${#running[@]}" -eq 0 ] && [ "${#stopped[@]}" -eq 0 ] ; then
echo -e "${NAME}: cntnr does ${BRIGHT_RED}not exist${RESET}."
fi
}
# Function to display help
show_help() {
cat <<END
${0##*/} <target> <command>[,<command>,...] [...]
Available commands:
build - create a dev image
remove - remove a dev image
start - start the container
stop - stop the container
status - check if built/running
connect - get a shell in the container
run <cmd> - run a command in the container
Available targets:
END
for t in $(available_targets); do
echo " $t"
done
echo
}
target="$1" ; shift
case "$target" in
help|--help|-h)
show_help
exit 0
;;
status|--status|-s)
for t in $(available_targets) ; do
set_target "$t"
show_status
echo
done
exit 0
;;
esac
set_target "$target"
cmd="$1" ; shift
case "$cmd" in
help|--help|-h)
show_help
exit 0
;;
*,*)
re="^[a-z,]+$"
[[ "$cmd" =~ $re ]] || die "bad"
for x in ${cmd//,/ } ; do
echo -e >&2 "# ${BRIGHT_YELLOW}$0 $x $*${RESET}"
if ! $0 "$target" "$x" "$@" ; then
die "failed: $0 $x $*"
fi
done
exit 0
;;
build)
set_build_args
( set -x
docker build "${@}" "${BUILD_ARGS[@]}" -t "${NAME}" -f "Dockerfile-$TARGET" .
)
exit $?
;;
rm|remove)
( set -x
docker image rm -f "${NAME}"
)
exit $?
;;
up|start)
( set -x
docker run -d --init -v "$(pwd):/home/$PROJECT" --user "$USER_UID:$USER_GID" --name "${NAME}" "${NAME}"
)
exit $?
;;
down|stop)
container_ids=( $(docker ps -a --filter "name=${NAME}" --format='{{.ID}}') )
if [ "${#container_ids[@]}" = 0 ]; then
echo "Container ${NAME} is not running."
else
for container_id in "${container_ids[@]}" ; do
( set -x
docker stop "$container_id"
)
done
for container_id in "${container_ids[@]}" ; do
( set -x
docker rm "$container_id"
)
done
fi
exit 0
;;
status)
show_status
exit $?
;;
connect|enter)
container_id="$(docker ps -a --filter "name=${NAME}" --format='{{.ID}}')"
if [ -z "$container_id" ]; then
echo "Container ${NAME} is not running."
else
( set -x
docker exec -it "$container_id" bash
)
fi
;;
run|exec)
container_id="$(docker ps -a --filter "name=${NAME}" --format='{{.ID}}')"
if [ -z "$container_id" ]; then
echo "Container ${NAME} is not running."
exit 1
elif [ $# -eq 0 ]; then
echo "No command provided to run in the container."
exit 1
else
( set -x
#docker exec -w /home/${PROJECT} "$container_id" "$@"
docker exec -i -w /home/${PROJECT} "$container_id" /bin/bash -l -c "$*"
)
fi
;;
*)
die "${0##*/} [ build | remove | start | stop | status | connect ] <target>"
;;
esac