Skip to content

Commit

Permalink
Moved all common configs to common.mk in common-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
massenz committed Jul 21, 2024
1 parent 322c795 commit 3d45eb3
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 61 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
export PATH=$GOPATH/bin:$PATH
go install github.com/cloudflare/cfssl/cmd/cfssl@v1.6.3
go install github.com/cloudflare/cfssl/cmd/cfssljson@v1.6.3
make gencert
make certs
- name: Build Container
run: |
make container
Expand All @@ -33,7 +33,7 @@ jobs:
export AWS_REGION=us-west-2
go test ./pkg/api ./pkg/grpc ./pkg/pubsub ./pkg/storage
# TODO: need to disable for now, as this fails and I can't figure out why
# TODO: need to disable for now, as this fails when using SSL and I can't figure out why
# - name: Test CLI
# run: |
# mkdir -p ${HOME}/.fsm/certs
Expand Down
121 changes: 63 additions & 58 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,78 +1,56 @@
# Copyright (c) 2022-2024 AlertAvert.com. All rights reserved.
# Created by M. Massenzio, 2022-03-14

GOOS ?= $(shell uname -s | tr "[:upper:]" "[:lower:]")
GOARCH ?= $(shell uname -m)
ifeq ($(GOARCH),x86_64)
GOARCH=amd64
endif
GOMOD := $(shell go list -m)

# Versioning
# The `version` is a static value, set in settings.yaml, and ONLY used to tag the release.
VERSION ?= $(shell cat settings.yaml | yq -r .version)
GIT_COMMIT := $(shell git rev-parse --short HEAD)
RELEASE := v$(VERSION)-g$(GIT_COMMIT)

prog := $(shell cat settings.yaml | yq -r .name)
bin := $(prog)-$(RELEASE)_$(GOOS)-$(GOARCH)
include thirdparty/common.mk
bin := $(appname)-$(release)_$(GOOS)-$(GOARCH)

# Source files & Test files definitions

pkgs := $(shell find pkg -mindepth 1 -type d)
all_go := $(shell for d in $(pkgs); do find $$d -name "*.go"; done)
test_srcs := $(shell for d in $(pkgs); do find $$d -name "*_test.go"; done)
srcs := $(filter-out $(test_srcs),$(all_go))

##@ General

# The help target prints out all targets with their descriptions organized
# beneath their categories. The categories are represented by '##@' and the
# target descriptions by '##'. The awk commands is responsible for reading the
# entire set of makefiles included in this invocation, looking for lines of the
# file as xyz: ## something, and then pretty-format the target and help. Then,
# if there's a line with ##@ something, that gets pretty-printed as a category.
# More info on the usage of ANSI control characters for terminal formatting:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php

.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
# Certificates
certs_dir := ssl-config
ca-csr := $(certs_dir)/ca-csr.json
ca-config := $(certs_dir)/ca-config.json
server-csr := $(certs_dir)/localhost-csr.json

