Skip to content

scripts/maintenance: Fix dates (tomorrow vs. today, etc.) #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
25 changes: 10 additions & 15 deletions scripts/maintenance/clean-aws.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ Options:
--tag-file A file containing a TagFilter list. See the AWS Resource Group
Tagging API 'TagFilter' documentation for file structure.

--date-override (optional) Date of the format YYYY-MM-DD that overrides the
default tag value of today's date. This script tags resources
with 'expirationDate: some-date-string', where some-date-string
is replaced with either the following days' date or date-override.
Only use if --tag-file is not used.
--date-override (optional) Date of the format YYYY-MM-DD to delete resources
tagged with 'expirationDate: some-date-string'. By default,
this script deletes resources which expired yesterday or
today. Not compatible with --tag-file.

--dry-run (optional) If set, grafiti will only do a dry run, i.e. not
delete any resources.
Expand All @@ -44,7 +43,7 @@ version=
region=
config_file=
tag_file=
date_override=
date_string=
dry_run=

while [ $# -gt 0 ]; do
Expand Down Expand Up @@ -73,7 +72,7 @@ while [ $# -gt 0 ]; do
shift
;;
--date-override)
date_override="${2:-}"
date_string="[\"${2:-}\"]"
shift
;;
--dry-run)
Expand Down Expand Up @@ -112,7 +111,7 @@ if [ -z "$version" ]; then
exit 1
fi

if [ -n "$tag_file" ] && [ -n "$date_override" ]; then
if [ -n "$tag_file" ] && [ -n "$date_string" ]; then
echo "Cannot use both --tag-file and --date-override flags simultaneously." >&2
exit 1
fi
Expand All @@ -132,16 +131,12 @@ fi
if [ -n "$tag_file" ]; then
cat "$tag_file" >"$tmp_dir/tag.json"
else
date_string="$(date "+%Y-%m-%d" -d "-1 day")\",\"$(date "+%Y-%-m-%-d" -d "-1 day")\",
\"$(date "+%m-%-d-%-Y" -d "-1 day")\",\"$(date "+%-m-%-d-%-Y" -d "-1 day")\",\"$(date "+%d-%m-%-Y" -d "-1 day")\",
\"$(date "+%d-%-m-%-Y" -d "-1 day")\",\"$(date +%m-%d-%Y)\",\"$(date +%d-%m-%Y)\",
\"$(date +%d-%-m-%Y)\",\"$(date +%Y-%m-%d)\",\"$(date +%Y-%-m-%-d)"
if [ -n "$date_override" ]; then
date_string="$date_override"
if [ -z "$date_string" ]; then
date_string="$(jq --null-input '[["%Y-%m-%d", "%Y-%-m-%-d", "%m-%d-%Y", "%m-%-d-%-Y", "%-m-%-d-%-Y", "%d-%m-%Y", "%d-%-m-%-Y"][] | . as $format | [now, now - 24*60*60][] | strftime($format)]')"
fi

cat <<EOF >"$tmp_dir/tag.json"
{"TagFilters":[{"Key":"expirationDate","Values":["${date_string}"]}]}
{"TagFilters":[{"Key":"expirationDate","Values":${date_string}}]}
EOF
fi

Expand Down
17 changes: 8 additions & 9 deletions scripts/maintenance/tag-aws.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ usage() {
cat <<EOF

$(basename "$0") tags AWS resources with 'expirationDate: some-date-string',
defaulting to the following days' date, and excludes all resources tagged with
defaulting to tomorrow's date, and excludes all resources tagged with
tag keys/values specified in an 'exclude' file. Requires that 'docker' is
installed.

Expand Down Expand Up @@ -33,9 +33,9 @@ Options:
--end-hour Integer hour to end looking at CloudTrail logs. Defaults to 1.

--date-override (optional) Date of the format YYYY-MM-DD that overrides the
default tag value of today's date. This script tags resources
default tag value of tomorrow's date. This script tags resources
with 'expirationDate: some-date-string', where some-date-string
is replaced with either the following days' date or date-override.
is replaced with either tomorrow's date or date-override.

--dry-run (optional) If set, grafiti will only do a dry run, i.e. not tag
any resources.
Expand All @@ -48,7 +48,7 @@ version=
region=
config_file=
exclude_file=
date_override=
date_string=
start_hour=8
end_hour=1
dry_run=
Expand Down Expand Up @@ -87,7 +87,7 @@ while [ $# -gt 0 ]; do
shift
;;
--date-override)
date_override="${2:-}"
date_string="\\\"${2:-}\\\""
shift
;;
--dry-run)
Expand Down Expand Up @@ -134,14 +134,13 @@ fi
set -e

# Tag all resources present in CloudTrail over the specified time period with the
# following day's date as default, or with the DATE_VALUE_OVERRIDE value.
# today's date as default, or with the --date-override value.
# Format YYYY-MM-DD.
tmp_dir="$(readlink -m "$(mktemp -d tag-aws-XXXXXXXXXX)")"
trap 'rm -rf "$tmp_dir"; exit' EXIT

date_string='now|strftime(\"%Y-%m-%d\")'
if [ -n "$date_override" ]; then
date_string='\"'"${date_override}"'\"'
if [ -z "$date_string" ]; then
date_string='(now + 24*60*60|strftime(\"%Y-%m-%d\"))'
fi

# Configure grafiti to tag all resources created between START_HOUR and END_HOUR's
Expand Down
17 changes: 8 additions & 9 deletions scripts/maintenance/tag-route53-hosted-zones.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ Options:
--force Override user input prompts. Useful for automation.

--date-override (optional) Date of the format YYYY-MM-DD that overrides the
default tag value of today's date. This script tags resources
default tag value of tomorrow's date. This script tags resources
with 'expirationDate: some-date-string', where some-date-string
is replaced with either the following days' date or date-override.
is replaced with either tomorrow's date or date-override.

EOF
}

force=
date_override=
date_string=

while [ $# -gt 0 ]; do
case $1 in
Expand All @@ -34,7 +34,7 @@ while [ $# -gt 0 ]; do
force=true
;;
--date-override)
date_override="${2:-}"
date_string="${2:-}"
shift
;;
*)
Expand All @@ -53,11 +53,10 @@ fi
set -e

# Tag all Route53 hosted zones that do not already have a tag with the same keys,
# in this case 'expirationDate', with today's date as default, or
# with the DATE_VALUE_OVERRIDE value. Format YYYY-MM-DD.
date_string="$(date "+%Y-%m-%d")"
if [ -n "$date_override" ]; then
date_string="${date_override}"
# in this case 'expirationDate', with tomorrow's date as default, or
# with the --date-override value. Format YYYY-MM-DD.
if [ -z "$date_string" ]; then
date_string="$(date -d tomorrow '+%Y-%m-%d')"
fi

tags="[{\"Key\":\"expirationDate\",\"Value\":\"$date_string\"}]"
Expand Down