Scott Baker | 8173950 | 2019-10-14 16:53:10 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016 the original author or authors. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | # set default shell |
| 18 | SHELL = bash -e -o pipefail |
| 19 | |
| 20 | # Variables |
| 21 | VERSION ?= $(shell cat ./VERSION) |
| 22 | |
| 23 | DOCKER_LABEL_VCS_DIRTY = false |
| 24 | ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0) |
| 25 | DOCKER_LABEL_VCS_DIRTY = true |
| 26 | endif |
| 27 | ## Docker related |
| 28 | DOCKER_EXTRA_ARGS ?= |
| 29 | DOCKER_REGISTRY ?= |
| 30 | DOCKER_REPOSITORY ?= |
| 31 | DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true) |
| 32 | PONSIMONU_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-adapter-ponsim-onu |
| 33 | |
| 34 | ## Docker labels. Only set ref and commit date if committed |
| 35 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 36 | DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD) |
| 37 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 38 | DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD) |
| 39 | |
| 40 | DOCKER_BUILD_ARGS ?= \ |
| 41 | ${DOCKER_EXTRA_ARGS} \ |
| 42 | --build-arg org_label_schema_version="${VERSION}" \ |
| 43 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 44 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 45 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 46 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 47 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 48 | |
| 49 | DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \ |
| 50 | --build-arg LOCAL_PYVOLTHA=${LOCAL_PYVOLTHA} \ |
| 51 | --build-arg LOCAL_PROTOS=${LOCAL_PROTOS} |
| 52 | |
| 53 | .PHONY: local-protos local-pyvoltha |
| 54 | |
| 55 | # This should to be the first and default target in this Makefile |
| 56 | help: |
| 57 | @echo "Usage: make [<target>]" |
| 58 | @echo "where available targets are:" |
| 59 | @echo |
| 60 | @echo "build : Build the docker images." |
| 61 | @echo " - If this is the first time you are building, choose 'make build' option." |
| 62 | @echo "adapter_ponsim_onu : Build the ponsim onu adapter docker image" |
| 63 | @echo "venv : Build local Python virtualenv" |
| 64 | @echo "clean : Remove files created by the build and tests" |
| 65 | @echo "distclean : Remove venv directory" |
| 66 | @echo "docker-push : Push the docker images to an external repository" |
| 67 | @echo "lint-dockerfile : Perform static analysis on Dockerfiles" |
| 68 | @echo "lint : Shorthand for lint-style & lint-sanity" |
| 69 | @echo "test : Generate reports for all go tests" |
| 70 | @echo |
| 71 | |
| 72 | ## Local Development Helpers |
| 73 | local-protos: |
| 74 | @mkdir -p local_imports |
| 75 | ifdef LOCAL_PROTOS |
| 76 | rm -rf local_imports/voltha-protos |
| 77 | mkdir -p local_imports/voltha-protos/dist |
| 78 | cp ../voltha-protos/dist/*.tar.gz local_imports/voltha-protos/dist/ |
| 79 | endif |
| 80 | |
| 81 | local-pyvoltha: |
| 82 | @mkdir -p local_imports |
| 83 | ifdef LOCAL_PYVOLTHA |
| 84 | rm -rf local_imports/pyvoltha |
| 85 | mkdir -p local_imports/pyvoltha/dist |
| 86 | cp ../pyvoltha/dist/*.tar.gz local_imports/pyvoltha/dist/ |
| 87 | endif |
| 88 | |
| 89 | ## Python venv dev environment |
| 90 | |
| 91 | VENVDIR := venv |
| 92 | |
| 93 | venv: distclean local-protos local-pyvoltha |
| 94 | virtualenv ${VENVDIR};\ |
| 95 | source ./${VENVDIR}/bin/activate ; set -u ;\ |
| 96 | rm -f ${VENVDIR}/local/bin ${VENVDIR}/local/lib ${VENVDIR}/local/include ;\ |
| 97 | pip install -r requirements.txt |
| 98 | ifdef LOCAL_PYVOLTHA |
| 99 | source ./${VENVDIR}/bin/activate ; set -u ;\ |
| 100 | pip install local_imports/pyvoltha/dist/*.tar.gz |
| 101 | endif |
| 102 | ifdef LOCAL_PROTOS |
| 103 | source ./${VENVDIR}/bin/activate ; set -u ;\ |
| 104 | pip install local_imports/voltha-protos/dist/*.tar.gz |
| 105 | endif |
| 106 | |
| 107 | ## Docker targets |
| 108 | |
| 109 | build: docker-build |
| 110 | |
| 111 | docker-build: adapter_ponsim_onu |
| 112 | |
| 113 | adapter_ponsim_onu: local-protos local-pyvoltha |
| 114 | docker build $(DOCKER_BUILD_ARGS_LOCAL) -t ${PONSIMONU_IMAGENAME}:${DOCKER_TAG} -t ${PONSIMONU_IMAGENAME}:latest -f docker/Dockerfile.adapter_ponsim_onu . |
| 115 | |
| 116 | docker-push: |
| 117 | docker push ${PONSIMONU_IMAGENAME}:${DOCKER_TAG} |
| 118 | |
| 119 | ## lint and unit tests |
| 120 | |
| 121 | PATH:=$(GOPATH)/bin:$(PATH) |
| 122 | HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint) |
| 123 | lint-dockerfile: |
| 124 | ifeq (,$(shell PATH=$(GOPATH):$(PATH) which hadolint)) |
| 125 | mkdir -p $(GOPATH)/bin |
| 126 | curl -o $(GOPATH)/bin/hadolint -sNSL https://github.com/hadolint/hadolint/releases/download/v1.17.1/hadolint-$(shell uname -s)-$(shell uname -m) |
| 127 | chmod 755 $(GOPATH)/bin/hadolint |
| 128 | endif |
| 129 | @echo "Running Dockerfile lint check ..." |
| 130 | @hadolint $$(find . -name "Dockerfile.*") |
| 131 | @echo "Dockerfile lint check OK" |
| 132 | |
| 133 | lint: lint-dockerfile |
| 134 | |
| 135 | test: |
| 136 | @ echo "Executing unit tests w/tox" |
| 137 | tox |
| 138 | |
| 139 | clean: |
| 140 | rm -rf local_imports |
| 141 | find . -name '*.pyc' | xargs rm -f |
| 142 | |
| 143 | distclean: clean |
| 144 | rm -rf ${VENVDIR} |
| 145 | |
| 146 | # end file |