Skip to content

Instantly share code, notes, and snippets.

@prog893
Last active September 26, 2019 05:11
Show Gist options
  • Save prog893/61ff103779c9855a28ccfcb23e26b4c7 to your computer and use it in GitHub Desktop.
Save prog893/61ff103779c9855a28ccfcb23e26b4c7 to your computer and use it in GitHub Desktop.
AWS EC2 user_data OS detector pattern prototype
#!/bin/bash
set -euox pipefail
IFS=$'\n\t'
# https://aws.amazon.com/premiumsupport/knowledge-center/ec2-linux-log-user-data
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
# Detect OS
if [ -e "/etc/os-release" ]; then
OS_NAME=$( sed '/^NAME="/!d' < "/etc/os-release" | sed 's/NAME="//' | sed 's/"$//' )
OS_VERSION=$( sed '/^VERSION="/!d' < "/etc/os-release" | sed 's/VERSION="//' | sed 's/"$//' )
if [[ ${OS_NAME} = 'Amazon Linux'* ]] ; then
if [[ ${OS_VERSION} = '2.0 '* ]] ; then
OS="amazonlinux2"
else
OS="amazonlinux1"
fi
else
OS="unknown"
echo "/etc/os-release file exists but OS does not seem to be Amazon Linux, falling back to unknown OS"
fi
elif [ -e "/etc/lsb-release" ]; then
OS_NAME=$( sed '/^DISTRIB_ID="/!d' < "/etc/os-release" | sed 's/DISTRIB_ID="//' | sed 's/"$//' )
OS_VERSION=$( sed '/^DISTRIB_RELEASE="/!d' < "/etc/os-release" | sed 's/DISTRIB_RELEASE="//' | sed 's/"$//' )
if [[ ${OS_NAME} = 'Ubuntu' ]] ; then
OS="ubuntu${OS_VERSION}"
else
OS="unknown"
echo "/etc/lsb-release file exists but OS does not seem to be Ubuntu (...Debian?), falling back to unknown OS"
fi
fi
# Different actions per-os
case ${OS} in
"amazonlinux2")
echo "Amazon Linux 2やで"
# Download and execute phase 2 script for particular OS
# aws s3 cp s3://bucket-name/amazon-linux-2-setup.sh ./
# . amazon-linux-2-setup.sh
;;
"amazonlinux1")
echo "Amazon Linux 1やで"
;;
"ubuntu16.04")
echo "Ubuntu 16.04やで"
;;
*)
echo "知らんOSやで"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment