Last active
January 25, 2024 10:00
-
-
Save shaon/161102845971fc6884f24944cfbc30cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This is an updated version of the aws-update-linux-instance | |
# script that supports SUSE. | |
PRE_UPDATE_SCRIPT_URL='' | |
POST_UPDATE_SCRIPT_URL='' | |
INCLUDE_PACKAGES='' | |
EXCLUDE_PACKAGES='' | |
function usage() { | |
cat <<- EOF | |
Usage: $0 [OPTION]... | |
Update the instance's distribution packages and Amazon software | |
[-h|--help] | |
Print this help message. | |
[-d|--debug] | |
Show additional debugging info. | |
[--pre-update-script <SCRIPT_URL>] | |
A script to run before the package manager is invoked for | |
updates. By default, when no script is provided, nothing is done | |
before system updates. | |
[--post-update-script <SCRIPT_URL>] | |
A script to run after the package manager is invoked for | |
updates. By default, when no script is provided, nothing is done | |
before system updates. | |
[-i|--include-packages <PACKAGE[,PACKAGE]...>] | |
A list of packages that will be updated. When provided, the | |
system will atempt to update only these packages and their | |
dependencies, but no other updates will be performed. By | |
default, when no include packages are explicitly specified, the | |
program will update all available packages. | |
[-x|--exclude-packages <PACKAGE[,PACKAGE]...>] | |
A list of packages that will be held back from updates. If | |
provided, these packages will stay at their current versions, | |
independent of any other options specified. By default, when no | |
exclude packages are specified, no packages will be held back. | |
EOF | |
exit $1 | |
} | |
function unhold_deb_packages() { | |
for package in $EXCLUDE_PACKAGES; do | |
apt-mark unhold $package | |
done | |
} | |
function die() { | |
if [ "$(get_dist)" == "debian" ]; then | |
unhold_deb_packages | |
fi | |
echo "$@" >&2 | |
exit 1 | |
} | |
function get_contents() { | |
if [ -x "$(which curl)" ]; then | |
curl -s -f "$1" | |
elif [ -x "$(which wget)" ]; then | |
wget "$1" -O - | |
else | |
die "No download utility (curl, wget)" | |
fi | |
} | |
function sanitize_inputs() { | |
value="$(echo $@ | sed 's/,/ /g' | xargs | xargs)" | |
if [ ! -z "$value" ] && | |
[ "$value" != "none" ] && | |
[ "$value" != "all" ]; then | |
echo "$value" | |
fi | |
} | |
function get_cli_options() { | |
while [ $# -gt 0 ]; do | |
arg_required="true" | |
case $1 in | |
-h|--help) | |
usage 0 | |
;; | |
-i|--include-packages) | |
INCLUDE_PACKAGES="$(sanitize_inputs $2)" | |
;; | |
-x|--exclude-packages) | |
EXCLUDE_PACKAGES="$(sanitize_inputs $2)" | |
;; | |
--pre-update-script) | |
PRE_UPDATE_SCRIPT_URL="$(sanitize_inputs $2)" | |
;; | |
--post-update-script) | |
POST_UPDATE_SCRIPT_URL="$(sanitize_inputs $2)" | |
;; | |
-d|--debug) | |
arg_required="false" | |
set -x | |
;; | |
*) | |
echo "Unknown option: $1" >&2 | |
usage 1 | |
;; | |
esac | |
if [ "$arg_required" == "true" ]; then | |
[ -z "$2" ] && die "$1 requires a value" | |
shift | |
fi | |
shift | |
done | |
} | |
function echo_options() { | |
echo \"\$PRE_UPDATE_SCRIPT_URL\" == \"$PRE_UPDATE_SCRIPT_URL\" | |
echo \"\$POST_UPDATE_SCRIPT_URL\" == \"$POST_UPDATE_SCRIPT_URL\" | |
echo \"\$INCLUDE_PACKAGES\" == \"$INCLUDE_PACKAGES\" | |
echo \"\$EXCLUDE_PACKAGES\" == \"$EXCLUDE_PACKAGES\" | |
} | |
function exec_cmd() { | |
echo "Invoking $@..." | |
eval "$@" | |
if [ $? -ne 0 ]; then | |
die "" | |
fi | |
} | |
function is_debuntu() { | |
grep -E -i -c 'Debian|Ubuntu' /etc/issue 2>&1 &>/dev/null | |
[ $? -eq 0 ] && echo "true" || echo "false" | |
} | |
function is_redhat() { | |
if [ -f "/etc/system-release" ] || | |
[ -f "/etc/redhat-release" ]; then | |
echo "true" | |
else | |
echo "false" | |
fi | |
} | |
function is_suse() { | |
if [ -f "/etc/os-release" ] || | |
[ -f "/etc/SuSE-release" ]; then | |
echo "true" | |
else | |
echo "false" | |
fi | |
} | |
function get_dist() { | |
if [ "$(is_debuntu)" == "true" ]; then | |
echo "debian" | |
elif [ "$(is_redhat)" == "true" ]; then | |
echo "redhat" | |
elif [ "$(is_suse)" == "true" ]; then | |
echo "suse" | |
else | |
die "Unknown distribution" | |
fi | |
} | |
function run_hook_script() { | |
script_url="$1" | |
tmp_file="$(mktemp)" | |
echo "Downloading hook script from $script_url" | |
get_contents "$script_url" > "$tmp_file" | |
chmod +x "$tmp_file" | |
exec_cmd "$tmp_file" | |
} | |
function update_cli() { | |
if [ -x "$(which pip 2>/dev/null)" ]; then | |
exec_cmd "pip install --upgrade awscli" | |
else | |
exec_cmd "easy_install --upgrade awscli" | |
fi | |
} | |
function apt_get_update() { | |
exec_cmd "apt-get clean" | |
exec_cmd "apt-get update" | |
for package in $EXCLUDE_PACKAGES; do | |
exec_cmd "apt-mark hold $package" | |
done | |
if [ -z "$INCLUDE_PACKAGES" ]; then | |
exec_cmd "apt-get -y dist-upgrade" | |
else | |
for package in $INCLUDE_PACKAGES; do | |
exec_cmd "apt-get -y install --only-upgrade $package" | |
done | |
fi | |
unhold_deb_packages | |
} | |
function yum_upgrade() { | |
exec_cmd 'yum clean all' | |
yum_cmd='yum -y upgrade' | |
for package in $EXCLUDE_PACKAGES; do | |
yum_cmd="$yum_cmd -x $package" | |
done | |
if [ ! -z "$INCLUDE_PACKAGES" ]; then | |
yum_cmd="$yum_cmd $INCLUDE_PACKAGES" | |
fi | |
exec_cmd "$yum_cmd" | |
} | |
function zypper_upgrade() { | |
exec_cmd 'zypper refresh' | |
zypper_cmd='zypper update -y' | |
for package in $EXCLUDE_PACKAGES; do | |
yum_cmd="zypper al $package" | |
done | |
exec_cmd "$zypper_cmd" | |
for package in $EXCLUDE_PACKAGES; do | |
yum_cmd="zypper rl $package" | |
done | |
} | |
function remove_excludes_from_includes() { | |
if [ -z "$EXCLUDE_PACKAGES" ] || [ -z "$INCLUDE_PACKAGES" ]; then | |
return | |
fi | |
declare -A includes | |
declare -A excludes | |
for package in $EXCLUDE_PACKAGES; do | |
excludes[$package]="true" | |
done | |
for package in $INCLUDE_PACKAGES; do | |
if [ "${excludes[$package]}" != "true" ]; then | |
includes[$package]="true" | |
fi | |
done | |
INCLUDE_PACKAGES="${!includes[@]}" | |
} | |
function update_packages() { | |
remove_excludes_from_includes | |
if [ "$(get_dist)" == "debian" ]; then | |
apt_get_update | |
elif [ "$(get_dist)" == "redhat" ]; then | |
yum_upgrade | |
elif [ "$(get_dist)" == "suse" ]; then | |
zypper_upgrade | |
fi | |
} | |
function main() { | |
get_cli_options "$@" | |
echo_options | |
if [ ! -z "$PRE_UPDATE_SCRIPT_URL" ]; then | |
run_hook_script "$PRE_UPDATE_SCRIPT_URL" | |
fi | |
update_cli | |
update_packages | |
if [ ! -z "$POST_UPDATE_SCRIPT_URL" ]; then | |
run_hook_script "$POST_UPDATE_SCRIPT_URL" | |
fi | |
exit 0 | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment