Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

imdsv2 and tags - fixes #61

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Amazon Elastic Block Store Autoscale

This is an example of a daemon process that monitors a filesystem mountpoint and automatically expands it when free space falls below a configured threshold. New [Amazon EBS](https://aws.amazon.com/ebs/) volumes are added to the instance as necessary and the underlying filesystem ([BTRFS](http://btrfs.wiki.kernel.org) or [LVM](https://en.wikipedia.org/wiki/Logical_Volume_Manager_(Linux)) with [ext4](https://en.wikipedia.org/wiki/Ext4)) expands as new devices are added.
This is an example of a daemon process that monitors a filesystem mountpoint and automatically expands it when free space falls below a configured threshold. New [Amazon EBS](https://aws.amazon.com/ebs/) volumes are added to the instance as necessary and the underlying filesystem ([BTRFS](http://btrfs.wiki.kernel.org) or [LVM](https://en.wikipedia.org/wiki/Logical_Volume_Manager_(Linux)) with [ext4](https://en.wikipedia.org/wiki/Ext4)) or [xfs](https://en.wikipedia.org/wiki/XFS)) expands as new devices are added.

## Assumptions:

Expand Down Expand Up @@ -53,9 +53,9 @@ Options
(Default: none - automatically create and attaches a volume)
If provided --initial-size is ignored.

-f, --file-system btrfs | lvm.ext4
-f, --file-system btrfs | lvm.ext4 | lvm.xfs
Filesystem to use (default: btrfs).
Options are btrfs or lvm.ext4
Options are btrfs, lvm.ext4, lvm.xfs

-h, --help
Print help and exit.
Expand Down
19 changes: 6 additions & 13 deletions bin/create-ebs-volume
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,11 @@ function create_and_attach_volume() {
local availability_zone=$(get_metadata placement/availability-zone)
local region=${availability_zone%?}

local instance_tags=""
# Render instance tags to match: --tag-specification
# Output Example:
# {Key=Name,Value=Jenkins},{Key=Owner,Value=DevOps}
instance_tags=$(
# Find all non aws: instance tags
local instance_tags=$(
aws ec2 describe-tags \
--region $region \
--filters "Name=resource-id,Values=$instance_id" | jq -r .Tags | jq -c 'map({Key, Value})' | tr -d '[]"' | sed 's/{Key:/{Key=/g ; s/,Value:/,Value=/g ; s/{Key=aws:[^}]*}//g ; s/,\{2,\}/,/g ; s/,$//g ; s/^,//g'
--filters "Name=resource-id,Values=$instance_id" | jq -rc '.Tags | map({Key,Value} | select(.Key | test("^aws:") | not))'
)

local max_attempts=10
Expand Down Expand Up @@ -269,12 +266,8 @@ function create_and_attach_volume() {
local volume=""
for i in $(eval echo "{0..$max_attempts}") ; do

# The $instance_tags variable could be empty and will cause a TagSpecifications[0].Tags[0] error if
# it is passed as an empty value because it must be comma-separated from the other key-value pairs.
# Use a Shell Parameter Expansion to determine if the variable contains a value or not. If it has a value,
# append a comma at the end so the aws cli syntax is compliant when it is subbed into the tag_specification variable.
local instance_tags=${instance_tags:+${instance_tags},}
local tag_specification="ResourceType=volume,Tags=[$instance_tags{Key=source-instance,Value=$instance_id},{Key=amazon-ebs-autoscale-creation-time,Value=$timestamp}]"
local ebs_autoscale_tags='[{"Key":"source-instance","Value":"'$instance_id'"},{"Key":"amazon-ebs-autoscale-creation-time","Value":"'$timestamp'"}]'
local tag_specifications='[{"ResourceType":"volume","Tags":'$(jq -sc 'add' <(echo "$ebs_autoscale_tags") <(echo "$instance_tags"))'}]'

# Note: Shellcheck says the $vars in this command should be double quoted to prevent globbing and word-splitting,
# but this ends up making the '--encrypted' argument to fail during the execution of the install script. Conversely, NOT putting double-quotes
Expand All @@ -285,7 +278,7 @@ function create_and_attach_volume() {
--region $region \
--availability-zone $availability_zone \
$volume_opts \
--tag-specification "$tag_specification" \
--tag-specifications "$tag_specifications" \
2> $tmpfile
)

Expand Down
13 changes: 9 additions & 4 deletions bin/ebs-autoscale
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,16 @@ add_space () {
logthis "Adding device ${DEVICE} to logical volume ${MOUNTPOINT}"
btrfs device add ${DEVICE} ${MOUNTPOINT}
btrfs balance start -m ${MOUNTPOINT}
else
logthis "Adding device ${DEVICE} to logical volume /dev/mapper/${LVM_VG}-${LVM_LV}"
elif [[ "$FILE_SYSTEM" =~ ^lvm\.(ext4|xfs)$ ]]; then
device_path="/dev/mapper/${LVM_VG}-${LVM_LV}"
logthis "Adding device ${DEVICE} to logical volume ${device_path}"
vgextend ${LVM_VG} ${DEVICE}
lvresize -l 100%VG /dev/mapper/${LVM_VG}-${LVM_LV}
resize2fs /dev/mapper/${LVM_VG}-${LVM_LV}
lvresize -l 100%VG ${device_path}
[ "${FILE_SYSTEM}" = "lvm.ext4" ] && resize2fs ${device_path}
[ "${FILE_SYSTEM}" = "lvm.xfs" ] && xfs_growfs ${device_path}
else
echo "Unknown file system type: ${FILE_SYSTEM}"
exit 1
fi
logthis "Finished extending logical volume"

Expand Down
1 change: 1 addition & 0 deletions config/ebs-autoscale.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"mountpoint": "%%MOUNTPOINT%%",
"filesystem": "%%FILESYSTEM%%",
"imdsv2": "%%IMDSV2%%",
"lvm": {
"volume_group": "autoscale_vg",
"logical_volume": "autoscale_lv"
Expand Down
40 changes: 21 additions & 19 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ Options
(Default: none - automatically create and attaches a volume)
If provided --initial-size is ignored.

-f, --file-system btrfs | lvm.ext4
-f, --file-system btrfs | lvm.ext4 | lvm.xfs
Filesystem to use (default: btrfs).
Options are btrfs or lvm.ext4
Options are btrfs, lvm.ext4, lvm.xfs

-h, --help
Print help and exit.
Expand Down Expand Up @@ -185,6 +185,21 @@ done

eval set -- "$PARAMS"

# install default config
cat ${BASEDIR}/config/ebs-autoscale.json | \
sed -e "s#%%MOUNTPOINT%%#${MOUNTPOINT}#" | \
sed -e "s#%%VOLUMETYPE%%#${VOLUMETYPE}#" | \
sed -e "s#%%VOLUMEIOPS%%#${VOLUMEIOPS}#" | \
sed -e "s#%%VOLUMETHOUGHPUT%%#${VOLUMETHOUGHPUT}#" | \
sed -e "s#%%FILESYSTEM%%#${FILE_SYSTEM}#" | \
sed -e "s#%%IMDSV2%%#${IMDSV2}#" | \
sed -e "s#%%MINEBSVOLUMESIZE%%#${MIN_EBS_VOLUME_SIZE}#" | \
sed -e "s#%%MAXEBSVOLUMESIZE%%#${MAX_EBS_VOLUME_SIZE}#" | \
sed -e "s#%%MAXLOGICALVOLUMESIZE%%#${MAX_LOGICAL_VOLUME_SIZE}#" | \
sed -e "s#%%MAXATTACHEDVOLUMES%%#${MAX_ATTACHED_VOLUMES}#" | \
sed -e "s#%%INITIALUTILIZATIONTHRESHOLD%%#${INITIAL_UTILIZATION_THRESHOLD}#" \
> /etc/ebs-autoscale.json

initialize

# for backwards compatibility evaluate positional parameters like previous 2.0.x and 2.1.x releases
Expand Down Expand Up @@ -214,20 +229,6 @@ cp ${BASEDIR}/shared/utils.sh /usr/local/amazon-ebs-autoscale/shared
# install the logrotate config
cp ${BASEDIR}/config/ebs-autoscale.logrotate /etc/logrotate.d/ebs-autoscale

# install default config
cat ${BASEDIR}/config/ebs-autoscale.json | \
sed -e "s#%%MOUNTPOINT%%#${MOUNTPOINT}#" | \
sed -e "s#%%VOLUMETYPE%%#${VOLUMETYPE}#" | \
sed -e "s#%%VOLUMEIOPS%%#${VOLUMEIOPS}#" | \
sed -e "s#%%VOLUMETHOUGHPUT%%#${VOLUMETHOUGHPUT}#" | \
sed -e "s#%%FILESYSTEM%%#${FILE_SYSTEM}#" | \
sed -e "s#%%MINEBSVOLUMESIZE%%#${MIN_EBS_VOLUME_SIZE}#" | \
sed -e "s#%%MAXEBSVOLUMESIZE%%#${MAX_EBS_VOLUME_SIZE}#" | \
sed -e "s#%%MAXLOGICALVOLUMESIZE%%#${MAX_LOGICAL_VOLUME_SIZE}#" | \
sed -e "s#%%MAXATTACHEDVOLUMES%%#${MAX_ATTACHED_VOLUMES}#" | \
sed -e "s#%%INITIALUTILIZATIONTHRESHOLD%%#${INITIAL_UTILIZATION_THRESHOLD}#" \
> /etc/ebs-autoscale.json

## Create filesystem
if [ -e $MOUNTPOINT ] && ! [ -d $MOUNTPOINT ]; then
echo "ERROR: $MOUNTPOINT exists but is not a directory."
Expand All @@ -248,15 +249,16 @@ if [ "${FILE_SYSTEM}" = "btrfs" ]; then
# add entry to fstab
# allows non-root users to mount/unmount the filesystem
echo -e "${DEVICE}\t${MOUNTPOINT}\tbtrfs\tdefaults\t0\t0" | tee -a /etc/fstab
elif [ "${FILE_SYSTEM}" = "lvm.ext4" ]; then
elif [[ "$FILE_SYSTEM" =~ ^lvm\.(ext4|xfs)$ ]]; then
TYPE=$(echo ${FILE_SYSTEM} | cut -d '.' -f 2)
VG=$(get_config_value .lvm.volume_group)
LV=$(get_config_value .lvm.logical_volume)
pvcreate $DEVICE
vgcreate $VG $DEVICE
lvcreate $VG -n $LV -l 100%VG
mkfs.ext4 /dev/mapper/${VG}-${LV}
mkfs -t ${TYPE} /dev/mapper/${VG}-${LV}
mount /dev/mapper/${VG}-${LV} $MOUNTPOINT
echo -e "/dev/mapper/${VG}-${LV}\t${MOUNTPOINT}\text4\tdefaults\t0\t0" | tee -a /etc/fstab
echo -e "/dev/mapper/${VG}-${LV}\t${MOUNTPOINT}\t${TYPE}\tdefaults\t0\t0" | tee -a /etc/fstab
else
echo "Unknown file system type: ${FILE_SYSTEM}"
exit 1
Expand Down
6 changes: 3 additions & 3 deletions shared/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ function get_metadata() {
local key=$1
local metadata_ip='169.254.169.254'

if [ ! -z "$IMDSV2" ]; then
if [ ! -z "$(get_config_value .imdsv2)" ]; then
local token=$(curl -s -X PUT "http://$metadata_ip/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 60")
local token_wrapper='-H "X-aws-ec2-metadata-token: $token"'
Copy link

@nathanthorpe nathanthorpe Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm testing this locally and it doesn't expand out properly when using single quotes.

> token_wrapper='-H "X-aws-ec2-metadata-token: $token"'
> echo $token_wrapper
-H "X-aws-ec2-metadata-token: $token"

Changing it to this works

> token_wrapper='-H "X-aws-ec2-metadata-token: '"$token"'"'
> echo $token_wrapper
-H "X-aws-ec2-metadata-token: <snip>"

The AMI I'm using is ami-0c6ee50d15e7364d4

This shows up in cloudwatch

image

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, I see that eval fixes it...

fi

echo `curl -s $token_wrapper http://$metadata_ip/latest/meta-data/$key`
eval "curl -s $token_wrapper http://$metadata_ip/latest/meta-data/$key"
}

function initialize() {
export EBS_AUTOSCALE_CONFIG_FILE=/etc/ebs-autoscale.json
export AWS_AZ=$(get_metadata placement/availability-zone)
export AWS_REGION=$(echo ${AWS_AZ} | sed -e 's/[a-z]$//')
export INSTANCE_ID=$(get_metadata instance-id)
export EBS_AUTOSCALE_CONFIG_FILE=/etc/ebs-autoscale.json
}

function detect_init_system() {
Expand Down