c9415aef156d71e1f643a9e982d5ae90b00959e3
[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) <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         exit 1
40 }
41
42 ID=""
43 IMAGE=""
44 FORCE_RESTART=false
45 UPDATE_UID=true
46 while [ $# -ne 0 ]; do
47     case $1 in
48         -h|--help|"")
49             usage
50             ;;
51         -fr|-force-restart)
52             FORCE_RESTART=true
53             ;;
54         -no-uid-update)
55             UPDATE_UID=false
56             ;;
57         *)
58             if [[ "$1" =~ ^[0-9]+$ ]]; then
59                 ID=$1
60             else
61                 IMAGE=$1
62             fi
63             ;;
64     esac
65     shift
66 done
67
68 [ "$ID" = "" ] && ID=0
69
70 # Dynamically retrieve image name
71 if [ "$IMAGE" = "" ]; then
72
73     VER_NUM=`docker images $REGISTRY/$REPO/$NAME-$FLAVOUR:* --format {{.Tag}} | wc -l`
74     if [ $VER_NUM -gt 1 ]; then
75         echo "ERROR: more than one xds image found, please set explicitly the image to use !"
76         exit 1
77     elif [ $VER_NUM -lt 1 ]; then
78         echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
79         exit 1
80     fi
81
82     VERSION=`docker images $REGISTRY/$REPO/$NAME-$FLAVOUR:* --format {{.Tag}}`
83     if [ "$VERSION" = "" ]; then
84         echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
85         usage
86         exit 1
87     fi
88
89     IMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$VERSION
90 fi
91
92 USER=$(id -un)
93 echo "Using instance ID #$ID (user $(id -un))"
94
95 NAME=agl-xds-$(hostname|cut -f1 -d'.')-$ID-$USER
96
97 docker ps -a |grep "$NAME" > /dev/null
98 [ "$?" = "0" ] && { echo "Image name already exist ! (use -h option to read help)"; exit 1; }
99
100 MIRRORDIR=$HOME/ssd/localmirror_$ID
101 XDTDIR=$HOME/ssd/xdt_$ID
102 SHAREDDIR=$HOME/$DOCKER_USER/docker/share
103 XDS_WKS=$HOME/xds-workspace
104
105 SSH_PORT=$((2222 + ID))
106 WWW_PORT=$((8000 + ID))
107 BOOT_PORT=$((69 + ID))
108 NBD_PORT=$((10809 + ID))
109
110 ### Create the new container
111 mkdir -p $MIRRORDIR $XDTDIR $SHAREDDIR $XDS_WKS || exit 1
112 docker run \
113         --publish=${SSH_PORT}:22 \
114         --publish=${WWW_PORT}:8000 \
115         --publish=${BOOT_PORT}:69/udp \
116         --publish=${NBD_PORT}:10809 \
117         --detach=true \
118         --hostname=$NAME --name=$NAME \
119         --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro \
120         -v $MIRRORDIR:/home/$DOCKER_USER/mirror \
121         -v $SHAREDDIR:/home/$DOCKER_USER/share \
122         -v $XDS_WKS:/home/$DOCKER_USER/xds-workspace \
123         -v $XDTDIR:/xdt \
124         -it $IMAGE
125 if [ "$?" != "0" ]; then
126     echo "An error was encountered while creating docker container."
127     exit 1
128 fi
129
130 ### Ssh key
131 echo "Copying your identity to container $NAME"
132 echo -n wait ssh service .
133 res=3
134 max=30
135 count=0
136 while [ $res -ne 0 ] && [ $count -le $max ]; do
137     sleep 1
138     docker exec ${NAME} bash -c "systemctl status ssh" 2>/dev/null 1>&2
139     res=$?
140     echo -n "."
141     count=$(expr $count + 1);
142 done
143 echo
144
145 ssh-keygen -R [localhost]:$SSH_PORT -f ~/.ssh/known_hosts
146 docker exec ${NAME} bash -c "mkdir -p /home/$DOCKER_USER/.ssh"
147 docker cp ~/.ssh/id_rsa.pub ${NAME}:/home/$DOCKER_USER/.ssh/authorized_keys
148 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/*"
149 ssh -o StrictHostKeyChecking=no -p $SSH_PORT $DOCKER_USER@localhost exit
150
151 echo "You can now login using:"
152 echo "   ssh -p $SSH_PORT $DOCKER_USER@localhost"
153
154 ### User / Group id
155 if ($UPDATE_UID); then
156     echo "Setup docker user and group id to match yours"
157     docker exec -t ${NAME} bash -c "systemctl stop xds-server" || exit 1
158     docker exec -t ${NAME} bash -c "usermod -u $(id -u) $DOCKER_USER && groupmod -g $(id -g) $DOCKER_USER" || exit 1
159     docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER /home/$DOCKER_USER /tmp/xds*" || exit 1
160     docker exec -t ${NAME} bash -c "systemctl start xds-server" || exit 1
161     docker exec -t ${NAME} bash -c "systemctl start xds-server" || exit 1
162 fi
163
164 ### Force xds-server restart
165 if ($FORCE_RESTART); then
166     echo "Stopping xds-server..."
167     docker exec -t ${NAME} bash -c "systemctl stop xds-server" || exit 1
168     sleep 1
169     echo "Starting xds-server..."
170     docker exec -t ${NAME} bash -c "systemctl start xds-server" || exit 1
171 fi