rewrite mailserver systemd setup to a Makefile

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2019-11-28 23:52:38 +01:00 committed by Jakub
parent f4d4c66d5b
commit e139af9a16
9 changed files with 138 additions and 124 deletions

View File

@ -23,4 +23,4 @@ make run-mailserver-systemd
```
This will generate the necessary config, define and then start the service.
For more details read the [README](_assets/systemd/README.md).
For more details read the [README](_assets/systemd/mailserver/README.md).

View File

@ -298,11 +298,14 @@ update-fleet-config: ##@other Update fleets configuration from fleets.status.im
@go generate ./static
@echo "Done"
run-bootnode-systemd: ##@Easy way to run a bootnode locally with Docker Compose
@cd _assets/systemd/bootnode/ && $(MAKE)
run-bootnode-docker: ##@Easy way to run a bootnode locally with Docker Compose
cd _assets/compose/bootnode/ && $(MAKE)
@cd _assets/compose/bootnode/ && $(MAKE)
run-mailserver-systemd: ##@Easy Run a mailserver locally with systemd
@_assets/systemd/start.sh
@cd _assets/systemd/mailserver/ && $(MAKE)
run-mailserver-docker: ##@Easy Run a mailserver locally with Docker Compose
@cd _assets/compose/mailserver/ && $(MAKE)

View File

@ -1,15 +0,0 @@
#!/usr/bin/env bash
# Settings & defaults
export SERVICE_NAME="${SERVICE_NAME:-statusd}"
# stop before removing
systemctl --user stop "${SERVICE_NAME}"
systemctl --user disable "${SERVICE_NAME}"
# remove the service definition file
rm -f "${HOME}/.config/systemd/user/${SERVICE_NAME}.service"
# make systemd forget about it
systemctl --user daemon-reload
systemctl --user reset-failed

View File

@ -1,25 +0,0 @@
#!/usr/bin/env bash
GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
mkdir -p "${HOME}/.config/systemd/user"
cat >"${HOME}/.config/systemd/user/${SERVICE_NAME}.service" << EOF
[Unit]
Description=Status.im Mailserver Service
[Service]
Type=notify
Restart=on-failure
WatchdogSec=60s
WorkingDirectory=${DATA_PATH}
ExecStart=${GIT_ROOT}/build/bin/statusd \\
-log "${LOG_LEVEL}" \\
-log-without-color \\
-dir "${DATA_PATH}" \\
-c "./config.json" \\
-metrics
[Install]
WantedBy=default.target
EOF

View File

@ -0,0 +1,101 @@
export GIT_ROOT = $(shell git rev-parse --show-toplevel)
RED := $(shell tput -Txterm setaf 1)
GRN := $(shell tput -Txterm setaf 2)
YLW := $(shell tput -Txterm setaf 3)
RST := $(shell tput -Txterm sgr0)
BLD := $(shell tput bold)
# Settings
export SERVICE_NAME ?= statusd
export SERVICE_DIR ?= $(HOME)/.config/systemd/user/
export SERVICE_PATH ?= $(SERVICE_DIR)/$(SERVICE_NAME).service
export LOG_LEVEL ?= INFO
export LISTEN_PORT ?= 30303
export DATA_PATH ?= /var/tmp/status-go-mail
# Necessary to make mailserver available publicly
export PUBLIC_IP ?= $(shell curl -s https://ipecho.net/plain)
# Info
STATUS = $(shell systemctl --user is-active $(SERVICE_NAME))
define INFO_MSG
* $(GRN)Your mailserver is listening on:$(RST) $(BLD)$(PUBLIC_IP):$(LISTEN_PORT)$(RST)
* $(YLW)Make sure that IP and TCP port are available from the internet!$(RST)
$(GRN)Your enode address is:$(RST)
$(shell $(GIT_ROOT)/_assets/scripts/get_enode.sh 2>/dev/null)
endef
export INFO_MSG
all: checks build config service enable restart info
clean: stop disable rm-service forget
checks:
# this setup wont work without an os with systemd
ifeq (, $(shell which systemctl))
$(error $(RED)Your system does not have systemd$(RST))
endif
# if the service is already up just show some info
ifeq (active, $(STATUS))
$(info $(INFO_MSG))
$(error $(YLW)Service already started$(RST))
endif
info:
@echo "$$INFO_MSG"
enode:
@$(GIT_ROOT)/_assets/scripts/get_enode.sh
status:
systemctl --user status --no-pager $(SERVICE_NAME)
logs:
journalctl --user-unit statusd
enable:
@echo "* $(BLD)Enabling '$(SERVICE_NAME)' service...$(RST)"
systemctl --user enable $(SERVICE_NAME)
disable:
@echo "* $(BLD)Disabling '$(SERVICE_NAME)' service...$(RST)"
systemctl --user disable "${SERVICE_NAME}"
start:
@echo "* $(BLD)Starting '$(SERVICE_NAME)' service...$(RST)"
systemctl --user start $(SERVICE_NAME)
restart:
@echo "* $(BLD)Restarting '$(SERVICE_NAME)' service...$(RST)"
systemctl --user restart $(SERVICE_NAME)
stop:
@echo "* $(BLD)Stopping '$(SERVICE_NAME)' service...$(RST)"
systemctl --user stop "${SERVICE_NAME}"
forget:
@echo "* $(BLD)Stopping '$(SERVICE_NAME)' service...$(RST)"
systemctl --user daemon-reload
systemctl --user reset-failed
$(GIT_ROOT)/build/bin/statusd:
@echo "* $(BLD)Building mailserver binary...$(RST)"
@cd "$(GIT_ROOT)" && make statusgo
build: $(GIT_ROOT)/build/bin/statusd
.PHONY: build
$(SERVICE_DIR):
@mkdir -p $(SERVICE_DIR)
service: $(SERVICE_DIR)
@echo "* $(BLD)Generating '$(SERVICE_NAME)' service...$(RST)"
@envsubst < ./service.template > $(SERVICE_PATH)
rm-service:
rm -f $(SERVICE_PATH)
config:
@echo "* $(BLD)Generating '$(SERVICE_NAME)' config...$(RST)"
@$(GIT_ROOT)/_assets/scripts/gen_config.sh

View File

@ -12,20 +12,21 @@ The steps it takes are:
# Usage
To simply configure and start the service run `./start.sh`.
To simply configure and start the service run `make`.
In order to manage the new `statusd` service you use `systemctl` command:
In order to manage the new `statusd` service you use other `Makefile` targets:
* `systemctl --user start statusd` - Start the service
* `systemctl --user stop statusd` - Stop the service
* `systemctl --user status statusd` - Check service status
* `systemctl --user disable statusd` - Disable the service
* `journalctl --user-unit statusd` - Read the service logs
* `make info` - Info about service
* `make enode` - Get enode address
* `make start` - Start the service
* `make stop` - Stop the service
* `make status` - Check service status
* `make enable` - Enable the service
* `make disable` - Disable the service
* `make logs` - Read the service logs
* `make clean` - Stop service and remove it
If you want to remove the service you can use the `clean.sh` script:
```bash
_assets/systemd/clean.sh
```
All the above commands are just wrappers around the [`systemctl`](http://man7.org/linux/man-pages/man1/systemctl.1.html) and [`journalctl`](http://man7.org/linux/man-pages/man1/journalctl.1.html) commands.
# Settings

View File

@ -0,0 +1,17 @@
[Unit]
Description=Status.im Mailserver Service
[Service]
Type=notify
Restart=on-failure
WatchdogSec=60s
WorkingDirectory=${DATA_PATH}
ExecStart=${GIT_ROOT}/build/bin/statusd \
-log-without-color \
-log "${LOG_LEVEL}" \
-dir "${DATA_PATH}" \
-c "./config.json" \
-metrics
[Install]
WantedBy=default.target

View File

@ -1,68 +0,0 @@
#!/usr/bin/env bash
RED=$(tput -Txterm setaf 1)
GRN=$(tput -Txterm setaf 2)
YLW=$(tput -Txterm setaf 3)
RST=$(tput -Txterm sgr0)
BLD=$(tput bold)
GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
# Settings & defaults
export SERVICE_NAME="${SERVICE_NAME:-statusd}"
export LOG_LEVEL="${LOG_LEVEL:-INFO}"
export LISTEN_PORT="${LISTEN_PORT:-30303}"
export DATA_PATH="${DATA_PATH:-/var/tmp/status-go-mail}"
# Necessary to make mailserver available publicly
export PUBLIC_IP=$(curl -s https://ipecho.net/plain)
function show_info() {
systemctl --user status --no-pager ${SERVICE_NAME}
echo
# just nice to show at the end
ENODE=$("${GIT_ROOT}/_assets/scripts/get_enode.sh")
echo "* ${GRN}Your mailserver is listening on:${RST} ${BLD}${PUBLIC_IP}:${LISTEN_PORT}${RST}"
echo "* ${YLW}Make sure that IP and TCP port are available from the internet!${RST}"
echo -e "${GRN}Your enode address is:${RST}\n${ENODE}"
exit 0
}
if ! [[ -x "$(command -v systemctl)" ]]; then
echo "${RED}Your system does not have systemd!${RST}"
exit 1
fi
# if the service is already up just show some info
if systemctl --user is-active --quiet ${SERVICE_NAME}; then
echo "${YLW}Service already started!${RST}"
show_info
fi
# if the service has failed just show the status
if systemctl --user is-failed --quiet ${SERVICE_NAME}; then
echo "${RED}Service has failed!${RST}"
systemctl --user status --no-pager ${SERVICE_NAME}
exit 1
fi
# Build the statusd binary
# TODO possibly download it in the future
if [[ ! -x "${GIT_ROOT}/build/bin/statusd" ]]; then
echo "* ${BLD}Building mailserver binary...${RST}"
cd "${GIT_ROOT}" && make statusgo
fi
echo "* ${BLD}Generating '${SERVICE_NAME}' config...${RST}"
"${GIT_ROOT}/_assets/scripts/gen_config.sh"
echo "* ${BLD}Generating '${SERVICE_NAME}' service...${RST}"
"${GIT_ROOT}/_assets/systemd/gen_service.sh"
echo "* ${BLD}Enabling '${SERVICE_NAME}' service...${RST}"
systemctl --user enable ${SERVICE_NAME}
echo "* ${BLD}Starting '${SERVICE_NAME}' service...${RST}"
systemctl --user restart ${SERVICE_NAME}
show_info

View File

@ -48,12 +48,12 @@ EOL
"result": [
{
"address": "enode://c42f368a23fa98ee546fd247220759062323249ef657d26d357a777443aec04db1b29a3a22ef3e7c548e18493ddaf51a31b0aed6079bd6ebe5ae838fcfaf3a49@206.189.243.162:443",
"rtt_ms": 31,
"rttMs": 31,
"error": null
},
{
"address": "enode://c42f368a23fa98ee546fd247220759062323249ef657d26d357a777443aec04db1b29a3a22ef3e7c548e18493ddaf51a31b0aed6079bd6ebe5ae838fcfaf3a49@206.189.243.162:999",
"rtt_ms": null,
"rttMs": null,
"error": "tcp check timeout: I/O timeout"
}
]