aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/create_container
blob: 392e4c9baacb5c446406569a27c48591661978fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/bin/bash

##########################################
# WARNING WARNING WARNING WARNING 
#
# This script is an example to start a new AGL container
#
# You should customize it to fit your environment and in particular 
# adjust the paths and permissions where needed.
# 
# Note that sharing volumes with the host system is not mandatory: it
# was just added for performance reasons: building from a SSD is
# just faster than using the container filesystem: that's why /xdt is 
# mounted from there. The same applies to ~/mirror and ~/share, which are 
# just 2 convenient folders to store reference build caches (used in
# prepare_meta script)
#
##########################################

CURDIR=$(cd $(dirname $0) && pwd -P)
PREFIX=agl-worker
DEFIMAGE=$(make -s -C $CURDIR/.. show-image)
OCCUPIED_ID=$(docker ps -a -f name=${PREFIX} --format "{{.Names}}" | grep -oE "[0-9]" | sort -n | sed ':a;N;$!ba;s/\n/ /g')
BOOTSRV=""
ID=""
IMAGE=""
DOCKER_USER="devel"
DOCKER_UID="1664"

function usage() {
	echo "Usage: $(basename $0) <instance ID> [image name] [--enable-boot-srv]"  >&2
	echo "Instance ID must be 0 or a positive integer (1,2,...)" >&2
	echo "Image name is optional: 'make show-image' is used by default to get image" >&2
	echo "--enable-boot-srv: Enable binding tftp/nbd port." >&2
	echo "Default image: $DEFIMAGE" >&2
	exit 1
}

function updateContainerUid () {
    echo -n "Setup docker user and group id to match yours "

    res=3
    max=30
    count=0
    while [ $res -ne 1 ] && [ $count -le $max ]; do
        sleep 1
        docker exec ${NAME} bash -c "loginctl user-status $DOCKER_USER |grep sd-pam" 2>/dev/null 1>&2
        res=$?
        echo -n "."
        count=$((count + 1));
    done

    echo -n "."

    # Set uid
    if docker exec -t ${NAME} bash -c "id $(id -u)" > /dev/null 2>&1 && \
            [ "$(id -u)" != "$DOCKER_UID" ]; then
        echo "Cannot set docker $DOCKER_USER user id to your id: conflicting id $(id -u) !"
        exit 1
    fi
    docker exec -t ${NAME} bash -c "usermod -u $(id -u) $DOCKER_USER" || exit 1
    echo -n "."

    # Set gid
    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
    fi
    echo -n "."

    docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER /home/$DOCKER_USER" || exit 1
    echo -n "."
    docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER $XDTDIR_MAPPED"
    echo "."
}

function setupContainerSsh () {
    echo "Copying your SSH identity to container $NAME"
    echo -n Waiting for the ssh service to come up in the container ...
    res=3
    max=30
    count=0
    while [ $res -ne 0 ] && [ $count -le $max ]; do
        sleep 1
        docker exec ${NAME} bash -c "systemctl status ssh" 2>/dev/null 1>&2 
        res=$?
        echo -n "."
        count=$(expr $count + 1);
    done
    echo

    ssh-keygen -R [$(hostname)]:$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@$(hostname) exit
}

while [ $# -ne 0 ]; do
	case $1 in 
		-h|--help|"")
			usage
			;;
		--enable-boot-srv)
			BOOTSRV="TRUE"; 
			shift
			;;
		*)
			if [ -z "${ID}" ] ; then 
				ID=$1
				shift
			elif [ -z "${IMAGE}" ] ; then 
				IMAGE=$1
				shift
			else
				usage
			fi
			;;
	esac
done

if [ -z "${IMAGE}" ] ; then 
	IMAGE=${DEFIMAGE}
fi

USER=$(id -un)
echo "Using instance ID #$ID (user $(id -un))"

NAME=${PREFIX}-$(hostname|cut -f1 -d'.')-$ID-$USER

for n in $(docker ps -a -f name=${PREFIX} --format "{{.Names}}")
do
	if [ $NAME = $n ]
	then
		echo "This id is already taken."
		echo "Please choose another one."
		echo "ids already in use: ${OCCUPIED_ID}"
		exit 2
	fi
done

MIRRORDIR=$HOME/ssd/localmirror_$ID
XDTDIR=$HOME/ssd/xdt_$ID
SHAREDDIR=$HOME/ssd/share

MIRRORDIR_MAPPED=/home/$DOCKER_USER/mirror
XDTDIR_MAPPED=/xdt
SHAREDDIR_MAPPED=/home/$DOCKER_USER/share

SSH_PORT=$((2222 + ID))
WWW_PORT=$((8000 + ID))
BOOT_PORT=69
NBD_PORT=10809

mkdir -p $MIRRORDIR $XDTDIR $SHAREDDIR

if [ -n "${BOOTSRV}" ] ; then
	BOOTSRV_CONFIG="--publish=${BOOT_PORT}:69/udp --publish=${NBD_PORT}:10809";
fi

docker run \
	--publish=${SSH_PORT}:22 \
	--publish=${WWW_PORT}:8000 \
	${BOOTSRV_CONFIG} \
	--detach=true \
	--hostname=$NAME --name=$NAME \
	--privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro \
	-v $MIRRORDIR:$MIRRORDIR_MAPPED \
	-v $SHAREDDIR:$SHAREDDIR_MAPPED \
	-v $XDTDIR:$XDTDIR_MAPPED \
	-it $IMAGE

if [ $? -ne 0 ]; then
	echo "Failed to run docker"
	exit 1
fi

setupContainerSsh
updateContainerUid

echo "You can now login using:"
echo "   ssh -p $SSH_PORT $DOCKER_USER@$(hostname)"