From ee709d0ff43547b1f2d97a901fbef4d835ee2c65 Mon Sep 17 00:00:00 2001 From: Richard Bremner Date: Mon, 1 Jul 2024 08:13:57 +1000 Subject: [PATCH] feat(install): auto updating binary (#213) feat(install): auto updating binary (#4) Note: install notes guide the user through these steps, this code doesn't run in a deployment --- ops/lilypad-updater.sh | 41 +++++++++++++++++++++++++++++ ops/systemd/lilpad-updater.timer | 9 +++++++ ops/systemd/lilypad-updater.service | 8 ++++++ 3 files changed, 58 insertions(+) create mode 100755 ops/lilypad-updater.sh create mode 100644 ops/systemd/lilpad-updater.timer create mode 100644 ops/systemd/lilypad-updater.service diff --git a/ops/lilypad-updater.sh b/ops/lilypad-updater.sh new file mode 100755 index 00000000..ef5faaaf --- /dev/null +++ b/ops/lilypad-updater.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +# Detect your machine's architecture and set it as $OSARCH +OSARCH=$(uname -m | awk '{if ($0 ~ /arm64|aarch64/) print "arm64"; else if ($0 ~ /x86_64|amd64/) print "amd64"; else print "unsupported_arch"}') +export OSARCH + +# Detect your operating system and set it as $OSNAME +OSNAME=$(uname -s | awk '{if ($1 == "Darwin") print "darwin"; else if ($1 == "Linux") print "linux"; else print "unsupported_os"}') +export OSNAME + +# Get the latest version URL +LATEST_URL=$(curl -s https://api.github.com/repos/lilypad-tech/lilypad/releases/latest | grep "browser_download_url.*lilypad-$OSNAME-$OSARCH" | cut -d : -f 2,3 | tr -d \") + +# Extract the latest version (including short SHA) from the URL +LATEST_VERSION=$(echo $LATEST_URL | sed -n 's#.*/download/\([^/]*\)/.*#\1#p') + +# Get the current version +CURRENT_VERSION=$(/usr/local/bin/lilypad version | grep "Lilypad:" | awk '{print $2}') + +echo "Current version: $CURRENT_VERSION" +echo "Latest version: $LATEST_VERSION" + +if [ "$CURRENT_VERSION" != "$LATEST_VERSION" ]; then + echo "Updating lilypad binary from version $CURRENT_VERSION to $LATEST_VERSION" + sudo systemctl stop lilypad-resource-provider + echo "Stopped the service: sudo systemctl stop lilypad-resource-provider" + + curl -L -o lilypad "$LATEST_URL" + echo "Downloaded the latest version from $LATEST_URL" + + chmod +x lilypad + sudo mv lilypad /usr/local/bin/lilypad + echo "Made the new binary executable and moved it to /usr/local/bin/lilypad" + + sudo systemctl start lilypad-resource-provider + echo "Restarted the service: sudo systemctl start lilypad-resource-provider" + + echo "Update complete." +else + echo "Lilypad binary is already up to date." +fi \ No newline at end of file diff --git a/ops/systemd/lilpad-updater.timer b/ops/systemd/lilpad-updater.timer new file mode 100644 index 00000000..dce25e71 --- /dev/null +++ b/ops/systemd/lilpad-updater.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run lilypad-updater every hour + +[Timer] +OnBootSec=10min +OnUnitActiveSec=1h + +[Install] +WantedBy=timers.target \ No newline at end of file diff --git a/ops/systemd/lilypad-updater.service b/ops/systemd/lilypad-updater.service new file mode 100644 index 00000000..73a04552 --- /dev/null +++ b/ops/systemd/lilypad-updater.service @@ -0,0 +1,8 @@ +[Unit] +Description=Check and update Lilypad binary +After=network-online.target +Wants=network-online.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/lilypad-updater.sh \ No newline at end of file