-
Notifications
You must be signed in to change notification settings - Fork 5
/
Makefile
201 lines (150 loc) · 6.17 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
.DEFAULT_GOAL := help
.EXPORT_ALL_VARIABLES:
PROVIDERS := libvirt vmware virtualbox
BOX_NAME := ClearLinux
OWNER ?= AntonioMeireles
REPOSITORY := $(OWNER)/$(BOX_NAME)
VERSION ?= $(shell curl -Ls $(CLR_BASE_URL)/latest)
CLR_BASE_URL := https://download.clearlinux.org
CLR_RELEASE_URL := $(CLR_BASE_URL)/releases/$(VERSION)/clear
NV := $(BOX_NAME)-$(VERSION)
OSV := clear-$(VERSION)
VAGRANT_REPO := https://app.vagrantup.com/api/v1/box/$(REPOSITORY)
UNAME := $(shell uname)
LIBVIRT_HOST ?= libvirt-host.clearlinux.local
LIBVIRT_CONNECT :=
ifneq ($(UNAME),Linux)
LIBVIRT_CONNECT := ssh clear@$(LIBVIRT_HOST)
endif
define mediaFactory
media/$(OSV)-$1-factory
endef
define VMDKtarget
ifneq ($T,libvirt)
media/$(OSV)-$T-factory.vmdk: media/$(OSV)-$T-factory.img
$(call imgToVMDK,$T)
endif
endef
define IMGtarget
media/$(OSV)-$T-factory.img:
$(call buildBaseImg,$T)
endef
define PROVIDERtarget
$T: boxes/$T/$(NV).$T.box
endef
define UploadBoxTarget
.PHONY: upload-$T-box
upload-$T-box: boxes/$T/$(NV).$T.box
$(call boxUpload,$T)
endef
define smokeTESTtarget
.PHONY: test-$T
test-$T: boxes/$T/$(NV).$T.box
$(call boxSmokeTest,$T)
$(if $(filter $T,libvirt),$(LIBVIRT_CONNECT) sudo virsh vol-delete clear-test_vagrant_box_image_0_box.img default,)
endef
define targetConfig
builders/$1.yml.$(VERSION)
endef
define buildBaseImg
@mkdir -p media
@echo "- assembling v$(VERSION) base img for $1 guests..."
@sed -e "s,^version:.*,version: $(VERSION)," builders/$1.yml > $(targetConfig)
sudo clr-installer --swupd-clean --config $(targetConfig) -l 4 -b installer:media/$(OSV)-$1-factory.img
rm -rf $(targetConfig)
endef
define imgToVMDK
qemu-img convert -p -C media/$(OSV)-$1-factory.img -O vmdk media/$(OSV)-$1-factory.vmdk
endef
define vmxBuilder
@mkdir -p media/$(OSV)-$1-factory
@for f in virtualbox.vmx vmware.vmx vmxf vmsd plist; do \
cp template/$(BOX_NAME).$$f.tmpl media/$(OSV)-$1-factory/$(NV).$$f; done
@cp media/$(OSV)-$1-factory/$(NV).$1.vmx media/$(OSV)-$1-factory/$(NV).vmx
@pushd media/$(OSV)-$1-factory && sed -i "s,VERSION,$(VERSION)," $(BOX_NAME)-* && popd
@ln -sf ../$(OSV)-$1-factory.vmdk media/$(OSV)-$1-factory/
@pushd media/$(OSV)-$1-factory && sed -i "s,VMDK_SIZE,$$( stat --printf="%s" ../$(OSV)-$1-factory.vmdk)," $(BOX_NAME)-* && popd
endef
define pack
packer build -force $1.pkr.hcl
endef
define builder
$(if $(filter $1,libvirt),qemu,)
$(if $(filter $1,vmware),vmware-vmx,)
$(if $(filter $1,virtualbox),virtualbox-ovf,)
endef
define provider
$(if $(filter $1,vmware),vmware_desktop,$1)
endef
define boxSmokeTest
vagrant box add --name clear-test --provider $(provider) boxes/$1/$(NV).$1.box --force
( cd extras/test; vagrant up --provider $(provider) && \
vagrant ssh -c "w; sudo swupd info ; echo ; df -kh" && \
vagrant halt -f && vagrant destroy -f && \
vagrant box remove clear-test --provider $(provider) )
endef
define authBearer
--header "Authorization: Bearer ${VAGRANT_CLOUD_TOKEN}"
endef
define isJson
--header "Content-Type: application/json"
endef
define boxUpload
@echo "- '$(OWNER)/$(BOX_NAME)/$(VERSION)/$1' uploading..."
@curl $(shell curl -s $(authBearer) $(VAGRANT_REPO)/version/${VERSION}/provider/$(if $(filter $1, vmware),vmware_desktop,$1)/upload | jq .upload_path | tr -d \") \
--upload-file boxes/$1/$(NV).$1.box && echo "- '$(OWNER)/$(BOX_NAME)/$(VERSION)/$1' uploaded"
endef
define addProviderToRelease
@echo "- adding '$1' provider to '$(OWNER)/$(BOX_NAME)/$(VERSION)'\n"
$(shell curl -s $(isJson) $(authBearer) $(VAGRANT_REPO)/version/${VERSION}/providers --data '{"provider": {"name": "$(if $(filter $1,vmware),vmware_desktop,$1)"}}' >/dev/null )
endef
.PHONY: help
help:
@echo
@echo "the following PROVIDERS are currently available: \033[36m$(PROVIDERS)\033[0m"
@echo
@echo "available 'make' targets:"
@echo
@grep -E "^.*:.*?## .*$$" $(MAKEFILE_LIST) | grep -vE "(grep|BEGIN)" | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\t\033[36m%-30s\033[0m %s\n", $$1, $$2}' | \
envsubst
@echo
@echo "By default the target VERSION is the 'latest' one, currently at $(VERSION)"
@echo "To target a specific one add 'VERSION=...' to your make invocation"
@echo
$(foreach T,$(PROVIDERS),$(eval $(IMGtarget)))
$(foreach T,$(PROVIDERS),$(eval $(VMDKtarget)))
$(call mediaFactory,virtualbox)/$(NV).ova: $(call mediaFactory,virtualbox).vmdk
# synthethising VirtualBox OVA
$(call vmxBuilder,virtualbox)
ovftool $(call mediaFactory,virtualbox)/$(NV).vmx $(call mediaFactory,virtualbox)/$(NV).ova
# VirtualBox VM (OVA) syntethised from vmdk
$(call mediaFactory,vmware)/$(NV).vmx: $(call mediaFactory,vmware).vmdk
# synthethising VMware VM
$(call vmxBuilder,vmware)
# vmware fusion VM (v$(VERSION)) syntetised from vmdk
.PHONY: media $(foreach p,$(PROVIDERS),media/$(p))
media: $(foreach p,$(PROVIDERS),media/$(p)) ## Media Fetcher Assembles locally all media needed by Packer
media/vmware: $(call mediaFactory,vmware)/$(NV).vmx
media/libvirt: $(call mediaFactory,libvirt).img
media/virtualbox: $(call mediaFactory,virtualbox).vmdk
.PHONY: all $(PROVIDERS) release upload publish clean
all: $(PROVIDERS) ## Packer Builds All providers boxes
$(foreach T,$(PROVIDERS),$(eval $(PROVIDERtarget)))
boxes/libvirt/$(NV).libvirt.box: $(call mediaFactory,libvirt).img
$(call pack,libvirt)
boxes/virtualbox/$(NV).virtualbox.box: $(call mediaFactory,virtualbox)/$(NV).ova
$(call pack,virtualbox)
boxes/vmware/$(NV).vmware.box: $(call mediaFactory,vmware)/$(NV).vmx
$(call pack,vmware)
release: ## Vagrant Cloud Create a new release
@echo "- '$(OWNER)/$(BOX_NAME)/$(VERSION)' creating release on Vagrant Cloud" && ( cat new.tmpl.json | envsubst | curl --silent $(isJson) $(authBearer) $(VAGRANT_REPO)/versions --data-binary @- >/dev/null )
@echo
$(foreach p, $(PROVIDERS), $(call addProviderToRelease,$(p)))
upload: $(foreach p,$(PROVIDERS),upload-$(p)-box) ## Vagrant Cloud Uploads all built boxes for version
$(foreach T,$(PROVIDERS),$(eval $(UploadBoxTarget)))
publish: ## Vagrant Cloud make uploaded boxes public
@curl --silent $(authBearer) $(VAGRANT_REPO)/version/$(VERSION)/release --request PUT | jq .
$(foreach T,$(PROVIDERS),$(eval $(smokeTESTtarget)))
clean: ## frees space
rm -rf media/* boxes/* packer_cache/*