Bump docker container version to 4.0 and update doc.
[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=$DEFIMAGE
44 FORCE=false
45 while [ $# -ne 0 ]; do
46     case $1 in
47         -h|--help|"")
48             usage
49             ;;
50         -fr|-force-restart)
51             FORCE=true
52             ;;
53         *)
54             if [[ "$1" =~ ^[0-9]+$ ]]; then
55                 ID=$1
56             else
57                 IMAGE=$1
58             fi
59             ;;
60     esac
61     shift
62 done
63
64 [ "$ID" = "" ] && ID=0
65
66 docker ps -a |grep "$IMAGE" > /dev/null
67 [ "$?" = "0" ] && { echo "Image name already exist ! (use -h option to read help)"; exit 1; }
68
69
70 USER=$(id -un)
71 echo "Using instance ID #$ID (user $(id -un))"
72
73 NAME=agl-xds-$(hostname|cut -f1 -d'.')-$ID-$USER
74
75 MIRRORDIR=$HOME/ssd/localmirror_$ID
76 XDTDIR=$HOME/ssd/xdt_$ID
77 SHAREDDIR=$HOME/$DOCKER_USER/docker/share
78
79 SSH_PORT=$((2222 + ID))
80 WWW_PORT=$((8000 + ID))
81 BOOT_PORT=$((69 + ID))
82 NBD_PORT=$((10809 + ID))
83
84 mkdir -p $MIRRORDIR $XDTDIR $SHAREDDIR || exit 1
85 docker run \
86         --publish=${SSH_PORT}:22 \
87         --publish=${WWW_PORT}:8000 \
88         --publish=${BOOT_PORT}:69/udp \
89         --publish=${NBD_PORT}:10809 \
90         --detach=true \
91         --hostname=$NAME --name=$NAME \
92         --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro \
93         -v $MIRRORDIR:/home/$DOCKER_USER/mirror \
94         -v $SHAREDDIR:/home/$DOCKER_USER/share \
95         -v $XDTDIR:/xdt \
96         -it $IMAGE
97 if [ "$?" != "0" ]; then
98     echo "An error was encountered while creating docker container."
99     exit 1
100 fi
101
102 if ($FORCE); then
103     echo "Stoping xds-server..."
104     docker exec --user $DOCKER_USER  ${NAME} bash -c "/usr/local/bin/xds-server-stop.sh" || exit 1
105     sleep 1
106     echo "Starting xds-server..."
107     docker exec --user $DOCKER_USER  ${NAME} bash -c "nohup /usr/local/bin/xds-server-start.sh" || exit 1
108 fi
109
110 echo "Copying your identity to container $NAME"
111 #wait ssh service
112 echo -n wait ssh service .
113 res=3
114 max=30
115 count=0
116 while [ $res -ne 0 ] && [ $count -le $max ]; do
117     sleep 1
118     docker exec ${NAME} bash -c "systemctl status ssh" 2>/dev/null 1>&2 
119     res=$?
120     echo -n "."
121     count=$(expr $count + 1);
122 done
123 echo
124
125 ssh-keygen -R [$(hostname)]:$SSH_PORT -f ~/.ssh/known_hosts
126 docker exec ${NAME} bash -c "mkdir -p /home/devel/.ssh"
127 docker cp ~/.ssh/id_rsa.pub ${NAME}:/home/devel/.ssh/authorized_keys
128 docker exec ${NAME} bash -c "chown devel:devel -R /home/devel/.ssh ;chmod 0700 /home/devel/.ssh;chmod 0600 /home/devel/.ssh/*"
129 ssh -o StrictHostKeyChecking=no -p $SSH_PORT devel@$(hostname) exit
130
131 echo "You can now login using:"
132 echo "   ssh -p $SSH_PORT $DOCKER_USER@$(hostname)"