VOL-546 Add support for proxy servers in build

- Propagate proxy environment variables into docker builds
- Generate mvn settings.xml based on proxy env vars and allow custom xml
- Update to latest docker installation within go-lang docker image (to avoid apt-key proxy issue)
- Allow additional parameters with DOCKER_BUILD_EXTRA_ARGS, for example --no-cache, --pull
- Also fixes 'overriding recipe for target' for voltha target

Change-Id: Ifa545a0262ecfe434ad230ddab4f61935c7244e0
diff --git a/Makefile b/Makefile
index 4fb8390..4675b73 100644
--- a/Makefile
+++ b/Makefile
@@ -20,6 +20,20 @@
 
 include setup.mk
 
+ifneq ($(http_proxy)$(https_proxy),)
+# Include proxies from the environment
+DOCKER_PROXY_ARGS = \
+       --build-arg http_proxy=$(http_proxy) \
+       --build-arg https_proxy=$(https_proxy) \
+       --build-arg ftp_proxy=$(ftp_proxy) \
+       --build-arg no_proxy=$(no_proxy) \
+       --build-arg HTTP_PROXY=$(HTTP_PROXY) \
+       --build-arg HTTPS_PROXY=$(HTTPS_PROXY) \
+       --build-arg FTP_PROXY=$(FTP_PROXY) \
+       --build-arg NO_PROXY=$(NO_PROXY)
+endif
+DOCKER_BUILD_ARGS = $(DOCKER_PROXY_ARGS) $(DOCKER_BUILD_EXTRA_ARGS)
+
 VENVDIR := venv-$(shell uname -s | tr '[:upper:]' '[:lower:]')
 
 .PHONY: $(DIRS) $(DIRS_CLEAN) $(DIRS_FLAKE8) flake8 docker-base voltha ofagent podder netconf shovel onos dashd vcli portainer grafana nginx consul registrator envoy golang envoyd tools opennms logstash unum
@@ -61,7 +75,6 @@
 
 ## New directories can be added here
 DIRS:=\
-voltha \
 voltha/northbound/openflow \
 voltha/northbound/openflow/agent \
 voltha/northbound/openflow/oftest
@@ -104,83 +117,83 @@
 containers: docker-base voltha ofagent podder netconf shovel onos tester config-push dashd vcli portainer grafana nginx consul registrator tools golang envoyd envoy fluentd unum
 
 docker-base:
-	docker build -t cord/voltha-base -f docker/Dockerfile.base .
+	docker build $(DOCKER_BUILD_ARGS) -t cord/voltha-base -f docker/Dockerfile.base .
 
 voltha: voltha-adapters
-	docker build -t cord/voltha -f docker/Dockerfile.voltha .
+	docker build $(DOCKER_BUILD_ARGS) -t cord/voltha -f docker/Dockerfile.voltha .
 
 voltha-adapters:
 	make -C voltha/adapters/asfvolt16_olt
 
 ofagent:
-	docker build -t cord/ofagent -f docker/Dockerfile.ofagent .
+	docker build $(DOCKER_BUILD_ARGS) -t cord/ofagent -f docker/Dockerfile.ofagent .
 
 podder:
-	docker build -t cord/podder -f docker/Dockerfile.podder .
+	docker build $(DOCKER_BUILD_ARGS) -t cord/podder -f docker/Dockerfile.podder .
 
 tools:
-	docker build -t voltha/tools -f docker/Dockerfile.tools .
+	docker build $(DOCKER_BUILD_ARGS) -t voltha/tools -f docker/Dockerfile.tools .
 
 fluentd:
-	docker build -t cord/fluentd -f docker/Dockerfile.fluentd .
+	docker build $(DOCKER_BUILD_ARGS) -t cord/fluentd -f docker/Dockerfile.fluentd .
 
 envoy:
-	docker build -t voltha/envoy -f docker/Dockerfile.envoy .
+	docker build $(DOCKER_BUILD_ARGS) -t voltha/envoy -f docker/Dockerfile.envoy .
 
 envoyd:
 	make -C envoy
 	make -C envoy/go/envoyd
 
 golang:
-	docker build -t go-builder -f envoy/go/golang-builder/Dockerfile ./envoy/go/golang-builder
+	docker build $(DOCKER_BUILD_ARGS) -t go-builder -f envoy/go/golang-builder/Dockerfile ./envoy/go/golang-builder
 
 netconf:
-	docker build -t cord/netconf -f docker/Dockerfile.netconf .
+	docker build $(DOCKER_BUILD_ARGS) -t cord/netconf -f docker/Dockerfile.netconf .
 
 netopeer:
