VOL-1613: Redo Makefile and Dockerfile to new standard

Needed for proper releases

Also update system-test compose and docs needed to
run images using new build.

Change-Id: Idc00317de476fe943ec937977bf863a46c1e8305
diff --git a/python/Makefile b/python/Makefile
index 4fa911e..4fd5fe2 100644
--- a/python/Makefile
+++ b/python/Makefile
@@ -14,227 +14,153 @@
 # limitations under the License.
 #
 
-ifneq ($(VOLTHA_BUILD),docker)
-ifeq ($(VOLTHA_BASE)_set,_set)
-$(error To get started, please source the env.sh file)
-endif
+# set default shell
+SHELL = bash -e -o pipefail
+
+# Variables
+VERSION                  ?= $(shell cat ../VERSION)
+
+## Docker related
+DOCKER_REGISTRY          ?=
+DOCKER_REPOSITORY        ?=
+DOCKER_BUILD_ARGS        ?=
+DOCKER_TAG               ?= ${VERSION}
+OFAGENT_IMAGENAME        := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-ofagent:${DOCKER_TAG}
+CLI_IMAGENAME            := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-cli:${DOCKER_TAG}
+PONSIMOLT_IMAGENAME      := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-adapter-ponsim-olt:${DOCKER_TAG}
+PONSIMONU_IMAGENAME      := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-adapter-ponsim-onu:${DOCKER_TAG}
+
+## Docker labels. Only set ref and commit date if committed
+DOCKER_LABEL_VCS_URL     ?= $(shell git remote get-url $(shell git remote))
+DOCKER_LABEL_BUILD_DATE  ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
+DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
+
+ifeq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
+  DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
+else
+  DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)+dirty
 endif
 
-ifeq ($(TAG),)
-TAG := latest
-endif
-
-ifeq ($(TARGET_TAG),)
-TARGET_TAG := latest
-endif
-
-# If no DOCKER_HOST_IP is specified grab a v4 IP address associated with
-# the default gateway
-ifeq ($(DOCKER_HOST_IP),)
-DOCKER_HOST_IP := $(shell ifconfig $$(netstat -rn | grep -E '^(default|0.0.0.0)' | head -1 | awk '{print $$NF}') | grep inet | awk '{print $$2}' | sed -e 's/addr://g')
-endif
-
-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 = \
-	--build-arg TAG=$(TAG) \
-	--build-arg REGISTRY=$(REGISTRY) \
-	--build-arg REPOSITORY=$(REPOSITORY) \
-	$(DOCKER_PROXY_ARGS) $(DOCKER_CACHE_ARG) \
-	 --rm --force-rm \
-	$(DOCKER_BUILD_EXTRA_ARGS)
-
-DOCKER_BUILD_EXTRA_ARGS = \
-	--build-arg LOCAL_PYVOLTHA=${LOCAL_PYVOLTHA} \
-	--build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
-
-VENVDIR := venv-$(shell uname -s | tr '[:upper:]' '[:lower:]')
-
-DOCKER_IMAGE_LIST = \
-	base \
-	adapter-ponsim-olt \
-	adapter-ponsim-onu
-
-# The following list was scavanged from the compose / stack files as well as
-# from the Dockerfiles. If nothing else it highlights that VOLTHA is not
-# using consistent versions for some of the containers.
-
-FETCH_BUILD_IMAGE_LIST = \
-       alpine:3.6 \
-       centos:7 \
-       centurylink/ca-certs:latest \
-       grpc/python:latest \
-       ubuntu:xenial
-
-FETCH_COMPOSE_IMAGE_LIST = \
-        wurstmeister/kafka:latest \
-        wurstmeister/zookeeper:latest
-
-# find k8s -type f | xargs grep image: | awk '{print $NF}' | sed -e 's/\"//g' | sed '/:.*$/!s/$/:latest/g' | sort -u | sed -e 's/^/       /g' -e 's/$/ \\/g'
-# Manually remove some image from this list as they don't reflect the new 
-# naming conventions for the VOLTHA build
-FETCH_K8S_IMAGE_LIST = \
-       wurstmeister/kafka:1.0.0 \
-       zookeeper:3.4.11
-
-FETCH_IMAGE_LIST = $(shell echo $(FETCH_BUILD_IMAGE_LIST) $(FETCH_COMPOSE_IMAGE_LIST) $(FETCH_K8S_IMAGE_LIST) | tr ' ' '\n' | sort -u)
-
-.PHONY: $(DIRS) $(DIRS_CLEAN) $(DIRS_FLAKE8) flake8 base ponsim_olt ponsim_onu protos cli ofagent kafka common start stop tag push pull
+.PHONY: venv ofagent cli adapter_ponsim_olt adapter_ponsim_onu local-protos local-pyvoltha
 
 # This should to be the first and default target in this Makefile
 help:
 	@echo "Usage: make [<target>]"
 	@echo "where available targets are:"
 	@echo
