summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile68
1 files changed, 68 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..1e9cdbd
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,68 @@
+# penguins.lan — build and OpenRC install.
+#
+# make build the binary into build/
+# make install stage the deploy tree into /opt/penguins.lan
+# make openrc install the service (init.d + conf.d + user), then:
+# rc-update add penguins default && rc-service penguins start
+#
+# install/openrc need root. Override any var, e.g. `make install APPDIR=/srv/penguins`.
+
+APP := penguins
+BUILD_DIR := build
+
+APPDIR ?= /opt/penguins.lan
+INITDIR ?= /etc/init.d
+CONFDIR ?= /etc/conf.d
+SVC_USER ?= penguins
+SVC_GROUP ?= penguins
+DESTDIR ?=
+
+GO ?= go
+LDFLAGS ?= -s -w
+# go-sqlite3 is a cgo package, so cgo must stay on.
+CGO_ENABLED ?= 1
+
+.PHONY: all build install openrc uninstall clean
+
+all: build
+
+build:
+ @mkdir -p $(BUILD_DIR)
+ CGO_ENABLED=$(CGO_ENABLED) $(GO) build -trimpath -ldflags '$(LDFLAGS)' -o $(BUILD_DIR)/$(APP) .
+
+# Stage the binary and the assets it resolves relative to its working directory
+# (templates/, static/, db/) into the service's deploy tree. Leaves any existing
+# .env and db/ contents untouched.
+install: build
+ install -d $(DESTDIR)$(APPDIR) $(DESTDIR)$(APPDIR)/db
+ install -m 0755 $(BUILD_DIR)/$(APP) $(DESTDIR)$(APPDIR)/$(APP)
+ rm -rf $(DESTDIR)$(APPDIR)/templates $(DESTDIR)$(APPDIR)/static
+ cp -r templates static $(DESTDIR)$(APPDIR)/
+ @echo "staged in $(DESTDIR)$(APPDIR) — run 'make openrc' to set up the service"
+
+# Install the OpenRC service: create the system user/group, drop the init script
+# and (if absent) the conf.d defaults, and hand the deploy tree to the service
+# user. After this: rc-update add penguins default && rc-service penguins start.
+openrc:
+ @getent group $(SVC_GROUP) >/dev/null 2>&1 || addgroup -S $(SVC_GROUP) 2>/dev/null || groupadd -r $(SVC_GROUP)
+ @getent passwd $(SVC_USER) >/dev/null 2>&1 || \
+ adduser -S -D -H -s /sbin/nologin -G $(SVC_GROUP) $(SVC_USER) 2>/dev/null || \
+ useradd -r -g $(SVC_GROUP) -d $(APPDIR) -s /sbin/nologin $(SVC_USER)
+ install -Dm 0755 init.d/penguins $(DESTDIR)$(INITDIR)/penguins
+ @if [ -f $(DESTDIR)$(CONFDIR)/penguins ]; then \
+ echo "keeping existing $(DESTDIR)$(CONFDIR)/penguins"; \
+ else \
+ install -Dm 0644 conf.d/penguins $(DESTDIR)$(CONFDIR)/penguins; \
+ fi
+ @chown -R $(SVC_USER):$(SVC_GROUP) $(DESTDIR)$(APPDIR) 2>/dev/null || true
+ @echo "service installed. enable it with:"
+ @echo " rc-update add penguins default && rc-service penguins start"
+
+uninstall:
+ -rc-service penguins stop 2>/dev/null
+ -rc-update del penguins 2>/dev/null
+ rm -f $(DESTDIR)$(INITDIR)/penguins
+ @echo "removed the init script; left $(CONFDIR)/penguins and $(APPDIR) in place"
+
+clean:
+ rm -rf $(BUILD_DIR)