Add --volume option support.
[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 CURDIR=$(cd $(dirname $0) && pwd -P)
20
21 REGISTRY=docker.automotivelinux.org
22 REPO=agl
23 NAME=worker
24 FLAVOUR=xds
25 VERSION=4.0
26
27 # ---------------------------------------------------
28 # --- computed - don't touch !
29 # ---------------------------------------------------
30 DOCKER_USER=devel
31
32 DEFIMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$VERSION
33
34 function usage() {
35         echo "Usage: $(basename $0) [-h] [-fr] [-v] <instance ID> [image name]"  >&2
36         echo "Instance ID must be 0 or a positive integer (1,2,...)" >&2
37         echo "Image name is optional: 'make show-image' is used by default to get image" >&2
38         echo "Default image: $DEFIMAGE" >&2
39     echo ""
40     echo "Options:"
41     echo " -fr | --force-restart   Force restart of xds-server service"
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 while [ $# -ne 0 ]; do
53     case $1 in
54         -h|--help|"")
55             usage
56             ;;
57         -fr|--force-restart)
58             FORCE_RESTART=true
59             ;;
60         -nuu|--no-uid-update)
61             UPDATE_UID=false
62             ;;
63         -v|--volume)
64             shift
65             if [[ "$1" =~ .*:.* ]]; then
66                 USER_VOLUME_OPTION="-v $1"
67             else
68                 echo "Invalid volume option, format must be /InDockerPath:/hostPath"
69                 exit 1
70             fi
71             ;;
72         *)
73             if [[ "$1" =~ ^[0-9]+$ ]]; then
74                 ID=$1
75             else
76                 IMAGE=$1
77             fi
78             ;;
79     esac
80     shift
81 done
82
83 [ "$ID" = "" ] && ID=0
84
85 # Dynamically retrieve image name
86 if [ "$IMAGE" = "" ]; then
87
88     VER_NUM=`docker images $REGISTRY/$REPO/$NAME-$FLAVOUR:* --format {{.Tag}} | wc -l`
89     if [ $VER_NUM -gt 1 ]; then
90         echo "ERROR: more than one xds image found, please set explicitly the image to use !"
91         exit 1
92     elif [ $VER_NUM -lt 1 ]; then
93         echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
94         exit 1
95     fi
96
97     VERSION=`docker images $REGISTRY/$REPO/$NAME-$FLAVOUR:* --format {{.Tag}}`
98     if [ "$VERSION" = "" ]; then
99         echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
100         usage
101         exit 1
102     fi
103
104     IMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$VERSION
105 fi
106
107 USER=$(id -un)
108 echo "Using instance ID #$ID (user $(id -un))"
109
110 NAME=agl-xds-$(hostname|cut -f1 -d'.')-$ID-$USER
111
112 docker ps -a |grep "$NAME" > /dev/null
113 [ "$?" = "0" ] && { echo "Image name already exist ! (use -h option to read help)"; exit 1; }
114
115 XDS_WKS=$HOME/xds-workspace
116 XDTDIR=$XDS_WKS/.xdt_$ID
117
118 SSH_PORT=$((2222 + ID))
119 WWW_PORT=$((8000 + ID))
120 BOOT_PORT=$((69 + ID))
121 NBD_PORT=$((10809 + ID))
122
123 ### Create the new container
124 mkdir -p $XDS_WKS $XDTDIR  || exit 1
125 docker run \
126         --publish=${SSH_PORT}:22 \
127         --publish=${WWW_PORT}:8000 \
128         --publish=${BOOT_PORT}:69/udp \
129         --publish=${NBD_PORT}:10809 \
130         --detach=true \
131         --hostname=$NAME --name=$NAME \
132         --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro \
133         -v $XDS_WKS:/home/$DOCKER_USER/xds-workspace \
134         -v $XDTDIR:/xdt \
135     $USER_VOLUME_OPTION \
136         -it $IMAGE
137 if [ "$?" != "0" ]; then
138     echo "An error was encountered while creating docker container."
139     exit 1
140 fi
141
142 ### Ssh key
143 echo "Copying your identity to container $NAME"
144 echo -n wait ssh service .
145 res=3
146 max=30
147 count=0
148 while [ $res -ne 0 ] && [ $count -le $max ]; do
149     sleep 1
150     docker exec ${NAME} bash -c "systemctl status ssh" 2>/dev/null 1>&2
151     res=$?
152     echo -n "."
153     count=$(expr $count + 1);
154 done
155 echo
156
157 ssh-keygen -R [localhost]:$SSH_PORT -f ~/.ssh/known_hosts
158 docker exec ${NAME} bash -c "mkdir -p /home/$DOCKER_USER/.ssh"
159 docker cp ~/.ssh/id_rsa.pub ${NAME}:/home/$DOCKER_USER/.ssh/authorized_keys
160 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/*"
161 ssh -o StrictHostKeyChecking=no -p $SSH_PORT $DOCKER_USER@localhost exit
162
163 echo "You can now login using:"
164 echo "   ssh -p $SSH_PORT $DOCKER_USER@localhost"
165
166 ### User / Group id
167 if ($UPDATE_UID); then
168     echo "Setup docker user and group id to match yours"
169     docker exec -t ${NAME} bash -c "systemctl stop xds-server" || exit 1
170     docker exec -t ${NAME} bash -c "usermod -u $(id -u) $DOCKER_USER && groupmod -g $(id -g) $DOCKER_USER" || exit 1
171     docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER /home/$DOCKER_USER /tmp/xds*" || exit 1
172     docker exec -t ${NAME} bash -c "systemctl start xds-server" || exit 1
173     docker exec -t ${NAME} bash -c "systemctl start xds-server" || exit 1
174 fi
175
176 ### Force xds-server restart
177 if ($FORCE_RESTART); then
178     echo "Stopping xds-server..."
179     docker exec -t ${NAME} bash -c "systemctl stop xds-server" || exit 1
180     sleep 1
181     echo "Starting xds-server..."
182     docker exec -t ${NAME} bash -c "systemctl start xds-server" || exit 1
183 fi
184
185 echo "Done, docker container $NAME is ready to be used."