Skip to content

Instantly share code, notes, and snippets.

@rohantmp
Last active August 19, 2020 13:10
Show Gist options
  • Save rohantmp/3305e93573947152d67b88fcfc5508b4 to your computer and use it in GitHub Desktop.
Save rohantmp/3305e93573947152d67b88fcfc5508b4 to your computer and use it in GitHub Desktop.
utility functions for attaching aws disks to OCP nodes
#!/bin/bash
# requires
# edit these
# export AWS_PROFILE
export OWNER=rojoseph
export ZONES=${ZONES:-"us-east-2a us-east-2b us-east-2c"};
export DISK_PATTERN="10 20 30 40"
aws_disks() {
local ZONE
local ZONES=${ZONES:-"us-east-2a us-east-2b us-east-2c"};
local DISK_PATTERN=${DISK_PATTERN:-10 20 30 40 50}
for ZONE in $ZONES;
do
get_instance
for size in 10 20 30 40;
do
create_volume "${OWNER}-${size}" $size
done
sleep 30
#global counter for device names, set by attach_available. TODO: set a different one per node
unset _nc
c=1
while ! attach_available
do
c=$(( $c +1 ))
[[ $c -gt 5 ]] && return
done
done
}
# most args can also be set as env vars
create_volume ()
{
NAME=test; # first argument
[[ $# -gt 0 ]] && NAME=$1 && shift
SIZE=30; # second argument
[[ $# -gt 0 ]] && SIZE=$1 && shift
ZONE=${ZONE:-us-east-2a} # third argument
[[ $# -gt 0 ]] && ZONE=$1 && shift
VOLID=$(aws ec2 create-volume --availability-zone $ZONE --size $SIZE --tag-specifications "ResourceType=volume,Tags=[{Key=Name,Value=${NAME}-${ZONE: -1}},{Key=owner,Value=${OWNER}}]"|tee /dev/tty |jq .VolumeId|sed 's/"//g');
echo "${VOLID}"
}
get_instance ()
{
N=1;
[[ $# -gt 0 ]] && N=$1 && shift
ZONE=${ZONE:-us-east-2a}
[[ $# -gt 0 ]] && ZONE=$1 && shift
PROVIDERS=$(oc get machine -n openshift-machine-api -o wide -l machine.openshift.io/zone=${ZONE},machine.openshift.io/cluster-api-machine-type=worker -o jsonpath='{ .items[*].spec.providerID }') &&
WORID=$(echo $PROVIDERS|awk "{ print \$${N} }"|cut -d / -f 5) &&
COUNT=$(echo $PROVIDERS|wc -w) &&
export MY_NODE_NAME=$(oc get machine -n openshift-machine-api -o wide|grep $WORID|awk '{ print $7 }');
echo -e "---\nPROVIDERS: ${PROVIDERS}\n#${N}/${COUNT}: ${WORID}\nMY_NODE_NAME: ${MY_NODE_NAME}"
}
attach_available ()
{
export ZONE=${ZONE:-us-east-2a}
export _nc=${_nc:-1}; # name count (max 20)
for VOLID in $(aws ec2 describe-volumes --filters "Name=tag:owner,Values=${OWNER}" "Name=status,Values=available" "Name=availability-zone,Values=${ZONE}" |jq '.Volumes[].VolumeId'|xargs|sed -e 's/"//g');
do
aws ec2 attach-volume --volume-id $VOLID --instance-id $WORID --device /dev/sd$(echo "g h i j k l m n o p q r s t u v w x y z a b c d e f g"|awk "{ print \$$_nc}");
export _nc=$(( _nc+1 ));
done
}
get_available ()
{
aws ec2 describe-volumes --filters "Name=tag:owner,Values=${OWNER}" "Name=status,Values=available" | jq '.Volumes[].VolumeId' | xargs | sed -e 's/"//g'
}
delete_available ()
{
for i in $(aws ec2 describe-volumes --filters "Name=tag:owner,Values=${OWNER}" "Name=status,Values=available"|jq '.Volumes[].VolumeId'|sed 's/"//g');
do
aws ec2 delete-volume --volume-id $i;
done
}
get_owned ()
{
aws ec2 describe-volumes --filters "Name=tag:owner,Values=${OWNER}" | jq '.Volumes[].VolumeId' | xargs | sed -e 's/"//g'
}
attach_available_all ()
{
local ZONE
local ZONES=${ZONES:-"us-east-2a us-east-2b us-east-2c"};
for ZONE in $ZONES;
do
get_instance;
_nc=1;
attach_available;
done
}
get_all() {
aws ec2 describe-volumes --filters "Name=tag:owner,Values=${OWNER}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment