R/isDockerBuildRecent.R

Defines functions isDockerBuildRecent

Documented in isDockerBuildRecent

#' Is the Docker build recent?
#'
#' @export
#' @note Updated 2023-03-16.
#'
#' @param image `character`.
#' Docker image name (e.g. `repo/image`).
#' Parameterized, supporting multiple image checks in a single call.
#'
#' @param days `integer(1)`.
#' Maximum number of days to consider build recent.
#'
#' @return `logical`.
#'
#' @examples
#' ## > image <- "public.ecr.aws/acidgenomics/koopa:debian"
#' ## > isDockerBuildRecent(image)
isDockerBuildRecent <- function(image, days = 2L) {
    assert(
        isDockerEnabled(),
        isCharacter(image),
        isNumber(days)
    )
    bapply(
        X = image,
        FUN = function(image) {
            ok <- tryCatch(
                expr = {
                    shell(
                        command = "docker",
                        args = c("pull", image),
                        print = FALSE
                    )
                    TRUE
                },
                error = function(e) {
                    FALSE
                }
            )
            if (isFALSE(ok)) {
                return(FALSE)
            }
            x <- tryCatch(
                expr = {
                    shell(
                        command = "docker",
                        args = c(
                            "inspect",
                            "--format='{{json .Created}}'",
                            image
                        ),
                        print = FALSE
                    )
                },
                error = function(e) {
                    FALSE
                }
            )
            if (isFALSE(x)) {
                return(FALSE)
            }
            assert(
                is.list(x),
                isSubset("stdout", names(x))
            )
            x <- x[["stdout"]]
            x <- sub(pattern = "\n$", replacement = "", x = x)
            ## e.g. "'\"2021-07-12T16:19:01.734591265Z\"'".
            x <- sub(pattern = "^'\"(.+)\"'$", replacement = "\\1", x = x)
            ## e.g. "2021-07-12T16:19:01.734591265Z".
            x <- sub(pattern = "\\.[0-9]+Z$", replacement = "", x = x)
            ## e.g. "2021-07-12T16:19:01".
            diffDays <- difftime(
                time1 = Sys.time(),
                time2 = as.POSIXct(x, format = "%Y-%m-%dT%H:%M:%S"),
                units = "days"
            )
            diffDays < days
        },
        USE.NAMES = TRUE
    )
}
acidgenomics/r-koopa documentation built on Oct. 31, 2023, 9:21 a.m.