Check dependency tools before running tests
[src/xds/xds-server.git] / scripts / xds-docker-create-container.sh
index f493dde..7990c45 100755 (executable)
@@ -1,4 +1,23 @@
 #!/bin/bash
+ ###########################################################################
+# Copyright 2017 IoT.bzh
+#
+# author: Sebastien Douheret <sebastien@iot.bzh>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+###########################################################################
+
+# shellcheck disable=SC2086
 
 ##########################################
 # WARNING WARNING WARNING WARNING
@@ -8,12 +27,6 @@
 # You should customize it to fit your environment and in particular
 # adjust the paths and permissions where needed.
 #
-# Note that sharing volumes with host system is not mandatory: it
-# was just added for performances reasons: building from a SSD is
-# just faster than using the container filesystem: that's why /xdt is
-# mounted from there. Same applies to ~/mirror and ~/share, which are
-# just 2 convenient folders to store reference build caches (used in prepare_meta script)
-#
 ##########################################
 
 REGISTRY=docker.automotivelinux.org
@@ -93,24 +106,24 @@ done
 # Dynamically retrieve image name
 if [ "$IMAGE" = "" ]; then
 
-    IMAGES_LIST=$(docker images $REGISTRY/$REPO/$NAME-$FLAVOUR --format '{{.Tag}}')
-    VER_NUM=$(echo "$IMAGES_LIST" | wc -l)
-    if [ $VER_NUM -gt 1 ]; then
+    VERSION_LIST=$(docker images $REGISTRY/$REPO/$NAME-$FLAVOUR --format '{{.Tag}}')
+    VER_NUM=$(echo "$VERSION_LIST" | wc -l)
+    if [ "$VER_NUM" -gt 1 ]; then
         echo "ERROR: more than one xds image found, please set explicitly the image to use !"
         echo "List of found images:"
-        echo "$IMAGES_LIST"
+        echo "$VERSION_LIST"
         exit 1
-    elif [ $VER_NUM -lt 1 ]; then
+    elif [ "$VER_NUM" -lt 1 ]; then
         echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
         exit 1
     fi
-    if [ "$IMAGES_LIST" = "" ]; then
+    if [ "$VERSION_LIST" = "" ]; then
         echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
         usage
         exit 1
     fi
 
-    IMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$VERSION
+    IMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$VERSION_LIST
 fi
 
 USER=$(id -un)
@@ -118,8 +131,10 @@ echo "Using instance ID #$ID (user $(id -un))"
 
 NAME=agl-xds-$(hostname|cut -f1 -d'.')-$ID-$USER
 
-docker ps -a |grep "$NAME" > /dev/null
-[ "$?" = "0" ] && { echo "Image name already exist ! (use -h option to read help)"; exit 1; }
+if docker ps -a |grep "$NAME" > /dev/null; then
+    echo "Image name already exist ! (use -h option to read help)"
+    exit 1
+fi
 
 XDS_WKS=$HOME/xds-workspace
 XDTDIR=$XDS_WKS/.xdt_$ID
@@ -134,14 +149,15 @@ creation_done=false
 trap "cleanExit" 0 1 2 15
 cleanExit ()
 {
-    if [ "$creation_done" != "true" -a "$NO_CLEANUP" != "true" ]; then
+    if [ "$creation_done" != "true" ] && [ "$NO_CLEANUP" != "true" ]; then
+        echo "Error detected, remove unusable docker image ${NAME}"
         docker rm -f "${NAME}" > /dev/null 2>&1
     fi
 }
 
 ### Create the new container
 mkdir -p $XDS_WKS $XDTDIR  || exit 1
-docker run \
+if ! docker run \
        --publish=${SSH_PORT}:22 \
        --publish=${WWW_PORT}:8000 \
        --publish=${BOOT_PORT}:69/udp \
@@ -152,8 +168,8 @@ docker run \
        -v $XDS_WKS:/home/$DOCKER_USER/xds-workspace \
        -v $XDTDIR:/xdt \
     $USER_VOLUME_OPTION \
-       -it $IMAGE
-if [ "$?" != "0" ]; then
+       -it $IMAGE;
+then
     echo "An error was encountered while creating docker container."
     exit 1
 fi