-	@echo "build        : Build the Adapters protos and docker images.\n\
-               If this is the first time you are building, choose \"make build\" option."
-	@echo "clean        : Remove files created by the build and tests"
-	@echo "distclean    : Remove venv directory"
-	@echo "fetch        : Pre-fetch artifacts for subsequent local builds"
-	@echo "help         : Print this help"
-	@echo "rebuild-venv : Rebuild local Python virtualenv from scratch"
-	@echo "venv         : Build local Python virtualenv if did not exist yet"
-	@echo "containers   : Build all the docker containers"
-	@echo "base         : Build the base docker container used by all other dockers"
-	@echo "adapter_ponsim_olt       : Build the ponsim olt adapter docker container"
-	@echo "adapter_ponsim_onu       : Build the ponsim olt adapter docker container"
-	@echo "ofagent      : Build the openflow agent docker container"
-	@echo "tag          : Tag a set of images"
-	@echo "push         : Push the docker images to an external repository"
-	@echo "pull         : Pull the docker images from a repository"
+	@echo "build                : Build all docker images"
+	@echo "                         - If this is the first time you are building, choose 'make build' option."
+	@echo "help                 : Print this help"
+	@echo "ofagent              : Build the openflow agent docker image"
+	@echo "cli                  : Build the voltha CLI docker image"
+	@echo "adapter_ponsim_olt   : Build the ponsim olt adapter docker image"
+	@echo "adapter_ponsim_onu   : Build the ponsim onu adapter docker image"
+	@echo "docker-push   : Push the docker images to an external repository"
+	@echo "venv                 : Build local Python virtualenv"
+	@echo "clean                : Remove files created by the build and tests"
+	@echo "distclean            : Remove venv directory"
 	@echo
 
-## New directories can be added here
-#DIRS:=
 
-## If one directory depends on another directory that
-## dependency can be expressed here
-##
-## For example, if the Tibit directory depended on the eoam
-## directory being built first, then that can be expressed here.
-##  driver/tibit: eoam
+## Docker targets
 
-# Parallel Build
-$(DIRS):
-	@echo "    MK $@"
-	$(Q)$(MAKE) -C $@
+build: docker-build
 
-# Parallel Clean
-DIRS_CLEAN = $(addsuffix .clean,$(DIRS))
-$(DIRS_CLEAN):
-	@echo "    CLEAN $(basename $@)"
-	$(Q)$(MAKE) -C $(basename $@) clean
+docker-build: ofagent cli adapter_ponsim_olt adapter_ponsim_onu
 
