-
Notifications
You must be signed in to change notification settings - Fork 1
/
dev-env
54 lines (44 loc) · 1.61 KB
/
dev-env
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
#! /bin/bash
set -e
VOLUME_NAME=${1:?"Use $0 <environment_folder> to start the development environment"}
FREE_PORT=${2:-8000}
CODE_PORT=${3:-8080}
function extract_payload {
match=$(grep --text --line-number '^PAYLOAD:$' $0 | cut -d ':' -f 1)
payload_start=$((match + 1))
tail -n +$payload_start $0 | tar -xzf -
}
# $1 workdir
function create_dockerfile {
distro=`jq -cr '.host_distro' ${VOLUME_NAME}/manifest.json`
extra_pkgs=`jq -r '.extra_pkgs // "" | @sh' ${VOLUME_NAME}/manifest.json`
sed -e "\
s/@DISTRO@/${distro}/;\
s/@EXTRA_PKGS@/${extra_pkgs}/;\
s/@ENV_FOLDER@/${VOLUME_NAME}/" Dockerfile.template > Dockerfile
}
function create_container {
if [[ $(docker volume ls --filter "name=${VOLUME_NAME}" -q | wc -l ) -eq 0 ]]; then
echo "creating volume and copying setup script into it"
docker volume create --ignore ${VOLUME_NAME}
docker run -it --rm -v ${VOLUME_NAME}:/workdir busybox \
/bin/sh -c "mkdir -p /workdir/src && chown -R 1000:1000 /workdir"
fi
echo "Building the development container"
docker build -t ${VOLUME_NAME} -f Dockerfile .
}
function start_container {
echo "Cleaning up old containers"
docker rmi $(docker images -qa -f 'dangling=true') || echo "Nothing to remove"
echo "Starting the development container"
docker run --rm --cap-add=sys_nice --cap-add=NET_RAW -it -v ${VOLUME_NAME}:/home/yoctouser -p ${FREE_PORT}:8000 -p ${CODE_PORT}:8080 ${VOLUME_NAME}
}
function main {
extract_payload
create_dockerfile
create_container
rm -rf resources Dockerfile*
start_container
}
main ${@}
exit 0