-	docker build -t cord/netopeer -f docker/Dockerfile.netopeer .
+	docker build $(DOCKER_BUILD_ARGS) -t cord/netopeer -f docker/Dockerfile.netopeer .
 
 shovel:
-	docker build -t cord/shovel -f docker/Dockerfile.shovel .
+	docker build $(DOCKER_BUILD_ARGS) -t cord/shovel -f docker/Dockerfile.shovel .
 
 dashd:
-	docker build -t cord/dashd -f docker/Dockerfile.dashd .
+	docker build $(DOCKER_BUILD_ARGS) -t cord/dashd -f docker/Dockerfile.dashd .
 
 vcli:
-	docker build -t cord/vcli -f docker/Dockerfile.cli .
+	docker build $(DOCKER_BUILD_ARGS) -t cord/vcli -f docker/Dockerfile.cli .
 
 portainer:
 	portainer/buildPortainer.sh
 
 nginx:
-	docker build -t voltha/nginx -f docker/Dockerfile.nginx .
+	docker build $(DOCKER_BUILD_ARGS) -t voltha/nginx -f docker/Dockerfile.nginx .
 
 consul:
-	docker build -t voltha/consul -f docker/Dockerfile.consul .
+	docker build $(DOCKER_BUILD_ARGS) -t voltha/consul -f docker/Dockerfile.consul .
 
 registrator:
-	docker build -t voltha/registrator -f docker/Dockerfile.registrator .
+	docker build $(DOCKER_BUILD_ARGS) -t voltha/registrator -f docker/Dockerfile.registrator .
 
 grafana:
-	docker build -t voltha/grafana -f docker/Dockerfile.grafana .
+	docker build $(DOCKER_BUILD_ARGS) -t voltha/grafana -f docker/Dockerfile.grafana .
 
 onos:
-	docker build -t cord/onos -f docker/Dockerfile.onos docker
+	docker build $(DOCKER_BUILD_ARGS) -t cord/onos -f docker/Dockerfile.onos docker
 
 unum:
-	docker build -t voltha/unum -f unum/Dockerfile ./unum
+	docker build $(DOCKER_BUILD_ARGS) -t voltha/unum -f unum/Dockerfile ./unum
 
 tester:
-	docker build -t cord/tester -f docker/Dockerfile.tester docker
+	docker build $(DOCKER_BUILD_ARGS) -t cord/tester -f docker/Dockerfile.tester docker
 
 config-push:
-	docker build -t cord/config-push -f docker/Dockerfile.configpush docker	
+	docker build $(DOCKER_BUILD_ARGS) -t cord/config-push -f docker/Dockerfile.configpush docker
 
 opennms:
-	docker build -t cord/opennms -f docker/Dockerfile.opennms .
+	docker build $(DOCKER_BUILD_ARGS) -t cord/opennms -f docker/Dockerfile.opennms .
 
 logstash:
-	docker build -t cord/logstash -f docker/Dockerfile.logstash .
+	docker build $(DOCKER_BUILD_ARGS) -t cord/logstash -f docker/Dockerfile.logstash .
 
 protos:
 	make -C voltha/protos
diff --git a/docker/Dockerfile.golang b/docker/Dockerfile.golang
index 47765fe..0b6648c 100644
--- a/docker/Dockerfile.golang
+++ b/docker/Dockerfile.golang
@@ -1,12 +1,12 @@
 FROM golang:1.9.2
 MAINTAINER Alex Peters <info@alexanderpeters.de>
 
-RUN apt-get update && apt-get install -y apt-transport-https ca-certificates jq
+RUN apt-get update && apt-get install -y apt-transport-https ca-certificates jq curl gnupg2 software-properties-common
 
-RUN echo "deb https://apt.dockerproject.org/repo debian-jessie main" > /etc/apt/sources.list.d/docker.list
-RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
+RUN curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg | apt-key add -
+RUN add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") $(lsb_release -cs) stable"
 
-RUN apt-get update && apt-cache policy docker-engine && apt-get install -y upx-ucl docker-engine && apt-get clean
+RUN apt-get update && apt-cache policy docker-ce && apt-get install -y upx-ucl docker-ce && apt-get clean
 
 RUN go get github.com/pwaller/goupx \
 	&& go get golang.org/x/tools/cmd/cover \
diff --git a/docker/Dockerfile.onos b/docker/Dockerfile.onos
index 4e9af3a..528d90b 100644
--- a/docker/Dockerfile.onos
+++ b/docker/Dockerfile.onos
@@ -9,8 +9,10 @@
 # Build the applications
 RUN mkdir -p ${DOWNLOAD_ROOT}
 WORKDIR ${DOWNLOAD_ROOT}
