-
Notifications
You must be signed in to change notification settings - Fork 36
/
Makefile
173 lines (154 loc) · 6.14 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Environment checkup
ifndef IMAGE_DIR
$(error "No image directory specified (IMAGE_DIR)")
endif
include $(IMAGE_DIR)/env.mk
ifndef IMAGE_NAME
$(error "No image base name found (IMAGE_NAME), e.g. 'ubuntu'")
endif
ifndef IMAGE_VERSION
$(error "No image version found (IMAGE_VERSION), e.g. 'xenial'")
endif
ifndef IMAGE_TITLE
$(error "No image title found (IMAGE_TITLE), e.g. 'Ubuntu Xenial (16.04)'")
endif
# Architecture variables setup
## Normalize host arch
HOST_ARCH := $(shell uname -m)
ifeq ($(HOST_ARCH), $(filter $(HOST_ARCH),arm armhf armv7l))
HOST_ARCH = arm
else ifeq ($(HOST_ARCH), $(filter $(HOST_ARCH),arm64 aarch64))
HOST_ARCH = arm64
else ifeq ($(HOST_ARCH), $(filter $(HOST_ARCH),x86_64 amd64))
HOST_ARCH = x86_64
endif
ARCH ?= $(HOST_ARCH)
## Normalize other arch variables
ifeq ($(ARCH), $(filter $(ARCH),arm armhf armv7l))
TARGET_QEMU_ARCH=arm
TARGET_SCW_ARCH=arm
TARGET_DOCKER_REPO_ARCH=arm32v7
TARGET_MULTIARCH_ARCH=armhf
TARGET_GOLANG_ARCH=arm
else ifeq ($(ARCH), $(filter $(ARCH),arm64 aarch64))
TARGET_QEMU_ARCH=aarch64
TARGET_SCW_ARCH=arm64
TARGET_DOCKER_REPO_ARCH=arm64v8
TARGET_MULTIARCH_ARCH=arm64
TARGET_GOLANG_ARCH=arm64
else ifeq ($(ARCH), $(filter $(ARCH),x86_64 amd64))
TARGET_QEMU_ARCH=x86_64
TARGET_SCW_ARCH=x86_64
TARGET_DOCKER_REPO_ARCH=amd64
TARGET_MULTIARCH_ARCH=amd64
TARGET_GOLANG_ARCH=amd64
endif
DOCKER_NAMESPACE ?= scaleway
BUILD_OPTS ?=
override BUILD_ARGS += SCW_ARCH=$(TARGET_SCW_ARCH) MULTIARCH_ARCH=$(TARGET_MULTIARCH_ARCH) DOCKER_ARCH=$(TARGET_DOCKER_REPO_ARCH)
REGION ?= par1
export REGION
BUILD_METHOD ?= from-rootfs
SERVE_ASSETS ?= y
EXPORT_DIR ?= $(IMAGE_DIR)/export/$(TARGET_SCW_ARCH)
ASSETS_DIR ?= $(EXPORT_DIR)/assets
OUTPUT_ID_TO ?= $(EXPORT_DIR)/image_id
export OUTPUT_ID_TO
ifdef IMAGE_BOOTSCRIPT_$(TARGET_SCW_ARCH)
IMAGE_BOOTSCRIPT = $(IMAGE_BOOTSCRIPT_$(TARGET_SCW_ARCH))
endif
IMAGE_DISK_SIZE ?= 10G
ifeq ($(shell which scw-metadata >/dev/null 2>&1; echo $$?), 0)
IS_SCW_HOST := y
LOCAL_SCW_REGION := $(shell scw-metadata --cached LOCATION_ZONE_ID)
export LOCAL_SCW_REGION
ifeq ($(LOCAL_SCW_REGION), $(REGION))
SERVE_IP := $(shell scw-metadata --cached PRIVATE_IP)
else
SERVE_IP := $(shell scw-metadata --cached PUBLIC_IP_ADDRESS)
endif
SERVE_PORT := $(shell shuf -i 10000-60000 -n 1)
else
IS_SCW_HOST := n
ifeq ($(SERVE_ASSETS), n)
ifndef SERVE_IP
$(error "Not a Scaleway host and no server IP given")
endif
ifndef SERVE_PORT
$(error "Not a Scaleway host and no server port given")
endif
endif
endif
export IS_SCW_HOST
# Default action: display usage
.PHONY: usage
usage:
@echo 'Usage'
@echo ' image build the Docker image'
@echo ' rootfs.tar export the Docker image to a rootfs.tar'
@echo ' scaleway_image create a Scaleway image, requires a working `scaleway-cli'
@echo ' local_tests run TIM tests against the Docker image'
@echo ' tests run TIM tests against the image on Scaleway'
.PHONY: fclean
fclean: clean
for tag in latest $(shell docker images | grep "^$(DOCKER_NAMESPACE)/$(IMAGE_NAME) " | awk '{print $$2}'); do\
echo "Creating a backup of '$(DOCKER_NAMESPACE)/$(IMAGE_NAME):$$tag' for caching"; \
docker tag $(DOCKER_NAMESPACE)/$(IMAGE_NAME):$$tag old$(DOCKER_NAMESPACE)/$(IMAGE_NAME):$$tag; \
docker rmi -f $(DOCKER_NAMESPACE)/$(IMAGE_NAME):$$tag; \
done
.PHONY: clean
clean:
-rm -f $(ASSETS_DIR) $(EXPORT_DIR)/export.tar
-rm -rf $(EXPORT_DIR)/rootfs
$(EXPORT_DIR):
mkdir -p $(EXPORT_DIR)
$(ASSETS_DIR):
mkdir -p $(ASSETS_DIR)
.PHONY: image
image: $(EXPORT_DIR)
ifneq ($(TARGET_SCW_ARCH), $(HOST_ARCH))
docker run --rm --privileged multiarch/qemu-user-static:register --reset
endif
ifdef IMAGE_BASE_FLAVORS
$(foreach bf,$(IMAGE_BASE_FLAVORS),rsync -az bases/overlay-$(bf)/ $(IMAGE_DIR)/overlay-base;)
endif
docker build $(BUILD_OPTS) -t $(DOCKER_NAMESPACE)/$(IMAGE_NAME):$(TARGET_SCW_ARCH)-$(IMAGE_VERSION) $(foreach ba,$(BUILD_ARGS),--build-arg $(ba)) $$([ -r Dockerfile.$(TARGET_SCW_ARCH) ] && echo "-f Dockerfile.$(TARGET_SCW_ARCH)") $(IMAGE_DIR)
echo $(DOCKER_NAMESPACE)/$(IMAGE_NAME):$(TARGET_SCW_ARCH)-$(IMAGE_VERSION) >$(EXPORT_DIR)/docker_tags
$(eval IMAGE_VERSION_ALIASES += $(IMAGE_VERSION)-$(shell date +%Y-%m-%d))
$(foreach v,$(IMAGE_VERSION_ALIASES),\
docker tag $(DOCKER_NAMESPACE)/$(IMAGE_NAME):$(TARGET_SCW_ARCH)-$(IMAGE_VERSION) $(DOCKER_NAMESPACE)/$(IMAGE_NAME):$(TARGET_SCW_ARCH)-$v;\
echo $(DOCKER_NAMESPACE)/$(IMAGE_NAME):$(TARGET_SCW_ARCH)-$v >>$(EXPORT_DIR)/docker_tags;)
$(ASSETS_DIR)/rootfs.tar: image $(ASSETS_DIR)
echo "IMAGE_ID=\"$(IMAGE_TITLE)\"" >> $(EXPORT_DIR)/scw-release
echo "IMAGE_RELEASE=$(shell date +%Y-%m-%d)" >> $(EXPORT_DIR)/scw-release
echo "IMAGE_CODENAME=$(IMAGE_NAME)" >> $(EXPORT_DIR)/scw-release
echo "IMAGE_DESCRIPTION=\"$(IMAGE_DESCRIPTION)\"" >> $(EXPORT_DIR)/scw-release
echo "IMAGE_HELP_URL=\"$(IMAGE_SOURCE_URL)\"" >> $(EXPORT_DIR)/scw-release
echo "IMAGE_SOURCE_URL=\"$(IMAGE_SOURCE_URL)\"" >> $(EXPORT_DIR)/scw-release
echo "IMAGE_DOC_URL=\"$(IMAGE_SOURCE_URL)\"" >> $(EXPORT_DIR)/scw-release
cat $(EXPORT_DIR)/scw-release | docker run --name $(IMAGE_NAME)-$(IMAGE_VERSION)-export -i $(DOCKER_NAMESPACE)/$(IMAGE_NAME):$(TARGET_SCW_ARCH)-$(IMAGE_VERSION) sh -c "rm /.dockerenv; cat >/etc/scw-release" 2>/dev/null || true
docker export -o [email protected] $(IMAGE_NAME)-$(IMAGE_VERSION)-export
docker rm $(IMAGE_NAME)-$(IMAGE_VERSION)-export
mv [email protected] $@
rootfs.tar: $(ASSETS_DIR)/rootfs.tar
ls -la $<
@echo $<
from-rootfs-common: rootfs.tar
$(eval ROOTFS_URL := $(SERVE_IP):$(SERVE_PORT)/rootfs.tar)
ifeq ($(SERVE_ASSETS), y)
scripts/assets_server.sh start $(SERVE_PORT) $(ASSETS_DIR)
endif
scripts/create_image_live_from_rootfs.sh "$(ROOTFS_URL)" "$(IMAGE_TITLE)" "$(TARGET_SCW_ARCH)" "$(IMAGE_DISK_SIZE)" "$(IMAGE_BOOTSCRIPT)" "$(BUILD_METHOD)"
ifeq ($(SERVE_ASSETS), y)
scripts/assets_server.sh stop $(SERVE_PORT)
endif
from-rootfs: from-rootfs-common
unpartitioned-from-rootfs: from-rootfs-common
.PHONY: scaleway_image
scaleway_image: $(BUILD_METHOD)
.PHONY: tests
tests: $(EXPORT_DIR)
scripts/test_image.sh start $(TARGET_SCW_ARCH) $(REGION) $(IMAGE_ID) $(EXPORT_DIR)/$(IMAGE_ID).servers $(IMAGE_DIR)/tim_tests
ifneq ($(NO_CLEANUP), true)
scripts/test_image.sh stop $(EXPORT_DIR)/$(IMAGE_ID).servers
endif