Skip to content
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

fix cron math #95

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
2 changes: 1 addition & 1 deletion features/src/utils/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "devcontainer-utils",
"id": "utils",
"version": "23.8.5",
"version": "23.8.7",
"description": "A feature to install RAPIDS devcontainer utility scripts",
"containerEnv": {
"BASH_ENV": "/etc/bash.bash_env"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#! /usr/bin/env bash

find ~/ -maxdepth 1 -exec bash -c '\
[ $(stat --format "%U:%G" "{}") != "$(id -u):$(id -g)" ] \
&& sudo chown -R $(id -u):$(id -g) {}' \;
find ~/ -maxdepth 1 -exec bash -c "\
[ \$(stat --format '%U:%G' '{}') != $(id -u):$(id -g) ] \
&& sudo chown -R $(id -u):$(id -g) {}" \;

. devcontainer-utils-init-git;
. devcontainer-utils-vault-s3-init;
30 changes: 18 additions & 12 deletions features/src/utils/opt/devcontainer/bin/vault/s3/creds/schedule.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,29 @@ schedule_s3_creds_refresh() {
set -euo pipefail;

local now="$(date '+%s')";
local ttl="${VAULT_S3_TTL:-"43200s"}";
local ttl="${VAULT_S3_TTL:-"43200"}";
ttl="${ttl%s}";

local stamp="$(cat ~/.aws/stamp || echo "${now}")";
local then="$((ttl - (now - stamp)))";
then="$((then < ttl ? ttl : then))";
then="$((((then + 59) / 60) * 60))";
then="$((now + then))";

crontab -u $(whoami) -r 2>/dev/null || true;

if [ -f ~/.aws/stamp ]; then
local stamp="$(cat ~/.aws/stamp)";
if [ $((now - stamp)) -le ${ttl} ]; then
local when="$((now + (ttl - (now - stamp)) - 1))";
local expr="$(date --date="@${when}" '+%M %H %d %m %w')";
cat <<________EOF | crontab -u $(whoami) -
cat <<____EOF | crontab -u $(whoami) -
SHELL=/bin/bash
BASH_ENV=/etc/bash.bash_env
${expr} source \$BASH_ENV && (eval devcontainer-utils-vault-s3-creds-{generate\ \&\&,schedule}) 2>&1 | tee -a /var/log/devcontainer-utils-vault-s3-creds-refresh.log
________EOF
fi
fi
BASH_ENV="${BASH_ENV:-}"
VAULT_HOST="${VAULT_HOST:-}"
GITHUB_USER="${GITHUB_USER:-}"
SCCACHE_BUCKET="${SCCACHE_BUCKET:-}"
SCCACHE_REGION="${SCCACHE_REGION:-}"
VAULT_GITHUB_ORGS="${VAULT_GITHUB_ORGS:-}"
AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION:-}"
$(date --date="@${then}" '+%M %H %d %m %w') \
bash -lc 'devcontainer-utils-vault-s3-creds-generate && devcontainer-utils-vault-s3-creds-schedule' 2>&1 | tee -a /var/log/devcontainer-utils-vault-s3-creds-refresh.log
____EOF
}

if test -n "${devcontainer_utils_debug:-}"; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_aws_creds() {
if test -f ~/.aws/stamp; then
local now="$(date '+%s')";
local stamp="$(cat ~/.aws/stamp)";
local ttl="${VAULT_S3_TTL:-"43200s"}";
local ttl="${VAULT_S3_TTL:-"43200"}";
if [ $((now - stamp)) -ge "${ttl%s}" ]; then
exit 1;
fi
Expand Down