forked from BorisWilhelms/devcontainer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevcontainer.sh
More file actions
executable file
·179 lines (144 loc) · 4.82 KB
/
devcontainer.sh
File metadata and controls
executable file
·179 lines (144 loc) · 4.82 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
#!/bin/bash
if ! [ -x "$(command -v jq)" ]; then
printf "\x1B[31m[ERROR] jq is not installed.\x1B[0m\n"
exit 1
fi
if ! [ -x "$(command -v sed)" ]; then
printf "\x1B[31m[ERROR] sed is not installed (only GNU sed works).\x1B[0m\n"
exit 1
fi
if ! sed --version | grep "sed (GNU sed)" &>/dev/null; then
printf "\x1B[31m[ERROR] GNU sed is not installed.\x1B[0m\n"
exit 1
fi
VERBOSE=0
DOCKER_OPTS=""
debug() {
if [ $VERBOSE == 1 ]; then
printf "\x1B[33m[DEBUG] %s\x1B[0m\n" "${1}"
fi
}
optspec=":v-:"
while getopts "$optspec" opt; do
case ${opt} in
v )
val="${!OPTIND}"
VERBOSE=1
;;
-)
case "${OPTARG}" in
docker-opts)
val="${!OPTIND}"; OPTIND=$((OPTIND + 1))
DOCKER_OPTS=$val
if [[ $VERBOSE = 1 ]]; then
debug "Setting DOCKER_OPTS $DOCKER_OPTS"
fi
;;
docker-opts=*)
val=${OPTARG#*=}
opt=${OPTARG%=$val}
DOCKER_OPTS=$val
if [[ $VERBOSE = 1 ]]; then
debug "Setting DOCKER_OPTS $DOCKER_OPTS"
fi
debug "docker-opts=* optind $OPTIND"
;;
*)
if [ "$OPTERR" = 1 ]; then
echo "Unknown option --${OPTARG}" >&2
exit 5
fi
;;
esac;;
*)
if [ "$OPTERR" = 1 ]; then
echo "Unknown option -${OPTARG}" >&2
exit 5
fi
;;
esac
done
WORKSPACE="${*:$OPTIND:1}"
WORKSPACE="${WORKSPACE:-$PWD}"
if [[ ! -d "$WORKSPACE" ]]; then
echo "Directory $WORKSPACE does not exist!" >&2
exit 6
fi
echo "Using workspace ${WORKSPACE}"
CONFIG_DIR=./.devcontainer
debug "CONFIG_DIR: ${CONFIG_DIR}"
CONFIG_FILE=devcontainer.json
debug "CONFIG_FILE: ${CONFIG_FILE}"
if ! [ -e "$CONFIG_DIR/$CONFIG_FILE" ]; then
echo "Folder contains no devcontainer configuration"
exit
fi
CONFIG="$(cat "$CONFIG_DIR/$CONFIG_FILE")"
# Replacing variables in the config file
localWorkspaceFolderBasename="$(basename "$(realpath "$CONFIG_DIR/..")")"
# shellcheck disable=SC2001
CONFIG="$(echo "$CONFIG" | sed "s#\${localWorkspaceFolderBasename}#$localWorkspaceFolderBasename#g")"
localWorkspaceFolder="$(dirname "$localWorkspaceFolderBasename")"
# shellcheck disable=SC2001
CONFIG="$(echo "$CONFIG" | sed "s#\${localWorkspaceFolder}#$localWorkspaceFolder#g")"
# Remove trailing comma's with sed
CONFIG=$(echo "$CONFIG" | grep -v // | sed -Ez 's#,([[:space:]]*[]}])#\1#gm')
debug "CONFIG: \n${CONFIG}"
if [[ ! -d "$CONFIG_DIR" ]]; then
echo "Config dir '$CONFIG_DIR' does not exist!" >&2
exit 7
fi
cd "$CONFIG_DIR" || return
DOCKER_FILE=$(echo "$CONFIG" | jq -r .dockerFile)
if [ "$DOCKER_FILE" == "null" ]; then
DOCKER_FILE=$(echo "$CONFIG" | jq -r .build.dockerfile)
fi
DOCKER_FILE="$(readlink -f "$DOCKER_FILE")"
debug "DOCKER_FILE: ${DOCKER_FILE}"
if ! [ -e "$DOCKER_FILE" ]; then
echo "Can not find dockerfile ${DOCKER_FILE}"
exit
fi
REMOTE_USER="$(echo "$CONFIG" | jq -r .remoteUser)"
debug "REMOTE_USER: ${REMOTE_USER}"
ARGS=$(echo "$CONFIG" | jq -r '.build.args | to_entries? | map("--build-arg \(.key)=\"\(.value)\"")? | join(" ")')
debug "ARGS: ${ARGS}"
SHELL=$(echo "$CONFIG" | jq -r '.settings."terminal.integrated.shell.linux"')
debug "SHELL: ${SHELL}"
PORTS=$(echo "$CONFIG" | jq -r '.forwardPorts | map("-p \(.):\(.)")? | join(" ")')
debug "PORTS: ${PORTS}"
ENVS=$(echo "$CONFIG" | jq -r '.remoteEnv | to_entries? | map("-e \(.key)=\(.value)")? | join(" ")')
debug "ENVS: ${ENVS}"
WORK_DIR="/workspace"
debug "WORK_DIR: ${WORK_DIR}"
MOUNT="${MOUNT} --mount type=bind,source=${WORKSPACE},target=${WORK_DIR}"
debug "MOUNT: ${MOUNT}"
echo "Building and starting container"
DOCKER_TAG=$(echo "$DOCKER_FILE" | md5sum - | awk '{ print $1 }')
# shellcheck disable=SC2086
docker build -f "$DOCKER_FILE" -t "$DOCKER_TAG" $ARGS .
build_status=$?
if [[ $build_status -ne 0 ]]; then
echo "Building docker image failed..." >&2
exit 7
fi
debug "DOCKER_TAG: ${DOCKER_TAG}"
set -e
PUID=$(id -u)
PGID=$(id -g)
# shellcheck disable=SC2086
docker run -it $DOCKER_OPTS $PORTS $ENVS $MOUNT -w "$WORK_DIR" "$DOCKER_TAG" "$SHELL" -c "\
if [ '$REMOTE_USER' != '' ] && command -v usermod &>/dev/null; \
then \
sudo=''
if [ \"$(stat -f -c '%u' "$(which sudo)")\" = '0' ]; then
sudo=sudo
fi
\$sudo usermod -u $PUID $REMOTE_USER && \
\$sudo groupmod -g $PGID $REMOTE_USER && \
\$sudo passwd -d $REMOTE_USER && \
\$sudo chown $REMOTE_USER:$REMOTE_USER -R ~$REMOTE_USER $WORK_DIR && \
su $REMOTE_USER -s $SHELL; \
else \
$SHELL; \
fi"