-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.sh
82 lines (64 loc) · 2.1 KB
/
runner.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
#!/bin/bash
# Basic runner to enable execution outside of vscode
# Check for jq
if ! [ -x "$(command -v jq)" ]; then
printf "\x1B[31m[ERROR] jq is not installed.\x1B[0m\n"
exit 1
fi
OPTIND=1
VERBOSE=0
while getopts "v" opt; do
case ${opt} in
v ) VERBOSE=1 ;;
esac
done
debug() {
if [ $VERBOSE == 1 ]; then
printf "\x1B[33m[DEBUG] ${1}\x1B[0m\n"
fi
}
WORKSPACE=${1:-`pwd`}
CURRENT_DIR=${PWD##*/}
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 | grep -v //)
debug "CONFIG: \n${CONFIG}"
cd $CONFIG_DIR
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}"
if ! [ "$REMOTE_USER" == "null" ]; then
REMOTE_USER="-u ${REMOTE_USER}"
fi
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_IMAGE_HASH=$(docker build -f $DOCKER_FILE $ARGS .)
debug "DOCKER_IMAGE_HASH: ${DOCKER_IMAGE_HASH}"
docker run -it $REMOTE_USER $PORTS $ENVS $MOUNT -w $WORK_DIR $DOCKER_IMAGE_HASH $SHELL