Merge "Use tacker plugin in SFC scenario"
[fuel.git] / build / docker / Makefile
1 ##############################################################################
2 # Copyright (c) 2015,2016 Ericsson AB and others.
3 # stefan.k.berg@ericsson.com
4 # jonas.bjurel@ericsson.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 SHELL = /bin/bash
12 FILES = $(wildcard ubuntu-builder/*) runcontext
13 DOCKER_VER := $(shell [[ "$$(docker version --format '{{.Client.Version}}')" =~ ([0-9]+)\.([0-9]+) ]] && echo $$(( $${BASH_REMATCH[1]} * 100 + $${BASH_REMATCH[2]} )))
14
15 # Builder tag lifespan, force container rebuild X days after tag creation
16 DOCKER_KEEP = 1 days
17 DOCKER_IMG  = opnfv.org/ubuntu-builder
18 DOCKER_TAG  = ${DOCKER_IMG}:14.04
19 # Shell contruct for checking our tag object did not expire
20 DOCKER_EXPIRED = D_TAG_BIRTH=`docker inspect --format="{{.Created}}" \
21         ${DOCKER_TAG} 2>/dev/null`; test -z "$$D_TAG_BIRTH" -o `date +%s` -gt \
22         `date -d "$$D_TAG_BIRTH +${DOCKER_KEEP}" +%s`; echo $$?
23
24 # Don't use -f flag when docker is newer than 1.9
25 # https://docs.docker.com/engine/deprecated/#/f-flag-on-docker-tag
26 ifeq ($(shell echo "$(DOCKER_VER)>109" | bc), 1)
27         tag_flags :=
28 else
29         tag_flags := -f
30 endif
31
32 .PHONY: all
33 all:    .docker
34
35 .dockercfg: $(FILES)
36         cp Dockerfile ubuntu-builder/Dockerfile
37         # Only add proxy ENVs where set in host - needed to pull the base Ubuntu image
38         test -n "${http_proxy}" && sed -i "s;INSERT_HTTP_PROXY;${http_proxy};" ubuntu-builder/Dockerfile || exit 0
39         test -n "${https_proxy}" && sed -i "s;INSERT_HTTPS_PROXY;${https_proxy};" ubuntu-builder/Dockerfile || exit 0
40         test -n "${no_proxy}" && sed -i "s;INSERT_NO_PROXY;${no_proxy};" ubuntu-builder/Dockerfile || exit 0
41         test -n "${HTTP_PROXY}" && sed -i "s;INSERT_HTTP_PROXY;${HTTP_PROXY};" ubuntu-builder/Dockerfile || exit 0
42         test -n "${HTTPS_PROXY}" && sed -i "s;INSERT_HTTPS_PROXY;${HTTPS_PROXY};" ubuntu-builder/Dockerfile || exit 0
43         test -n "${NO_PROXY}" && sed -i "s;INSERT_NO_PROXY;${NO_PROXY};" ubuntu-builder/Dockerfile || exit 0
44         sed -i '/INSERT_/d' ubuntu-builder/Dockerfile
45         touch $@
46
47 .docker: .dockercfg
48         @if test "$(shell ${DOCKER_EXPIRED})" -eq "0"; then \
49                 /usr/bin/docker build --rm=true --no-cache=true \
50                         -t ${DOCKER_TAG} ubuntu-builder && \
51                 /usr/bin/docker tag ${tag_flags} ${DOCKER_TAG} ${DOCKER_IMG}; \
52         else \
53                 echo "Docker: Tag '${DOCKER_TAG}' was created less than" \
54                         "${DOCKER_KEEP} ago, skipping re-build."; \
55         fi
56         touch $@
57         test -f .cacheid && $(MAKE) -f Makefile put-cache || exit 0
58
59 .PHONY: clean
60 clean:
61         rm -f .docker* ubuntu-builder/Dockerfile
62
63 .PHONY: deepclean
64 deepclean: clean clean-cache
65
66 ##############################################################################
67 # Cache operations - only used when building through ci/build.sh
68 ##############################################################################
69
70 # NOTE: For docker, we only get/put cache to fingerprint build scripts and
71 # env vars, its cached data holds only an empty .docker file.
72
73 # Create a unique hash to be used for getting and putting cache, based on:
74 #   - ubuntu-builder Dockerfile (includes eventual proxy env vars), runcontext;
75 #   - The contents of this Makefile
76 .cacheid: .dockercfg
77         sha1sum Makefile runcontext $(wildcard ubuntu-builder/*) > .cachedata
78         cat .cachedata | $(CACHETOOL) getid > .cacheid
79
80 # Clean local data related to caching - called prior to ordinary build
81 .PHONY: clean-cache
82 clean-cache:
83         rm -f .cachedata .cacheid
84
85 # Try to download cache - called prior to ordinary build
86 .PHONY: get-cache
87 get-cache: .cacheid
88         @if $(CACHETOOL) check $(shell cat .cacheid); then \
89                 if test "$(shell ${DOCKER_EXPIRED})" -eq "0"; then \
90                         echo "Docker: Tag '${DOCKER_TAG}' missing" \
91                                 "or older than ${DOCKER_KEEP}, not using it."; \
92                 else \
93                         touch .docker; \
94                 fi; \
95         else \
96                 echo "No cache item found for $(shell cat .cacheid)" ;\
97                 exit 0;\
98         fi
99
100 # Store cache if not already stored - called after ordinary build
101 .PHONY: put-cache
102 put-cache: .cacheid
103         @tar cf - .docker | $(CACHETOOL) put $(shell cat .cacheid)