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