diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 1577a99..7341419 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -11,4 +11,4 @@ jobs: - name: Run ShellCheck uses: ludeeus/action-shellcheck@master env: - SHELLCHECK_OPTS: -x --source-path=src -e SC2001 -e SC2002 -e SC2223 -e SC2034 -e SC2064 -e SC2317 -e SC2028 -e SC2153 -e SC2004 + SHELLCHECK_OPTS: -x --source-path=src -e SC2001 -e SC2002 -e SC2223 -e SC2034 -e SC2064 -e SC2317 -e SC2153 -e SC2028 diff --git a/agent/agent.sh b/agent/agent.sh deleted file mode 100644 index 71c9c56..0000000 --- a/agent/agent.sh +++ /dev/null @@ -1,140 +0,0 @@ -#!/usr/bin/env bash -set -u - -VERSION="9" -HEADER="VirtualDSM Agent" - -# Functions - -error () { echo -e "\E[1;31m❯ ERROR: $1\E[0m" ; } -info () { echo -e "\E[1;34m❯\E[1;36m $1\E[0m" ; } - -finish() { - - echo "❯ $HEADER: Shutting down.." - exit - -} - -function checkNMI { - - local nmi - nmi=$(cat /proc/interrupts | grep NMI | sed 's/[^1-9]*//g') - - if [ "$nmi" != "" ]; then - - info "Received shutdown request through NMI.." - - /usr/syno/sbin/synoshutdown -s > /dev/null - finish - - fi -} - -function downloadUpdate { - - TMP="/tmp/agent.sh" - rm -f "${TMP}" - - # Auto update the agent - - URL="https://raw.githubusercontent.com/vdsm/virtual-dsm/master/agent/agent.sh" - - remote_size=$(curl -sIk -m 4 "${URL}" | grep -i "content-length:" | tr -d " \t" | cut -d ':' -f 2) - remote_size=${remote_size//$'\r'} - - [[ "$remote_size" == "" || "$remote_size" == "0" ]] && return - - remote_size=$(($remote_size+0)) - ((remote_size<100)) && return - - SCRIPT=$(readlink -f "${BASH_SOURCE[0]}") - local_size=$(stat -c%s "$SCRIPT") - local_size=$(($local_size+0)) - - [[ remote_size -eq local_size ]] && return - - if ! curl -sfk -m 10 -o "${TMP}" "${URL}"; then - error "$HEADER: curl error ($?)" && return - fi - - if [ ! -f "${TMP}" ]; then - error "$HEADER: update error, file not found.." && return - fi - - line=$(head -1 "${TMP}") - - if [[ "$line" != "#!/usr/bin/env bash" ]]; then - error "$HEADER: update error, invalid header: $line" && return - fi - - if cmp --silent -- "${TMP}" "${SCRIPT}"; then - error "$HEADER: update file is already equal? (${local_size} / ${remote_size})" && return - fi - - mv -f "${TMP}" "${SCRIPT}" - chmod 755 "${SCRIPT}" - - info "$HEADER: succesfully installed update..." - -} - -function installPackages { - - for filename in /usr/local/packages/*.spk; do - if [ -f "$filename" ]; then - - BASE=$(basename "$filename" .spk) - BASE="${BASE%%-*}" - - [[ $BASE == "ActiveInsight" ]] && continue - - info "Installing package ${BASE}.." - - /usr/syno/bin/synopkg install "$filename" > /dev/null - /usr/syno/bin/synopkg start "$BASE" > /dev/null & - - rm "$filename" - - fi - done - -} - -trap finish SIGINT SIGTERM - -ts=$(date +%s%N) - -echo "" -echo "❯ Started $HEADER v$VERSION..." - -checkNMI - -# Install packages - -first_run=false - -for filename in /usr/local/packages/*.spk; do - if [ -f "$filename" ]; then - first_run=true - fi -done - -if [ "$first_run" = true ]; then - - installPackages - -else - - downloadUpdate - -fi - -# Wait for NMI interrupt as a shutdown signal - -while true; do - - checkNMI - sleep 2 & wait $! - -done diff --git a/agent/service.sh b/agent/service.sh deleted file mode 100644 index 2f1c720..0000000 --- a/agent/service.sh +++ /dev/null @@ -1,87 +0,0 @@ -#!/bin/bash - -PIDFILE="/var/run/agent.pid" -SCRIPT="/usr/local/bin/agent.sh" - -error () { echo -e "\E[1;31m❯ ERROR: $1\E[0m" ; } -info () { echo -e "\E[1;34m❯\E[1;36m $1\E[0m" ; } - -status() { - - if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")"; then - echo 'Service running' - return 1 - fi - - return 0 -} - -start() { - - if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")"; then - echo 'Service already running' - return 1 - fi - - echo 'Starting agent service...' - chmod 666 /dev/ttyS0 - - if [ ! -f "$SCRIPT" ]; then - - URL="https://raw.githubusercontent.com/vdsm/virtual-dsm/master/agent/agent.sh" - - if ! curl -sfk -m 10 -o "${SCRIPT}" "${URL}"; then - error 'Failed to download agent script.' > /dev/ttyS0 - rm -f "${SCRIPT}" - return 1 - else - info 'Agent script was missing?' > /dev/ttyS0 - fi - - chmod 755 "${SCRIPT}" - - fi - - echo "-" > /var/lock/subsys/agent.sh - "$SCRIPT" &> /dev/ttyS0 & echo $! > "$PIDFILE" - - return 0 -} - -stop() { - - if [ ! -f "$PIDFILE" ] || ! kill -0 "$(cat "$PIDFILE")"; then - echo 'Service not running' - return 1 - fi - - echo 'Stopping agent service...' - - chmod 666 /dev/ttyS0 - info 'Stopping agent service...' > /dev/ttyS0 - - kill -15 "$(cat "$PIDFILE")" && rm -f "$PIDFILE" - rm -f /var/lock/subsys/agent.sh - - echo 'Service stopped' - return 0 -} - -case "$1" in - start) - start - ;; - stop) - stop - ;; - status) - status - ;; - restart) - stop - start - ;; - *) - echo "Usage: $0 {start|stop|restart}" - exit 1 -esac