Support for building Fuel behind a http proxy
[fuel.git] / fuel / 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`
20     rm -f $CID_FILE
21     set +e
22     docker kill $CID > /dev/null 2>&1
23     docker rm -f $CID > /dev/null 2>&1
24     docker rmi -f $IID > /dev/null 2>&1
25     set -e
26 }
27
28 #
29 # End of Exit handlers
30 ############################################################################
31
32
33 trap do_exit SIGINT SIGTERM EXIT
34
35 context=$1
36 shift
37 GID=`id -g`
38 USER=`whoami`
39 res=`docker build -q --force-rm -  <<EOF
40 FROM $context
41 $(env | egrep -i 'proxy|rsync' | sed 's/^/ENV /' | sed 's/=/ /')
42 RUN date || date
43 RUN /root/setcontext $USER $UID $GID $HOME
44 EOF`
45 GITROOT=`git rev-parse --show-toplevel`
46 IID=`echo $res | sed 's/.* //'`
47
48 CID_FILE=`mktemp -u -t runcontext.XXXXXXXXXX`
49
50 # Handle proxy settings passed to the context
51 if env | grep -iq .*proxy; then
52     envfile="$(readlink -f $(dirname $0)/..)/environment.mk"
53
54     test -n "$HTTP_PROXY" && my_http_proxy=$HTTP_PROXY
55     test -n "$http_proxy" && my_http_proxy=$http_proxy
56
57     test -n "$HTTPS_PROXY" && my_https_proxy=$HTTPS_PROXY
58     test -n "$https_proxy" && my_https_proxy=$https_proxy
59
60     test -n "$NO_PROXY" && my_no_proxy=$NO_PROXY
61     test -n "$no_proxy" && my_no_proxy=$no_proxy
62
63     # Make sure to add the Docker socket in no_proxy
64     if [ -n "$my_no_proxy" ]; then
65         my_no_proxy+=",/var/run/docker.sock"
66     else
67         my_no_proxy="/var/run/docker.sock"
68     fi
69
70     echo "Creating $envfile"
71     echo "# This file is automatically generated by runcontext, do not edit!" > $envfile
72     test -n "$my_http_proxy" && echo "export http_proxy=$my_http_proxy" >> $envfile
73     test -n "$my_https_proxy" && echo "export https_proxy=$my_https_proxy" >> $envfile
74     test -n "$my_no_proxy" && echo "export no_proxy=$my_no_proxy" >> $envfile
75     test -n "$RSYNC_PROXY" && echo "export RSYNC_PROXY=$RSYNC_PROXY" >> $envfile
76     test -n "$RSYNC_CONNECT_PROG" && echo "export RSYNC_CONNECT_PROG=$RSYNC_CONNECT_PROG" >> $envfile
77     echo "export npm_config_registry=http://registry.npmjs.org/" >> $envfile
78 else
79     echo "No need to generate environment.mk"
80     rm -f $envfile
81 fi
82
83 # Evaluate the need for bind mounting the cache directory
84 if [ -n "$CACHEBASE" ]; then
85     if echo $CACHEBASE | grep -q '^file://'; then
86         CACHEMOUNT="-v $(echo $CACHEBASE | sed 's;file://;;'):$(echo $CACHEBASE | sed 's;file://;;')"
87     fi
88 fi
89
90 RUN_CONTEXT_OPT="--cidfile $CID_FILE --privileged=true --rm -e HOME=$HOME -e CACHEDEBUG -e CACHETRANSPORT -e CACHEMAXAGE -e CACHEBASE -u $USER -w $PWD -v ${HOME}/.ssh:${HOME}/.ssh -v $GITROOT:$GITROOT $CACHEMOUNT"
91
92 # Passing "debug" puts up an interactive bash shell
93 if [ "$1" == "debug" ]; then
94   echo command: docker run ${RUN_CONTEXT_OPT} $IID $@
95   docker run -i -t ${RUN_CONTEXT_OPT} $IID bash
96 else
97   echo command: docker run ${RUN_CONTEXT_OPT} $IID $@
98   docker run -t ${RUN_CONTEXT_OPT} $IID $@
99 fi
100