summaryrefslogtreecommitdiff
path: root/Makefile
blob: 1e9cdbdb7c4d5f0b903fcd34bcc9f141223cad58 (plain) (blame)
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
# 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)