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