forked from HKUST-Aerial-Robotics/Nxt-FC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_build.sh
executable file
·139 lines (121 loc) · 3.13 KB
/
docker_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
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
#!/bin/bash
script_dir=$(cd $(dirname $0);pwd)
# Config
# version_map[<branch_name>]=<docker_image_name>
declare -A version_map
version_map["develop_v1.14.x"]="px4io/px4-dev-nuttx-focal:2023-12-04"
# Set parameters
DOCEKR_NAME="";
DOCKER_IMAGE_VERSION="";
VOLUME_PATH="";
set_param(){
DOCKER_IMAGE_VERSION=${version_map[$1]}
DOCEKR_NAME="$2"
echo "Docker image version: ${DOCKER_IMAGE_VERSION}"
}
# Git functions
switch_branch(){
cd $script_dir/PX4-Autopilot
# Check if branch exists
if git branch --list | grep -q "$1"; then
echo "Branch $1 exists"
else
echo "Branch $1 does not exist"
fi
# Switch branch
if [[ "$2" != [Yy] ]]; then
echo "Warning: this will remove all changes including untracked files"
read -p "Confirm?(Y/N)" ans
if [[ "$ans" == [Yy] ]]; then
echo "Confirmed"
git checkout -f $1
git clean -xdf -f
git submodule update --recursive
git submodule foreach --recursive git fetch --all
git submodule foreach --recursive git reset --hard
git submodule foreach --recursive git clean -fdx -f
echo "Successfully switched to branch $1"
else
echo "Canceled"
exit 1
fi
fi
}
# Docker functions
pull_docker(){
docker pull "${DOCKER_IMAGE_VERSION}"
}
check_image(){
docker_image_exist=$(docker images ${DOCKER_IMAGE_VERSION})
if [ -n "$docker_image_exist" ]
then
echo "Docker image is downloaded will build the containter"
return 0
else
echo "erro: Docker images need to be downloaded"
return 1
fi
}
stop_container(){
docker stop ${DOCEKR_NAME}
}
start_containter(){
docker start ${DOCEKR_NAME}
}
build_frameware(){
docker exec ${DOCEKR_NAME} bash -c "cd /src/NxtPX4/PX4-Autopilot; make clean ; make $1; chmod 777 -R /src/NxtPX4/PX4-Autopilot"
stop_container
}
build_run_container(){
# check mount dir
VOLUME_PATH=$script_dir;
echo "Mount ${VOLUME_PATH} to docker:/src/NxtPX4/ "
if [ ! -d $VOLUME_PATH ]
then
echo "Path: ${VOLUME_PATH} is not NxtPX4 repo please run this script under xxx/NxtPX4/PX4-Autopilot/"
fi
docker run --rm -dit --privileged \
--env=LOCAL_USER_ID="$(id -u)" \
-v ${VOLUME_PATH}:/src/NxtPX4/:rw \
-v /tmp/.X11-unix:/tmp/.X11-unix:ro \
-e DISPLAY=:0 \
-p 14556:14556/udp \
--name=${DOCEKR_NAME} ${DOCKER_IMAGE_VERSION}
return 0
}
help(){
echo "Usage: ./docker_build.sh <branch_name> <frameware_name>"
echo "Example: ./docker_build.sh develop_v1.14.x hkust_nxt-v1"
echo " ./docker_build.sh develop_v1.14.x hkust_nxt-v1_bootloader"
}
check_docker(){
echo "Check Docker......"
docker -v
if [ $? -ne 0 ]
then
echo "Install docker using the following command:"
echo "curl -sSL https://get.daocloud.io/docker | sh"
exit 1
fi
}
main(){
switch_branch $1 $3
set_param $1 $2
check_docker
docker_exist=$(docker ps -a|grep ${DOCEKR_NAME})
if [ -n "$docker_exist" ]
then
echo "Docker container exist start container to compile"
start_containter
else
echo "build run container"
build_run_container
fi
build_frameware $2
}
if [ $# -ne 2 ] && [ $# -ne 3 ]
then
help
else
main $1 $2 $3
fi