Check dependency tools before running tests
[src/xds/xds-server.git] / scripts / xds-docker-create-container.sh
1 #!/bin/bash
2  ###########################################################################
3 # Copyright 2017 IoT.bzh
4 #
5 # author: Sebastien Douheret <sebastien@iot.bzh>
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 ###########################################################################
19
20 # shellcheck disable=SC2086
21
22 ##########################################
23 # WARNING WARNING WARNING WARNING
24 #
25 # This script is an example to start a new AGL XDS container
26 #
27 # You should customize it to fit your environment and in particular
28 # adjust the paths and permissions where needed.
29 #
30 ##########################################
31
32 REGISTRY=docker.automotivelinux.org
33 REPO=agl
34 NAME=worker
35 FLAVOUR=xds
36 VERSION=4.0
37
38 # ---------------------------------------------------
39 # --- computed - don't touch !
40 # ---------------------------------------------------
41 DOCKER_USER=devel
42
43 DEFIMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$VERSION
44
45 function usage() {
46         echo "Usage: $(basename $0) [-h|--help] [-fr] [-id <instance container ID>] "
47     echo "          [-nc] [-nuu] [-v|--volume <inpath:outpath>] [image name]"
48         echo "Image name is optional; 'docker images' is used by default to get image"
49         echo "Default image:"
50     echo " $DEFIMAGE"
51     echo ""
52     echo "Options:"
53     echo " -fr | --force-restart   Force restart of xds-server service"
54     echo " -id                     Instance ID used to build container name, a positive integer (0,1,2,...)"
55     echo " -nuu | --no-uid-update  Don't update user/group id within docker"
56     echo " -v | --volume           Additional docker volume to bind, syntax is -v /InDockerPath:/HostPath "
57         exit 1
58 }
59
60 ID=""
61 IMAGE=""
62 FORCE_RESTART=false
63 UPDATE_UID=true
64 USER_VOLUME_OPTION=""
65 NO_CLEANUP=false
66 while [ $# -ne 0 ]; do
67     case $1 in
68         -h|--help|"")
69             usage
70             ;;
71         -fr|--force-restart)
72             FORCE_RESTART=true
73             ;;
74         -nc|--no-cleanup)
75             NO_CLEANUP=true
76             ;;
77         -nuu|--no-uid-update)
78             UPDATE_UID=false
79             ;;
80         -v|--volume)
81             shift
82             if [[ "$1" =~ .*:.* ]]; then
83                 USER_VOLUME_OPTION="-v $1"
84             else
85                 echo "Invalid volume option, format must be /InDockerPath:/hostPath"
86                 exit 1
87             fi
88             ;;
89         -id)
90             shift
91             ID=$1
92             ;;
93         *)
94             if [[ "$1" =~ ^[\.0-9]+$ ]]; then
95                 IMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$1
96             else
97                 IMAGE=$1
98             fi
99             ;;
100     esac
101     shift
102 done
103
104 [ "$ID" = "" ] && ID=0
105
106 # Dynamically retrieve image name
107 if [ "$IMAGE" = "" ]; then
108
109     VERSION_LIST=$(docker images $REGISTRY/$REPO/$NAME-$FLAVOUR --format '{{.Tag}}')
110     VER_NUM=$(echo "$VERSION_LIST" | wc -l)
111     if [ "$VER_NUM" -gt 1 ]; then
112         echo "ERROR: more than one xds image found, please set explicitly the image to use !"
113         echo "List of found images:"
114         echo "$VERSION_LIST"
115         exit 1
116     elif [ "$VER_NUM" -lt 1 ]; then
117         echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
118         exit 1
119     fi
120     if [ "$VERSION_LIST" = "" ]; then
121         echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
122         usage
123         exit 1
124     fi
125
126     IMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$VERSION_LIST
127 fi
128
129 USER=$(id -un)
130 echo "Using instance ID #$ID (user $(id -un))"
131
132 NAME=agl-xds-$(hostname|cut -f1 -d'.')-$ID-$USER
133
134 if docker ps -a |grep "$NAME" > /dev/null; then
135     echo "Image name already exist ! (use -h option to read help)"
136     exit 1
137 fi
138
139 XDS_WKS=$HOME/xds-workspace
140 XDTDIR=$XDS_WKS/.xdt_$ID
141
142 SSH_PORT=$((2222 + ID))
143 WWW_PORT=$((8000 + ID))
144 BOOT_PORT=$((69 + ID))
145 NBD_PORT=$((10809 + ID))
146
147 # Delete container on error
148 creation_done=false
149 trap "cleanExit" 0 1 2 15
150 cleanExit ()
151 {
152     if [ "$creation_done" != "true" ] && [ "$NO_CLEANUP" != "true" ]; then
153         echo "Error detected, remove unusable docker image ${NAME}"
154         docker rm -f "${NAME}" > /dev/null 2>&1
155     fi
156 }
157
158 ### Create the new container
159 mkdir -p $XDS_WKS $XDTDIR  || exit 1
160 if ! docker run \
161         --publish=${SSH_PORT}:22 \
162         --publish=${WWW_PORT}:8000 \
163         --publish=${BOOT_PORT}:69/udp \
164         --publish=${NBD_PORT}:10809 \
165         --detach=true \
166         --hostname="$NAME" --name="$NAME" \
167         --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro \
168         -v $XDS_WKS:/home/$DOCKER_USER/xds-workspace \
169         -v $XDTDIR:/xdt \
170     $USER_VOLUME_OPTION \
171         -it $IMAGE;
172 then
173     echo "An error was encountered while creating docker container."
174     exit 1
175 fi
176
177 ### Ssh key
178 echo "Copying your identity to container $NAME"
179 echo -n wait ssh service .
180 res=3
181 max=30
182 count=0
183 while [ $res -ne 0 ] && [ $count -le $max ]; do
184     sleep 1
185     docker exec ${NAME} bash -c "systemctl status ssh" 2>/dev/null 1>&2
186     res=$?
187     echo -n "."
188     count=$((count + 1));
189 done
190 echo
191
192 [ -f ~/.ssh/known_hosts ] && { ssh-keygen -R "[localhost]:$SSH_PORT" -f ~/.ssh/known_hosts || exit 1; }
193 [ ! -f ~/.ssh/id_rsa.pub ] && { ssh-keygen -t rsa -f ~/.ssh/id_rsa -P "" > /dev/null || exit 1; }
194 docker exec ${NAME} bash -c "mkdir -p /home/$DOCKER_USER/.ssh" || exit 1
195 docker cp -L ~/.ssh/id_rsa.pub ${NAME}:/home/$DOCKER_USER/.ssh/authorized_keys || exit 1
196 docker exec ${NAME} bash -c "chown $DOCKER_USER:$DOCKER_USER -R /home/$DOCKER_USER/.ssh ;chmod 0700 /home/$DOCKER_USER/.ssh; chmod 0600 /home/$DOCKER_USER/.ssh/*" || exit 1
197 ssh -n -o StrictHostKeyChecking=no -p $SSH_PORT $DOCKER_USER@localhost exit || exit 1
198
199 echo "You can now login using:"
200 echo "   ssh -p $SSH_PORT $DOCKER_USER@localhost"
201
202
203 ### User / Group id
204 if ($UPDATE_UID); then
205     echo -n "Setup docker user and group id to match yours "
206
207     docker exec -t ${NAME} bash -c "/bin/loginctl kill-user devel"
208     res=3
209     max=30
210     count=0
211     while [ $res -ne 1 ] && [ $count -le $max ]; do
212         sleep 1
213         docker exec ${NAME} bash -c "loginctl user-status devel |grep sd-pam" 2>/dev/null 1>&2
214         res=$?
215         echo -n "."
216         count=$((count + 1));
217     done
218
219     echo -n " ."
220
221      # Set uid
222     if docker exec -t ${NAME} bash -c "id $(id -u)" > /dev/null 2>&1 && [ "$(id -u)" != "1664" ]; then
223         echo "Cannot set docker devel user id to your id: conflict id $(id -u) !"
224         exit 1
225     fi
226     docker exec -t ${NAME} bash -c "usermod -u $(id -u) $DOCKER_USER" || exit 1
227     echo -n "."
228
229     # Set gid
230     if docker exec -t ${NAME} bash -c "grep $(id -g) /etc/group" > /dev/null 2>&1; then
231         docker exec -t ${NAME} bash -c "usermod -g $(id -g) $DOCKER_USER" || exit 1
232     else
233         docker exec -t ${NAME} bash -c "groupmod -g $(id -g) $DOCKER_USER" || exit 1
234     fi
235     echo -n "."
236
237     docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER /home/$DOCKER_USER" || exit 1
238     echo -n "."
239     docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER /tmp/xds*"
240     echo -n "."
241     docker exec -t ${NAME} bash -c "systemctl start autologin"
242     echo -n "."
243     ssh -n -p $SSH_PORT $DOCKER_USER@localhost "systemctl --user start xds-server" || exit 1
244     echo "."
245     docker restart ${NAME}
246 fi
247
248 creation_done=true
249
250 ### Force xds-server restart
251 if ($FORCE_RESTART); then
252     echo "Restart xds-server..."
253     ssh -n -p $SSH_PORT $DOCKER_USER@localhost "systemctl --user restart xds-server" || exit 1
254 fi
255
256 echo "Done, docker container $NAME is ready to be used."