From a8cc6fc7d590ea5d458a37a70e3b76fb8caa7cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=C5=A0ari=C4=87?= Date: Sat, 2 Dec 2023 11:44:23 +0100 Subject: [PATCH] first working draft --- Makefile | 17 +++-------------- check_vars.sh | 8 ++++---- create_service_templates.sh | 17 +++++++++++++++++ create_symlinks.sh | 3 ++- motion@.service | 16 ++++++++++++++++ onfig_path/example.env | 6 ++++++ onfig_path/motion.sh | 1 + onfig_path/motion@.service | 16 ++++++++++++++++ onfig_path/time.sh | 1 + onfig_path/time@.service | 16 ++++++++++++++++ services/goto-preset@.service | 16 ++++++++++++++++ services/goto.sh | 1 + services/motion.sh | 2 +- services/motion@.service | 8 ++++---- services/set-preset.sh | 1 + services/set-preset@.service | 16 ++++++++++++++++ services/time.sh | 2 +- services/time@.service | 8 ++++---- time@.service | 16 ++++++++++++++++ 19 files changed, 142 insertions(+), 29 deletions(-) create mode 100644 create_service_templates.sh create mode 100644 motion@.service create mode 100644 onfig_path/example.env create mode 100644 onfig_path/motion.sh create mode 100644 onfig_path/motion@.service create mode 100644 onfig_path/time.sh create mode 100644 onfig_path/time@.service create mode 100644 services/goto-preset@.service create mode 100644 services/goto.sh create mode 100644 services/set-preset.sh create mode 100644 services/set-preset@.service create mode 100644 time@.service diff --git a/Makefile b/Makefile index bce287f..ec8b7e1 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,10 @@ install: ## check if vars are set - source ./check_vars.sh GOPATH - ## check for sudo privileges - source ./check_sudo.sh + bash ./check_vars.sh GOPATH ## run go install go install ./... ## create system-wide symlink to executables - source ./create_symlinks.sh motion-poll set-preset goto-preset set-time + bash ./create_symlinks.sh motion-poll set-preset goto-preset set-time ## create config folder - echo "Path for configuration folder [~/.config/onvif-cam-poll]" - read path - if [ -z "$path" ] - then path="~/.config/onvif-cam-poll" - mkdir -p $path - ## create service templates - cp ./services/* $path/. - ## overwrite service files - environment file, user, group, execstart - ## copy service files to system service folder - ## run systemctl daemon-reload + bash ./create_service_templates.sh diff --git a/check_vars.sh b/check_vars.sh index 9150570..f576996 100644 --- a/check_vars.sh +++ b/check_vars.sh @@ -1,11 +1,11 @@ -function check_var() { +#!/bin/bash +function check_var { declare -n var_ref=$1 - declare -p "$var_ref" &>/dev/null declared=$? - if ["$declared" != 0 ] + if [ "$declared" != 0 ] then echo "$var_ref must be set! Exiting!" - exit + exit 1 fi } diff --git a/create_service_templates.sh b/create_service_templates.sh new file mode 100644 index 0000000..3f3ecc5 --- /dev/null +++ b/create_service_templates.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +read -r -p "Path for configuration folder [~/.config/onvif-cam-poll]" $CONFIG_PATH +export CONFIG_PATH=${config_path:-~/.config/onvif-cam-poll} +echo "$CONFIG_PATH" +mkdir --p "$CONFIG_PATH" +export CONFIG_PATH +## create service templates +cp ./services/* "$CONFIG_PATH"/. +envsubst < "$CONFIG_PATH"/motion@.service > "$CONFIG_PATH"/motion@.service.temp && mv "$CONFIG_PATH"/motion@.service.temp "$CONFIG_PATH"/motion@.service +envsubst < "$CONFIG_PATH"/time@.service > "$CONFIG_PATH"/time@.service.temp && mv "$CONFIG_PATH"/time@.service.temp "$CONFIG_PATH"/time@.service +sudo cp "$CONFIG_PATH"/*.service /lib/systemd/system/. +sudo systemctl daemon-reload +echo "To track a new camera create an .env file for it in $CONFIG_PATH" +echo "Fill in all the variables that are listed in the example.env file" +echo "Afterwards, run systemctl start motion@camera_envfile_name" +echo "Don't forget to enable the service if you want it to run after a restart" \ No newline at end of file diff --git a/create_symlinks.sh b/create_symlinks.sh index 6247c75..aabd312 100644 --- a/create_symlinks.sh +++ b/create_symlinks.sh @@ -1,5 +1,6 @@ function create_symlink() { - ln -s "$GOPATH/$1" "/usr/bin/$1" + sudo rm "/usr/bin/$1" + sudo ln -s "$GOPATH/$1" "/usr/bin/$1" } for var in "$@" diff --git a/motion@.service b/motion@.service new file mode 100644 index 0000000..e0a9950 --- /dev/null +++ b/motion@.service @@ -0,0 +1,16 @@ +[Unit] +DefaultDependencies=no +After=network.target + +[Service] +Type=simple +EnvironmentFile=/home/isaric/.config/onvif-cam-poll/%i.env +Restart=always +RestartSec=3 +User=isaric +Group=isaric +ExecStart=/bin/bash /home/isaric/.config/onvif-cam-poll/motion.sh +TimeoutStartSec=0 + +[Install] +WantedBy=default.target diff --git a/onfig_path/example.env b/onfig_path/example.env new file mode 100644 index 0000000..cdcf406 --- /dev/null +++ b/onfig_path/example.env @@ -0,0 +1,6 @@ +CAMERA_ADDRESS=bla.bla.bla:8899 +CAMERA_USER=admin +CAMERA_PASSWORD=xxxxxx +CAMERA_NAME=garage +SLACK_BOT_TOKEN=xoxb-xxxxxx-xxxxxx-xxxxxx +SLACK_CHANNEL_ID=xxxxxx diff --git a/onfig_path/motion.sh b/onfig_path/motion.sh new file mode 100644 index 0000000..0620f7e --- /dev/null +++ b/onfig_path/motion.sh @@ -0,0 +1 @@ +motion-poll -a ${CAMERA_ADDRESS} -u ${CAMERA_USER} -p ${CAMERA_PASSWORD} -n ${CAMERA_NAME} -s ${SLACK_WEBHOOK} -t 30 -b ${SLACK_BOT_TOKEN} -c ${SLACK_CHANNEL_ID} diff --git a/onfig_path/motion@.service b/onfig_path/motion@.service new file mode 100644 index 0000000..daccf77 --- /dev/null +++ b/onfig_path/motion@.service @@ -0,0 +1,16 @@ +[Unit] +DefaultDependencies=no +After=network.target + +[Service] +Type=simple +EnvironmentFile=$path/%i.env +Restart=always +RestartSec=3 +User=$SUDO_USER +Group=$SUDO_USER +ExecStart=/bin/bash $path/motion.sh +TimeoutStartSec=0 + +[Install] +WantedBy=default.target diff --git a/onfig_path/time.sh b/onfig_path/time.sh new file mode 100644 index 0000000..b664ebd --- /dev/null +++ b/onfig_path/time.sh @@ -0,0 +1 @@ +set-time -a ${CAMERA_ADDRESS} -u admin -p ${CAMERA_PASSWORD} \ No newline at end of file diff --git a/onfig_path/time@.service b/onfig_path/time@.service new file mode 100644 index 0000000..40cdaf5 --- /dev/null +++ b/onfig_path/time@.service @@ -0,0 +1,16 @@ +[Unit] +DefaultDependencies=no +After=network.target + +[Service] +Type=simple +EnvironmentFile=$path/%i.env +Restart=always +RestartSec=3 +User=$SUDO_USER +Group=$SUDO_USER +ExecStart=/bin/bash $path/time.sh +TimeoutStartSec=0 + +[Install] +WantedBy=default.target diff --git a/services/goto-preset@.service b/services/goto-preset@.service new file mode 100644 index 0000000..e132d3d --- /dev/null +++ b/services/goto-preset@.service @@ -0,0 +1,16 @@ +[Unit] +DefaultDependencies=no +After=network.target + +[Service] +Type=simple +EnvironmentFile=${CONFIG_PATH}/%i.env +Restart=always +RestartSec=3 +User=${USER} +Group=${USER} +ExecStart=/bin/bash ${CONFIG_PATH}/goto.sh +TimeoutStartSec=0 + +[Install] +WantedBy=default.target diff --git a/services/goto.sh b/services/goto.sh new file mode 100644 index 0000000..99251b8 --- /dev/null +++ b/services/goto.sh @@ -0,0 +1 @@ +goto-preset -a ${CAMERA_ADDRESS} -u ${CAMERA_USER} -p ${CAMERA_PASSWORD} -t 30 -l 001 \ No newline at end of file diff --git a/services/motion.sh b/services/motion.sh index 10a5432..0620f7e 100644 --- a/services/motion.sh +++ b/services/motion.sh @@ -1 +1 @@ -/path/to/motion-poll -a ${CAMERA_ADDRESS} -u ${CAMERA_USER} -p ${CAMERA_PASSWORD} -n ${CAMERA_NAME} -s ${SLACK_WEBHOOK} -t 30 -b ${SLACK_BOT_TOKEN} -c ${SLACK_CHANNEL_ID} +motion-poll -a ${CAMERA_ADDRESS} -u ${CAMERA_USER} -p ${CAMERA_PASSWORD} -n ${CAMERA_NAME} -s ${SLACK_WEBHOOK} -t 30 -b ${SLACK_BOT_TOKEN} -c ${SLACK_CHANNEL_ID} diff --git a/services/motion@.service b/services/motion@.service index 8a70cec..27ee987 100644 --- a/services/motion@.service +++ b/services/motion@.service @@ -4,12 +4,12 @@ After=network.target [Service] Type=simple -EnvironmentFile=/home/your-user/motion-poll-services/%i.env +EnvironmentFile=${CONFIG_PATH}/%i.env Restart=always RestartSec=3 -User=isaric -Group=isaric -ExecStart=/bin/bash /home/your-user/motion-poll-services/motion.sh +User=${USER} +Group=${USER} +ExecStart=/bin/bash ${CONFIG_PATH}/motion.sh TimeoutStartSec=0 [Install] diff --git a/services/set-preset.sh b/services/set-preset.sh new file mode 100644 index 0000000..55c1211 --- /dev/null +++ b/services/set-preset.sh @@ -0,0 +1 @@ +set-preset -a ${CAMERA_ADDRESS} -u ${CAMERA_USER} -p ${CAMERA_PASSWORD} -l 001 diff --git a/services/set-preset@.service b/services/set-preset@.service new file mode 100644 index 0000000..f3ac08b --- /dev/null +++ b/services/set-preset@.service @@ -0,0 +1,16 @@ +[Unit] +DefaultDependencies=no +After=network.target + +[Service] +Type=simple +EnvironmentFile=${CONFIG_PATH}/%i.env +Restart=always +RestartSec=3 +User=${USER} +Group=${USER} +ExecStart=/bin/bash ${CONFIG_PATH}/set-preset.sh +TimeoutStartSec=0 + +[Install] +WantedBy=default.target diff --git a/services/time.sh b/services/time.sh index d785970..b664ebd 100644 --- a/services/time.sh +++ b/services/time.sh @@ -1 +1 @@ -/path/to/set-time -a ${CAMERA_ADDRESS} -u admin -p ${CAMERA_PASSWORD} \ No newline at end of file +set-time -a ${CAMERA_ADDRESS} -u admin -p ${CAMERA_PASSWORD} \ No newline at end of file diff --git a/services/time@.service b/services/time@.service index 2193401..5ab5f0d 100644 --- a/services/time@.service +++ b/services/time@.service @@ -4,12 +4,12 @@ After=network.target [Service] Type=simple -EnvironmentFile=/home/your-user/motion-poll-services/%i.env +EnvironmentFile=${CONFIG_PATH}/%i.env Restart=always RestartSec=3 -User=isaric -Group=isaric -ExecStart=/bin/bash /home/your-user/motion-poll-services/time.sh +User=${USER} +Group={$USER} +ExecStart=/bin/bash ${CONFIG_PATH}/time.sh TimeoutStartSec=0 [Install] diff --git a/time@.service b/time@.service new file mode 100644 index 0000000..604aac6 --- /dev/null +++ b/time@.service @@ -0,0 +1,16 @@ +[Unit] +DefaultDependencies=no +After=network.target + +[Service] +Type=simple +EnvironmentFile=/home/isaric/.config/onvif-cam-poll/%i.env +Restart=always +RestartSec=3 +User=isaric +Group=isaric +ExecStart=/bin/bash /home/isaric/.config/onvif-cam-poll/time.sh +TimeoutStartSec=0 + +[Install] +WantedBy=default.target