##@ General
.PHONY: clean
img=$(shell docker images -q --filter=reference=$(image))
clean: ## Cleans up the binary, container image and other data
clean: clean-cert ## Cleans up the binary, container image and other data
@rm -rf build/*
@find . -name "*.out" -exec rm {} \;
@[ -n "$(img)" ] && docker rmi $(img) || true
@rm -rf certs
ifneq (,$(img))
@docker rmi $(img) || true
endif

version: ## Displays the current version tag (release)
@echo v$(VERSION)
@echo v$(version)

fmt: ## Formats the Go source code using 'go fmt'
@go fmt $(pkgs) ./cmd fsm-cli/client fsm-cli/cmd

##@ Development
.PHONY: build
build: cmd/main.go $(srcs)
build: cmd/main.go $(srcs) ## Builds the binary
@mkdir -p build/bin
@echo "Building rel. $(RELEASE); OS/Arch: $(GOOS)/$(GOARCH) - Pkg: $(GOMOD)"
@echo "Building rel. $(release); OS/Arch: $(GOOS)/$(GOARCH) - Pkg: $(GOMOD)"
@GOOS=$(GOOS) GOARCH=$(GOARCH) go build \
-ldflags "-X $(GOMOD)/api.Release=$(RELEASE)" \
-ldflags "-X $(GOMOD)/api.Release=$(release)" \
-o build/bin/$(bin) cmd/main.go
@echo "Majordomo Server $(shell basename $(bin)) built"
@echo "$(GREEN)[SUCCESS]$(RESET) Binary $(shell basename $(bin)) built"

build/bin/$(bin): build

.PHONY: test
test: $(srcs) $(test_srcs) ## Runs all tests
test: $(srcs) $(test_srcs) check_certs ## Runs all tests
@mkdir -p build/reports
ginkgo -keepGoing -cover -coverprofile=coverage.out -outputdir=build/reports $(pkgs)
# Clean up the coverage files (they are not needed once the
# report is generated)
# FIXME: once we move packages to pkg/ adjust the path to ./pkg
@find . -name "coverage.out" -exec rm {} \;
@find ./pkg -name "coverage.out" -exec rm {} \;

.PHONY: watch
watch: $(srcs) $(test_srcs) ## Runs all tests every time a source or test file changes
Expand All @@ -96,50 +74,74 @@ dev: build ## Runs the server binary in development mode
##@ Container Management
# Convenience targets to run locally containers and
# setup the test environments.
image := massenz/$(prog)
image := massenz/$(appname)
compose := docker/compose.yaml
dockerfile := docker/Dockerfile

.PHONY: container
container: build/bin/$(bin) ## Builds the container image
docker build -f $(dockerfile) \
--build-arg="VERSION=$(VERSION)" \
-t $(image):$(RELEASE) .
--build-arg="VERSION=$(version)" \
-t $(image):$(release) .
@echo "$(GREEN)[SUCCESS]$(RESET) Container image $(YELLOW)$(image):$(release)$(RESET) built"

.PHONY: start
start: ## Runs the container locally
@RELEASE=$(RELEASE) BASEDIR=$(shell pwd) docker compose -f $(compose) --project-name sm up redis localstack -d
@echo "$(GREEN)[STARTING]$(RESET) Stopping containers"
@RELEASE=$(release) BASEDIR=$(shell pwd) docker compose -f $(compose) --project-name sm up redis localstack -d
@sleep 3
@for queue in events notifications; do \
aws --no-cli-pager --endpoint-url=http://localhost:4566 \
--region us-west-2 \
sqs create-queue --queue-name $$queue; done
@RELEASE=$(RELEASE) BASEDIR=$(shell pwd) docker compose -f $(compose) --project-name sm up server
@RELEASE=$(release) BASEDIR=$(shell pwd) docker compose -f $(compose) --project-name sm up server
@echo "$(GREEN)[SUCCESS]$(RESET) Containers started"

.PHONY: stop
stop: ## Stops the running containers
@echo "$(RED)[STOPPING]$(RESET) Stopping containers"
@RELEASE=$(release) BASEDIR=$(shell pwd) docker compose -f $(compose) --project-name sm down


##@ TLS Support
#
# This section is WIP and subject to change
# Dependency checks for the 'test' target
.PHONY: check_certs
check_certs:
@num_certs=$$(ls -1 certs/*.pem 2>/dev/null | wc -l); \
if [ $$num_certs != 4 ]; then \
echo "$(YELLOW)[WARN]$(RESET) No certificates found in $(shell pwd)/certs"; \
make certs; \
echo "$(GREEN)[SUCCESS]$(RESET) Certificates generated in $(shell pwd)/certs"; \
else \
echo "$(GREEN)[OK]$(RESET) Certificates found in $(shell pwd)/certs"; \
fi

config_dir := ssl-config
ca-csr := $(config_dir)/ca-csr.json
ca-config := $(config_dir)/ca-config.json
server-csr := $(config_dir)/localhost-csr.json

.PHONY: gencert
gencert: $(ca-csr) $(config) $(server-csr) ## Generates all certificates in the certs directory (requires cfssl, see https://github.com/cloudflare/cfssl#installation)
cfssl gencert \
cfssl != which cfssl
cfssljson != which cfssljson
ifeq ($(strip $(cfssl)),)
$(error cfssl not installed)
endif
ifeq ($(strip $(cfssljson)),)
$(error cfssljson not installed)
endif

.PHONY: certs
certs: $(ca-csr) $(config) $(server-csr) ## Generates all certificates in the certs directory (requires cfssl, see https://github.com/cloudflare/cfssl#installation)
$(cfssl) gencert \
-initca $(ca-csr) | cfssljson -bare ca
cfssl gencert \
$(cfssl) gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=$(ca-config) \
-profile=server \
$(server-csr) | cfssljson -bare server
$(server-csr) | $(cfssljson) -bare server
@mkdir -p certs
@mv *.pem certs/
@rm *.csr
Expand All @@ -150,20 +152,23 @@ gencert: $(ca-csr) $(config) $(server-csr) ## Generates all certificates in the
clean-cert:
@rm -rf certs

##@ CLI Client
# TODO: move to a separate Makefile in the subdirectory
# See: https://www.gnu.org/software/make/manual/html_node/Recursion.html
# CLI Configuration
cli := out/bin/fsm-cli-$(VERSION)_$(GOOS)-$(GOARCH)
cli := out/bin/$(appname)-cli-$(version)_$(GOOS)-$(GOARCH)
cli_config := ${HOME}/.fsm
.PHONY: cli
cli: fsm-cli/cmd/main.go ## Builds the CLI client used to connect to the server
@mkdir -p $(out)
@mkdir -p build/bin
cd fsm-cli && GOOS=$(GOOS) GOARCH=$(GOARCH) go build \
-ldflags "-X main.Release=$(RELEASE)" \
-o ../$(out)/$(cli) cmd/main.go
-ldflags "-X main.Release=$(release)" \
-o ../build/bin/$(cli) cmd/main.go

.PHONY: cli-test
cli-test: ## Run tests for the CLI Client
@mkdir -p $(cli_config)/certs
@cp certs/ca.pem $(cli_config)/certs || true
cd fsm-cli && RELEASE=$(RELEASE) BASEDIR=$(shell pwd) \
cd fsm-cli && RELEASE=$(release) BASEDIR=$(shell pwd) \
CLI_TEST_COMPOSE=$(shell pwd)/docker/cli-test-compose.yaml \
ginkgo test ./client
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright AlertAvert.com (c) 2023
# Created by M. Massenzio, 2022-04-28

FROM golang:1.21 as builder
FROM golang:1.22 as builder

WORKDIR /server
COPY . .
Expand Down
9 changes: 9 additions & 0 deletions thirdparty/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Third-Party Libraries

## common.mk

This file is a makefile that contains common rules for building and testing
copied from the [`common-utils`](https://github.com/massenz/common-utils) repository.

It needs to be copied here as it cannot be included in `Makefile` directly from the
original repository.
83 changes: 83 additions & 0 deletions thirdparty/common.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright (c) 2024 AlertAvert.com. All rights reserved.
# Created by M. Massenzio, 2022-03-14

# ANSI color codes
GREEN=$(shell tput -Txterm setaf 2)
YELLOW=$(shell tput -Txterm setaf 3)
RED=$(shell tput -Txterm setaf 1)
BLUE=$(shell tput -Txterm setaf 6)
RESET=$(shell tput -Txterm sgr0)

# Go platform management
GOOS ?= $(shell uname -s | tr "[:upper:]" "[:lower:]")
GOMOD := $(shell go list -m)

UNAME_M := $(shell uname -m)
ifeq ($(UNAME_M),x86_64)
GOARCH = amd64
else ifeq ($(UNAME_M),aarch64)
GOARCH = arm64
else ifeq ($(UNAME_M),armv6l)
GOARCH = arm
else ifeq ($(UNAME_M),armv7l)
GOARCH = arm
else ifeq ($(UNAME_M),armv8l)
GOARCH = arm64
else
$(error Unsupported architecture $(UNAME_M))
endif

yq != which yq
ifeq ($(strip $(yq)),)
$(error yq not installed)
endif

settings != find . -name settings.yaml
ifeq ($(strip $(settings)),)
$(warning $(YELLOW)This makefile requires a settings.yaml file to define appname and version$(RESET))
else
appname != yq -r .name settings.yaml
version != yq -r .version settings.yaml
endif

# Versioning
# The `version` is a static value, set in settings.yaml, and ONLY used to tag the release,
# `release` includes the git SHA and will be used to tag the binary and container.
git_commit != git rev-parse --short HEAD
ifndef version
$(error $(RED)version must be defined, use yq and settings.yaml, or define it before including this file$(RESET))
endif
release := v$(version)-g$(git_commit)


# Certificates
certs_dir := ssl-config
ca-csr := $(certs_dir)/ca-csr.json
ca-config := $(certs_dir)/ca-config.json
server-csr := $(certs_dir)/localhost-csr.json

# Dockerfile
compose := docker/docker-compose.yaml
dockerfile := docker/Dockerfile

##@ Help
#
# The help target prints out all targets with their descriptions organized
# beneath their categories.
#
# The categories are represented by '##@' and the target descriptions by '##'.
#
# A category is defined if there's a line starting with ##@ <CATEGORY>,
# that gets pretty-printed as a category.
# A target is defined by a trailing comment starting with ##.
#
# More info on the usage of ANSI control characters for terminal formatting:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
#
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php
#
.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

0 comments on commit 3d45eb3

Please sign in to comment.