-
Notifications
You must be signed in to change notification settings - Fork 17
/
Makefile
240 lines (221 loc) · 7.6 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#################
## Env Configs ##
#################
GOOS ?= linux
GOARCH ?= amd64
CTHULHU_DIR ?= null
SKIP_VERSION_CHECK ?= 0
VERSION ?= $(shell ./scripts/get_version.sh)
ifeq ($(GOARCH),386)
PKG_ARCH = i386
else
PKG_ARCH = amd64
endif
############
## Macros ##
############
mkdir = @mkdir -p $(dir $@)
touch = @touch $@
cp = @cp $< $@
print = @printf "\n:::::::::::::::: [$(shell date -u)] $@ ::::::::::::::::\n"
now = $(shell date -u)
fpm = @docker run --platform linux/amd64 --rm -i -v "$(CURDIR):$(CURDIR)" -w "$(CURDIR)" -u $(shell id -u) digitalocean/fpm:latest
shellcheck = @docker run --platform linux/amd64 --rm -i -v "$(CURDIR):$(CURDIR)" -w "$(CURDIR)" -u $(shell id -u) koalaman/shellcheck:v0.6.0
version_check = @./scripts/check_version.sh
linter = docker run --platform linux/amd64 --rm -i -v "$(CURDIR):$(CURDIR)" -w "$(CURDIR)" -e "GOOS=$(GOOS)" -e "GOARCH=$(GOARCH)" -e "GO111MODULE=on" -e "GOFLAGS=-mod=vendor -buildvcs=false" -e "XDG_CACHE_HOME=$(CURDIR)/target/.cache/go" \
-u $(shell id -u) golangci/golangci-lint:v1.58 \
golangci-lint run --skip-files=.*_test.go -D errcheck -E revive -E gosec -E gofmt
go_docker_linux = golang:1.22.3
ifeq ($(GOOS), linux)
go = docker run --platform linux/amd64 --rm -i \
-e "GOOS=$(GOOS)" \
-e "GOARCH=$(GOARCH)" \
-e "GOCACHE=$(CURDIR)/target/.cache/go" \
-e "CGO_ENABLED=0" \
-v "$(CURDIR):$(CURDIR)" \
-w "$(CURDIR)" \
$(go_docker_linux) \
go
go_w_cgo = docker run --platform linux/amd64 --rm -i \
-e "GOOS=$(GOOS)" \
-e "GOARCH=$(GOARCH)" \
-e "GOCACHE=$(CURDIR)/target/.cache/go" \
-e "CGO_ENABLED=1" \
-v "$(CURDIR):$(CURDIR)" \
-w "$(CURDIR)" \
$(go_docker_linux) \
go
else
go = GOOS=$(GOOS) \
GOARCH=$(GOARCH) \
GO111MODULE=on \
GOFLAGS=-mod=vendor \
CGO_ENABLED=0 \
GOCACHE=$(CURDIR)/target/.cache/go \
$(shell which go)
go_w_cgo = GOOS=$(GOOS) \
GOARCH=$(GOARCH) \
GO111MODULE=on \
GOFLAGS=-mod=vendor \
CGO_ENABLED=1 \
GOCACHE=$(CURDIR)/target/.cache/go \
$(shell which go)
endif
ldflags = '\
-s -w \
-X "main.version=$(VERSION)" \
-X "main.buildDate=$(now)" \
'
SYSINIT_CONF="packaging/syscfg/init/droplet-agent.conf"
SYSTEMD_CONF="packaging/syscfg/systemd/droplet-agent.service"
UPDATE_SCRIPT="packaging/scripts/update.sh"
###########
## Paths ##
###########
out := target
package_dir := $(out)/pkg
cache := $(out)/.cache
project := droplet-agent
pkg_project := $(subst _,-,$(project))# make sure package does not have underscores in the name
gofiles := $(shell find . -type f -iname '*.go' ! -path './vendor/*')
shellscripts := $(shell find . -type f -iname '*.sh' ! -path './vendor/*' ! -path './.git/*')
binary := $(out)/$(project)-$(GOOS)-$(GOARCH) # the name of the binary built with local resources
base_linux_package := $(package_dir)/$(pkg_project).$(VERSION).$(PKG_ARCH).BASE.deb
# note: to be compliant with repository's naming requirement:
# deb files should end with _version_arch.deb
# rpm files should end with -version-release.arch.rpm
deb_package := $(package_dir)/$(pkg_project)_$(VERSION)_$(PKG_ARCH).deb
rpm_package := $(package_dir)/$(pkg_project).$(VERSION).$(PKG_ARCH).rpm
tar_package := $(package_dir)/$(pkg_project).$(VERSION).$(PKG_ARCH).tar.gz
#############
## Targets ##
#############
.PHONY: test
test:
ifndef pkg
$(linter) ./...
$(go_w_cgo) test -cover -race ./...
else
$(linter) $(pkg)
$(go_w_cgo) test -cover -race -v ./$(pkg)
endif
.PHONY: internal/config/version.go
internal/config/version.go:
$(print)
@printf "version = v%s" $(VERSION)
@printf "// SPDX-License-Identifier: Apache-2.0\n\npackage config\n\n// Version is the current package version.\nconst Version = \"v%s\"\n" $(VERSION) > $@
.PHONY: target/VERSION internal/config/version.go
target/VERSION:
$(print)
$(mkdir)
ifneq ($(SKIP_VERSION_CHECK), 1)
$(version_check)
else
@echo "========Skipping Version Check======"
endif
@echo $(VERSION) > $@
.INTERMEDIATE: $(binary)
build: $(binary)
$(binary): $(gofiles)
$(print)
$(mkdir)
$(go) build -ldflags $(ldflags) -trimpath -o "$@" ./cmd/agent/
shellcheck: $(cache)/shellcheck
$(cache)/shellcheck: $(shellscripts)
$(print)
$(mkdir)
@$(shellcheck) --version
@$(shellcheck) $^
$(touch)
.PHONY: clean
clean:
$(print)
@rm -rf $(out)
release: target/VERSION
$(print)
@GOOS=linux GOARCH=386 $(MAKE) build deb rpm tar
@GOOS=linux GOARCH=amd64 $(MAKE) build deb rpm tar
lint: $(cache)/lint $(cache)/shellcheck
$(cache)/lint: $(gofiles)
$(print)
$(mkdir)
@$(linter) ./...
$(touch)
.INTERMEDIATE: $(base_linux_package)
$(base_linux_package): $(binary)
$(print)
$(mkdir)
@$(fpm) --output-type deb \
--verbose \
--input-type dir \
--force \
--architecture $(PKG_ARCH) \
--package $@ \
--no-depends \
--name $(pkg_project) \
--maintainer "DigitalOcean Droplet Engineering" \
--version $(VERSION) \
--url https://github.com/digitalocean/droplet-agent \
--description "DigitalOcean Droplet Agent" \
--license apache-2.0 \
--vendor DigitalOcean \
--log info \
--after-install packaging/scripts/after_install.sh \
--after-remove packaging/scripts/after_remove.sh \
--config-files /etc/init/droplet-agent.conf \
--config-files /etc/systemd/system/droplet-agent.service \
$<=/opt/digitalocean/bin/droplet-agent \
$(UPDATE_SCRIPT)=/opt/digitalocean/droplet-agent/scripts/update.sh \
$(SYSINIT_CONF)=/etc/init/droplet-agent.conf \
$(SYSTEMD_CONF)=/etc/systemd/system/droplet-agent.service
deb: $(deb_package)
$(deb_package): $(base_linux_package)
$(print)
$(mkdir)
@$(fpm) --output-type deb \
--verbose \
--input-type deb \
--force \
--depends cron \
-p $@ \
$<
# print information about the compiled deb package
@docker run --platform linux/amd64 --rm -i -v "$(CURDIR):$(CURDIR)" -w "$(CURDIR)" ubuntu:xenial /bin/bash -c 'dpkg --info $@ && dpkg -c $@'
rpm: $(rpm_package)
$(rpm_package): $(base_linux_package)
$(print)
$(mkdir)
@$(fpm) \
--verbose \
--output-type rpm \
--input-type deb \
--depends cronie \
--rpm-posttrans packaging/scripts/after_install.sh \
--force \
-p $@ \
$<
# print information about the compiled rpm package
@docker run --platform linux/amd64 --rm -i -v "$(CURDIR):$(CURDIR)" -w "$(CURDIR)" centos:7 rpm -qilp $@
tar: $(tar_package)
$(tar_package): $(base_linux_package)
$(print)
$(mkdir)
@$(fpm) \
--verbose \
--output-type tar \
--input-type deb \
--force \
-p $@ \
$<
# print all files within the archive
@docker run --platform linux/amd64 --rm -i -v "$(CURDIR):$(CURDIR)" -w "$(CURDIR)" ubuntu:xenial tar -ztvf $@
## mockgen: generates the mocks for the droplet agent service
mockgen:
@echo "Generating mocks"
mockgen -source=internal/sysaccess/common.go -package=mocks -destination=internal/sysaccess/internal/mocks/mocks.go
mockgen -source=internal/sysaccess/ssh_helper.go -package=sysaccess -destination=internal/sysaccess/ssh_helper_mocks.go
mockgen -source=internal/sysaccess/authorized_keys_file_updater.go -package=sysaccess -destination=internal/sysaccess/authorized_keys_file_updater_mocks.go
mockgen -source=internal/metadata/actioner/do_managed_keys_actioner.go -package=mocks -destination=internal/metadata/actioner/internal/mocks/mocks.go
GOOS=linux mockgen -source=internal/netutil/tcp_sniffer_helper_linux.go -package=mocks -destination=internal/netutil/internal/mocks/dependent_functions_mock.go
mockgen -source=internal/metadata/updater/updater.go -package=updater -destination=internal/metadata/updater/updater_mocks.go
mockgen -destination=internal/metadata/updater/readcloser_mocks.go -package=updater -build_flags=--mod=mod io ReadCloser