komazarari
10/24/2016 - 1:21 AM

jp_spot_price.bash

#!/bin/bash

if uname | grep Darwin > /dev/null; then
    BSD=true
else
    BSD=false
fi

if $BSD; then
    DATE_B2D=$(date -u -v-2d +"%Y-%m-%dT00:00:00")
    DATE_B1D=$(date -u -v-1d +"%Y-%m-%dT00:00:00")
    SHA1="openssl sha1"
else
    DATE_B2D=$(date -u -d "2 day ago" +%Y-%m-%dT00:00:00)
    DATE_B1D=$(date -u -d "1 day ago" +%Y-%m-%dT00:00:00)
    SHA1="sha1sum"
fi

describe_regions() {
    aws ec2  describe-regions --region ap-northeast-1 --query 'Regions[].RegionName' --output text | sed -e 's/[[:blank:]]/,/g' | tr ',' '\n'
}

describe_spot_price_history() {
    local region=$1

#    for az in a b c d; do
    for az in a c; do
        local history=$(aws ec2  describe-spot-price-history --region $region \
            --start-time $DATE_B2D --end-time $DATE_B1D \
            --availability-zone $region$az --output text 2> /dev/null \
                               | sed -e "s/$region$az//" | sort)
        if [[ -n $history ]]; then
            echo "$region$az $($SHA1<<<"$history")"
        fi
    done
}

#describe_regions | while read r
echo ap-northeast-1 | while read r
do
    describe_spot_price_history $r
done