From b5d979dbf8da8ae9552a3882923a255518b94a7b Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sun, 18 Aug 2024 13:49:31 +0200 Subject: [PATCH 1/8] Created update script and systemd service --- cpolis.service | 9 +++++++++ update.sh | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 cpolis.service create mode 100755 update.sh diff --git a/cpolis.service b/cpolis.service new file mode 100644 index 0000000..0ccc66a --- /dev/null +++ b/cpolis.service @@ -0,0 +1,9 @@ +[Unit] +Description=cpolis + +[Service] +ExecStart=/usr/local/bin/cpolis +Restart=on-failure + +[Install] +WantedBy=default.target diff --git a/update.sh b/update.sh new file mode 100755 index 0000000..b81e314 --- /dev/null +++ b/update.sh @@ -0,0 +1,34 @@ +#! /bin/sh - + +CPOLIS_REPO_URL="https://git.streifling.com/api/v1/repos/jason/cpolis/releases" +CPOLIS_PATH=$HOME/cpolis +TAILWINDCSS_REPO_URL=https://api.github.com/repos/tailwindlabs/tailwindcss/releases/latest +SYSTEMD_USER_DIR=$HOME/.config/systemd/user + +! which curl >/dev/null && echo "curl needs to be installed" +! which go >/dev/null && echo "go needs to be installed" +! which jq >/dev/null && echo "jq needs to be installed" +! which tar >/dev/null && echo "tar needs to be installed" +! which xargs >/dev/null && echo "xargs needs to be installed" + +latest_release=$(curl -s $CPOLIS_REPO_URL | jq -r '.[0].tag_name') +curl -Lo cpolis.tar.gz https://git.streifling.com/jason/cpolis/archive/$latest_release.tar.gz +rm -fr $CPOLIS_PATH +tar -xzf cpolis.tar.gz -C $CPOLIS_PATH +rm cpolis.tar.gz + +curl -s $TAILWINDCSS_REPO_URL | + grep -F browser_download_url | + grep -F linux-x64 | + cut -d'"' -f4 | + xargs -r curl -Lo $CPOLIS_PATH/tailwindcss +chmod +x $CPOLIS_PATH/tailwindcss +$CPOLIS_PATH/tailwindcss -i web/static/css/input.css -o web/static/css/style.css + +go build -o $CPOLIS_PATH/cpolis $CPOLIS_PATH/cmd/main.go +chmox +x $CPOLIS_PATH/cpolis + +[ ! -f $SYSTEMD_USER_DIR ] && mkdir -p $SYSTEMD_USER_DIR +mv $CPOLIS_PATH/cpolis.service $SYSTEMD_USER_DIR +systemctl --user daemon-reload +systemctl --user is-active --quiet cpolis.service && systemctl --user restart cpolis.service From b1a6359473a2c4ca585310c66685e03061106402 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sun, 18 Aug 2024 13:53:37 +0200 Subject: [PATCH 2/8] Exit update script if any requirement is not met --- update.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/update.sh b/update.sh index b81e314..07a1c34 100755 --- a/update.sh +++ b/update.sh @@ -5,11 +5,11 @@ CPOLIS_PATH=$HOME/cpolis TAILWINDCSS_REPO_URL=https://api.github.com/repos/tailwindlabs/tailwindcss/releases/latest SYSTEMD_USER_DIR=$HOME/.config/systemd/user -! which curl >/dev/null && echo "curl needs to be installed" -! which go >/dev/null && echo "go needs to be installed" -! which jq >/dev/null && echo "jq needs to be installed" -! which tar >/dev/null && echo "tar needs to be installed" -! which xargs >/dev/null && echo "xargs needs to be installed" +! which curl >/dev/null && echo "curl needs to be installed" && exit 1 +! which go >/dev/null && echo "go needs to be installed" && exit 1 +! which jq >/dev/null && echo "jq needs to be installed" && exit 1 +! which tar >/dev/null && echo "tar needs to be installed" && exit 1 +! which xargs >/dev/null && echo "xargs needs to be installed" && exit 1 latest_release=$(curl -s $CPOLIS_REPO_URL | jq -r '.[0].tag_name') curl -Lo cpolis.tar.gz https://git.streifling.com/jason/cpolis/archive/$latest_release.tar.gz From 129c85929c41d9e50825c28e1da6ced53fd22c38 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sun, 18 Aug 2024 13:56:41 +0200 Subject: [PATCH 3/8] Small cleanup --- update.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/update.sh b/update.sh index 07a1c34..f328e76 100755 --- a/update.sh +++ b/update.sh @@ -5,11 +5,11 @@ CPOLIS_PATH=$HOME/cpolis TAILWINDCSS_REPO_URL=https://api.github.com/repos/tailwindlabs/tailwindcss/releases/latest SYSTEMD_USER_DIR=$HOME/.config/systemd/user -! which curl >/dev/null && echo "curl needs to be installed" && exit 1 -! which go >/dev/null && echo "go needs to be installed" && exit 1 -! which jq >/dev/null && echo "jq needs to be installed" && exit 1 -! which tar >/dev/null && echo "tar needs to be installed" && exit 1 -! which xargs >/dev/null && echo "xargs needs to be installed" && exit 1 +! which curl >/dev/null 2>&1 && echo "curl needs to be installed" >&2 && exit 1 +! which go >/dev/null 2>&1 && echo "go needs to be installed" >&2 && exit 1 +! which jq >/dev/null 2>&1 && echo "jq needs to be installed" >&2 && exit 1 +! which tar >/dev/null 2>&1 && echo "tar needs to be installed" >&2 && exit 1 +! which xargs >/dev/null 2>&1 && echo "xargs needs to be installed" >&2 && exit 1 latest_release=$(curl -s $CPOLIS_REPO_URL | jq -r '.[0].tag_name') curl -Lo cpolis.tar.gz https://git.streifling.com/jason/cpolis/archive/$latest_release.tar.gz From ff6c7a66d7f7890e011214c768798a5a6bc61fc5 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sun, 18 Aug 2024 16:59:49 +0200 Subject: [PATCH 4/8] Fixed a typo --- update.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/update.sh b/update.sh index f328e76..513eb71 100755 --- a/update.sh +++ b/update.sh @@ -11,9 +11,9 @@ SYSTEMD_USER_DIR=$HOME/.config/systemd/user ! which tar >/dev/null 2>&1 && echo "tar needs to be installed" >&2 && exit 1 ! which xargs >/dev/null 2>&1 && echo "xargs needs to be installed" >&2 && exit 1 +rm -fr $CPOLIS_PATH latest_release=$(curl -s $CPOLIS_REPO_URL | jq -r '.[0].tag_name') curl -Lo cpolis.tar.gz https://git.streifling.com/jason/cpolis/archive/$latest_release.tar.gz -rm -fr $CPOLIS_PATH tar -xzf cpolis.tar.gz -C $CPOLIS_PATH rm cpolis.tar.gz @@ -26,7 +26,7 @@ chmod +x $CPOLIS_PATH/tailwindcss $CPOLIS_PATH/tailwindcss -i web/static/css/input.css -o web/static/css/style.css go build -o $CPOLIS_PATH/cpolis $CPOLIS_PATH/cmd/main.go -chmox +x $CPOLIS_PATH/cpolis +chmod +x $CPOLIS_PATH/cpolis [ ! -f $SYSTEMD_USER_DIR ] && mkdir -p $SYSTEMD_USER_DIR mv $CPOLIS_PATH/cpolis.service $SYSTEMD_USER_DIR From 9ddd8198ee63ee43e93fd2fa830a53c62c3e57a6 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sun, 18 Aug 2024 17:03:31 +0200 Subject: [PATCH 5/8] Use /tmp directory for downloading .tar.gz archive --- update.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/update.sh b/update.sh index 513eb71..4c39d52 100755 --- a/update.sh +++ b/update.sh @@ -13,9 +13,9 @@ SYSTEMD_USER_DIR=$HOME/.config/systemd/user rm -fr $CPOLIS_PATH latest_release=$(curl -s $CPOLIS_REPO_URL | jq -r '.[0].tag_name') -curl -Lo cpolis.tar.gz https://git.streifling.com/jason/cpolis/archive/$latest_release.tar.gz -tar -xzf cpolis.tar.gz -C $CPOLIS_PATH -rm cpolis.tar.gz +curl -Lo /tmp/cpolis.tar.gz https://git.streifling.com/jason/cpolis/archive/$latest_release.tar.gz +tar -xzf /tmp/cpolis.tar.gz -C $CPOLIS_PATH +rm /tmp/cpolis.tar.gz curl -s $TAILWINDCSS_REPO_URL | grep -F browser_download_url | From 8d2944d00c9b4d00acded155fb54a07b18a303f2 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sun, 18 Aug 2024 17:06:49 +0200 Subject: [PATCH 6/8] Only delete contents of $CPOLIS_PATH --- update.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update.sh b/update.sh index 4c39d52..beb222e 100755 --- a/update.sh +++ b/update.sh @@ -11,7 +11,7 @@ SYSTEMD_USER_DIR=$HOME/.config/systemd/user ! which tar >/dev/null 2>&1 && echo "tar needs to be installed" >&2 && exit 1 ! which xargs >/dev/null 2>&1 && echo "xargs needs to be installed" >&2 && exit 1 -rm -fr $CPOLIS_PATH +rm -fr $CPOLIS_PATH/* latest_release=$(curl -s $CPOLIS_REPO_URL | jq -r '.[0].tag_name') curl -Lo /tmp/cpolis.tar.gz https://git.streifling.com/jason/cpolis/archive/$latest_release.tar.gz tar -xzf /tmp/cpolis.tar.gz -C $CPOLIS_PATH From 151d89d9f0109269ae74d8f7148a4d0391a46fc5 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sun, 18 Aug 2024 17:10:10 +0200 Subject: [PATCH 7/8] Create $EXTRACTION_PATH --- update.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/update.sh b/update.sh index beb222e..9fa06c9 100755 --- a/update.sh +++ b/update.sh @@ -1,7 +1,8 @@ #! /bin/sh - CPOLIS_REPO_URL="https://git.streifling.com/api/v1/repos/jason/cpolis/releases" -CPOLIS_PATH=$HOME/cpolis +EXTRACTION_PATH=$HOME +CPOLIS_PATH=$EXTRACTION_PATH/cpolis TAILWINDCSS_REPO_URL=https://api.github.com/repos/tailwindlabs/tailwindcss/releases/latest SYSTEMD_USER_DIR=$HOME/.config/systemd/user @@ -14,7 +15,7 @@ SYSTEMD_USER_DIR=$HOME/.config/systemd/user rm -fr $CPOLIS_PATH/* latest_release=$(curl -s $CPOLIS_REPO_URL | jq -r '.[0].tag_name') curl -Lo /tmp/cpolis.tar.gz https://git.streifling.com/jason/cpolis/archive/$latest_release.tar.gz -tar -xzf /tmp/cpolis.tar.gz -C $CPOLIS_PATH +tar -xzf /tmp/cpolis.tar.gz -C $EXTRACTION_PATH rm /tmp/cpolis.tar.gz curl -s $TAILWINDCSS_REPO_URL | From d56cdc78ebe05e95d270d486178cbeb896e52d7d Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sun, 18 Aug 2024 17:30:43 +0200 Subject: [PATCH 8/8] Use directories that require administrative privileges --- update.sh | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/update.sh b/update.sh index 9fa06c9..943cd87 100755 --- a/update.sh +++ b/update.sh @@ -1,10 +1,14 @@ #! /bin/sh - CPOLIS_REPO_URL="https://git.streifling.com/api/v1/repos/jason/cpolis/releases" -EXTRACTION_PATH=$HOME -CPOLIS_PATH=$EXTRACTION_PATH/cpolis +EXTRACTION_DIR=$HOME +CPOLIS_DIR=$EXTRACTION_DIR/cpolis TAILWINDCSS_REPO_URL=https://api.github.com/repos/tailwindlabs/tailwindcss/releases/latest -SYSTEMD_USER_DIR=$HOME/.config/systemd/user +TMP_DIR=/tmp +BIN_DIR=/usr/local/bin +SYSTEMD_DIR=/lib/systemd/system + +! groups | grep -E 'root|wheel|sudo' && echo "You need administrative privileges for this script" && exit 1 ! which curl >/dev/null 2>&1 && echo "curl needs to be installed" >&2 && exit 1 ! which go >/dev/null 2>&1 && echo "go needs to be installed" >&2 && exit 1 @@ -12,24 +16,25 @@ SYSTEMD_USER_DIR=$HOME/.config/systemd/user ! which tar >/dev/null 2>&1 && echo "tar needs to be installed" >&2 && exit 1 ! which xargs >/dev/null 2>&1 && echo "xargs needs to be installed" >&2 && exit 1 -rm -fr $CPOLIS_PATH/* +rm -fr $CPOLIS_DIR/* latest_release=$(curl -s $CPOLIS_REPO_URL | jq -r '.[0].tag_name') -curl -Lo /tmp/cpolis.tar.gz https://git.streifling.com/jason/cpolis/archive/$latest_release.tar.gz -tar -xzf /tmp/cpolis.tar.gz -C $EXTRACTION_PATH -rm /tmp/cpolis.tar.gz +curl -Lo $TMP_DIR/cpolis.tar.gz https://git.streifling.com/jason/cpolis/archive/$latest_release.tar.gz +tar -xzf $TMP_DIR/cpolis.tar.gz -C $EXTRACTION_DIR +rm $TMP_DIR/cpolis.tar.gz curl -s $TAILWINDCSS_REPO_URL | grep -F browser_download_url | grep -F linux-x64 | cut -d'"' -f4 | - xargs -r curl -Lo $CPOLIS_PATH/tailwindcss -chmod +x $CPOLIS_PATH/tailwindcss -$CPOLIS_PATH/tailwindcss -i web/static/css/input.css -o web/static/css/style.css + xargs -r curl -Lo $CPOLIS_DIR/tailwindcss +chmod +x $CPOLIS_DIR/tailwindcss +$CPOLIS_DIR/tailwindcss -i web/static/css/input.css -o web/static/css/style.css -go build -o $CPOLIS_PATH/cpolis $CPOLIS_PATH/cmd/main.go -chmod +x $CPOLIS_PATH/cpolis +go build -o $TMP_DIR/cpolis $CPOLIS_DIR/cmd/main.go +sudo mv $TMP_DIR/cpolis $BIN_DIR/cpolis +chmod +x $BIN_DIR/cpolis -[ ! -f $SYSTEMD_USER_DIR ] && mkdir -p $SYSTEMD_USER_DIR -mv $CPOLIS_PATH/cpolis.service $SYSTEMD_USER_DIR -systemctl --user daemon-reload -systemctl --user is-active --quiet cpolis.service && systemctl --user restart cpolis.service +[ ! -f $SYSTEMD_DIR ] && mkdir -p $SYSTEMD_DIR +sudo mv $CPOLIS_DIR/cpolis.service $SYSTEMD_DIR +sudo systemctl daemon-reload +sudo systemctl is-active --quiet cpolis.service && sudo systemctl restart cpolis.service