-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.sh
executable file
·86 lines (80 loc) · 2.49 KB
/
build.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
#!/bin/sh
CONTAINER=service-app
SERVICE=service
ROOT=./
IMAGE=igorskh/service-app
CONFIG_PATH_HOST=
CONFIG_PATH_CONT=
GUI=false
usage() {
echo "Build Docker container"
echo ""
echo "./build.sh"
echo "\t-h --help"
echo "\t--container=$CONTAINER"
echo "\t--service=$SERVICE"
echo "\t--root=$ROOT"
echo "\t--image=$IMAGE"
echo "\t--config-path-host=$CONFIG_PATH_HOST"
echo "\t--config-path-cont=$CONFIG_PATH_CONT"
echo "\t--gui=$GUI"
echo ""
}
while [ "$1" != "" ]; do
PARAM=$(echo $1 | awk -F= '{print $1}')
VALUE=$(echo $1 | awk -F= '{print $2}')
case $PARAM in
-h | --help)
usage
exit 0
;;
-c | --container)
CONTAINER=$VALUE
;;
-s | --service)
SERVICE=$VALUE
;;
-r | --root)
ROOT=$VALUE
;;
-i | --image)
IMAGE=$VALUE
;;
--config-path-host)
CONFIG_PATH_HOST=$VALUE
;;
--config-path-cont)
CONFIG_PATH_CONT=$VALUE
;;
--gui)
GUI=$VALUE
;;
*)
echo "ERROR: unknown parameter \"$PARAM\""
usage
exit 1
;;
esac
shift
done
BUILD_REQUIRED=true
if [ ! "$(docker images -a | grep $IMAGE)" ]; then
docker build --build-arg SERVICE_FOLDER=$SERVICE --build-arg WORKFOLDER=$SRC -t $IMAGE --force-rm $ROOT
BUILD_REQUIRED=false
[ ! "$(docker images -a | grep $IMAGE)" ] && echo "Unable to create image $IMAGE" && exit 1
fi
RUN_ARGS="--privileged -di -v /lib/modules:/lib/modules --network=host -d --name $CONTAINER"
[ ! -z "$CONFIG_PATH_HOST" ] && echo "Config path configured" && RUN_ARGS="$RUN_ARGS -v $CONFIG_PATH_HOST:$CONFIG_PATH_CONT"
[ $GUI = true ] && echo "GUI configured" && RUN_ARGS="$RUN_ARGS -e DISPLAY=$DISPLAY -v /tmp/.X11-unix/:/tmp/.X11-unix"
[ ! "$(docker ps -a --format {{.Names}} | grep $CONTAINER)" ] && docker run $RUN_ARGS $IMAGE
[ ! "$(docker ps -a --format {{.Names}} | grep $CONTAINER)" ] && echo "Unable to create container $CONTAINER from $IMAGE" && exit 1
if $BUILD_REQUIRED; then
echo Preserving build folder
docker exec -w / $CONTAINER /bin/sh -c "mv /$SERVICE/build /tmp_build"
docker exec -w / $CONTAINER /bin/sh -c "rm -rf /$SERVICE"
echo Copying files from $ROOT to $CONTAINER:/$SERVICE
docker cp $ROOT/. $CONTAINER:/$SERVICE
echo Move build folder back
docker exec -w / $CONTAINER /bin/sh -c "rm -rf /$SERVICE/build && mv /tmp_build /$SERVICE/build"
docker exec -w /$SERVICE/build $CONTAINER /bin/sh -c "make"
fi