From 4d106f5df8729105023da1d9fbe126240197b314 Mon Sep 17 00:00:00 2001 From: Bryan Black Date: Mon, 21 May 2018 11:14:17 -0700 Subject: [PATCH] Try @extenda changes Trying the changes made by sclausson from extenda. https://github.com/extenda/aws-helper-scripts --- bin/i-attach-volume | 81 ++++++++++++++++++++------------------------- inc/ec2-include | 16 +++++---- 2 files changed, 46 insertions(+), 51 deletions(-) diff --git a/bin/i-attach-volume b/bin/i-attach-volume index 2dcd5ef..5de0330 100755 --- a/bin/i-attach-volume +++ b/bin/i-attach-volume @@ -6,44 +6,33 @@ if [ ! $3 ]; then echo "Usage: $(basename $0) VOLUME_ID DEVICE MOUNT_POINT" echo echo " VOLUME_ID - The Volume ID of the EBS volume you'd like to attach to this ec2 instance." - echo " DEVICE - The device location on the EC2 instance that you would like the EBS volume attached to." - echo " Can be specified as 'auto' to automatically choose an available device." - echo " MOUNT_POINT - The location you would like the EBS Volume mounted to. (E.g. /ebs)" + echo " DEVICE - The device location on the EC2 instance that you would like the EBS volume attached to. (E.g. /dev/sdf)" + echo " MOUNT_POINT - The location you would like the EBS Volume mounted to. (E.g. /mnt/ebs)" exit fi VOLUME_ID=$1 DEVICE=$2 MOUNT_POINT=$3 -FORMAT=$4 # This value is only parsed in when i-create-volume needs a drive attached. +FORMAT=ext4 #we presently only support Ext4 filesystem +INTERNAL_DEVICE_NAME=/dev/xvd$(echo "${DEVICE: -1}") + +if [ -b "$INTERNAL_DEVICE_NAME" ]; then + echo "Error: The device name you specified: $DEVICE is already in use." + echo " Please choose an available device name." + exit 1 +fi if [ -d "$MOUNT_POINT" ]; then - echo "Warning: The mount point you specified ($MOUNT_POINT) already contains a directory" + echo "Error: The mount point you specified ($MOUNT_POINT) already contains a directory" echo " please ensure an EBS volume isn't already mounted at that location by checking" echo " the output of the 'df' command. Otherwise if the directory is empty remove" echo " it and try reattaching this EBS volume." exit 1 fi -# Tag this EBS volume -echo "Adding tags to the EBS volume..." -INSTANCE_NAME=$(ec2-describe-tags --filter "resource-id=$INSTANCE_ID" --filter "key=Name" | cut -f5) -ec2-create-tags $VOLUME_ID --tag "Server=$INSTANCE_NAME" --tag "Mount point=$MOUNT_POINT" -echo - -# If the user specified auto we locate the next avaliable device location -if [ $DEVICE = "auto" ]; then - if ls /dev/xvdk* >/dev/null 2>&1; then - LAST_DEVICE_NUMBER=$(ls -1 /dev/xvdk* | sed 's/[a-z\/]//g' | sort -n | tail -1) - DEVICE_NUMBER=$(expr $LAST_DEVICE_NUMBER + 1) - else - DEVICE_NUMBER="1" - fi -fi - # Check if volume is already attached to an instance -VOLUME_ATTACHED=( $(ec2-describe-volumes $VOLUME_ID | grep "attached") ) -VOLUME_ATTACHED=${VOLUME_ATTACHED[2]} +VOLUME_ATTACHED=$(aws ec2 describe-volumes --volume-id $VOLUME_ID --query Volumes[0].Attachments[0].InstanceId --output text) # Check if volume is attached to this instance if [ "$VOLUME_ATTACHED" = "$INSTANCE_ID" ]; then @@ -51,46 +40,48 @@ if [ "$VOLUME_ATTACHED" = "$INSTANCE_ID" ]; then exit 1 # Check if volume is attached to another instance -elif [ $VOLUME_ATTACHED ]; then +elif [ ! "$VOLUME_ATTACHED" = "None" ]; then echo "Error: Volume is already attached to $VOLUME_ATTACHED" exit 1 # Attach volume else echo "Attaching EBS Volume..." - ec2-attach-volume $VOLUME_ID --instance $INSTANCE_ID --device /dev/sdk$DEVICE_NUMBER + aws ec2 attach-volume --volume-id $VOLUME_ID --instance-id $INSTANCE_ID --device $DEVICE echo # Wait for the volume to be attached before we format and mount it - while [ ! -e /dev/xvdk$DEVICE_NUMBER ]; do sleep 1; done + while [ ! -e $INTERNAL_DEVICE_NAME ]; do sleep 1; done fi -if [ "$FORMAT" == "true" ]; then - echo "Formatting the device with an Ext4 filesystem..." - mkfs.ext4 /dev/xvdk$DEVICE_NUMBER +file -s $INTERNAL_DEVICE_NAME |grep $FORMAT >/dev/null 2>&1 +if [ $? -eq 0 ]; then + echo "Volume already formatted with $FORMAT filesystem..." +else + echo "Formatting the device with an $FORMAT filesystem..." + mkfs -t $FORMAT $INTERNAL_DEVICE_NAME echo fi -# Remove the EBS Volume from /etc/fstab +# Check /etc/fstab for existin mount point entry FSTAB_CONTENT=$(grep " $MOUNT_POINT " /etc/fstab); if [ "$FSTAB_CONTENT" ]; then - echo "Found an old entry for this device in '/etc/fstab'. Removing:" + echo "Entry already exists in /etc/fstab for $MOUNT_POINT:" echo "$FSTAB_CONTENT" - echo - mv /etc/fstab /etc/fstab.bak - grep -v " $MOUNT_POINT " /etc/fstab.bak > /etc/fstab +else + echo "Adding mount point '$MOUNT_POINT' to '/etc/fstab'..." + echo "$INTERNAL_DEVICE_NAME $MOUNT_POINT ext4 noatime 0 0" >> /etc/fstab fi -# Mount the EBS Volume persistantly -echo "Adding mount point '$MOUNT_POINT' to '/etc/fstab'..." -echo "/dev/xvdk$DEVICE_NUMBER $MOUNT_POINT ext4 noatime 0 0" >> /etc/fstab -echo +#TODO: Add the ability to automate Volume Snapshots +# # Add cron entry to perform EBS Snapshots of this Volume +# echo "Adding cron entry for ec2-consistent-snapshot to '/etc/cron.d/snapshot-ebs'..." +# echo "42 * * * * root bash -c '((i-snapshot-volume $MOUNT_POINT mysql true \"automated backup\" 2>&1 1>&3 | tee >(logger -p cron.err -t \"i-snapshot-volume $MOUNT_POINT\")) 3>&1 1>&2) > >(logger -p cron.info -t \"i-snapshot-volume $MOUNT_POINT\") 2>&1'" >> /etc/cron.d/snapshot-ebs +# echo -# Add cron entry to perform EBS Snapshots of this Volume -echo "Adding cron entry for ec2-consistent-snapshot to '/etc/cron.d/snapshot-ebs'..." -echo "42 * * * * root bash -c '((i-snapshot-volume $MOUNT_POINT mysql true \"automated backup\" 2>&1 1>&3 | tee >(logger -p cron.err -t \"i-snapshot-volume $MOUNT_POINT\")) 3>&1 1>&2) > >(logger -p cron.info -t \"i-snapshot-volume $MOUNT_POINT\") 2>&1'" >> /etc/cron.d/snapshot-ebs -echo - -echo "Creating mount point and mounting device..." -if [ ! -d "$MOUNT_POINT" ]; then mkdir $MOUNT_POINT; fi +if [ ! -d "$MOUNT_POINT" ]; then + echo "Creating mount point $MOUNT_POINT" + mkdir -p $MOUNT_POINT +fi +echo "Mounting $MOUNT_POINT" mount $MOUNT_POINT diff --git a/inc/ec2-include b/inc/ec2-include index 63459a3..ecfa464 100755 --- a/inc/ec2-include +++ b/inc/ec2-include @@ -1,17 +1,21 @@ # Exit the script if any command returns an error #set -e -# Check that Amazon EC2 API Tools are installed. -if ! type ec2-version >/dev/null 2>&1; then - echo "Error: Cannot find Amazon EC2 API Tools please install from http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip" - echo " and ensure the directory is in the servers path variable." +# Check that Amazon AWS CLI is installed. +aws --version >/dev/null 2>&1 +if [ ! $? -eq 0 ] ; then + echo "Error: Cannot find Amazon AWS CLI please see http://docs.aws.amazon.com/cli/latest/userguide/installing.html for installation information" echo exit 1 fi -CURL_META_DATA="curl --retry 3 --silent --show-error --fail -L http://169.254.169.254/2011-01-01" +CURL_META_DATA="curl --retry 3 --silent --show-error --fail -L http://169.254.169.254/latest" INSTANCE_ID=$($CURL_META_DATA/meta-data/instance-id) +REGION=$($CURL_META_DATA/dynamic/instance-identity/document|grep region|awk -F\" '{print $4}') AVAILABILITY_ZONE=$($CURL_META_DATA/meta-data/placement/availability-zone) -IP=$($CURL_META_DATA/meta-data/public-ipv4) +PUBLIC_IP=$($CURL_META_DATA/meta-data/public-ipv4) +PRIVATE_IP=$($CURL_META_DATA/meta-data/local-ipv4) +echo "Setting AWS CLI Region to $REGION" +$(aws configure set region $REGION)