Skip to content

Commit

Permalink
allow running systemd service as root
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Sokołowski <jakub@status.im>
  • Loading branch information
jakubgs committed Dec 22, 2019
1 parent ccf3cd5 commit e24a7ce
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 23 deletions.
32 changes: 21 additions & 11 deletions _assets/systemd/bootnode/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@ YLW := $(shell tput -Txterm setaf 3)
RST := $(shell tput -Txterm sgr0)
BLD := $(shell tput bold)

# by default we run the service as user
SCTL_OPTS := --user
JRNL_OPTS := --user-unit
SERVICE_DIR := $(HOME)/.config/systemd/user/
# with sudo it will run as a system service
ifeq ($(USER), root)
SCTL_OPTS :=
JRNL_OPTS := --unit
SERVICE_DIR := /etc/systemd/system/
endif

# Settings
export SERVICE_NAME ?= bootnode
export SERVICE_DIR ?= $(HOME)/.config/systemd/user/
export SERVICE_PATH ?= $(SERVICE_DIR)/$(SERVICE_NAME).service
export LOG_LEVEL ?= 3
export LISTEN_PORT ?= 30303
Expand All @@ -19,7 +29,7 @@ export ADDR_PATH ?= $(DATA_PATH)/nodeaddr
export PUBLIC_IP ?= $(shell curl -s https://ipecho.net/plain)

# Info
STATUS = $(shell systemctl --user is-active $(SERVICE_NAME))
STATUS = $(shell systemctl $(SCTL_OPTS) is-active $(SERVICE_NAME))
NODE_ADDR = $(shell cat $(ADDR_PATH))
ENODE = enode://$(NODE_ADDR)@$(PUBLIC_IP):$(LISTEN_PORT)

Expand Down Expand Up @@ -62,35 +72,35 @@ save-address:
@$(GIT_ROOT)/build/bin/bootnode -nodekey=$(KEY_PATH) -writeaddress > $(ADDR_PATH)

status:
systemctl --user status --no-pager $(SERVICE_NAME)
systemctl $(SCTL_OPTS) status --no-pager $(SERVICE_NAME)

logs:
journalctl --user-unit statusd
journalctl $(JRNL_OPTS) statusd

enable:
@echo "* $(BLD)Enabling '$(SERVICE_NAME)' service...$(RST)"
systemctl --user enable $(SERVICE_NAME)
systemctl $(SCTL_OPTS) enable $(SERVICE_NAME)

disable:
@echo "* $(BLD)Disabling '$(SERVICE_NAME)' service...$(RST)"
systemctl --user disable "${SERVICE_NAME}"
systemctl $(SCTL_OPTS) disable "${SERVICE_NAME}"

start:
@echo "* $(BLD)Starting '$(SERVICE_NAME)' service...$(RST)"
systemctl --user start $(SERVICE_NAME)
systemctl $(SCTL_OPTS) start $(SERVICE_NAME)

restart:
@echo "* $(BLD)Restarting '$(SERVICE_NAME)' service...$(RST)"
systemctl --user restart $(SERVICE_NAME)
systemctl $(SCTL_OPTS) restart $(SERVICE_NAME)

stop:
@echo "* $(BLD)Stopping '$(SERVICE_NAME)' service...$(RST)"
systemctl --user stop "${SERVICE_NAME}"
systemctl $(SCTL_OPTS) stop "${SERVICE_NAME}"

forget:
@echo "* $(BLD)Stopping '$(SERVICE_NAME)' service...$(RST)"
systemctl --user daemon-reload
systemctl --user reset-failed
systemctl $(SCTL_OPTS) daemon-reload
systemctl $(SCTL_OPTS) reset-failed

$(GIT_ROOT)/build/bin/bootnode:
@echo "* $(BLD)Building bootnode binary...$(RST)"
Expand Down
6 changes: 6 additions & 0 deletions _assets/systemd/bootnode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ All settings are passed through environment variables:
* `KEY_PATH` - Location of Bootnode private key file. (Default: `/var/tmp/status-go-boot/nodekey`)
* `LOG_LEVEL` - Set level of log messages to show. (Values:`0-9`, Default: `3`)`

# System Service

By default this `Makefile` configures the Bootnode as a [systemd user service](https://www.freedesktop.org/software/systemd/man/user@.service.html). This is done to simplify the proces and remove the need for `sudo`. The disadvantage of this solution is that the service is stopped when the user logs out.

In order to make your service a system service use `sudo make`.

# Known Issues

* `No journal files were opened due to insufficient permissions.` from `systemctl`
Expand Down
31 changes: 21 additions & 10 deletions _assets/systemd/mailserver/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ YLW := $(shell tput -Txterm setaf 3)
RST := $(shell tput -Txterm sgr0)
BLD := $(shell tput bold)

# by default we run the service as user
SCTL_OPTS := --user
JRNL_OPTS := --user-unit
SERVICE_DIR := $(HOME)/.config/systemd/user/
# with sudo it will run as a system service
ifeq ($(USER), root)
SCTL_OPTS :=
JRNL_OPTS := --unit
SERVICE_DIR := /etc/systemd/system/
endif

# Settings
export SERVICE_NAME ?= statusd
export SERVICE_DIR ?= $(HOME)/.config/systemd/user/
Expand All @@ -17,7 +28,7 @@ export DATA_PATH ?= /var/tmp/status-go-mail
export PUBLIC_IP ?= $(shell curl -s https://ipecho.net/plain)

# Info
STATUS = $(shell systemctl --user is-active $(SERVICE_NAME))
STATUS = $(shell systemctl $(SCTL_OPTS) is-active $(SERVICE_NAME))

define INFO_MSG
* $(GRN)Your mailserver is listening on:$(RST) $(BLD)$(PUBLIC_IP):$(LISTEN_PORT)$(RST)
Expand Down Expand Up @@ -52,35 +63,35 @@ enode:
@$(GIT_ROOT)/_assets/scripts/get_enode.sh

status:
systemctl --user status --no-pager $(SERVICE_NAME)
systemctl $(SCTL_OPTS) status --no-pager $(SERVICE_NAME)

logs:
journalctl --user-unit statusd
journalctl $(JRNL_OPTS) statusd

enable:
@echo "* $(BLD)Enabling '$(SERVICE_NAME)' service...$(RST)"
systemctl --user enable $(SERVICE_NAME)
systemctl $(SCTL_OPTS) enable $(SERVICE_NAME)

disable:
@echo "* $(BLD)Disabling '$(SERVICE_NAME)' service...$(RST)"
systemctl --user disable "${SERVICE_NAME}"
systemctl $(SCTL_OPTS) disable "${SERVICE_NAME}"

start:
@echo "* $(BLD)Starting '$(SERVICE_NAME)' service...$(RST)"
systemctl --user start $(SERVICE_NAME)
systemctl $(SCTL_OPTS) start $(SERVICE_NAME)

restart:
@echo "* $(BLD)Restarting '$(SERVICE_NAME)' service...$(RST)"
systemctl --user restart $(SERVICE_NAME)
systemctl $(SCTL_OPTS) restart $(SERVICE_NAME)

stop:
@echo "* $(BLD)Stopping '$(SERVICE_NAME)' service...$(RST)"
systemctl --user stop "${SERVICE_NAME}"
systemctl $(SCTL_OPTS) stop "${SERVICE_NAME}"

forget:
@echo "* $(BLD)Stopping '$(SERVICE_NAME)' service...$(RST)"
systemctl --user daemon-reload
systemctl --user reset-failed
systemctl $(SCTL_OPTS) daemon-reload
systemctl $(SCTL_OPTS) reset-failed

$(GIT_ROOT)/build/bin/statusd:
@echo "* $(BLD)Building mailserver binary...$(RST)"
Expand Down
10 changes: 8 additions & 2 deletions _assets/systemd/mailserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@ All settings are passed through environment variables:
* `RPC_PORT` - Control port making it possible to use the [JSON-RPC API](https://github.com/ethereum/wiki/wiki/JSON-RPC).
* `API_MODULES` - API modules to be made available via the `RPC_PORT`.
* `DATA_PATH` - Location of Mailserver storage and keys. (Default: `/var/tmp/status-go-mail`)
* `REGISTER_TOPIC` - Mynamic mailserver discovery topic. (Default: `whispermail`)
* `MAIL_PASSWORD` - Basic HTTP auth password for mailserver. (Default: `status-offline-inbox`)
* `REGISTER_TOPIC` - Mynamic Mailserver discovery topic. (Default: `whispermail`)
* `MAIL_PASSWORD` - Basic HTTP auth password for Mailserver. (Default: `status-offline-inbox`)
* `LOG_LEVEL` - Set level of log messages to show. (`ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`)

The generated configuration file end up under `${DATA_PATH}/config.json`.

# System Service

By default this `Makefile` configures the Mailserver as a [systemd user service](https://www.freedesktop.org/software/systemd/man/user@.service.html). This is done to simplify the proces and remove the need for `sudo`. The disadvantage of this solution is that the service is stopped when the user logs out.

In order to make your service a system service use `sudo make`.

# Known Issues

* `No journal files were opened due to insufficient permissions.` from `systemctl`
Expand Down

0 comments on commit e24a7ce

Please sign in to comment.