Skip to content

Instantly share code, notes, and snippets.

@meowtochondria
Created October 8, 2019 00:53
Show Gist options
  • Save meowtochondria/d8e9a3071e6fe306e5267fe72283e62e to your computer and use it in GitHub Desktop.
Save meowtochondria/d8e9a3071e6fe306e5267fe72283e62e to your computer and use it in GitHub Desktop.
Helper to install https://github.com/go-acme/lego on shared hosting providers.
#!/bin/bash
###########################################
# Some initializations. #
###########################################
project='go-acme'
product='lego'
pkg_version='latest'
isDebug=false
release_link="https://api.github.com/repos/$project/$product/releases"
install_dir="$HOME/bin"
execute_features=()
declare -A available_versions
###########################################
# Functions #
###########################################
function print_debug_line()
{
if [ "$isDebug" = true ]; then
printf "DEBUG: %s \n" "$1"
fi
}
function usage()
{
echo "Script to setup and update $product.";
echo "The script should _NOT_ be run as root. It will ask for";
echo "password via sudo if something needs it.";
echo "";
echo "Usage: ${0} [-v version_to_install]";
echo -e "\n-v\tVersion of $product to build. This version should be";
echo -e " \tavailable upstream. Default: latest ($pkg_version)";
echo -e "\n-i\tInstallation path. Default is $install_dir.";
echo -e "\n-l\tList available versions.";
echo -e "\n-h\tShow this help message and exit.";
echo -e "\n-d\tPrint debugging statements.";
echo -e "\nExample: ${0} -v $pkg_version";
}
function parse_command()
{
SHORT=v:i:lhd
LONG=version:,install_path:,list,help,debug
PARSED=$(getopt --options $SHORT --longoptions $LONG --name "$0" -- "$@")
if [[ $? -ne 0 ]]; then
# e.g. $? == 1
# then getopt has complained about wrong arguments to stdout
exit 4
fi
# use eval with "$PARSED" to properly handle the quoting
eval set -- "$PARSED"
# Parse options until we see --
while true; do
case "$1" in
-d|--debug)
isDebug=true;
print_debug_line "ON";
shift
;;
-v|--version)
pkg_version="$2"
shift 2
;;
-i|--install_path)
install_dir="$2"
shift 2
;;
-l|--list)
execute_features+=('print_available_versions')
shift
;;
-h|--help)
execute_features+=('usage')
shift
;;
--)
shift
break
;;
*)
echo -e "\nProgrammer has the dumbz."
exit 3
;;
esac
done
}
function perform_safety_checks()
{
# Ensure we are not running as root.
if [ $EUID -eq 0 ]; then
echo 'Please do not run this script as root.'
exit 1
fi
}
function validate_inputs()
{
# Check if correct version has been supplied. Otherwise downloads will fail.
[ ${#available_versions[@]} -eq 0 ] && get_available_versions
is_version_valid='false'
if [ "$pkg_version" == "latest" ]; then
pkg_version=$(echo ${!available_versions[@]} | tr ' ' '\n' | sort -h | tail -n 1)
is_version_valid='true'
else
for available_version in ${!available_versions[@]}; do
if [ "$available_version" == "$pkg_version" ]; then
is_version_valid='true'
break
fi
done
fi
if [ "$is_version_valid" == 'false' ]; then
echo "'$pkg_version' of $product is not available upstream. Versions available:"
print_available_versions
exit 2
fi
print_debug_line "Using version: $pkg_version"
}
function get_available_versions()
{
print_debug_line "Getting available versions from $release_link"
for link in $(curl --silent $release_link | grep -oP "https.+$product_v\d+\.\d+\.\d+_linux_amd64.tar.gz"); do
ver=$(echo $link | cut -f 8 -d '/' | tr -d 'v')
available_versions["$ver"]=$link
done
}
function print_available_versions()
{
echo "Versions available upstream..."
[ ${#available_versions[@]} -eq 0 ] && get_available_versions
# Reference: http://www.tldp.org/LDP/abs/html/arrays.html
echo ${!available_versions[@]} | tr -s ' ' '\n' | sort -hr
}
function download_packages()
{
# prometheus is not publishing checksums for every release :/
core_archive_name=$(basename ${available_versions[$pkg_version]})
failed_download='false'
# Skip download if file already exists
if [ -f "$install_dir/$core_archive_name" ]; then
print_debug_line "$install_dir/$core_archive_name already exists. Not downloading again..."
else
print_debug_line "${FUNCNAME[0]} : Downloading ${available_versions[$pkg_version]} to $install_dir/$core_archive_name"
wget -O $install_dir/$core_archive_name ${available_versions[$pkg_version]}
# Print a message if download leads to file of size 0
if [ ! -s $install_dir/$core_archive_name ]; then
echo
echo "Failed to download ${available_versions[$pkg_version]}."
echo "Please verify if the link is accurate and network connectivity"
echo "is available."
failed_download='true'
fi
if [ "$failed_download" == 'true' ]; then
echo -e "\nDownload(s) failed :(. Exiting.\n"
exit 4
fi
fi
echo -e "\nNow extracting $core_archive_name..."
/bin/tar --directory="$install_dir" --gzip --extract --verbose --file="$install_dir/$core_archive_name"
if [ "$?" -ne 0 ]; then
echo -e "\nFailed to extract downloaded archive - $install_dir/$core_archive_name"
echo -e "\nFailed command: /bin/tar --directory=\"$install_dir\" --gzip --extract --verbose --file=\"$install_dir/$core_archive_name\""
exit 5
fi
# Remove archive once its all done.
/bin/rm -f "$install_dir/$core_archive_name"
echo -e "\n\nInstallation was successful!\nInvoking lego --version to verify expected version was installed.\n"
$install_dir/lego --version
}
##################
# Pass all args of the script to the function.
parse_command "$@"
perform_safety_checks
validate_inputs
for func in ${execute_features[@]}; do
($func)
done
[ ${#execute_features[@]} -gt 0 ] && exit 0
download_packages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment