-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
140 lines (121 loc) · 4.38 KB
/
Copy pathMakefile
File metadata and controls
140 lines (121 loc) · 4.38 KB
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
.DEFAULT_GOAL := help
.PHONY: help acceptance test lint format check run install-service start stop restart
PYTEST ?= uv run --project . --group dev python -m pytest
RUFF ?= uv run --project . --group dev python -m ruff
UVICORN ?= uv run --project . python -m uvicorn
TEST ?= tests
SG ?= prod
RUN_HOST ?= 0.0.0.0
RUN_PORT ?= 8016
SG_SERVICE ?= sg
SG_SERVICE_UNIT ?= /etc/systemd/system/$(SG_SERVICE).service
SG_SUDOERS_FILE ?= /etc/sudoers.d/$(SG_SERVICE)-service
SG_SUDOERS_USER ?= $(shell id -un)
SYSTEMCTL_BIN ?= /usr/bin/systemctl
SYSTEMCTL := sudo -n $(SYSTEMCTL_BIN)
ifeq ($(SG),dev)
RUN_ENV = SHARED_GOALS_ENABLE_SEED_ENDPOINT=1 SHARED_GOALS_MVP_SEED_FILE=$(abspath $(SEED_FILE))
else
RUN_ENV =
endif
SEED_FILE ?= seed.json
define require_prod_service_mode
@if [ "$(SG)" != "prod" ]; then \
echo "This target is prod-only. Use SG=prod."; \
exit 1; \
fi
endef
define require_systemctl
@if ! command -v systemctl >/dev/null 2>&1; then \
echo "systemctl is required for service management targets."; \
exit 1; \
fi
endef
define require_sudo
@if ! command -v sudo >/dev/null 2>&1; then \
echo "sudo is required for service management targets."; \
exit 1; \
fi
endef
help:
@printf "Shared Goals instance targets:\n"
@printf " make acceptance Run HTTP acceptance tests\n"
@printf " make test Run all tests, or TEST=path for one slice\n"
@printf " make lint Run Ruff lint checks\n"
@printf " make format Format Python code with Ruff\n"
@printf " make check Run the commit-gate validation\n"
@printf " make run Start the API server (SG selects the default port; dev run enables seed endpoint)\n"
@printf " make install-service Install and enable system service + passwordless sudo ops (prod only)\n"
@printf " make start Start system service with passwordless sudo (prod only)\n"
@printf " make stop Stop system service with passwordless sudo (prod only)\n"
@printf " make restart Restart system service with passwordless sudo (prod only)\n"
acceptance:
@printf "Running HTTP acceptance tests...\n"
$(PYTEST) -q tests/acceptance
test:
@printf "Running tests: $(TEST)\n"
$(PYTEST) -q $(TEST)
lint:
@printf "Running Ruff lint checks...\n"
$(RUFF) check .
format:
@printf "Formatting Python code with Ruff...\n"
$(RUFF) check --fix .
$(RUFF) format .
check: lint
@printf "Running commit-gate checks...\n"
$(PYTEST) -q tests
run:
$(RUN_ENV) $(UVICORN) shared_goals_instance.app:create_app --factory --reload --host "$(RUN_HOST)" --port "$(RUN_PORT)"
install-service:
$(require_prod_service_mode)
$(require_systemctl)
$(require_sudo)
@set -e; \
unit_file="$$(mktemp)"; \
sudoers_file="$$(mktemp)"; \
trap 'rm -f "$$unit_file" "$$sudoers_file"' EXIT; \
printf '%s\n' \
'[Unit]' \
'Description=Shared Goals Instance API' \
'After=network-online.target' \
'Wants=network-online.target' \
'' \
'[Service]' \
'Type=simple' \
'WorkingDirectory=$(CURDIR)' \
'ExecStart=/usr/bin/env bash -lc '\''cd "$(CURDIR)" && uv run --project . python -m uvicorn shared_goals_instance.app:create_app --factory --host 0.0.0.0 --port $(RUN_PORT)'\''' \
'Restart=always' \
'RestartSec=5' \
'Environment=SG=prod' \
'' \
'[Install]' \
'WantedBy=multi-user.target' \
> "$$unit_file"; \
printf '%s\n' \
'$(SG_SUDOERS_USER) ALL=(root) NOPASSWD: /usr/bin/test -f $(SG_SERVICE_UNIT), $(SYSTEMCTL_BIN) daemon-reload, $(SYSTEMCTL_BIN) enable $(SG_SERVICE), $(SYSTEMCTL_BIN) start $(SG_SERVICE), $(SYSTEMCTL_BIN) stop $(SG_SERVICE), $(SYSTEMCTL_BIN) restart $(SG_SERVICE)' \
> "$$sudoers_file"; \
sudo -n install -D -m 0644 "$$unit_file" "$(SG_SERVICE_UNIT)"; \
sudo -n install -D -m 0440 "$$sudoers_file" "$(SG_SUDOERS_FILE)"; \
sudo -n visudo -cf "$(SG_SUDOERS_FILE)" >/dev/null; \
$(SYSTEMCTL) daemon-reload
$(SYSTEMCTL) enable "$(SG_SERVICE)"
start:
$(require_prod_service_mode)
$(require_systemctl)
$(require_sudo)
@if ! sudo -n test -f "$(SG_SERVICE_UNIT)"; then \
echo "Service $(SG_SERVICE).service not found. Installing..."; \
$(MAKE) install-service SG=prod SG_SERVICE="$(SG_SERVICE)" RUN_PORT="$(RUN_PORT)"; \
fi
$(SYSTEMCTL) start "$(SG_SERVICE)"
stop:
$(require_prod_service_mode)
$(require_systemctl)
$(require_sudo)
$(SYSTEMCTL) stop "$(SG_SERVICE)"
restart:
$(require_prod_service_mode)
$(require_systemctl)
$(require_sudo)
$(SYSTEMCTL) restart "$(SG_SERVICE)"