Skip to content

Commit

Permalink
feat: change auto update script to allow getting release
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgomesdev committed Dec 21, 2023
1 parent edba2e4 commit d9ef9a2
Showing 1 changed file with 68 additions and 19 deletions.
87 changes: 68 additions & 19 deletions server/scripts/auto-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,74 @@ WARNING=$(tput setaf 3)
INFO=$(tput setaf 6)
RESET=$(tput sgr0)

# Params

BUILD_BINARY="n"

# Vars

REPO_URL="https://github.com/DavidGomesDev/RustyController"
RUSTY_HOME_DIR="$HOME/RustyController"
BINARY_PATH="$RUSTY_HOME_DIR/server/target/release/rusty_controller"
HASH_FILE="$RUSTY_HOME_DIR/current.sha256"

# Get params

show_usage () {
echo "Parameters:"
echo "-b: build binary (instead of downloading the latest release)"
}

while getopts "bh" opt; do
case ${opt} in
b)
BUILD_BINARY="y"
;;
h)
show_usage
exit 0
;;
?)
echo "Invalid option: -${OPTARG}."
show_usage
exit 1
;;
esac
done

echo "$START* Updating server$RESET"

cd "$HOME" || exit 1

download_latest () {
# Get arch (and trim output)
arch=$(dpkg --print-architecture | xargs echo -n)

mkdir -p target/release || exit 1
wget -q "$REPO_URL/releases/download/latest/server-$arch" -O target/release/rusty_controller || exit 1

echo "$INFO* Downloaded latest release binary!$RESET"
}

build () {
echo
echo "$INFO* Updating crates...$RESET"
echo

cargo update -q || exit 1

echo "$INFO* Build...$RESET"
echo

time cargo build --release -q || exit 1

newest_hash=$(sha256sum "$BINARY_PATH" | gawk '{print $1}')

echo "$newest_hash" > "$HASH_FILE"
echo "$SUCCESS* Built successfully!$RESET"
}

update () {
echo "$INFO* Checking out main...$RESET"
echo

Expand All @@ -30,21 +88,12 @@ build () {

cd server/ || exit 1

echo
echo "$INFO* Updating crates...$RESET"
echo

cargo update -q || exit 1

echo "$INFO* Build...$RESET"
echo

time cargo build --release -q || exit 1

newest_hash=$(sha256sum "$BINARY_PATH" | gawk '{print $1}')
if [[ "$BUILD_BINARY" == "y" ]]; then
build
else
download_latest
fi

echo "$newest_hash" > "$HASH_FILE"
echo "$SUCCESS* Built successfully!$RESET"
cd ..
}

Expand All @@ -54,10 +103,10 @@ launch () {
}

if [[ ! -d "$RUSTY_HOME_DIR" ]]; then
echo "$WARNING* Rusty not found. Cloning...$RESET"
echo "$WARNING* Rusty repo not found. Cloning...$RESET"
git clone "$REPO_URL"

build
update
launch
exit 0
fi
Expand All @@ -67,13 +116,13 @@ cd "$RUSTY_HOME_DIR" || exit 1
if [[ -f "$HASH_FILE" ]]; then
if [ ! -f "$BINARY_PATH" ]; then
echo "$INFO* Binary not found, building...$RESET"
build
update
launch
exit 0
fi

current_hash=$(cat "$HASH_FILE")
build
update
if [[ "$current_hash" != "$newest_hash" ]]; then
echo "$INFO* Built a new version!$RESET"
launch
Expand All @@ -82,6 +131,6 @@ if [[ -f "$HASH_FILE" ]]; then
fi
else
echo "$WARNING* Couldn't find current hash. Updating to latest version anyway.$RESET"
build
update
launch
fi

0 comments on commit d9ef9a2

Please sign in to comment.