Add update routine
This commit is contained in:
parent
465d0daa9c
commit
ac29436f35
1
include/version.hpp
Normal file
1
include/version.hpp
Normal file
@ -0,0 +1 @@
|
|||||||
|
static const char *VERSION = "1";
|
61
install.sh
Executable file
61
install.sh
Executable file
@ -0,0 +1,61 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
PROJECT="dehydrate"
|
||||||
|
|
||||||
|
# 0. see if we were passed a folder to install to
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
INSTALL_DIR="$1"
|
||||||
|
if [[ -z "$INSTALL_DIR" ]]; then
|
||||||
|
INSTALL_DIR="/usr/local/bin"
|
||||||
|
else
|
||||||
|
echo "Installing $PROJECT to $INSTALL_DIR"
|
||||||
|
if [[ ! -d "$INSTALL_DIR" ]]; then
|
||||||
|
mkdir -p "$INSTALL_DIR"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 0. see if we were passed a user to chown to
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
CHOWN_USER="$2"
|
||||||
|
if [[ -z "$CHOWN_USER" ]]; then
|
||||||
|
CHOWN_USER=$USER
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 1. Determine architecture
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
ARCH=$(uname -m)
|
||||||
|
if [[ "$ARCH" == "x86_64" ]]; then
|
||||||
|
BIN=$PROJECT.amd64
|
||||||
|
elif [[ "$ARCH" == "aarch64" || "$ARCH" == "arm64" ]]; then
|
||||||
|
BIN=$PROJECT.arm64
|
||||||
|
else
|
||||||
|
echo "Unsupported architecture: $ARCH" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 3. Download the appropriate binary to a temp directory
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
TMPDIR=$(mktemp -d)
|
||||||
|
trap 'rm -rf "$TMPDIR"' EXIT
|
||||||
|
URL="https://gitea.jde.nz/public/$PROJECT/releases/download/latest/$BIN"
|
||||||
|
echo "Downloading $BIN from $URL..."
|
||||||
|
|
||||||
|
curl -fsSL -o "$TMPDIR/$PROJECT" "$URL"
|
||||||
|
|
||||||
|
# 4. Make it executable
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
chmod +x "$TMPDIR/$PROJECT"
|
||||||
|
|
||||||
|
# 5. Move to /usr/local/bin
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
docker run --rm -v "$TMPDIR:/tmp" -v "$INSTALL_DIR:/target" alpine sh -c "cp /tmp/$PROJECT /target/$PROJECT; chown $CHOWN_USER /target/$PROJECT"
|
||||||
|
rm "$TMPDIR/$PROJECT"
|
||||||
|
|
||||||
|
# 6. Print success message
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
echo "$PROJECT installed successfully to $INSTALL_DIR/$PROJECT (arch $ARCH)"
|
||||||
|
echo " "
|
||||||
|
echo "Update $PROJECT with:"
|
||||||
|
echo " $PROJECT -u"
|
95
publish.sh
Executable file
95
publish.sh
Executable file
@ -0,0 +1,95 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# Increment version
|
||||||
|
VERFILE="include/version.hpp"
|
||||||
|
PROJECT="dehydrate"
|
||||||
|
|
||||||
|
if [ ! -f $VERFILE ]; then
|
||||||
|
echo "$VERFILE not found!" >&2
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
v=$(cat $VERFILE | grep -o 'static const char \*VERSION = "[0-9.]*";' | cut -d'"' -f2)
|
||||||
|
oldv=$v
|
||||||
|
v=$((v+1))
|
||||||
|
echo "Incrementing version from $oldv to $v" >&2
|
||||||
|
echo "static const char *VERSION = \"$v\";" > $VERFILE
|
||||||
|
fi
|
||||||
|
TAG="v$v"
|
||||||
|
|
||||||
|
# Build binaries
|
||||||
|
./build.sh
|
||||||
|
|
||||||
|
# make sure we've commited.
|
||||||
|
git add . && git commit -m "$PROJECT release $TAG" && git push
|
||||||
|
|
||||||
|
|
||||||
|
# Find repo info from .git/config
|
||||||
|
REPO_URL=$(git config --get remote.origin.url)
|
||||||
|
if [[ ! $REPO_URL =~ gitea ]]; then
|
||||||
|
echo "Remote origin is not a Gitea repository: $REPO_URL" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
# Extract base URL, owner, and repo
|
||||||
|
# Example: https://gitea.example.com/username/reponame.git
|
||||||
|
BASE_URL=$(echo "$REPO_URL" | sed -E 's#(https?://[^/]+)/.*#\1#')
|
||||||
|
OWNER=$(echo "$REPO_URL" | sed -E 's#.*/([^/]+)/[^/]+(\.git)?$#\1#')
|
||||||
|
REPO=$(echo "$REPO_URL" | sed -E 's#.*/([^/]+)(\.git)?$#\1#')
|
||||||
|
|
||||||
|
API_URL="$BASE_URL/api/v1/repos/$OWNER/$REPO"
|
||||||
|
|
||||||
|
# Check for GITEA_TOKEN_DEPLOY or GITEA_TOKEN
|
||||||
|
if [ -n "$GITEA_TOKEN_DEPLOY" ]; then
|
||||||
|
TOKEN="$GITEA_TOKEN_DEPLOY"
|
||||||
|
elif [ -n "$GITEA_TOKEN" ]; then
|
||||||
|
TOKEN="$GITEA_TOKEN"
|
||||||
|
else
|
||||||
|
echo "GITEA_TOKEN_DEPLOY or GITEA_TOKEN environment variable not set!" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Create release
|
||||||
|
RELEASE_DATA=$(cat <<EOF
|
||||||
|
{
|
||||||
|
"tag_name": "$TAG",
|
||||||
|
"name": "$TAG",
|
||||||
|
"body": "$PROJECT release $TAG",
|
||||||
|
"draft": false,
|
||||||
|
"prerelease": false
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
)
|
||||||
|
|
||||||
|
RELEASE_ID=$(curl -s -X POST "$API_URL/releases" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-H "Authorization: token $TOKEN" \
|
||||||
|
-d "$RELEASE_DATA" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
|
||||||
|
|
||||||
|
if [ -z "$RELEASE_ID" ]; then
|
||||||
|
echo "Failed to create release on Gitea." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Upload binaries and install.sh
|
||||||
|
for FILE in $PROJECT.amd64 $PROJECT.arm64 install.sh; do
|
||||||
|
if [ -f "output/$FILE" ]; then
|
||||||
|
filetoupload="output/$FILE"
|
||||||
|
elif [ -f "$FILE" ]; then
|
||||||
|
filetoupload="$FILE"
|
||||||
|
else
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Auto-detect content type
|
||||||
|
ctype=$(file --mime-type -b "$filetoupload")
|
||||||
|
|
||||||
|
curl -s -X POST "$API_URL/releases/$RELEASE_ID/assets?name=$FILE" \
|
||||||
|
-H "Content-Type: $ctype" \
|
||||||
|
-H "Authorization: token $TOKEN" \
|
||||||
|
--data-binary @"$filetoupload"
|
||||||
|
echo "Uploaded $FILE to release $TAG as $ctype."
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "Published $PROJECT version $v to $REPO_URL (tag $TAG) with binaries."
|
||||||
|
|
||||||
|
|
52
src/main.cpp
52
src/main.cpp
@ -1,8 +1,60 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "../include/argparse.hpp"
|
#include "../include/argparse.hpp"
|
||||||
#include "../include/generator.hpp"
|
#include "../include/generator.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
std::string get_arch()
|
||||||
|
{
|
||||||
|
// determine the architecture of the system
|
||||||
|
std::string arch;
|
||||||
|
#ifdef __aarch64__
|
||||||
|
arch = "arm64";
|
||||||
|
#elif __x86_64__
|
||||||
|
arch = "amd64";
|
||||||
|
#endif
|
||||||
|
return arch;
|
||||||
|
}
|
||||||
|
|
||||||
|
int update()
|
||||||
|
{
|
||||||
|
// determine path to this executable
|
||||||
|
std::filesystem::path exepath = std::filesystem::canonical("/proc/self/exe");
|
||||||
|
std::filesystem::path parent_path = exepath.parent_path();
|
||||||
|
std::string project_name = exepath.filename().string();
|
||||||
|
|
||||||
|
// determine the architecture of the system
|
||||||
|
std::string arch = get_arch();
|
||||||
|
|
||||||
|
std::string url = "https://gitea.jde.nz/public/"+project_name+"/releases/download/latest/"+project_name+"." + arch;
|
||||||
|
|
||||||
|
// download new version, preserve permissions and ownership
|
||||||
|
std::string bash_script;
|
||||||
|
bash_script += "docker run --rm -v "+parent_path.string()+":/target";
|
||||||
|
bash_script += " gitea.jde.nz/public/debian-curl:latest";
|
||||||
|
bash_script += " sh -c \"";
|
||||||
|
bash_script += " curl -fsSL " + url + " -o /target/"+project_name+"_temp &&";
|
||||||
|
bash_script += " chmod --reference=/target/"+project_name+" /target/"+project_name+"_temp &&";
|
||||||
|
bash_script += " chown --reference=/target/"+project_name+" /target/"+project_name+"_temp &&";
|
||||||
|
bash_script += " mv /target/"+project_name+"_temp /target/"+project_name;
|
||||||
|
bash_script += "\"";
|
||||||
|
|
||||||
|
std::cout << "Updating " << exepath << " to the latest " << arch << " version." << std::endl;
|
||||||
|
|
||||||
|
// std::cout << "bash_script: " << std::endl
|
||||||
|
// << bash_script << std::endl;
|
||||||
|
|
||||||
|
// run the bash script
|
||||||
|
execlp("bash", "bash", "-c", bash_script.c_str(), (char *)nullptr);
|
||||||
|
std::cerr << "Failed to execute command." << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
try {
|
try {
|
||||||
Args args = parse_args(argc, argv);
|
Args args = parse_args(argc, argv);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user