Skip to content

Instantly share code, notes, and snippets.

@avifenesh
Last active October 27, 2024 23:43
Show Gist options
  • Save avifenesh/9ecb3cb0c0042973a4db24571f345d30 to your computer and use it in GitHub Desktop.
Save avifenesh/9ecb3cb0c0042973a4db24571f345d30 to your computer and use it in GitHub Desktop.
A script for easily switching betwen ValKey version
#!/bin/bash
#remove comment to print the lines running.
#set -x
set -e
valkey_version=
should_clone_repo=false
valkey_clone_git=https://github.com/valkey-io/valkey.git
uninstall_old_version=true
valkey_repo_local_path=$HOME/valkey
redis_symlinks=true
tls=true
clean_cache=true
allocator=
verbose_build=false
rdma=false
prefix=
function help_message {
echo "Usage:"
echo "switch_server_version.sh -h - Prints this help message"
echo "switch_server_version.sh --version <major.minor> - use default settings"
echo "default settings - no clone, uninstall old version, redis symlinks, local path $HOME/valkey, tls on, clean cache, no allocator, verbose build off, no rdma, no prefix"
echo "Available options:"
echo "--version <major.minor> - set valkey version"
echo "--clone [git-path] - clone valkey repo instead of using local repo checkout. If git-path is not provided, uses default: $valkey_clone_git"
echo "--no-uninstall - do not uninstall old valkey version, if cloning automatically set to false"
echo "--local-path <valkey-repo-local-path> - set local path for valkey repo, if cloning will clone to this path"
echo "--no-symlinks - do not create redis symlinks"
echo "--no-tls - disable tls"
echo "--no-clean - do not clean caches"
echo "--allocator <allocator> - set allocator (jemalloc, libc)"
echo "--verbose-build - enable verbose build"
echo "--rdma - enable rdma support"
echo "--prefix <prefix> - set prefix for valkey install path"
}
# Parse options using getopts
while getopts ":h-:" opt; do
case $opt in
-)
case "${OPTARG}" in
version)
valkey_version="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
;;
clone)
should_clone_repo=true
if [[ -n "${!OPTIND}" && "${!OPTIND}" != -* ]]; then
valkey_clone_git="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
fi
uninstall_old_version=false
;;
no-uninstall)
uninstall_old_version=false
;;
local-path)
valkey_repo_local_path="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
;;
no-symlinks)
redis_symlinks=false
;;
no-tls)
tls=false
;;
no-clean)
clean_cache=false
;;
allocator)
allocator="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
;;
verbose-build)
verbose_build=true
;;
rdma)
rdma=true
;;
prefix)
prefix="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
;;
*)
echo "Invalid option: --${OPTARG}" >&2
help_message
exit 1
;;
esac
;;
h)
help_message
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
help_message
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
help_message
exit 1
;;
esac
done
# Validate inputs
if [[ -z "$valkey_version" ]]; then
echo "Error: Valkey version is required." >&2
exit 1
fi
if [[ -n "$allocator" && "$allocator" != "jemalloc" && "$allocator" != "libc" ]]; then
echo "Error: Invalid allocator. Must be 'jemalloc' or 'libc'." >&2
exit 1
fi
# Clone if needed
if [[ "$should_clone_repo" == true ]]; then
git clone "$valkey_clone_git" "$valkey_repo_local_path"
fi
# Check if the directory exists before changing to it
if [ ! -d "$valkey_repo_local_path" ]; then
echo "Error: Directory $valkey_repo_local_path does not exist." >&2
exit 1
fi
# Cd into repo
cd "$valkey_repo_local_path"
git fetch
# Uninstall old version if needed
if [[ "$uninstall_old_version" == true ]]; then
# Uninstall old version logic
echo "Uninstalling old valkey version"
git checkout "unstable"
cd src
sudo make uninstall
fi
# Checkout tag
cd $HOME/valkey
git checkout "$valkey_version"
git pull
cd src
# Create build command
make_command="make"
# Add flags based on options
if [[ "$clean_cache" == true ]]; then
sudo make distclean
fi
if [[ "$verbose_build" == true ]]; then
sudo make V=1
fi
if [[ "$allocator" != "" ]]; then
sudo make MALLOC=$allocator
fi
if [[ "$rdma" == true ]]; then
sudo make BUILD_RDMA=module
fi
if [[ "$prefix" != "" ]]; then
sudo make PREFIX=$prefix
fi
if [[ "$tls" == true ]]; then
sudo make BUILD_TLS=yes
fi
if [[ "$redis_symlinks" == false ]]; then
sudo make USE_REDIS_SYMLINKS=no
fi
echo "[$(date)] Starting build with command: $make_command"
if ! eval "$make_command"; then
echo "Build failed"
exit 1
fi
echo "[$(date)] Build completed"
echo "[$(date)] Starting installation"
if ! sudo make install; then
echo "Installation failed"
exit 1
fi
echo "[$(date)] Installation completed"
echo "Cleaning up..."
make clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment