Skip to content

Commit

Permalink
add systemd deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-m committed Nov 25, 2023
1 parent e40e4a1 commit 1fd04d4
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 41 deletions.
33 changes: 23 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ GO_ARCHIVE_SHA256 = 73cac0215254d0c7d1241fa40837851f3b9a8a742d0b54714cbdfb3feaf8
# https://github.com/dominikh/go-tools/releases
GO_STATICCHECK_VERSION = 2023.1.6

TRAINBOT_DEPLOY_TARGET_SSH_HOST_ = ${TRAINBOT_DEPLOY_TARGET_SSH_HOST}

DEFAULT: format build_host build_arm64

format:
Expand Down Expand Up @@ -118,14 +116,29 @@ run_videofile:
--input="internal/pkg/stitch/testdata/set0/day.mp4" \
-X 0 -Y 0 -W 300 -H 300

deploy_trainbot: build_arm64
test -n "$(TRAINBOT_DEPLOY_TARGET_SSH_HOST_)" # missing env var, please set up env file from env.example and source it
scp env $(TRAINBOT_DEPLOY_TARGET_SSH_HOST_):
scp build/trainbot-arm64 $(TRAINBOT_DEPLOY_TARGET_SSH_HOST_):

deploy_confighelper: build_arm64
test -n "$(TRAINBOT_DEPLOY_TARGET_SSH_HOST_)" # missing env var, please set up env file from env.example and source it
scp build/confighelper-arm64 $(TRAINBOT_DEPLOY_TARGET_SSH_HOST_):
# Usage: make deploy_trainbot host=TRAINBOT_DEPLOY_TARGET_SSH_HOST
# Example: make deploy_trainbot [email protected]
deploy_trainbot: docker_build
test -n "$(host)" # missing target host, usage: make deploy_trainbot host=TRAINBOT_DEPLOY_TARGET_SSH_HOST !

ssh $(host) mkdir -p trainbot/
scp env $(host):trainbot/
ssh $(host) systemctl --user stop trainbot.service
scp build/trainbot-arm64 $(host):trainbot/

ssh $(host) mkdir -p .config/systemd/user/
scp trainbot.service $(host):.config/systemd/user/

ssh $(host) loginctl enable-linger
ssh $(host) systemctl --user enable trainbot.service
ssh $(host) systemctl --user start trainbot.service

# Usage: make deploy_confighelper host=TRAINBOT_DEPLOY_TARGET_SSH_HOST
# Example: make deploy_confighelper [email protected]
deploy_confighelper: docker_build
test -n "$(host)" # missing target host, usage: make deploy_confighelper host=TRAINBOT_DEPLOY_TARGET_SSH_HOST !
ssh $(host) mkdir -p trainbot/
scp build/confighelper-arm64 $(host):trainbot/

list:
@LC_ALL=C $(MAKE) -pRrq -f $(firstword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/(^|\n)# Files(\n|$$)/,/(^|\n)# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | grep -E -v -e '^[^[:alnum:]]' -e '^$@$$'
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,39 @@ There are multiple options:

### Raspberry Pi

Run the interactive tool to adjust camera and select a crop rectangle:

```bash
sudo usermod -a -G video pi
# On the host machine
make deploy_confighelper host=TRAINBOT_DEPLOY_TARGET_SSH_HOST
# Example:
make deploy_confighelper [email protected]

# confighelper
# On the raspberry pi
sudo usermod -a -G video pi
# The --input arg has to be adapted to your actual camera config.
./confighelper-arm64 --log-pretty --input=picam3 --listen-addr=0.0.0.0:8080
```

The current production deployment is in a Tmux session... to be improved one day, but it has worked for 6 months now.
Example "Production" deployment to a remote host (will install a systemd user unit):

First, you need to create a `env` file (copy `env.example`).
Then, from the host machine:

```bash
source ./env
make deploy_trainbot host=TRAINBOT_DEPLOY_TARGET_SSH_HOST

while true; do \
./trainbot-arm64; \
done
# To see logs, on the target device:
journalctl --user -eu trainbot.service
```

Download latest data from Raspberry Pi:

```bash
ssh "$TRAINBOT_DEPLOY_TARGET_SSH_HOST" sqlite3 data/db.sqlite3
.backup data/db.sqlite3.bak
ssh "$TRAINBOT_DEPLOY_TARGET_SSH_HOST" sqlite3 trainbot/data/db.sqlite3
.backup trainbot/data/db.sqlite3.bak
# Ctrl+D
rsync --verbose --archive --rsh=ssh "$TRAINBOT_DEPLOY_TARGET_SSH_HOST:data/" data/
rsync --verbose --archive --rsh=ssh "$TRAINBOT_DEPLOY_TARGET_SSH_HOST:trainbot/data/" data/
rm data/db.sqlite3-shm data/db.sqlite3-wal
mv data/db.sqlite3.bak data/db.sqlite3
```
Expand Down Expand Up @@ -195,6 +204,5 @@ Note that the mounting plate for the Raspberry Pi is 1-2mm too wide, because the
## TODOs

- [ ] Add machine learning to classify trains (MobileNet, EfficientNet, https://mediapipe-studio.webapps.google.com/demo/image_classifier)
- [ ] Better deployment setup (at least a systemd unit)
- [ ] Add run/deploy instructions to README (including confighelper)
- [ ] Maybe compress URL params - favorites list is getting longer and longer...
38 changes: 18 additions & 20 deletions env.example
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
export LOG_PRETTY=false
export LOG_LEVEL=info
LOG_PRETTY=false
LOG_LEVEL=info

export INPUT=picam3
export CAMERA_FORMAT_FOURCC=MJPG
export RECT_X=990
export RECT_Y=240
export RECT_W=240
export RECT_H=280
export PX_PER_M=40
export MIN_SPEED_KPH=25
export MAX_SPEED_KPH=130
INPUT=picam3
CAMERA_FORMAT_FOURCC=MJPG
RECT_X=990
RECT_Y=240
RECT_W=240
RECT_H=280
PX_PER_M=40
MIN_SPEED_KPH=25
MAX_SPEED_KPH=130

export ENABLE_UPLOAD=true
export UPLOAD_FTP_HOST="ftp.example.org"
export UPLOAD_FTP_PORT=21
export UPLOAD_FTP_USER="ftpuser"
export UPLOAD_FTP_PASSWORD="ftp-password"
export UPLOAD_FTP_PWD="wwwroot/trains/data"
ENABLE_UPLOAD=true
UPLOAD_FTP_HOST="ftp.example.org"
UPLOAD_FTP_PORT=21
UPLOAD_FTP_USER="ftpuser"
UPLOAD_FTP_PASSWORD="ftp-password"
UPLOAD_FTP_PWD="wwwroot/trains/data"

export ENABLE_TEMPERATURE_MEASUREMENT=true

export TRAINBOT_DEPLOY_TARGET_SSH_HOST="[email protected]"
ENABLE_TEMPERATURE_MEASUREMENT=true
22 changes: 22 additions & 0 deletions trainbot.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Assumes the binary to be available at ~/trainbot/trainbot-arm64,
# data folder at ~/trainbot/data.
#
# Correctness can be checked using: systemd-analyze verify .config/systemd/user/trainbot.service

[Unit]
Description=Trainbot backend
Documentation=https://github.com/jo-m/trainbot/

[Service]
Type=exec
WorkingDirectory=%h/trainbot
ExecStart=%h/trainbot/trainbot-arm64
EnvironmentFile=%h/trainbot/env

# Restart forever.
Restart=always
RestartSec=500ms
StartLimitIntervalSec=0

[Install]
WantedBy=default.target

0 comments on commit 1fd04d4

Please sign in to comment.