Skip to content

Commit

Permalink
Add jitter to auto-update, as well as simple CLI tool
Browse files Browse the repository at this point in the history
  • Loading branch information
hsoerensen committed Apr 1, 2024
1 parent 9d1c1e0 commit 9a77a8a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions bin/get-node-status
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ echo "Voi Swarm Docker status:"
echo "**************"
swarm_running_image=$(docker inspect --format='{{.Image}}' "${container_id}" | cut -d':' -f2)
echo "Running container image (sha256): ${swarm_running_image}"
echo "Auto-update enabled: $(awk -F'=' '/swarm.cronjob.enable=/ {print $2}' "${HOME}"/voi/docker/compose.yml)"
echo "Auto-update schedule (cron format): $(awk -F'=' '/swarm.cronjob.schedule=/ {print $2}' "${HOME}"/voi/docker/compose.yml)"
echo ""

echo "AVM version:"
Expand Down
28 changes: 28 additions & 0 deletions bin/set-autoupdate
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

echo "Enable/Disable autoupdate for your node"
echo ""
echo "To enable autoupdate use: set-autoupdate true"
echo "To disable autoupdate use: set-autoupdate false"
echo ""

if [[ -z $1 ]]; then
echo "Please provide a value for autoupdate"
exit 1
fi

if [[ $1 != "true" && $1 != "false" ]]; then
echo "Invalid value for autoupdate. Please use 'true' or 'false'."
exit 1
fi

VOINETWORK_PROFILE=$(cat "${HOME}/voi/profile")

if [[ ${VOINETWORK_PROFILE} == "relay" ]]; then
docker_filename="${HOME}/voi/docker/relay.yml"
else
docker_filename="${HOME}/voi/docker/compose.yml"
fi

echo "Setting autoupdate to $1 in ${docker_filename}"
sed -i -E "s|(swarm.cronjob.enable=).*|\1$1|" "${docker_filename}"
2 changes: 1 addition & 1 deletion docker/relay.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ services:
restart_policy:
condition: none
labels:
- swarm.cronjob.enable=false
- swarm.cronjob.enable=true
- swarm.cronjob.schedule=0 */4 * * *
- swarm.cronjob.skip-running=true
placement:
Expand Down
34 changes: 33 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ start_stack() {
docker_file="${voi_home}/docker/compose.yml"

if [[ ${VOINETWORK_PROFILE} == "relay" ]]; then
echo "Starting relay node stack"
docker_file="${voi_home}/docker/relay.yml"
fi
command="env VOINETWORK_TELEMETRY_NAME=$VOINETWORK_TELEMETRY_NAME docker stack deploy -c ${docker_file}"
Expand Down Expand Up @@ -767,6 +766,37 @@ set_profile() {
display_banner "Setting up Voi Swarm using profile: ${VOINETWORK_PROFILE}"
}

preserve_autoupdate() {
echo "Preserving autoupdate settings..."
if [[ ${VOINETWORK_PROFILE} == "relay" ]]; then
docker_filename="${voi_home}/docker/relay.yml"
else
docker_filename="${voi_home}/docker/compose.yml"
fi

autoupdate_state=$(awk -F'=' '/swarm.cronjob.enable=/ {print $2}' "${HOME}"/voi/docker/compose.yml)

if [[ ${autoupdate_state} == "false" ]]; then
sed -i -E "s/(swarm.cronjob.enable=).*/\1false" "${docker_filename}"
fi
}

add_update_jitter() {
echo "Add jitter to autoupdate schedule..."
if [[ ${VOINETWORK_PROFILE} == "relay" ]]; then
schedule_filename="${voi_home}/docker/relay.yml"
else
schedule_filename="${voi_home}/docker/compose.yml"
fi

random_minute=$(( RANDOM % 60 ))
# Generate a random number between 0 and 2, and add +1 to shift the range to 1-3
random_hour=$(( RANDOM % 3 + 1 ))

new_cron_schedule="swarm.cronjob.schedule=${random_minute} */${random_hour} * * *"
sed -i -E "s|(swarm.cronjob.schedule=).*|\1${new_cron_schedule}|" "${schedule_filename}"
}

if [ -z "${BASH_VERSION:-}" ]; then
abort "Bash is required to interpret this script."
fi
Expand Down Expand Up @@ -868,6 +898,8 @@ rm "${voi_home}"/voi-swarm.tar.gz

cleanup_deprecated_files_and_folders

add_update_jitter

start_stack

wait_for_stack_to_be_ready
Expand Down

0 comments on commit 9a77a8a

Please sign in to comment.