-# Parallel Flake8
-DIRS_FLAKE8 = $(addsuffix .flake8,$(DIRS))
-$(DIRS_FLAKE8):
-	@echo "    FLAKE8 $(basename $@)"
-	-$(Q)$(MAKE) -C $(basename $@) flake8
+local-protos:
+	mkdir -p local_imports
+ifdef LOCAL_PROTOS
+	mkdir -p local_imports/voltha-protos/dist
+	cp ../../voltha-protos/dist/*.tar.gz local_imports/voltha-protos/dist/
+endif
 
-build: containers
-
-containers: base adapter_ponsim_olt adapter_ponsim_onu ofagent cli
-
-base:
+local-pyvoltha:
 	mkdir -p local_imports
 ifdef LOCAL_PYVOLTHA
 	mkdir -p local_imports/pyvoltha/dist
 	cp ../../pyvoltha/dist/*.tar.gz local_imports/pyvoltha/dist/
 endif
-ifdef LOCAL_PROTOS
-	mkdir -p local_imports/voltha-protos/dist
-	cp ../../voltha-protos/dist/*.tar.gz local_imports/voltha-protos/dist/
+
+ofagent: local-protos local-pyvoltha
+	docker build $(DOCKER_BUILD_ARGS) \
+    -t ${OFAGENT_IMAGENAME} \
+    --build-arg LOCAL_PYVOLTHA=$(LOCAL_PYVOLTHA) \
+    --build-arg LOCAL_PROTOS=$(LOCAL_PROTOS) \
+    --build-arg org_label_schema_version="${VERSION}" \
+    --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
+    --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
+    --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
+    --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
+    -f docker/Dockerfile.ofagent .
+
+cli: local-protos local-pyvoltha
+	docker build $(DOCKER_BUILD_ARGS) \
+    -t ${CLI_IMAGENAME} \
+    --build-arg LOCAL_PYVOLTHA=$(LOCAL_PYVOLTHA) \
+    --build-arg LOCAL_PROTOS=$(LOCAL_PROTOS) \
+    --build-arg org_label_schema_version="${VERSION}" \
+    --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
+    --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
+    --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
+    --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
+    -f docker/Dockerfile.cli .
+
+adapter_ponsim_olt: local-protos local-pyvoltha
+	docker build $(DOCKER_BUILD_ARGS) \
+    -t ${PONSIMOLT_IMAGENAME} \
+    --build-arg LOCAL_PYVOLTHA=$(LOCAL_PYVOLTHA) \
+    --build-arg LOCAL_PROTOS=$(LOCAL_PROTOS) \
+    --build-arg org_label_schema_version="${VERSION}" \
+    --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
+    --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
+    --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
+    --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
+    -f docker/Dockerfile.adapter_ponsim_olt .
+
+adapter_ponsim_onu: local-protos local-pyvoltha
+	docker build $(DOCKER_BUILD_ARGS) \
+    -t ${PONSIMONU_IMAGENAME} \
+    --build-arg LOCAL_PYVOLTHA=$(LOCAL_PYVOLTHA) \
+    --build-arg LOCAL_PROTOS=$(LOCAL_PROTOS) \
+    --build-arg org_label_schema_version="${VERSION}" \
+    --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
+    --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
+    --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
+    --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
+    -f docker/Dockerfile.adapter_ponsim_onu .
+
+docker-push:
+	docker push ${OFAGENT_IMAGENAME}
+	docker push ${CLI_IMAGENAME}
+	docker push ${PONSIMOLT_IMAGENAME}
+	docker push ${PONSIMONU_IMAGENAME}
+
+
+## Python venv dev environment
+
+VENVDIR := venv-volthago
+
+venv: distclean local-protos local-pyvoltha
+	virtualenv ${VENVDIR};\
+        source ./${VENVDIR}/bin/activate ; set -u ;\
+	rm ${VENVDIR}/local/bin ${VENVDIR}/local/lib ${VENVDIR}/local/include ;\
+        pip install -r requirements.txt
+
+ifdef LOCAL_PYVOLTHA
+	source ./${VENVDIR}/bin/activate ; set -u ;\
+	pip install local_imports/pyvoltha/dist/*.tar.gz
 endif
-	docker build $(DOCKER_BUILD_ARGS) -t ${REGISTRY}${REPOSITORY}voltha-base:${TAG} -f docker/Dockerfile.base .
-
-
-adapter_ponsim_olt:
-	docker build $(DOCKER_BUILD_ARGS) -t ${REGISTRY}${REPOSITORY}voltha-adapter-ponsim-olt:${TAG} -f docker/Dockerfile.adapter_ponsim_olt .
-
-adapter_ponsim_onu:
-	docker build $(DOCKER_BUILD_ARGS) -t ${REGISTRY}${REPOSITORY}voltha-adapter-ponsim-onu:${TAG} -f docker/Dockerfile.adapter_ponsim_onu .
-
-ofagent:
-	docker build $(DOCKER_BUILD_ARGS) -t ${REGISTRY}${REPOSITORY}voltha-ofagent:${TAG} -f docker/Dockerfile.ofagent .
-
-cli:
-	docker build $(DOCKER_BUILD_ARGS) -t ${REGISTRY}${REPOSITORY}voltha-cli:${TAG} -f docker/Dockerfile.cli .
-
-tag: $(patsubst  %,%.tag,$(DOCKER_IMAGE_LIST))
-
-push: tag $(patsubst  %,%.push,$(DOCKER_IMAGE_LIST))
-
-pull: $(patsubst  %,%.pull,$(DOCKER_IMAGE_LIST))
-
-%.tag:
-	docker tag ${REGISTRY}${REPOSITORY}voltha-$(subst .tag,,$@):${TAG} ${TARGET_REGISTRY}${TARGET_REPOSITORY}voltha-$(subst .tag,,$@):${TARGET_TAG}
-
-%.push:
-	docker push ${TARGET_REGISTRY}${TARGET_REPOSITORY}voltha-$(subst .push,,$@):${TARGET_TAG}
-
-%.pull:
-	docker pull ${REGISTRY}${REPOSITORY}voltha-$(subst .pull,,$@):${TAG}
+ifdef LOCAL_PROTOS
+	source ./${VENVDIR}/bin/activate ; set -u ;\
+	pip install local_imports/voltha-protos/dist/*.tar.gz
+endif
 
 clean:
-	find . -name '*.pyc' | xargs rm -f
 	rm -rf local_imports
+	find . -name '*.pyc' | xargs rm -f
 
 distclean: clean
 	rm -rf ${VENVDIR}
 
-fetch:
-	@bash -c ' \
-		for i in $(FETCH_IMAGE_LIST); do \
-			docker pull $$i; \
-		done'
-
-purge-venv:
-	rm -fr ${VENVDIR}
-
-rebuild-venv: purge-venv venv
-
-ifneq ($(VOLTHA_BUILD),docker)
-venv: ${VENVDIR}/.built
-else
-venv:
-endif
-
-${VENVDIR}/.built:
-	@ virtualenv ${VENVDIR}
-	@ . ${VENVDIR}/bin/activate && \
-	    pip install --upgrade pip; \
-	    if ! pip install -r requirements.txt; \
-	    then \
-	        echo "On MAC OS X, if the installation failed with an error \n'<openssl/opensslv.h>': file not found,"; \
-	        echo "see the BUILD.md file for a workaround"; \
-	    else \
-	        uname -s > ${VENVDIR}/.built; \
-	    fi
-
-ifdef LOCAL_PYVOLTHA
-	mkdir -p local_imports/pyvoltha/dist
-	cp ../../pyvoltha/dist/*.tar.gz local_imports/pyvoltha/dist/
-	. ${VENVDIR}/bin/activate && \
-	    pip install local_imports/pyvoltha/dist/*.tar.gz
-endif
-ifdef LOCAL_PROTOS
-	mkdir -p local_imports/voltha-protos/dist
-	cp ../../voltha-protos/dist/*.tar.gz local_imports/voltha-protos/dist/
-	. ${VENVDIR}/bin/activate && \
-	    pip install local_imports/voltha-protos/dist/*.tar.gz
-endif
-
-flake8: $(DIRS_FLAKE8)
-
 # end file