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