Skip to content

Commit a33ba24

Browse files
authored
improvements for Makefile
* makefile:proposal for Makefile Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:update the PLANTUML variable usage and fix wrong name at .PHONY rule Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:Use '$(DOCKER)' to abstract the container engineer that is used Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:call 'make vendor' to retrieve all the depencies at 'vendor' directory Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * doc:update to match the makefile changes Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:install swag using a temporay empty GOPATH Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:read database config from configs/config.yaml Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:add rule to print the variables Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:update rule to install swag locally more isolated Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:fix help text for the 'install-swag' rule Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:fix dependency on install-swag Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:use the same workstation rules in the pipeline Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * Avoid vendor directory is copied when building the container image Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:move start / stop database container after the configuration Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:use the same makefile rules to the dockerfile Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:use mod vendor optionally and add get-deps rule Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:conditional assignment for DATABASE_* and read from configs/config.yaml only if yq tool is available Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:fix linter hints about spaces and end of files Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:fix tmp directory generation Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:fix arch alias that was not executing plantuml Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:add healthcheck to postgres container and wait loop to get it ready Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:delete extra message when running 'make db-up'; db-migrate-up is executed in the rule Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:remove .db-health-wait dependencies from the other rules and add correct dependency for 'db-migrate-seed' Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:add rule 'db-cli-connect' to open a postgres cli from the container Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com> * makefile:remove duplicated file Signed-off-by: Alejandro Visiedo <avisiedo@redhat.com>
1 parent a573307 commit a33ba24

20 files changed

+426
-70
lines changed

.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
release/**
2+
vendor

.github/workflows/content-sources-actions.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,10 @@ jobs:
8080
with:
8181
go-version: "1.16"
8282
- run: |
83-
make dbmigrate
84-
./release/dbmigrate up
85-
go test ./...
83+
make get-deps dbmigrate db-migrate-up test
8684
env:
8785
DATABASE_HOST: localhost
8886
DATABASE_PORT: 5432
8987
DATABASE_USER: postgres
9088
DATABASE_NAME: postgres
9189
DATABASE_PASSWORD: postgres
92-

Makefile

+23-36
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
1-
.PHONY: test
2-
-include .env
3-
4-
clean:
5-
go clean
6-
rm release/*
7-
8-
content-sources:
9-
go build -o release/content-sources cmd/content-sources/main.go
10-
11-
dbmigrate:
12-
go build -o release/dbmigrate cmd/dbmigrate/main.go
13-
14-
seed:
15-
go run cmd/dbmigrate/main.go seed
16-
17-
test:
18-
CONFIG_PATH="$(shell pwd)/configs/" go test ./...
19-
20-
test-ci:
21-
go test ./...
22-
23-
openapi:
24-
swag init --generalInfo api.go --o ./api --dir pkg/handler/ --pd pkg/api
25-
#convert from swagger to openapi
26-
go run ./cmd/swagger2openapi/main.go api/swagger.json api/openapi.json
27-
rm ./api/swagger.json ./api/swagger.yaml
28-
29-
arch: #yum install plantuml if not installed
30-
java -jar /usr/share/java/plantuml.jar docs/architecture.puml
31-
32-
build: content-sources dbmigrate
33-
34-
35-
image:
36-
podman build -f ./build/Dockerfile --tag content-sources:$(shell git rev-parse --short HEAD) ./
1+
##
2+
# Entrypoint for the Makefile
3+
#
4+
# It is composed at mk/includes.mk by including
5+
# small make files which provides all the necessary
6+
# rules.
7+
#
8+
# Some considerations:
9+
#
10+
# - Variables customization can be
11+
# stored at '.env', 'mk/private.mk' files.
12+
# - By default the 'help' rule is executed.
13+
# - No parallel jobs are executed from the main Makefile,
14+
# so that multiple rules from the command line will be
15+
# executed in serial.
16+
##
17+
18+
include mk/includes.mk
19+
20+
.NOT_PARALLEL:
21+
22+
# Set the default rule
23+
.DEFAULT_GOAL := help

README.md

+49-29
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,64 @@
11
# Content Sources
22

3-
##What is it?
3+
## What is it?
4+
45
Content Sources is an application for storing information about external content (currently YUM repositories) in a central location.
56

67

7-
##Developing
8+
## Developing
89

9-
### Configuring Postgresql
10+
### Create your configuration
1011

11-
As root run:
12-
```shell
13-
yum install -y postgresql
12+
Create a config file from the example:
1413

15-
echo "host all all 127.0.0.1/32 trust" >> /var/lib/pgsql/data/pg_hba.conf
16-
echo "host all all ::1/128 trust" >> /var/lib/pgsql/data/pg_hba.conf
17-
systemctl start postgresql
18-
systemctl enable postgresql
19-
sudo -u postgres createdb content
20-
sudo -u postgres psql -c "CREATE USER content WITH PASSWORD 'content'"
21-
sudo -u postgres psql -c "grant all privileges on database content TO content"
14+
```sh
15+
$ cp ./configs/config.yaml.example ./configs/config.yaml
2216
```
2317

24-
### Create your configuration
25-
Create a config file from the example:
26-
```
27-
cp ./configs/config.yaml.example ./configs/config.yaml
28-
```
18+
### Start / Stop postgres
19+
20+
- Start the database container by:
21+
22+
```sh
23+
$ make db-up
24+
```
25+
26+
---
27+
28+
- You can stop it by:
29+
30+
```sh
31+
$ make db-down
32+
```
33+
34+
- And clean the volume that it uses by (this stop
35+
the container before doing it if it were running):
36+
37+
```sh
38+
$ make db-clean
39+
```
40+
41+
- Or inspect inside the container if necessary meanwhile it is
42+
running by:
43+
44+
```sh
45+
$ podman exec -it postgresql bash
46+
```
2947

3048
### Migrate your database (and seed it if desired)
49+
50+
```sh
51+
$ make db-migrate-up
3152
```
32-
go run ./cmd/dbmigrate/main.go up
33-
```
34-
```
35-
go run ./cmd/dbmigrate/main.go seed
53+
54+
```sh
55+
$ make db-migrate-seed
3656
```
3757

3858
### Run the server!
3959

40-
```
41-
go run ./cmd/content-sources/main.go
60+
```sh
61+
$ make run
4262
```
4363

4464
###
@@ -47,12 +67,12 @@ Hit the api:
4767
```
4868
curl http://localhost:8000/api/content_sources/v1.0/repositories/ ```
4969
```
70+
5071
### Generating new openapi docs:
5172

52-
~~~
53-
go install github.com/swaggo/swag/cmd/swag@latest
54-
make openapi
55-
~~~
73+
```sh
74+
$ make openapi
75+
```
5676

5777
### Configuration
5878

@@ -63,6 +83,6 @@ The default configuration file in ./configs/config.yaml.example shows all availa
6383
* Pull requests should come with good tests
6484
* Generally, feature PRs should be backed by a JIRA ticket and included in the subject using the format:
6585
* `CONTENT-23: Some great feature`
66-
86+
6787
## More info
6888
* [Architecture](docs/architecture.md)

build/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ COPY . .
55

66
USER 0
77

8-
RUN go get -d ./... && make build
8+
RUN make get-deps build
99

1010
FROM registry.redhat.io/ubi8/ubi-minimal:8.5
1111

mk/alias.mk

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
##
2+
# This file contains different alias rules to keep
3+
# compatibility with the previous rules.
4+
##
5+
6+
.PHONY: arch
7+
arch: PLANTUML ?= $(shell command -v plantuml 2>/dev/null)
8+
arch: PLANTUML ?= false
9+
arch: docs/architecture.svg ## Alias for 'make plantuml-generate'
10+
11+
.PHONY: image
12+
image: DOCKER_DOCKERFILE := ./build/Dockerfile
13+
image: DOCKER_IMAGE := content-sources:$(DOCKER_IMAGE_TAG)
14+
image: ## Alias for 'make docker-build DOCKER_DOCKERFILE=build/Dockerfile DOCKER_IMAGE=content-sources:$(git rev-parse --short HEAD)
15+
$(MAKE) docker-build \
16+
DOCKER_DOCKERFILE=$(DOCKER_DOCKERFILE) \
17+
DOCKER_IMAGE=$(DOCKER_IMAGE)
18+
19+
.PHONY: seed
20+
seed: db-migrate-seed ## Alias for 'make db-migrate-seed'
21+
22+
.PHONY: dbmigrate
23+
dbmigrate: $(GO_OUTPUT)/dbmigrate ## Alias for 'make build' for dbmigrate
24+
25+
.PHONY: content-sources
26+
content-sources: $(GO_OUTPUT)/content-sources ## Alias for 'make build' for content-sources
27+
28+
.PHONY: swagger2openapi
29+
swagger2openapi: $(GO_OUTPUT)/swagger2openapi ## Alias for 'make build' for swagger2openapi

mk/db.mk

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
##
2+
# Set of rules to interact with a local database
3+
# from a container and database initialization.
4+
#
5+
# Requires 'mk/docker.mk'
6+
##
7+
8+
.PHONY: db-up
9+
db-up: DOCKER_IMAGE=docker.io/postgres:14
10+
db-up: $(GO_OUTPUT)/dbmigrate ## Start postgres database
11+
$(DOCKER) volume exists postgres || $(DOCKER) volume create postgres
12+
$(DOCKER) container exists postgres || $(DOCKER) run \
13+
-d \
14+
--rm \
15+
--name postgres \
16+
-p 5432:5432 \
17+
-e POSTGRES_PASSWORD=$(DATABASE_PASSWORD) \
18+
-e POSTGRES_USER=$(DATABASE_USER) \
19+
-e POSTGRES_DB=$(DATABASE_NAME) \
20+
-v postgres:/var/lib/postgresql/data \
21+
--health-cmd pg_isready \
22+
--health-interval 5s \
23+
--health-retries 10 \
24+
--health-timeout 3s \
25+
$(DOCKER_IMAGE)
26+
$(MAKE) .db-health-wait
27+
$(MAKE) db-migrate-up
28+
@echo "Run 'make db-migrate-seed' to seed the database"
29+
30+
.PHONY: .db-health
31+
.db-health:
32+
@echo -n "Checking database is ready: "
33+
@$(DOCKER) container exists postgres
34+
@$(DOCKER) exec postgres pg_isready
35+
36+
.PHONY: .db-health-wait
37+
.db-health-wait:
38+
@$(DOCKER) container exists postgres
39+
@while [ "$$($(DOCKER) inspect -f '{{.State.Healthcheck.Status}}' postgres)" != "healthy" ]; do echo -n "."; sleep 1; done
40+
41+
.PHONY: db-migrate-up
42+
db-migrate-up: $(GO_OUTPUT)/dbmigrate ## Run dbmigrate up
43+
$(GO_OUTPUT)/dbmigrate up
44+
45+
.PHONY: db-migrate-seed
46+
db-migrate-seed: $(GO_OUTPUT)/dbmigrate ## Run dbmigrate seed
47+
$(GO_OUTPUT)/dbmigrate seed
48+
49+
.PHONY: db-down
50+
db-down: ## Stop postgres database
51+
! $(DOCKER) container exists postgres || $(DOCKER) container stop postgres
52+
53+
.PHONY: db-clean
54+
db-clean: db-down ## Clean database volume
55+
! $(DOCKER) volume exists postgres || $(DOCKER) volume rm postgres
56+
57+
.PHONY: db-cli-connect
58+
db-cli-connect: ## Open a postgres cli in the container (it requires db-up)
59+
! $(DOCKER) container exists postgres || $(DOCKER) container exec -it postgres psql "sslmode=disable dbname=$(DATABASE_NAME) user=$(DATABASE_USER) host=$(DATABASE_HOST) port=$(DATABASE_PORT) password=$(DATABASE_PASSWORD)"

mk/docker.mk

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
##
2+
# General rules for interacting with container
3+
# manager (podman or docker).
4+
##
5+
6+
ifneq (,$(shell command podman -v 2>/dev/null))
7+
DOCKER ?= podman
8+
else
9+
ifneq (,$(shell command docker -v 2>/dev/null))
10+
DOCKER ?= docker
11+
else
12+
DOCKER ?= false
13+
endif
14+
endif
15+
16+
DOCKER_CONTEXT_DIR ?= .
17+
DOCKER_DOCKERFILE ?= Dockerfile
18+
DOCKER_IMAGE_BASE ?= quay.io/$(USER)/myapp
19+
DOCKER_IMAGE_TAG ?= $(shell git rev-parse --short HEAD)
20+
DOCKER_IMAGE ?= $(DOCKER_IMAGE_BASE):$(DOCKER_IMAGE_TAG)
21+
# DOCKER_OPTS
22+
# DOCKER_RUN_ARGS
23+
24+
.PHONY: docker-build
25+
docker-build: ## Build image DOCKER_IMAGE from DOCKER_DOCKERFILE using the DOCKER_CONTEXT_DIR
26+
$(DOCKER) build -t "$(DOCKER_IMAGE)" -f $(DOCKER_DOCKERFILE) $(DOCKER_CONTEXT_DIR)
27+
28+
.PHONY: docker-push
29+
docker-push: ## Push image to remote registry
30+
$(DOCKER) push "$(DOCKER_IMAGE)"
31+
32+
# TODO Indicate in the options the IP assigned to the postgres container
33+
# .PHONY: docker-run
34+
# docker-run: DOCKER_OPTS += --env-file .env
35+
# docker-run: ## Run with DOCKER_OPTS the DOCKER_IMAGE using DOCKER_RUN_ARGS as arguments (eg. make docker-run DOCKER_OPTS="-p 9000:9000")
36+
# $(DOCKER) run $(DOCKER_OPTS) $(DOCKER_IMAGE) $(DOCKER_RUN_ARGS)

mk/go-rules.mk

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
##
2+
# Golang rules to build the binaries, tidy dependencies,
3+
# generate vendor directory, download dependencies and clean
4+
# the generated binaries.
5+
##
6+
7+
# Directory where the built binaries will be generated
8+
GO_OUTPUT ?= $(PROJECT_DIR)/bin
9+
ifeq (,$(shell ls -1d vendor 2>/dev/null))
10+
MOD_VENDOR :=
11+
else
12+
MOD_VENDOR ?= -mod vendor
13+
endif
14+
15+
.PHONY: build
16+
build: $(patsubst cmd/%,$(GO_OUTPUT)/%,$(wildcard cmd/*)) ## Build binaries
17+
18+
# export CGO_ENABLED
19+
# $(GO_OUTPUT)/%: CGO_ENABLED=0
20+
$(GO_OUTPUT)/%: cmd/%/main.go
21+
@[ -e "$(GO_OUTPUT)" ] || mkdir -p "$(GO_OUTPUT)"
22+
go build $(MOD_VENDOR) -o "$@" "$<"
23+
24+
.PHONY: clean
25+
clean: ## Clean binaries and testbin generated
26+
@[ ! -e "$(GO_OUTPUT)" ] || for item in cmd/*; do rm -vf "$(GO_OUTPUT)/$${item##cmd/}"; done
27+
# @[ ! -e testbin ] || rm -rf testbin
28+
29+
.PHONY: run
30+
run: build ## Run the service locally
31+
"$(GO_OUTPUT)/content-sources"
32+
33+
.PHONY: tidy
34+
tidy:
35+
go mod tidy
36+
37+
.PHONY: get-deps
38+
get-deps: ## Download golang dependencies
39+
go get -d ./...
40+
41+
.PHONY: vendor
42+
vendor: ## Generate vendor/ directory populated with the dependencies
43+
go mod vendor
44+
45+
.PHONY: test
46+
test: ## Run tests
47+
CONFIG_PATH="$(PROJECT_DIR)/configs/" go test $(MOD_VENDOR) ./...
48+
49+
.PHONY: test-ci
50+
test-ci: ## Run tests for ci
51+
go test $(MOD_VENDOR) ./...

mk/help.mk

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
##
2+
# This file only contains the rule that generate the
3+
# help content from the comments in the different files.
4+
#
5+
# Use '##@ My group text' at the beginning of a line to
6+
# print out a group text.
7+
#
8+
# Use '## My help text' at the end of a rule to print out
9+
# content related with a rule. Try to short the description.
10+
##
11+
.PHONY: help
12+
help: ## Print out the help content
13+
@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 commit comments

Comments
 (0)