Skip to content

Instantly share code, notes, and snippets.

@shaon
Last active July 7, 2022 05:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaon/62eb6836e95b4fc6036b0a3f292dbde0 to your computer and use it in GitHub Desktop.
Save shaon/62eb6836e95b4fc6036b0a3f292dbde0 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This is an updated version of the aws-install-ssm-agent
# script that supports SUSE.
function die() {
echo "$@, exiting." >&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 issue_match() {
grep -E -i -c "$1" /etc/issue 2>&1 &>/dev/null
[ $? -eq 0 ] && echo "true" || echo "false"
}
function is_debuntu() {
echo "$(issue_match 'Debian|Ubuntu')"
}
function is_ubuntu() {
echo "$(issue_match 'Ubuntu')"
}
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_ubuntu_major() {
lsb_release --release | cut -f 2 | cut -d '.' -f 1
}
function get_arch() {
if [ "$(uname -m)" == "x86_64" ]; then
echo "amd64"
elif [[ "$(uname -m)" =~ i[3-6]86 ]]; then
echo "386"
else
die "Unsupported architecture $(uname -m)"
fi
}
function get_package_type() {
if [ "$(is_debuntu)" == "true" ]; then
echo "deb"
elif [ "$(is_redhat|is_suse)" == "true" ]; then
echo "rpm"
fi
}
function get_dist() {
if [ "$(is_debuntu)" == "true" ]; then
echo "debian"
elif [ "$(is_redhat|is_suse)" == "true" ]; then
echo "linux"
else
die "Unknown distribution"
fi
}
function get_ssm_agent() {
extension="$(get_package_type)"
dist="$(get_dist)"
arch="$(get_arch)"
package="amazon-ssm-agent.$extension"
url_base="https://amazon-ssm-$REGION.s3.amazonaws.com"
url="${url_base}/latest/${dist}_${arch}/${package}"
get_contents "$url" > "$package"
if [ ! -f "$package" ]; then
die "Could not download the package from $url"
fi
}
function start_ssm_agent() {
if [ "$(is_ubuntu)" == "false" ]; then
return
fi
case "$(get_ubuntu_major)" in
16)
systemctl start amazon-ssm-agent
;;
14)
start amazon-ssm-agent
;;
*)
die "Unknown major revision $(get_ubuntu_major)"
esac
}
function install_ssm_agent() {
if [ "$(is_debuntu)" == "true" ]; then
dpkg -i amazon-ssm-agent.deb
elif [ "$(is_redhat)" == "true" ]; then
yum install --nogpgcheck -y amazon-ssm-agent.rpm
elif [ "$(is_suse)" == "true" ]; then
rpm --install amazon-ssm-agent.rpm
else
die "Unknown distribution"
fi
if [ ! -x "$(which amazon-ssm-agent)" ]; then
die "No SSM agent was installed"
fi
}
function main() {
cd /tmp
get_ssm_agent
install_ssm_agent
start_ssm_agent
}
main $@ 2>&1 | tee /tmp/aws-install-ssm-agent.log
#!/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 "$@"
#!/bin/bash
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
}
BUCKET_NAME="<CHANGE ME!!>"
readonly SCRIPT_NAME="aws-install-ssm-agent"
SCRIPT_URL="https://s3.amazonaws.com/$BUCKET_NAME/ssm/$SCRIPT_NAME"
cd /tmp
FILE_SIZE=0
MAX_RETRY_COUNT=3
RETRY_COUNT=0
while [ $RETRY_COUNT -lt $MAX_RETRY_COUNT ] ; do
echo AWS-UpdateLinuxAmi: Downloading script from $SCRIPT_URL
get_contents "$SCRIPT_URL" > "$SCRIPT_NAME"
FILE_SIZE=$(du -k /tmp/$SCRIPT_NAME | cut -f1)
echo AWS-UpdateLinuxAmi: Finished downloading script, size: $FILE_SIZE
if [ $FILE_SIZE -gt 0 ]; then
break
else
if [[ $RETRY_COUNT -lt MAX_RETRY_COUNT ]]; then
RETRY_COUNT=$((RETRY_COUNT+1));
echo AWS-UpdateLinuxAmi: FileSize is 0, retryCount: $RETRY_COUNT
fi
fi
done
if [ $FILE_SIZE -gt 0 ]; then
chmod +x "$SCRIPT_NAME"
echo AWS-UpdateLinuxAmi: Running UpdateSSMAgent script now ....
./"$SCRIPT_NAME"
else
echo AWS-UpdateLinuxAmi: Unable to download script, quitting ....
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment