Use mirrors snapshots
[fuel.git] / build / docker / runcontext
1 #!/bin/bash
2 set -e
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 ##############################################################################
12 #
13
14 ############################################################################
15 # BEGIN of Exit handlers
16 #
17
18 do_exit () {
19     CID=`cat $CID_FILE </dev/null`
20     rm -f $CID_FILE
21     rm -rf $CONTEXT_DIR
22     set +e
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
26     set -e
27 }
28
29 #
30 # End of Exit handlers
31 ############################################################################
32
33 trap do_exit SIGINT SIGTERM EXIT
34
35 context=$1
36 shift
37 USER_ID=`id -u`
38 USER=`whoami`
39 GROUP_ID=`id -g`
40
41 GITROOT=`git rev-parse --show-toplevel`
42 CID_FILE=`mktemp -u -t runcontext.XXXXXXXXXX`
43 CONTEXT_DIR=`mktemp -d ${GITROOT}/.docker_contextXXXXXX`
44
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
47 # used.
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
51 else
52     mkdir $CONTEXT_DIR/.ssh
53 fi
54
55 # Disable verification of unknown keys
56 cat >> $CONTEXT_DIR/.ssh/config <<EOF
57 StrictHostKeyChecking=no
58 EOF
59
60 cat > $CONTEXT_DIR/Dockerfile <<EOF
61 FROM $context
62 $(env | egrep -i 'proxy|rsync' | sed 's/^/ENV /' | sed 's/=/ /')
63 RUN date || date
64 COPY .ssh  $HOME/.ssh
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
69 EOF
70
71 res=`docker build -q --force-rm $CONTEXT_DIR`
72 IID=`echo $res | sed 's/.* //'`
73
74 # Handle proxy settings passed to the context
75 if env | grep -iq .*proxy; then
76     envfile="$(readlink -f $(dirname $0)/..)/environment.mk"
77
78     test -n "$HTTP_PROXY" && my_http_proxy=$HTTP_PROXY
79     test -n "$http_proxy" && my_http_proxy=$http_proxy
80
81     test -n "$HTTPS_PROXY" && my_https_proxy=$HTTPS_PROXY
82     test -n "$https_proxy" && my_https_proxy=$https_proxy
83
84     test -n "$NO_PROXY" && my_no_proxy=$NO_PROXY
85     test -n "$no_proxy" && my_no_proxy=$no_proxy
86
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"
90     else
91         my_no_proxy="/var/run/docker.sock"
92     fi
93
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
102 else
103     echo "No need to generate environment.mk"
104     rm -f $envfile
105 fi
106
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://;;')"
111     fi
112 fi
113
114 RUN_CONTEXT_OPT="--cidfile $CID_FILE --privileged=true --rm -e HOME=$HOME -e CACHEDEBUG -e CACHETRANSPORT -e CACHEMAXAGE -e CACHEBASE -e BUILD_FUEL_PLUGINS -e MIRROR_UBUNTU -e MIRROR_UBUNTU_ROOT -e MIRROR_MOS_UBUNTU -e MIRROR_MOS_UBUNTU_ROOT -e MIRROR_FUEL -u $USER_ID:$GROUP_ID -w $PWD -v $GITROOT:$GITROOT -v /sys/fs/cgroup:/sys/fs/cgroup:ro $CACHEMOUNT"
115
116 # Passing "debug" puts up an interactive bash shell
117 if [ "$1" == "debug" ]; then
118   echo command: docker run ${RUN_CONTEXT_OPT} $IID bash
119   docker run -i -t ${RUN_CONTEXT_OPT} $IID bash
120 else
121   echo command: docker run ${RUN_CONTEXT_OPT} $IID $@
122   docker run -t ${RUN_CONTEXT_OPT} $IID $@
123 fi
124