From b6c8e590de6337cfa4536986ed95b0991d3bd917 Mon Sep 17 00:00:00 2001 From: Jansen Price Date: Fri, 11 Sep 2020 02:14:22 -0500 Subject: [PATCH] Add Makefile to install as systemctl service --- Makefile | 75 ++++++++++++++++++++++++++++++++++++++++++++ composer.json | 3 ++ config/localhost.ini | 2 +- orbit.service | 14 +++++++++ 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 Makefile create mode 100644 orbit.service diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dda3339 --- /dev/null +++ b/Makefile @@ -0,0 +1,75 @@ +.SILENT: help + +ccyellow = $(shell echo "\033[33m") +ccend = $(shell tput op) +notice = $(ccyellow) $(0) $(ccend) + +_path := $(shell pwd) +_user := gemini-data + +ORBIT_HOSTNAME ?= localhost +SERVICE_PATH = /etc/systemd/system/orbit.service +EXEC_PATH = /usr/local/bin/orbit +CONF_PATH = /etc/orbit +LOG_PATH = /var/log/orbit + +help: + echo "To run tests, run make test, to install on system run make install" + +test: + cd tests; ../vendor/bin/phpunit . + +install_bin: + @echo + @echo "$(ccyellow)> Installing orbit bin$(ccend)" + rm $(EXEC_PATH) + ln -s $(_path)/bin/orbit $(EXEC_PATH) + +install_conf: + @echo + @echo "$(ccyellow)> Installing orbit config; Hostname: $(ORBIT_HOSTNAME)$(ccend)" + install -d $(CONF_PATH) + install config/localhost.ini $(CONF_PATH)/config.ini + sed -i -e 's#Example##' $(CONF_PATH)/config.ini + sed -i -e 's#localhost#$(ORBIT_HOSTNAME)#' $(CONF_PATH)/config.ini + sed -i -e 's#certs#$(CONF_PATH)/certs#' $(CONF_PATH)/config.ini + sed -i -e 's#log/#$(LOG_PATH)/#' $(CONF_PATH)/config.ini + install -o $(_user) -d $(LOG_PATH) + +install_cert: +ifeq (,$(wildcard $(CONF_PATH)/certs/$(ORBIT_HOSTNAME).cert.pem)) + @echo + @echo "$(ccyellow)> Generating certs for host $(ORBIT_HOSTNAME)$(ccend)" + bin/makecert $(ORBIT_HOSTNAME) + install -d $(CONF_PATH)/certs + install certs/$(ORBIT_HOSTNAME).cert.pem $(CONF_PATH)/certs/$(ORBIT_HOSTNAME).cert.pem + install certs/$(ORBIT_HOSTNAME).key.pem $(CONF_PATH)/certs/$(ORBIT_HOSTNAME).key.pem +endif + +install_user: +ifeq ($(shell id -u $(_user) 2>/dev/null),) + @echo + @echo "$(ccyellow)> Creating user $(_user)$(ccend)" + useradd -m -d /var/$(_user) -s /usr/sbin/nologin -c "Orbit gemini service account" -U $(_user) +endif + +install_service: + @echo + @echo "$(ccyellow)> Creating orbit.service file at $(SERVICE_PATH)$(ccend)" + install orbit.service $(SERVICE_PATH) + sed -i -e 's/{USER}/$(_user)/' $(SERVICE_PATH) + +install: install_bin install_conf install_cert install_user install_service + @echo + @echo "--------------------------------" + @echo "done" + @echo "> The service runs with the user $(_user), which I created for you" + @echo "> The actual orbit script is at $(EXEC_PATH)" + @echo " - But that is just a symlink to $(_path)/bin/orbit" + @echo "> I created a config file at $(CONF_PATH)/config.ini" + @echo " - Edit settings in that file to change configuration settings" + @echo "> I created a self-signed cert at $(CONF_PATH)/certs" + @echo "> The server log will be written at $(LOG_PATH)/orbit.log" + @echo "Run 'sudo systemctl start orbit' to start the service" + @echo "Run 'sudo systemctl restart orbit' to restart the service" + @echo "Run 'sudo systemctl enable orbit' to configure orbit to auto-start when machine starts" diff --git a/composer.json b/composer.json index e14d374..94a1b48 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,9 @@ "require-dev": { "phpunit/phpunit": "^9.3" }, + "bin": [ + "bin/orbit" + ], "autoload": { "psr-4" : { "Orbit\\" : "src/Orbit" diff --git a/config/localhost.ini b/config/localhost.ini index 91077e0..bcf5492 100644 --- a/config/localhost.ini +++ b/config/localhost.ini @@ -34,7 +34,7 @@ log_file = "log/orbit.log" log_level = "info" ; Root dir : Root directory of the server files -root_dir = "." +root_dir = "/var/gemini" ; Index file : Name of the index file (when a directory is accessed) index_file = "index.gmi" diff --git a/orbit.service b/orbit.service new file mode 100644 index 0000000..ff3a252 --- /dev/null +++ b/orbit.service @@ -0,0 +1,14 @@ +[Unit] +Description=Orbit gemini server +After=network.target +StartLimitIntervalSec=0 + +[Service] +Type=simple +Restart=always +RestartSec=1 +User={USER} +ExecStart=/usr/local/bin/orbit --config /etc/orbit/config.ini -q + +[Install] +WantedBy=multi-user.target