SUDO ?= sudo SERVICE ?= skd SERVICE_USER ?= skd SERVICE_GROUP ?= $(SERVICE_USER) INSTALL_DIR ?= /opt/sk ENV_DIR ?= /etc/$(SERVICE) ENV_FILE ?= $(ENV_DIR)/env SYSTEMD_PATH ?= /etc/systemd/system/$(SERVICE).service BRANCH ?= main HOST ?= 127.0.0.1 PORT ?= 80 HEALTH_URL ?= http://$(HOST):$(PORT)/health TOKEN ?= $(shell awk -F= '/^SKD_AUTH_TOKEN=/{print $$2}' $(ENV_FILE) 2>/dev/null) KEEP_INSTALL_DIR ?= 1 .PHONY: install up down restart uninstall healthcheck update token install: $(SUDO) env SERVICE_NAME=$(SERVICE) SERVICE_USER=$(SERVICE_USER) SERVICE_GROUP=$(SERVICE_GROUP) INSTALL_DIR=$(INSTALL_DIR) ./scripts/install.sh up: $(SUDO) systemctl start $(SERVICE).service down: $(SUDO) systemctl stop $(SERVICE).service restart: $(SUDO) systemctl restart $(SERVICE).service uninstall: -$(SUDO) systemctl stop $(SERVICE).service -$(SUDO) systemctl disable $(SERVICE).service $(SUDO) rm -f $(SYSTEMD_PATH) $(SUDO) systemctl daemon-reload @if [ "$(KEEP_INSTALL_DIR)" != "1" ]; then \ echo "Removing $(INSTALL_DIR)..."; \ $(SUDO) rm -rf "$(INSTALL_DIR)"; \ else \ echo "Keeping install dir $(INSTALL_DIR); set KEEP_INSTALL_DIR=0 to remove."; \ fi healthcheck: @if [ -z "$(TOKEN)" ]; then \ echo "No token set; set TOKEN=... or populate $(ENV_FILE) with SKD_AUTH_TOKEN."; \ exit 1; \ fi curl -fsS -H "Authorization: Bearer $(TOKEN)" "$(HEALTH_URL)" || (echo "Health check failed" && exit 1) update: $(SUDO) env PROJECT_ROOT=$(INSTALL_DIR) SERVICE_NAME=$(SERVICE) SERVICE_USER=$(SERVICE_USER) BRANCH=$(BRANCH) bash -c 'cd $(INSTALL_DIR) && ./scripts/update.sh' token: @if [ -z "$(TOKEN)" ]; then \ TOKEN=$$(head -c 32 /dev/urandom | base64 | tr -d '\n'); \ else \ TOKEN="$(TOKEN)"; \ fi; \ $(SUDO) mkdir -p "$(ENV_DIR)"; \ tmp="$(ENV_FILE).tmp"; \ $(SUDO) sh -c 'grep -v "^SKD_AUTH_TOKEN=" "$(ENV_FILE)" 2>/dev/null > "$$tmp" || true'; \ $(SUDO) sh -c 'echo "SKD_AUTH_TOKEN='"'"'$$TOKEN'"'"'" >> "$$tmp"'; \ $(SUDO) mv "$$tmp" "$(ENV_FILE)"; \ $(SUDO) chown root:$(SERVICE_GROUP) "$(ENV_FILE)"; \ $(SUDO) chmod 640 "$(ENV_FILE)"; \ echo "Token written to $(ENV_FILE)"