sh launch_ubuntu_on_aws.sh
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh launch_ubuntu_on_aws.sh相关的知识,希望对你有一定的参考价值。
#!/bin/bash -e
## Launch the latest daily Ubuntu in AWS, optionally with user-data
## provided in a file and tagged with a name
function usage() {
echo "$0: [-r region] [-V ubuntu_version] [-i instance_type] \\"
echo " [-u user_data_file] [-n instance_name] [-d disk_size] \\"
echo " [-D] to disable termination via the API"
echo " [-I] iam instance profile [-a AMI ID]"
echo " [-P profile] aws cli profile"
echo " [-m] minimal"
echo ""
echo "Defaults: region: <default set in aws cli>"
echo " ubuntu_version: ${UBUNTU_VERSION}"
echo " instance_type: t2.nano"
}
function find_ami() {
# Find the latest HVM EBS Ubuntu AMI from the official publication account
REGION=$1
UBUNTU_VERSION=$2
MINIMAL=$3
if [ "${MINIMAL}" = "true" ] ; then
FILTERS="--filters Name=name,Values=ubuntu-minimal/images-testing/hvm-ssd/ubuntu-${UBUNTU_VERSION}-daily-amd64-minimal-*"
else
FILTERS="--filters Name=name,Values=ubuntu/images-testing/hvm-ssd/ubuntu-${UBUNTU_VERSION}-daily-amd64-server-*"
fi
aws ${PROFILE_ARG} --region "${REGION}" ec2 describe-images --owners 099720109477 \
$FILTERS \
--output json | \
jq -r '.Images | sort_by(.["CreationDate"])[-1].ImageId'
}
function wait_instance_running() {
# Wait for 2m for instance to enter running state
REGION=$1
INSTANCE_ID=$2
echo -n "Waiting for running state..."
for attempt in $(seq 12); do
STATE=$(aws ${PROFILE_ARG} --output json --region "${REGION}" \
ec2 describe-instances \
--instance-ids "${INSTANCE_ID}" | \
jq -r .Reservations[0].Instances[0].State.Name)
if [ "${STATE}" = "running" ]; then
echo " running."
return
else
echo -n "."
sleep 10
fi
done
echo "failed."
echo "Instance did not enter 'running' state after 2 minutes."
return 1
}
# Check for awscli and 'jq'
set +e
aws --version 2>/dev/null >/dev/null
if [ "$?" != 0 ] ; then
echo "This command requires the aws cli to be installed."
exit 1
fi
jq --version 2>/dev/null >/dev/null
if [ "$?" != 0 ] ; then
echo "This command requires the json query command 'jq' to be installed."
exit 1
fi
set -e
REGION=$(aws configure get region)
UBUNTU_VERSION=xenial
INSTANCE_TYPE=t2.nano
USER_DATA_FILE=
INSTANCE_NAME=
DISK_SIZE=
API_TERM=
KEY_NAME="${AWS_KEY_NAME}"
IAM_PROFILE=
MINIMAL="false"
while getopts "a:r:V:i:u:n:d:Dk:I:P:mh" opt; do
case "${opt}" in
a) AMI="${OPTARG}";;
r) REGION="${OPTARG}";;
V) UBUNTU_VERSION="${OPTARG}";;
i) INSTANCE_TYPE="${OPTARG}";;
u) USER_DATA_FILE="${OPTARG}";;
n) INSTANCE_NAME="${OPTARG}";;
d) DISK_SIZE="${OPTARG}";;
D) API_TERM="--disable-api-termination";;
k) KEY_NAME="${OPTARG}";;
I) IAM_PROFILE="${OPTARG}";;
P) PROFILE="${OPTARG}";;
m) MINIMAL="true";;
h)
usage
exit 0
;;
\?)
echo "Invalid option: -${OPTARG}" >&2
usage
exit 1
;;
esac
done
echo "${KEY_NAME:?Key name must be specified via -k or envvar AWS_KEY_NAME}" > /dev/null
if [ -n "${PROFILE}" ] ; then
PROFILE_ARG="--profile ${PROFILE}"
else
PROFILE_ARG=
fi
if [ -z "${REGION}" ] ; then
REGION=$(aws ${PROFILE_ARG} configure get region)
fi
# Latest HVM EBS-SSD ${UBUNTU_VERSION} daily
if [ -n "${AMI}" ] ; then
echo "Using user-supplied AMI id: ${AMI}"
else
echo -n "Finding an ami in ${REGION}... "
AMI=$(find_ami "${REGION}" "${UBUNTU_VERSION}" "${MINIMAL}")
echo "${AMI}"
fi
USER_DATA=''
if [ -n "${USER_DATA_FILE}" ] ; then
USER_DATA="--user-data file://${USER_DATA_FILE}"
fi
BDM=''
if [ -n "${DISK_SIZE}" ] ; then
BDM="--block-device-mappings DeviceName=/dev/sda1,Ebs={VolumeSize=${DISK_SIZE},VolumeType=gp2}"
fi
IAM_INST_PROFILE=''
if [ -n "${IAM_PROFILE}" ] ; then
IAM_INST_PROFILE="--iam-instance-profile Name=${IAM_PROFILE}"
fi
if [ -n "${INSTANCE_NAME}" ]; then
TAG_SPEC="--tag-specifications ResourceType=instance,Tags=[{Key=Name,Value=${INSTANCE_NAME}}]"
fi
echo -n "Launching instance... "
INSTID=$(aws ${PROFILE_ARG} --output json --region "${REGION}" ec2 run-instances \
--key-name "${KEY_NAME}" \
--instance-type "${INSTANCE_TYPE}" \
--image-id "${AMI}" ${TAG_SPEC} \
${USER_DATA} ${BDM} ${API_TERM} ${IAM_INST_PROFILE} | \
jq -r .Instances[0].InstanceId)
echo "${INSTID}"
wait_instance_running "${REGION}" "${INSTID}"
echo -n "IP address: "
aws ${PROFILE_ARG} --output json --region "${REGION}" ec2 describe-instances \
--instance-ids "${INSTID}" | \
jq -r '.Reservations[0].Instances[0].PublicIpAddress'
以上是关于sh launch_ubuntu_on_aws.sh的主要内容,如果未能解决你的问题,请参考以下文章
配置告警系统主脚本main.sh mon.sh load.sh 502.sh disk.sh