-
-
Notifications
You must be signed in to change notification settings - Fork 0
cli cold start #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cli cold start #186
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,9 @@ DB_SECRETS_DIR := $(ROOT_PATH)/database/infra/secrets | |
| GO_LOCAL_TOOLCHAIN ?= auto | ||
| GOIMPORTS_VERSION ?= v0.43.0 | ||
| GOBIN := $(shell go env GOPATH)/bin | ||
| CLI_DOCKER_BINARY_HOST := $(ROOT_PATH)/bin/metal-cli | ||
| CLI_DOCKER_BINARY_CONTAINER := /app/bin/metal-cli | ||
| CLI_DOCKER_BUILD_INPUTS := $(shell git ls-files '*.go' go.mod go.sum 2>/dev/null) | ||
|
|
||
| DB_SECRET_USERNAME ?= $(DB_SECRETS_DIR)/pg_username | ||
| DB_SECRET_PASSWORD ?= $(DB_SECRETS_DIR)/pg_password | ||
|
|
@@ -25,7 +28,7 @@ DB_SECRET_DBNAME ?= $(DB_SECRETS_DIR)/pg_dbname | |
| # PHONY Targets | ||
| # -------------------------------------------------------------------------------------------------------------------- # | ||
|
|
||
| .PHONY: fresh destroy audit watch format run-cli test-all run-cli-docker run-metal install-air install-goimports | ||
| .PHONY: fresh destroy audit watch format run-cli test-all run-cli-docker run-metal install-air install-goimports build-cli-docker | ||
|
|
||
| run-cli run-cli-docker: export DB_SECRET_USERNAME := $(value DB_SECRET_USERNAME) | ||
| run-cli run-cli-docker: export DB_SECRET_PASSWORD := $(value DB_SECRET_PASSWORD) | ||
|
|
@@ -96,6 +99,28 @@ install-goimports: | |
| # CLI Commands | ||
| # -------------------------------------------------------------------------------------------------------------------- # | ||
|
|
||
| build-cli-docker: $(CLI_DOCKER_BINARY_HOST) | ||
| @printf " $(CYAN)Docker CLI binary ready at %s.$(NC)\n" "$(CLI_DOCKER_BINARY_HOST)" | ||
|
|
||
| $(CLI_DOCKER_BINARY_HOST): $(CLI_DOCKER_BUILD_INPUTS) | ensure-base-images | ||
| @mkdir -p "$(dir $@)" | ||
| @status=0; \ | ||
| if command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; then \ | ||
| printf "Building Docker CLI binary at $(CLI_DOCKER_BINARY_CONTAINER).\n"; \ | ||
| docker compose run --rm --no-deps api-runner sh -lc '/usr/local/go/bin/go build -o "$(CLI_DOCKER_BINARY_CONTAINER)" ./metal/cli/main.go' || status=$$?; \ | ||
| elif command -v docker-compose >/dev/null 2>&1; then \ | ||
| printf "Building Docker CLI binary at $(CLI_DOCKER_BINARY_CONTAINER).\n"; \ | ||
| docker-compose run --rm --no-deps api-runner sh -lc '/usr/local/go/bin/go build -o "$(CLI_DOCKER_BINARY_CONTAINER)" ./metal/cli/main.go' || status=$$?; \ | ||
| else \ | ||
| printf "\n$(RED)❌ Neither 'docker compose' nor 'docker-compose' is available.$(NC)\n"; \ | ||
| printf " Install Docker Compose or run the CLI locally without containers.\n\n"; \ | ||
| exit 1; \ | ||
| fi; \ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| if [ $$status -ne 0 ]; then \ | ||
| printf "\n$(RED)❌ Failed to build the Docker CLI binary (status $$status).$(NC)\n"; \ | ||
| exit $$status; \ | ||
| fi | ||
|
|
||
| run-cli: | ||
| @missing_values=""; \ | ||
| missing_files=""; \ | ||
|
|
@@ -164,17 +189,53 @@ run-cli: | |
| esac`; \ | ||
| printf " DB_SECRET_DBNAME=%s\n\n" "$$DB_SECRET_DBNAME_DISPLAY" | ||
| @status=0; \ | ||
| compose_cmd=""; \ | ||
| if command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; then \ | ||
| compose_cmd="docker compose"; \ | ||
| printf "Using docker compose to run the CLI.\n"; \ | ||
| docker compose run --rm api-runner go run ./metal/cli/main.go || status=$$?; \ | ||
| elif command -v docker-compose >/dev/null 2>&1; then \ | ||
| compose_cmd="docker-compose"; \ | ||
| printf "Using docker-compose to run the CLI.\n"; \ | ||
| docker-compose run --rm api-runner go run ./metal/cli/main.go || status=$$?; \ | ||
| else \ | ||
| printf "\n$(RED)❌ Neither 'docker compose' nor 'docker-compose' is available.$(NC)\n"; \ | ||
| printf " Install Docker Compose or run the CLI locally without containers.\n\n"; \ | ||
| exit 1; \ | ||
| fi; \ | ||
| db_running() { docker inspect --format '{{.State.Running}}' $(DB_DOCKER_CONTAINER_NAME) 2>/dev/null || true; }; \ | ||
| db_health() { docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}unknown{{end}}' $(DB_DOCKER_CONTAINER_NAME) 2>/dev/null || true; }; \ | ||
| $(MAKE) --no-print-directory ensure-base-images || status=$$?; \ | ||
| if [ $$status -eq 0 ] && [ "$$(db_running)" = "true" ] && [ "$$(db_health)" = "healthy" ]; then \ | ||
| printf "Database container $(DB_DOCKER_CONTAINER_NAME) is already healthy.\n"; \ | ||
| elif [ $$status -eq 0 ]; then \ | ||
| printf "Database container $(DB_DOCKER_CONTAINER_NAME) is not ready. Starting $(DB_DOCKER_SERVICE_NAME)...\n"; \ | ||
| $(MAKE) --no-print-directory ensure-db-volume || status=$$?; \ | ||
| if [ $$status -eq 0 ]; then \ | ||
| $$compose_cmd up -d $(DB_DOCKER_SERVICE_NAME) || status=$$?; \ | ||
| fi; \ | ||
| if [ $$status -eq 0 ]; then \ | ||
| printf "Waiting for database to become healthy...\n"; \ | ||
| attempt=0; max_attempts=30; \ | ||
| while [ $$attempt -lt $$max_attempts ]; do \ | ||
| if [ "$$(db_running)" = "true" ] && [ "$$(db_health)" = "healthy" ]; then \ | ||
| printf "Database is healthy.\n"; \ | ||
| break; \ | ||
| fi; \ | ||
| attempt=$$((attempt + 1)); \ | ||
| if [ $$attempt -eq $$max_attempts ]; then \ | ||
| printf "\n$(RED)❌ Database failed to become healthy after 60 seconds.$(NC)\n"; \ | ||
| status=1; \ | ||
| break; \ | ||
| fi; \ | ||
| sleep 2; \ | ||
| done; \ | ||
| fi; \ | ||
| fi; \ | ||
| if [ $$status -eq 0 ]; then \ | ||
| $(MAKE) --no-print-directory build-cli-docker || status=$$?; \ | ||
| fi; \ | ||
| if [ $$status -eq 0 ]; then \ | ||
| $$compose_cmd run --rm --no-deps api-runner $(CLI_DOCKER_BINARY_CONTAINER) || status=$$?; \ | ||
| fi; \ | ||
| if [ $$status -ne 0 ]; then \ | ||
| printf "\n$(RED)❌ CLI exited with status $$status.$(NC)\n"; \ | ||
| exit $$status; \ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoding the
PATHvariable can be brittle. If the Go installation path in the base image changes, this will break. Since other parts of the Makefile are being updated to use absolute paths to thegoexecutable (e.g.,/usr/local/go/bin/go), this explicitPATHdefinition seems redundant. It would be more robust to remove thePATHoverride and rely on the base image'sPATH.