2 ###########################################################################
3 # Copyright 2017 IoT.bzh
5 # author: Sebastien Douheret <sebastien@iot.bzh>
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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 ###########################################################################
20 # shellcheck disable=SC2086
22 ##########################################
23 # WARNING WARNING WARNING WARNING
25 # This script is an example to start a new AGL XDS container
27 # You should customize it to fit your environment and in particular
28 # adjust the paths and permissions where needed.
30 ##########################################
32 REGISTRY=docker.automotivelinux.org
38 # ---------------------------------------------------
39 # --- computed - don't touch !
40 # ---------------------------------------------------
43 DEFIMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$VERSION
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"
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 "
66 while [ $# -ne 0 ]; do
82 if [[ "$1" =~ .*:.* ]]; then
83 USER_VOLUME_OPTION="-v $1"
85 echo "Invalid volume option, format must be /InDockerPath:/hostPath"
94 if [[ "$1" =~ ^[\.0-9]+$ ]]; then
95 IMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$1
104 [ "$ID" = "" ] && ID=0
106 # Dynamically retrieve image name
107 if [ "$IMAGE" = "" ]; then
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:"
116 elif [ "$VER_NUM" -lt 1 ]; then
117 echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
120 if [ "$VERSION_LIST" = "" ]; then
121 echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
126 IMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$VERSION_LIST
130 echo "Using instance ID #$ID (user $(id -un))"
132 NAME=agl-xds-$(hostname|cut -f1 -d'.')-$ID-$USER
134 if docker ps -a |grep "$NAME" > /dev/null; then
135 echo "Image name already exist ! (use -h option to read help)"
139 XDS_WKS=$HOME/xds-workspace
140 XDTDIR=$XDS_WKS/.xdt_$ID
142 SSH_PORT=$((2222 + ID))
143 WWW_PORT=$((8000 + ID))
144 BOOT_PORT=$((69 + ID))
145 NBD_PORT=$((10809 + ID))
147 # Delete container on error
149 trap "cleanExit" 0 1 2 15
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
158 ### Create the new container
159 mkdir -p $XDS_WKS $XDTDIR || exit 1
161 --publish=${SSH_PORT}:22 \
162 --publish=${WWW_PORT}:8000 \
163 --publish=${BOOT_PORT}:69/udp \
164 --publish=${NBD_PORT}:10809 \
166 --hostname="$NAME" --name="$NAME" \
167 --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro \
168 -v $XDS_WKS:/home/$DOCKER_USER/xds-workspace \
170 $USER_VOLUME_OPTION \
173 echo "An error was encountered while creating docker container."
178 echo "Copying your identity to container $NAME"
179 echo -n wait ssh service .
183 while [ $res -ne 0 ] && [ $count -le $max ]; do
185 docker exec ${NAME} bash -c "systemctl status ssh" 2>/dev/null 1>&2
188 count=$((count + 1));
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
199 echo "You can now login using:"
200 echo " ssh -p $SSH_PORT $DOCKER_USER@localhost"
204 if ($UPDATE_UID); then
205 echo -n "Setup docker user and group id to match yours "
207 docker exec -t ${NAME} bash -c "/bin/loginctl kill-user devel"
211 while [ $res -ne 1 ] && [ $count -le $max ]; do
213 docker exec ${NAME} bash -c "loginctl user-status devel |grep sd-pam" 2>/dev/null 1>&2
216 count=$((count + 1));
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) !"
226 docker exec -t ${NAME} bash -c "usermod -u $(id -u) $DOCKER_USER" || exit 1
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
233 docker exec -t ${NAME} bash -c "groupmod -g $(id -g) $DOCKER_USER" || exit 1
237 docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER /home/$DOCKER_USER" || exit 1
239 docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER /tmp/xds*"
241 docker exec -t ${NAME} bash -c "systemctl start autologin"
243 ssh -n -p $SSH_PORT $DOCKER_USER@localhost "systemctl --user start xds-server" || exit 1
245 docker restart ${NAME}
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
256 echo "Done, docker container $NAME is ready to be used."