Skip to content

Commit 5ab0ae5

Browse files
Merge pull request #77 from wking/maintenance-script-dates
scripts/maintenance: Fix dates (tomorrow vs. today, etc.)
2 parents c0ab37a + 184a0bf commit 5ab0ae5

File tree

3 files changed

+26
-33
lines changed

3 files changed

+26
-33
lines changed

scripts/maintenance/clean-aws.sh

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ Options:
2727
--tag-file A file containing a TagFilter list. See the AWS Resource Group
2828
Tagging API 'TagFilter' documentation for file structure.
2929
30-
--date-override (optional) Date of the format YYYY-MM-DD that overrides the
31-
default tag value of today's date. This script tags resources
32-
with 'expirationDate: some-date-string', where some-date-string
33-
is replaced with either the following days' date or date-override.
34-
Only use if --tag-file is not used.
30+
--date-override (optional) Date of the format YYYY-MM-DD to delete resources
31+
tagged with 'expirationDate: some-date-string'. By default,
32+
this script deletes resources which expired yesterday or
33+
today. Not compatible with --tag-file.
3534
3635
--dry-run (optional) If set, grafiti will only do a dry run, i.e. not
3736
delete any resources.
@@ -44,7 +43,7 @@ version=
4443
region=
4544
config_file=
4645
tag_file=
47-
date_override=
46+
date_string=
4847
dry_run=
4948

5049
while [ $# -gt 0 ]; do
@@ -73,7 +72,7 @@ while [ $# -gt 0 ]; do
7372
shift
7473
;;
7574
--date-override)
76-
date_override="${2:-}"
75+
date_string="[\"${2:-}\"]"
7776
shift
7877
;;
7978
--dry-run)
@@ -112,7 +111,7 @@ if [ -z "$version" ]; then
112111
exit 1
113112
fi
114113

115-
if [ -n "$tag_file" ] && [ -n "$date_override" ]; then
114+
if [ -n "$tag_file" ] && [ -n "$date_string" ]; then
116115
echo "Cannot use both --tag-file and --date-override flags simultaneously." >&2
117116
exit 1
118117
fi
@@ -132,16 +131,12 @@ fi
132131
if [ -n "$tag_file" ]; then
133132
cat "$tag_file" >"$tmp_dir/tag.json"
134133
else
135-
date_string="$(date "+%Y-%m-%d" -d "-1 day")\",\"$(date "+%Y-%-m-%-d" -d "-1 day")\",
136-
\"$(date "+%m-%-d-%-Y" -d "-1 day")\",\"$(date "+%-m-%-d-%-Y" -d "-1 day")\",\"$(date "+%d-%m-%-Y" -d "-1 day")\",
137-
\"$(date "+%d-%-m-%-Y" -d "-1 day")\",\"$(date +%m-%d-%Y)\",\"$(date +%d-%m-%Y)\",
138-
\"$(date +%d-%-m-%Y)\",\"$(date +%Y-%m-%d)\",\"$(date +%Y-%-m-%-d)"
139-
if [ -n "$date_override" ]; then
140-
date_string="$date_override"
134+
if [ -z "$date_string" ]; then
135+
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)]')"
141136
fi
142137

143138
cat <<EOF >"$tmp_dir/tag.json"
144-
{"TagFilters":[{"Key":"expirationDate","Values":["${date_string}"]}]}
139+
{"TagFilters":[{"Key":"expirationDate","Values":${date_string}}]}
145140
EOF
146141
fi
147142

scripts/maintenance/tag-aws.sh

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ usage() {
44
cat <<EOF
55
66
$(basename "$0") tags AWS resources with 'expirationDate: some-date-string',
7-
defaulting to the following days' date, and excludes all resources tagged with
7+
defaulting to tomorrow's date, and excludes all resources tagged with
88
tag keys/values specified in an 'exclude' file. Requires that 'docker' is
99
installed.
1010
@@ -33,9 +33,9 @@ Options:
3333
--end-hour Integer hour to end looking at CloudTrail logs. Defaults to 1.
3434
3535
--date-override (optional) Date of the format YYYY-MM-DD that overrides the
36-
default tag value of today's date. This script tags resources
36+
default tag value of tomorrow's date. This script tags resources
3737
with 'expirationDate: some-date-string', where some-date-string
38-
is replaced with either the following days' date or date-override.
38+
is replaced with either tomorrow's date or date-override.
3939
4040
--dry-run (optional) If set, grafiti will only do a dry run, i.e. not tag
4141
any resources.
@@ -48,7 +48,7 @@ version=
4848
region=
4949
config_file=
5050
exclude_file=
51-
date_override=
51+
date_string=
5252
start_hour=8
5353
end_hour=1
5454
dry_run=
@@ -87,7 +87,7 @@ while [ $# -gt 0 ]; do
8787
shift
8888
;;
8989
--date-override)
90-
date_override="${2:-}"
90+
date_string="\\\"${2:-}\\\""
9191
shift
9292
;;
9393
--dry-run)
@@ -134,14 +134,13 @@ fi
134134
set -e
135135

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

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

147146
# Configure grafiti to tag all resources created between START_HOUR and END_HOUR's

scripts/maintenance/tag-route53-hosted-zones.sh

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ Options:
1414
--force Override user input prompts. Useful for automation.
1515
1616
--date-override (optional) Date of the format YYYY-MM-DD that overrides the
17-
default tag value of today's date. This script tags resources
17+
default tag value of tomorrow's date. This script tags resources
1818
with 'expirationDate: some-date-string', where some-date-string
19-
is replaced with either the following days' date or date-override.
19+
is replaced with either tomorrow's date or date-override.
2020
2121
EOF
2222
}
2323

2424
force=
25-
date_override=
25+
date_string=
2626

2727
while [ $# -gt 0 ]; do
2828
case $1 in
@@ -34,7 +34,7 @@ while [ $# -gt 0 ]; do
3434
force=true
3535
;;
3636
--date-override)
37-
date_override="${2:-}"
37+
date_string="${2:-}"
3838
shift
3939
;;
4040
*)
@@ -53,11 +53,10 @@ fi
5353
set -e
5454

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

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

0 commit comments

Comments
 (0)