+COPY config/mvn_settings.* ${DOWNLOAD_ROOT}/
+RUN ./mvn_settings.sh
 ADD config/dependencies.xml .
-RUN mvn dependency:copy -Dmdep.useBaseVersion=true -DoutputDirectory=${DOWNLOAD_ROOT} -Dsilent=true -f dependencies.xml
+RUN mvn dependency:copy -Dmdep.useBaseVersion=true -DoutputDirectory=${DOWNLOAD_ROOT} -Dsilent=true -f dependencies.xml -s mvn_settings.xml
 
 FROM onosproject/onos:1.10.9 as install
 MAINTAINER Open Networking Foundation <info@opennetworking.org>
diff --git a/docker/config/mvn_settings.sh b/docker/config/mvn_settings.sh
new file mode 100755
index 0000000..771743f
--- /dev/null
+++ b/docker/config/mvn_settings.sh
@@ -0,0 +1,35 @@
+#!/bin/bash
+if [ -f mvn_settings.custom.xml ] ; then
+  cp mvn_settings.custom.xml mvn_settings.xml
+  exit 0
+fi
+
+if [ "$http_proxy$https_proxy" != "" ] ; then
+  echo "  <proxies>" >> mvn_settings.proxy.xml
+  for PROTOCOL in http https ; do
+    proxy="${PROTOCOL}_proxy"
+    proxy="${!proxy}"
+    if [ "$proxy" = "" ] ; then continue ; fi
+
+    # username/password not yet included
+    PROXY_HOST=$(echo "$proxy" | sed "s@.*://@@;s/.*@//;s@:.*@@")
+    PROXY_PORT=$(echo "$proxy" | sed "s@.*://@@;s@.*:@@;s@/.*@@")
+    NON_PROXY=$(echo "$no_proxy" | sed "s@,@|@g")
+
+    echo "   <proxy>
+      <id>$PROTOCOL</id>
+      <active>true</active>
+      <protocol>$PROTOCOL</protocol>
+      <host>$PROXY_HOST</host>
+      <port>$PROXY_PORT</port>
+      <nonProxyHosts>$NON_PROXY</nonProxyHosts>
+    </proxy>" >> mvn_settings.proxy.xml
+  done
+  echo "  </proxies>" >> mvn_settings.proxy.xml
+
+  sed -i '/<!--PROXY-->/r mvn_settings.proxy.xml' mvn_settings.xml
+fi
+
+if [ -f mvn_settings.extra.xml ] ; then
+  sed -i 's/<!--EXTRA-->/r mvn_settings.extra.xml' mvn_settings.xml
+fi
diff --git a/docker/config/mvn_settings.xml b/docker/config/mvn_settings.xml
new file mode 100644
index 0000000..26f9926
--- /dev/null
+++ b/docker/config/mvn_settings.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
+          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
+  <!--PROXY-->
+  <!--EXTRA-->
+</settings>
diff --git a/envoy/go/envoyd/build_binary.sh b/envoy/go/envoyd/build_binary.sh
index 1772809..037ebd8 100755
--- a/envoy/go/envoyd/build_binary.sh
+++ b/envoy/go/envoyd/build_binary.sh
@@ -2,7 +2,7 @@
 
 rm -fr buildreport
 rm -f envoyd
-docker run -v $(pwd):/src go-builder
+docker run -e "http_proxy=$http_proxy" -e "https_proxy=$https_proxy" -v $(pwd):/src go-builder
 #/build.sh
 uid=`id -u`
 gid=`id -g`
diff --git a/envoy/go/golang-builder/Dockerfile b/envoy/go/golang-builder/Dockerfile
index 47765fe..0b6648c 100644
--- a/envoy/go/golang-builder/Dockerfile
+++ b/envoy/go/golang-builder/Dockerfile
@@ -1,12 +1,12 @@
 FROM golang:1.9.2
 MAINTAINER Alex Peters <info@alexanderpeters.de>
 
-RUN apt-get update && apt-get install -y apt-transport-https ca-certificates jq
+RUN apt-get update && apt-get install -y apt-transport-https ca-certificates jq curl gnupg2 software-properties-common
 
-RUN echo "deb https://apt.dockerproject.org/repo debian-jessie main" > /etc/apt/sources.list.d/docker.list
-RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
+RUN curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg | apt-key add -
+RUN add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") $(lsb_release -cs) stable"
 
-RUN apt-get update && apt-cache policy docker-engine && apt-get install -y upx-ucl docker-engine && apt-get clean
+RUN apt-get update && apt-cache policy docker-ce && apt-get install -y upx-ucl docker-ce && apt-get clean
 
 RUN go get github.com/pwaller/goupx \
 	&& go get golang.org/x/tools/cmd/cover \