-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
214 lines (170 loc) · 7.49 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
######################################################
# Docker Targets
# Needs docker installed on the system
######################################################
# Define variables for PostgreSQL container
POSTGRES_CONTAINER := postgres
POSTGRES_DB := cc_terrarium
POSTGRES_USER := postgres
FARM_DB_DUMP_FILE := $(POSTGRES_DB).psql
COVERAGE_FILE = coverage/coverage.txt
FARM_DB_DUMP_FILE_SQL_ZIP := $(POSTGRES_DB)_data.sql.gz
# Define variables for pg_dump command
DUMP_DIR := ./data
.PHONY: docker-init
docker-init: ## Initialize the environment before running docker commands
@touch ${HOME}/.netrc
.PHONY: db-dump
db-dump: start-db ## Target for dumping PostgreSQL database to a file
docker compose exec -T $(POSTGRES_CONTAINER) pg_dump -U $(POSTGRES_USER) -f /docker-entrypoint-initdb.d/$(FARM_DB_DUMP_FILE) -Fc $(POSTGRES_DB)
docker compose exec -T $(POSTGRES_CONTAINER) pg_dump -Z 9 --column-inserts --rows-per-insert=100 --data-only --exclude-table=taxonomies -U $(POSTGRES_USER) -f /docker-entrypoint-initdb.d/$(FARM_DB_DUMP_FILE_SQL_ZIP) -Fp $(POSTGRES_DB)
.PHONY: db-update ## Restore database from the database bump file. Ignore errors for existing rows.
db-update: data/$(FARM_DB_DUMP_FILE) start-db
docker compose exec -T $(POSTGRES_CONTAINER) pg_restore -a -d $(POSTGRES_DB) -U $(POSTGRES_USER) /docker-entrypoint-initdb.d/$(FARM_DB_DUMP_FILE) || echo ignore errors for already existing rows.
.PHONY: docker-build
docker-build: ## Build API container image
docker compose build
.PHONY: docker-run
docker-run: ## Starts app in docker containers
docker compose up -d
.PHONY: start-db
start-db: ## Starts database in docker containers
docker compose up -d postgres
.PHONY: docker-stop
docker-stop: ## Stops and removes docker containers
docker compose down
.PHONY: docker-stop-clean
docker-stop-clean: ## Stops and removes containers as well as volumes to cleanup database
docker compose down -v
docker-tools-build: ## Build CLI & farm-harvester images
docker compose --profile tooling build
docker-harvest: docker-tools-build start-db ## Run harvest scripts on the Farm folder in a containerized environment
docker compose run --rm farm-harvester
docker-api-test: ## Run API unit tests in a containerized environment
docker compose run --build --rm terrarium-unit-test
######################################################
# Go Targets
# Needs Go installed on the system
######################################################
-include .env
export
CLI_SRCS := $(shell find ./src/pkg ./src/cli \( -name '*.go' -o -name 'go.mod' \))
BUILD_DATE=$(shell date "+%Y-%m-%d")
BUILD_DIR=.bin
CLI_NAME = terrarium
BINARY_NAME = $(BUILD_DIR)/$(CLI_NAME)
ifeq (${TAG},)
TAG=$(shell git describe --exact-match --tags 2> /dev/null)
VERSION=$(shell git rev-parse --short HEAD 2> /dev/null)
ZIP_DATE=$(shell date "+%Y%m%d")
ZIP_FILE=$(BUILD_DIR)/terrarium_$(ZIP_DATE)_$(VERSION).zip
else
VERSION=$(TAG)
ZIP_FILE=$(BUILD_DIR)/terrarium_$(TAG).zip
VERFILE=$(shell echo $(TAG) > $(BUILD_DIR)/latest_version.txt)
endif
GO_LDFLAGS := -s -w
GO_LDFLAGS := -X github.com/cldcvr/terrarium/src/cli/internal/build.Version=$(VERSION) $(GO_LDFLAGS)
GO_LDFLAGS := -X github.com/cldcvr/terrarium/src/cli/internal/build.Date=$(BUILD_DATE) $(GO_LDFLAGS)
GO_LDFLAGS := -ldflags "$(GO_LDFLAGS)"
.PHONY: mod-clean
mod-clean: ## delete go*.sum files
@echo "deleting .sum files..."
@rm -f ./src/api/go.sum ./src/cli/go.sum ./src/pkg/go.sum ./go.work.sum
.PHONY: clean-binaries
clean-binaries: ## Clean up artifacts generated by make
@echo "-- Cleaning build directory"
@rm -rf $(BUILD_DIR)/
.PHONY: mod-tidy
mod-tidy: ## run go mod tidy on each workspace entity, and then sync workspace
@echo "running tidy on api go module..."
@cd src/api && go mod tidy -e
@echo "running tidy on cli go module..."
@cd src/cli && go mod tidy -e
@echo "running tidy on pkg go module..."
@cd src/pkg && go mod tidy -e
@echo "running sync on go workspace..."
@go mod download && go work sync
.PHONY: test
test: start-db --test ## Run go unit tests
--test:
mkdir -p coverage
go test -tags=mock,dbtest -coverprofile $(COVERAGE_FILE) github.com/cldcvr/terrarium/...
@echo "-- Test coverage for terrarium --"
@go tool cover -func=$(COVERAGE_FILE)|grep "total:"
define make_binary
@echo "-- Building application binary $(1) for $(2)-$(3)"
CGO_ENABLED=1 GOOS=$(2) GOARCH=$(3) go build $(GO_LDFLAGS) -v -o $(1) ./src/cli/terrarium
endef
$(BINARY_NAME): $(CLI_SRCS)
$(call make_binary, $(BINARY_NAME),$(shell go env GOOS),$(shell go env GOARCH))
.PHONY: binary
binary: $(BINARY_NAME) ## Build application binary (native)
.PHONY: install
install: ## Install the CLI native binary into GOBIN
@echo "-- Installing native binary in $(shell go env GOBIN)"
CGO_ENABLED=1 go install $(GO_LDFLAGS) github.com/cldcvr/terrarium/src/cli/terrarium
######################################################
# Farm Targets
# Needs terraform installed on the system
######################################################
ifeq ($(HARVEST_WD),)
HARVEST_WD := /tmp/harvest
endif
ifeq ($(FARM_DIR),)
FARM_DIR := examples/farm
endif
ifeq ($(FARM_DEPENDENCY_DIR),)
FARM_DEPENDENCY_DIR := $(FARM_DIR)/dependencies/
endif
ifeq ($(FARM_PLATFORM_DIR),)
FARM_PLATFORM_DIR := $(FARM_DIR)/platform/
endif
.PHONY: farm-harvest
farm-harvest: farm-dependency-harvest farm-platform-harvest farm-resource-harvest farm-module-harvest farm-mapping-harvest## Run all harvest commands on the farm directory
.PHONY: farm-resource-harvest ## Harvest terraform provider resources from module list file
farm-resource-harvest: $(FARM_DIR)/modules.yaml
@mkdir -p $(HARVEST_WD)
terrarium harvest resources --module-list-file $(FARM_DIR)/modules.yaml --workdir $(HARVEST_WD)
.PHONY: farm-module-harvest ## Harvest terraform modules from module list file
farm-module-harvest: $(FARM_DIR)/modules.yaml
@mkdir -p $(HARVEST_WD)
terrarium harvest modules --module-list-file $(FARM_DIR)/modules.yaml --workdir $(HARVEST_WD)
.PHONY: farm-mapping-harvest ## Harvest attribute mappings from module list file
farm-mapping-harvest: $(FARM_DIR)/modules.yaml
@mkdir -p $(HARVEST_WD)
terrarium harvest mappings --module-list-file $(FARM_DIR)/modules.yaml --workdir $(HARVEST_WD)
.PHONY: farm-dependency-harvest ## Harvest dependency interface from the farm directory
farm-dependency-harvest: $(FARM_DEPENDENCY_DIR)
terrarium harvest dependencies --dir $(FARM_DEPENDENCY_DIR)
.PHONY: farm-platform-harvest ## Harvest platform from the farm directory
farm-platform-harvest: $(FARM_PLATFORM_DIR)
terrarium harvest platforms --dir $(FARM_PLATFORM_DIR)
######################################################
# Farm releases pull
# Needs access to the farm repo
######################################################
FARM_REPO := https://api.github.com/repos/cldcvr/terrarium-farm
ifeq ($(FARM_VERSION),)
FARM_VERSION := latest
endif
data/$(FARM_DB_DUMP_FILE):
$(call farm-release-pull)
.PHONY: farm-release-pull
farm-release-pull:
@echo "Downloading db dump from the latest terrarium-farm release..."
@DB_DUMP_URL=$$(curl -s $(FARM_REPO)/releases/latest | grep browser_download_url | grep .psql | cut -d '"' -f 4); \
if [ -z "$$DB_DUMP_URL" ]; then \
echo "db dump file not found in the latest release"; \
exit 1; \
fi; \
curl -L -o data/$(FARM_DB_DUMP_FILE) "$$DB_DUMP_URL"
.PHONY: toc
toc:
./scripts/doc_tree.sh
.PHONY: help
help:
@grep -hE '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
-include scripts/mocks.mak
-include scripts/protoc.mak
-include scripts/copyright.mak