@@ -169,15 +185,16 @@ while [ $res -ne 0 ] && [ $count -le $max ]; do
     docker exec ${NAME} bash -c "systemctl status ssh" 2>/dev/null 1>&2
     res=$?
     echo -n "."
-    count=$(expr $count + 1);
+    count=$((count + 1));
 done
 echo
 
-ssh-keygen -R [localhost]:$SSH_PORT -f ~/.ssh/known_hosts
-docker exec ${NAME} bash -c "mkdir -p /home/$DOCKER_USER/.ssh"
-docker cp ~/.ssh/id_rsa.pub ${NAME}:/home/$DOCKER_USER/.ssh/authorized_keys
-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/*"
-ssh -o StrictHostKeyChecking=no -p $SSH_PORT $DOCKER_USER@localhost exit
+[ -f ~/.ssh/known_hosts ] && { ssh-keygen -R "[localhost]:$SSH_PORT" -f ~/.ssh/known_hosts || exit 1; }
+[ ! -f ~/.ssh/id_rsa.pub ] && { ssh-keygen -t rsa -f ~/.ssh/id_rsa -P "" > /dev/null || exit 1; }
+docker exec ${NAME} bash -c "mkdir -p /home/$DOCKER_USER/.ssh" || exit 1
+docker cp -L ~/.ssh/id_rsa.pub ${NAME}:/home/$DOCKER_USER/.ssh/authorized_keys || exit 1
+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
+ssh -n -o StrictHostKeyChecking=no -p $SSH_PORT $DOCKER_USER@localhost exit || exit 1
 
 echo "You can now login using:"
 echo "   ssh -p $SSH_PORT $DOCKER_USER@localhost"
@@ -196,14 +213,13 @@ if ($UPDATE_UID); then
         docker exec ${NAME} bash -c "loginctl user-status devel |grep sd-pam" 2>/dev/null 1>&2
         res=$?
         echo -n "."
-        count=$(expr $count + 1);
+        count=$((count + 1));
     done
 
     echo -n " ."
 
      # Set uid
-    docker exec -t ${NAME} bash -c "id $(id -u)" > /dev/null 2>&1
-    if [ "$?" = "0" -a  "$(id -u)" != "1664" ]; then
+    if docker exec -t ${NAME} bash -c "id $(id -u)" > /dev/null 2>&1 && [ "$(id -u)" != "1664" ]; then
         echo "Cannot set docker devel user id to your id: conflict id $(id -u) !"
         exit 1
     fi
@@ -211,8 +227,7 @@ if ($UPDATE_UID); then
     echo -n "."
 
     # Set gid
-    docker exec -t ${NAME} bash -c "grep $(id -g) /etc/group" > /dev/null 2>&1
-    if [ "$?" = "0" ]; then
+    if docker exec -t ${NAME} bash -c "grep $(id -g) /etc/group" > /dev/null 2>&1; then
         docker exec -t ${NAME} bash -c "usermod -g $(id -g) $DOCKER_USER" || exit 1
     else
         docker exec -t ${NAME} bash -c "groupmod -g $(id -g) $DOCKER_USER" || exit 1
@@ -225,7 +240,7 @@ if ($UPDATE_UID); then
     echo -n "."
     docker exec -t ${NAME} bash -c "systemctl start autologin"
     echo -n "."
-    ssh -p $SSH_PORT $DOCKER_USER@localhost -- "systemctl --user start xds-server" || exit 1
+    ssh -n -p $SSH_PORT $DOCKER_USER@localhost "systemctl --user start xds-server" || exit 1
     echo "."
     docker restart ${NAME}
 fi
@@ -235,7 +250,7 @@ creation_done=true
 ### Force xds-server restart
 if ($FORCE_RESTART); then
     echo "Restart xds-server..."
-    ssh -p $SSH_PORT $DOCKER_USER@localhost -- "systemctl --user restart xds-server" || exit 1
+    ssh -n -p $SSH_PORT $DOCKER_USER@localhost "systemctl --user restart xds-server" || exit 1
 fi
 
 echo "Done, docker container $NAME is ready to be used."