#' Prune app binary tarballs on AWS S3
#'
#' @export
#' @note Updated 2023-01-31.
#'
#' @param dryRun `logical(1)`.
#' Perform a dry run. Just return the S3 URLs, but don't delete anything.
#'
#' @examples
#' out <- pruneAppBinaries(dryRun = TRUE)
#' print(out)
pruneAppBinaries <- function(dryRun = FALSE) {
assert(
requireNamespaces("jsonlite"),
isFlag(dryRun)
)
dict <- c(
"bucket" = "private.koopa.acidgenomics.com",
"profile" = "acidgenomics",
"subdir" = "binaries"
)
alert(sprintf(
"Pruning binaries in {.url %s}.",
paste0("s3://", dict[["bucket"]], "/", dict[["subdir"]], "/")
))
x <- shell(
command = "aws",
args = c(
"--profile", dict[["profile"]],
"s3api", "list-objects",
"--bucket", dict[["bucket"]],
"--output", "json"
),
print = FALSE
)
x <- x[["stdout"]]
json <- jsonlite::fromJSON(x)
assert(
is.list(json),
isSubset("Contents", names(json))
)
df <- json[["Contents"]][, c("Key", "LastModified")]
assert(is.data.frame(df))
colnames(df) <- c("key", "lastModified")
keep <- grepl(pattern = paste0("^", dict[["subdir"]], "/"), x = df[["key"]])
df <- df[keep, , drop = FALSE]
dupes <- unique(dirname(df[["key"]])[duplicated(dirname(df[["key"]]))])
if (!hasLength(dupes)) {
alertWarning("No binaries to prune.")
return(invisible(character()))
}
keep <- dirname(df[, "key"]) %in% dupes
df <- df[keep, ]
ts <- df[["lastModified"]]
ts <- sub(pattern = "T", replacement = " ", x = ts, fixed = TRUE)
ts <- as.POSIXlt(ts)
df[["lastModified"]] <- ts
df[["dirname"]] <- dirname(df[["key"]])
df <- df[order(df[["lastModified"]], decreasing = TRUE), ]
spl <- split(x = df, f = df[["dirname"]])
pruneKeys <- lapply(
X = spl,
FUN = function(x) {
x[["key"]][seq(from = 2L, to = nrow(x))]
}
)
pruneKeys <- unlist(pruneKeys, recursive = FALSE, use.names = FALSE)
prunePaths <- paste0("s3://", dict[["bucket"]], "/", pruneKeys)
if (isTRUE(dryRun)) {
alertInfo("Dry run mode.")
cat(prunePaths, sep = "\n")
return(invisible(prunePaths))
}
invisible(lapply(
X = prunePaths,
FUN = function(path) {
shell(
command = "aws",
args = append(
x = c(
"--profile", dict[["profile"]],
"s3", "rm"
),
values = path
),
print = TRUE
)
}
))
alertSuccess(sprintf(
"Pruning of binaries in {.url %s} was successful.",
paste0("s3://", dict[["bucket"]], "/", dict[["subdir"]], "/")
))
invisible(prunePaths)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.