644df355399f17cf7e70ca7346281f56ff7032c4
[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     IMAGES_LIST=$(docker images $REGISTRY/$REPO/$NAME-$FLAVOUR:* --format '{{.Tag}}')
97     VER_NUM=$(echo "$IMAGES_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 ! List of found images:"
100         echo "$IMAGES_LIST"
101         exit 1
102     elif [ $VER_NUM -lt 1 ]; then
103         echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
104         exit 1
105     fi
106     if [ "$IMAGES_LIST" = "" ]; then
107         echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
108         usage
109         exit 1
110     fi
111
112     IMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$VERSION
113 fi
114
115 USER=$(id -un)
116 echo "Using instance ID #$ID (user $(id -un))"
117
118 NAME=agl-xds-$(hostname|cut -f1 -d'.')-$ID-$USER
119
120 docker ps -a |grep "$NAME" > /dev/null
121 [ "$?" = "0" ] && { echo "Image name already exist ! (use -h option to read help)"; exit 1; }
122
123 XDS_WKS=$HOME/xds-workspace
124 XDTDIR=$XDS_WKS/.xdt_$ID
125
126 SSH_PORT=$((2222 + ID))
127 WWW_PORT=$((8000 + ID))
128 BOOT_PORT=$((69 + ID))
129 NBD_PORT=$((10809 + ID))
130
131 # Delete container on error
132 creation_done=false
133 trap "cleanExit" 0 1 2 15
134 cleanExit ()
135 {
136     if [ "$creation_done" != "true" -a "$NO_CLEANUP" != "true" ]; then
137         docker rm -f "${NAME}" > /dev/null 2>&1
138     fi
139 }
140
141 ### Create the new container
142 mkdir -p $XDS_WKS $XDTDIR  || exit 1
143 docker run \
144         --publish=${SSH_PORT}:22 \
145         --publish=${WWW_PORT}:8000 \
146         --publish=${BOOT_PORT}:69/udp \
147         --publish=${NBD_PORT}:10809 \
148         --detach=true \
149         --hostname="$NAME" --name="$NAME" \
150         --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro \
151         -v $XDS_WKS:/home/$DOCKER_USER/xds-workspace \
152         -v $XDTDIR:/xdt \
153     $USER_VOLUME_OPTION \
154         -it $IMAGE
155 if [ "$?" != "0" ]; then
156     echo "An error was encountered while creating docker container."
157     exit 1
158 fi
159
160 ### Ssh key
161 echo "Copying your identity to container $NAME"
162 echo -n wait ssh service .
163 res=3
164 max=30
165 count=0
166 while [ $res -ne 0 ] && [ $count -le $max ]; do
167     sleep 1
168     docker exec ${NAME} bash -c "systemctl status ssh" 2>/dev/null 1>&2
169     res=$?
170     echo -n "."
171     count=$(expr $count + 1);
172 done
173 echo
174
175 ssh-keygen -R [localhost]:$SSH_PORT -f ~/.ssh/known_hosts
176 docker exec ${NAME} bash -c "mkdir -p /home/$DOCKER_USER/.ssh"
177 docker cp ~/.ssh/id_rsa.pub ${NAME}:/home/$DOCKER_USER/.ssh/authorized_keys
178 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/*"
179 ssh -o StrictHostKeyChecking=no -p $SSH_PORT $DOCKER_USER@localhost exit
180
181 echo "You can now login using:"
182 echo "   ssh -p $SSH_PORT $DOCKER_USER@localhost"
183
184
185 ### User / Group id
186 if ($UPDATE_UID); then
187     echo -n "Setup docker user and group id to match yours "
188
189     docker exec -t ${NAME} bash -c "/bin/loginctl kill-user devel"
190     res=3
191     max=30
192     count=0
193     while [ $res -ne 1 ] && [ $count -le $max ]; do
194         sleep 1
195         docker exec ${NAME} bash -c "loginctl user-status devel |grep sd-pam" 2>/dev/null 1>&2
196         res=$?
197         echo -n "."
198         count=$(expr $count + 1);
199     done
200
201     echo -n " ."
202
203      # Set uid
204     docker exec -t ${NAME} bash -c "id $(id -u)" > /dev/null 2>&1
205     if [ "$?" = "0" -a  "$(id -u)" != "1664" ]; then
206         echo "Cannot set docker devel user id to your id: conflict id $(id -u) !"
207         exit 1
208     fi
209     docker exec -t ${NAME} bash -c "usermod -u $(id -u) $DOCKER_USER" || exit 1
210     echo -n "."
211
212     # Set gid
213     docker exec -t ${NAME} bash -c "grep $(id -g) /etc/group" > /dev/null 2>&1
214     if [ "$?" = "0" ]; then
215         docker exec -t ${NAME} bash -c "usermod -g $(id -g) $DOCKER_USER" || exit 1
216     else
217         docker exec -t ${NAME} bash -c "groupmod -g $(id -g) $DOCKER_USER" || exit 1
218     fi
219     echo -n "."
220
221     docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER /home/$DOCKER_USER" || exit 1
222     echo -n "."
223     docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER /tmp/xds*"
224     echo -n "."
225     docker exec -t ${NAME} bash -c "systemctl start autologin"
226     echo -n "."
227     ssh -p $SSH_PORT $DOCKER_USER@localhost -- "systemctl --user start xds-server" || exit 1
228     echo "."
229     docker restart ${NAME}
230 fi
231
232 creation_done=true
233
234 ### Force xds-server restart
235 if ($FORCE_RESTART); then
236     echo "Restart xds-server..."
237     ssh -p $SSH_PORT $DOCKER_USER@localhost -- "systemctl --user restart xds-server" || exit 1
238 fi
239
240 echo "Done, docker container $NAME is ready to be used."