Skip to content
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Imports:
Suggests:
covr,
paws.common,
renv,
testthat (>= 3.0.0),
withr
Config/testthat/edition: 3
Expand Down
54 changes: 45 additions & 9 deletions R/lambda-util.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ aws_connect <- function(service) {
install_deps_line <- function(deps) {
checkmate::assert_character(deps)

repo <- single_quote(
"https://packagemanager.posit.co/cran/__linux__/rhel9/latest"
)
glued <- glue_collapse(single_quote(deps), sep = ", ")
glue('RUN Rscript -e "install.packages(c({glued}), repos = {repo})"')
glue(
'RUN Rscript -e "install.packages(c({glued}), repos = Sys.getenv(\'R_REPO\'))"'
)
}

#' system_deps_line
Expand All @@ -76,12 +75,46 @@ support_line <- function(support) {
}

#' renv_line
#'
#' Restores from the lockfile's repositories, except that CRAN and the CRAN
#' repositories of Posit Package Manager are replaced with `R_REPO`, which has
#' binaries for the image's OS.
#' @param renvlock_path path of the lockfile in the image's build folder
#' @param git logical, whether to install git, which renv needs for packages
#' from a git remote
#' @noRd
renv_line <- function(renvlock_path) {
renv_line <- function(renvlock_path, git = FALSE) {
checkmate::assert_character(renvlock_path)
cmd1 <- glue("COPY {renvlock_path} renv.lock")
cmd2 <- "RUN Rscript -e 'renv::restore()'"
glue_collapse(c(cmd1, cmd2), sep = "\n")
checkmate::assert_flag(git)
restore <- paste(
'repos <- unlist(renv::lockfile_read("renv.lock")$R$Repositories);',
'cran <- names(repos) == "CRAN" |',
'grepl("(packagemanager[.]posit[.]co|p3m[.]dev)/cran/", repos);',
'repos[cran] <- Sys.getenv("R_REPO");',
'if (!any(cran)) repos <- c(CRAN = Sys.getenv("R_REPO"), repos);',
"renv::restore(repos = repos)"
)
glue_collapse(
c(
if (git) "RUN dnf -y install git && dnf clean all",
glue("COPY {renvlock_path} renv.lock"),
glue("RUN Rscript -e '{restore}'")
),
sep = "\n"
)
}

#' Whether an renv lockfile has packages from a git remote
#' @param renvlock_path path to the lockfile
#' @noRd
lockfile_has_git <- function(renvlock_path) {
packages <- jsonlite::fromJSON(renvlock_path, simplifyVector = FALSE)$Packages
# renv treats the xgit and git2r remote types as git too.
is_git <- function(x) {
identical(x$Source, "git") ||
isTRUE(x$RemoteType %in% c("git", "xgit", "git2r"))
}
any(vapply(packages, is_git, TRUE))
}

#' parse password from ecr token
Expand Down Expand Up @@ -314,7 +347,10 @@ create_lambda_dockerfile <-

if (!is.null(renvlock_path)) {
file.copy(renvlock_path, file.path(folder, "renv.lock"))
renv_string <- renv_line("renv.lock")
renv_string <- renv_line(
"renv.lock",
git = lockfile_has_git(renvlock_path)
)
write(renv_string, file = file.path(folder, "Dockerfile"), append = TRUE)
}

Expand Down
6 changes: 3 additions & 3 deletions inst/lambdr_dockerfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ RUN rpm_file=R-${R_VERSION}-1-1.$(uname -m).rpm \
&& dnf clean all

ENV PATH="${PATH}:/opt/R/${R_VERSION}/bin/"
# Install binary packages, also when restoring an renv.lock.
ENV RENV_CONFIG_REPOS_OVERRIDE="https://packagemanager.posit.co/cran/__linux__/rhel9/latest"
# Posit Package Manager's binary packages for RHEL 9.
ENV R_REPO="https://packagemanager.posit.co/cran/__linux__/rhel9/latest"

RUN Rscript -e 'install.packages(c("lambdr", "renv"), repos = "https://packagemanager.posit.co/cran/__linux__/rhel9/latest")'
RUN Rscript -e 'install.packages(c("lambdr", "renv"), repos = Sys.getenv("R_REPO"))'

RUN printf '#!/bin/sh\ncd /lambda\nRscript runtime.R' > /var/runtime/bootstrap \
&& chmod +x /var/runtime/bootstrap
Expand Down
95 changes: 87 additions & 8 deletions tests/testthat/test-lambda-util.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,94 @@ test_that("runtime_line works with correct input", {
test_that("renv_line works", {
expect_error(renv_line(renvlock = 1))

renvlock <- renv_line("renv.lock")
test <- glue::glue_collapse(
c(
"COPY renv.lock renv.lock",
"RUN Rscript -e 'renv::restore()'"
),
sep = "\n"
lines <- strsplit(renv_line("renv.lock"), "\n")[[1]]
expect_equal(lines[[1]], "COPY renv.lock renv.lock")
expect_match(lines[[2]], "^RUN Rscript -e '[^']+'$")

lines <- strsplit(renv_line("renv.lock", git = TRUE), "\n")[[1]]
expect_equal(lines[[1]], "RUN dnf -y install git && dnf clean all")
})

test_that("lockfile_has_git finds packages from a git remote", {
lockfile <- withr::local_tempfile(fileext = ".lock")
write_lock <- function(packages) {
jsonlite::write_json(
list(R = list(Version = "4.6.1"), Packages = packages),
lockfile,
auto_unbox = TRUE
)
}
write_lock(list(glue = list(Package = "glue", Source = "Repository")))
expect_false(lockfile_has_git(lockfile))
write_lock(list(
glue = list(Package = "glue", Source = "Repository"),
mypkg = list(Package = "mypkg", RemoteType = "git")
))
expect_true(lockfile_has_git(lockfile))
write_lock(list(mypkg = list(Package = "mypkg", RemoteType = "xgit")))
expect_true(lockfile_has_git(lockfile))
write_lock(list(mypkg = list(Package = "mypkg", Source = "git")))
expect_true(lockfile_has_git(lockfile))
write_lock(list())
expect_false(lockfile_has_git(lockfile))
})

test_that("renv_line restores from the lockfile repos with CRAN replaced", {
skip_if_not_installed("renv")
restore_code <- function() {
line <- strsplit(renv_line("renv.lock"), "\n")[[1]][[2]]
parse(text = sub("^RUN Rscript -e '(.*)'$", "\\1", line))
}
restore_repos <- function(repositories) {
dir <- withr::local_tempdir()
lockfile <- list(R = list(Version = "4.6.1"), Packages = list())
lockfile$R$Repositories <- repositories
jsonlite::write_json(
lockfile,
file.path(dir, "renv.lock"),
auto_unbox = TRUE
)
withr::local_dir(dir)
withr::local_envvar(R_REPO = "https://ppm/rhel9")
used <- NULL
local_mocked_bindings(
restore = function(repos) {
used <<- repos
},
.package = "renv"
)
eval(restore_code(), envir = new.env())
used
}

expect_equal(
restore_repos(list(
list(Name = "CRAN", URL = "https://cloud.r-project.org"),
list(Name = "ru", URL = "https://x.r-universe.dev")
)),
c(CRAN = "https://ppm/rhel9", ru = "https://x.r-universe.dev")
)
expect_equal(
restore_repos(list(
list(
Name = "RSPM",
URL = "https://packagemanager.posit.co/cran/__linux__/jammy/latest"
)
)),
c(RSPM = "https://ppm/rhel9")
)
bioc <- "https://packagemanager.posit.co/bioconductor/packages/3.19/bioc"
expect_equal(
restore_repos(list(
list(Name = "CRAN", URL = "https://cloud.r-project.org"),
list(Name = "BioCsoft", URL = bioc)
)),
c(CRAN = "https://ppm/rhel9", BioCsoft = bioc)
)
expect_equal(
restore_repos(list(list(Name = "ru", URL = "https://x.r-universe.dev"))),
c(CRAN = "https://ppm/rhel9", ru = "https://x.r-universe.dev")
)
expect_equal(test, renvlock)
})

#####
Expand Down
Loading