3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB and others.
5 # stefan.k.berg@ericsson.com
6 # jonas.bjurel@ericsson.com
7 # All rights reserved. This program and the accompanying materials
8 # are made available under the terms of the Apache License, Version 2.0
9 # which accompanies this distribution, and is available at
10 # http://www.apache.org/licenses/LICENSE-2.0
11 ##############################################################################
14 ############################################################################
15 # BEGIN of Exit handlers
19 CID=`cat $CID_FILE </dev/null`
23 docker kill $CID > /dev/null 2>&1
24 docker rm -f $CID > /dev/null 2>&1
25 docker rmi -f $IID > /dev/null 2>&1
30 # End of Exit handlers
31 ############################################################################
33 trap do_exit SIGINT SIGTERM EXIT
41 GITROOT=`git rev-parse --show-toplevel`
42 CID_FILE=`mktemp -u -t runcontext.XXXXXXXXXX`
43 CONTEXT_DIR=`mktemp -d ${GITROOT}/.docker_contextXXXXXX`
45 # If RSYNC_CONNECT_PROG is used, we need to copy all of
46 # the SSH structure, should one of the keys need to be
48 if [ -n "$RSYNC_CONNECT_PROG" -a -x $HOME/.ssh ]; then
49 cp -rp $HOME/.ssh $CONTEXT_DIR
50 rm -f $CONTEXT_DIR/.ssh/known_hosts
52 mkdir $CONTEXT_DIR/.ssh
55 # Disable verification of unknown keys
56 cat >> $CONTEXT_DIR/.ssh/config <<EOF
57 StrictHostKeyChecking=no
60 cat > $CONTEXT_DIR/Dockerfile <<EOF
62 $(env | egrep -i 'proxy|rsync' | sed 's/^/ENV /' | sed 's/=/ /')
65 RUN chown -R $USER_ID:$GROUP_ID $HOME/.ssh
66 RUN chown -R $USER_ID:$GROUP_ID $HOME
67 RUN chmod 700 $HOME/.ssh
68 RUN /root/setcontext $USER $USER_ID $GROUP_ID $HOME
71 res=`docker build -q --force-rm $CONTEXT_DIR`
72 IID=`echo $res | sed 's/.* //'`
74 # Handle proxy settings passed to the context
75 if env | grep -iq .*proxy; then
76 envfile="$(readlink -f $(dirname $0)/..)/environment.mk"
78 test -n "$HTTP_PROXY" && my_http_proxy=$HTTP_PROXY
79 test -n "$http_proxy" && my_http_proxy=$http_proxy
81 test -n "$HTTPS_PROXY" && my_https_proxy=$HTTPS_PROXY
82 test -n "$https_proxy" && my_https_proxy=$https_proxy
84 test -n "$NO_PROXY" && my_no_proxy=$NO_PROXY
85 test -n "$no_proxy" && my_no_proxy=$no_proxy
87 # Make sure to add the Docker socket in no_proxy
88 if [ -n "$my_no_proxy" ]; then
89 my_no_proxy+=",/var/run/docker.sock"
91 my_no_proxy="/var/run/docker.sock"
94 echo "Creating $envfile"
95 echo "# This file is automatically generated by runcontext, do not edit!" > $envfile
96 test -n "$my_http_proxy" && echo "export http_proxy=$my_http_proxy" >> $envfile
97 test -n "$my_https_proxy" && echo "export https_proxy=$my_https_proxy" >> $envfile
98 test -n "$my_no_proxy" && echo "export no_proxy=$my_no_proxy" >> $envfile
99 test -n "$RSYNC_PROXY" && echo "export RSYNC_PROXY=$RSYNC_PROXY" >> $envfile
100 test -n "$RSYNC_CONNECT_PROG" && echo "export RSYNC_CONNECT_PROG=$RSYNC_CONNECT_PROG" >> $envfile
101 echo "export npm_config_registry=http://registry.npmjs.org/" >> $envfile
103 echo "No need to generate environment.mk"
107 # Evaluate the need for bind mounting the cache directory
108 if [ -n "$CACHEBASE" ]; then
109 if echo $CACHEBASE | grep -q '^file://'; then
110 CACHEMOUNT="-v $(echo $CACHEBASE | sed 's;file://;;'):$(echo $CACHEBASE | sed 's;file://;;')"
114 RUN_CONTEXT_OPT="--cidfile $CID_FILE --privileged=true --rm \
115 -e HOME=$HOME -e CACHEDEBUG -e CACHETRANSPORT -e CACHEMAXAGE -e CACHEBASE \
116 -e BUILD_FUEL_PLUGINS -e MIRROR_UBUNTU -e MIRROR_UBUNTU_ROOT \
117 -e MIRROR_MOS_UBUNTU -e MIRROR_MOS_UBUNTU_ROOT -e MIRROR_FUEL \
118 -e LATEST_TARGET_UBUNTU -e UBUNTU_ARCH \
119 -u $USER_ID:$GROUP_ID -w $PWD \
120 -v $GITROOT:$GITROOT -v /sys/fs/cgroup:/sys/fs/cgroup:ro $CACHEMOUNT"
122 # Passing "debug" puts up an interactive bash shell
123 if [ "$1" == "debug" ]; then
124 echo command: docker run ${RUN_CONTEXT_OPT} $IID bash
125 docker run -i -t ${RUN_CONTEXT_OPT} $IID bash
127 echo command: docker run ${RUN_CONTEXT_OPT} $IID $@
128 docker run -t ${RUN_CONTEXT_OPT} $IID $@