Skip to content

Commit 184a0bf

Browse files
committed
scripts/maintenance: Fix dates (tomorrow vs. today, etc.)
tag-aws.sh is using grafiti, whose tagPatterns takes jq expressions [1]. We've been using strftime since the script landed in 82bdd9f (installer/scripts: AWS tag and delete scripts, 2017-06-28, coreos/tectonic-installer#1239). jq's strftime doesn't respect your configured $TZ, but the coming jq 1.6 will add strflocaltime which does [2,3]. jq uses seconds since the epoch for date-time values [4]. You can test the new construct with: $ jq --null-input --raw-output 'now + 24*60*60 | strftime("%Y-%m-%d")' 2018-07-27 -d is not part of the POSIX date specification [5], but it (and the 'tomorrow' value) are supported by GNU Coreutils [6,7]. We've been using -d in clean-aws.sh for a while now, so this is now a new dependency. I've also dropped date_override, since we can just set date_string directly. And I've shuffled around some of the conditionals to avoid calling the 'date' and 'jq' commands needlessly when --date-override is set. I've also replaced the multiple date calls in clean-aws.sh with a single call to jq. jq was already a required dependency for this script, and only needing a single child process is much faster: $ time for i in $(seq 100); do A="$(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)]')"; done real 0m0.256s user 0m0.186s sys 0m0.077s $ time for i in $(seq 100); do A="$(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)"; done real 0m1.358s user 0m0.604s sys 0m0.832s And that's despite the fact that the old approach skipped some formats for today (e.g. %m-%-d-%-Y had been only used to format yesterday). The plethora of date formats are mostly from 3995263 (ci: add more date format when grafiti apply the cleanning, 2017-09-12, coreos/tectonic-installer#1890), although we've had some since 82bdd9f. The motivation seems to be matching human-generated tags [8], which are less reliably formatted. [1]: https://github.com/coreos/grafiti/blob/89a8bc92ad7fde49cd3dd78197c6b3616a857f36/README.md#configure-grafiti [2]: https://github.com/stedolan/jq/wiki/FAQ [3]: jqlang/jq@06f2060 [4]: https://stedolan.github.io/jq/manual/v1.5/#Dates [5]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html [6]: https://www.gnu.org/software/coreutils/manual/html_node/Options-for-date.html [7]: https://www.gnu.org/software/coreutils/manual/html_node/Relative-items-in-date-strings.html [8]: coreos/tectonic-installer#1890 (comment)
1 parent 78e6f76 commit 184a0bf

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)