Skip to content

Commit

Permalink
Merge pull request #28 from discindo/feat/no-ref/renv
Browse files Browse the repository at this point in the history
Feat/no ref/renv
  • Loading branch information
teofiln authored Oct 17, 2024
2 parents c721e16 + ac5b0be commit 8ebfdbf
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 80 deletions.
13 changes: 11 additions & 2 deletions R/lambda-deploy.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
#' @param tag A name for the Docker container and Lambda function
#' @param runtime_function name of the runtime function
#' @param runtime_path path to the script containing the runtime function
#' @param dependencies list of dependencies
#' @param renvlock_path path to the renv.lock file (if any)
#' @param dependencies list of dependencies (if any)
#'
#' @details Use either `renvlock_path` or `dependencies` to install required
#' packages, not both. By default, both are `NULL`, so the Docker image will
#' have no additional packages installed.
#'
#' @importFrom glue glue glue_collapse single_quote double_quote
#' @export
build_lambda <- function(tag, runtime_function, runtime_path, dependencies) {
build_lambda <- function(
tag, runtime_function, runtime_path,
renvlock_path = NULL, dependencies = NULL) {

logger::log_info("[build_lambda] Checking system dependencies.")
check_system_dependencies()
Expand All @@ -22,6 +30,7 @@ build_lambda <- function(tag, runtime_function, runtime_path, dependencies) {
folder = folder,
runtime_function = runtime_function,
runtime_path = runtime_path,
renvlock_path = renvlock_path,
dependencies = dependencies
)
},
Expand Down
51 changes: 49 additions & 2 deletions R/lambda-util.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ runtime_line <- function(runtime) {
glue("CMD [{rt}]")
}

#' renv_line
#' @noRd
renv_line <- function(renvlock_path) {
checkmate::assert_character(renvlock_path)
cmd1 <- glue("COPY {renvlock_path} renv.lock")
cmd2 <- glue("RUN Rscript -e 'renv::restore()'")
glue_collapse(c(cmd1, cmd2), sep = "\n")
}

#' parse password from ecr token
#' @noRd
parse_password <- function(ecr_token) {
Expand All @@ -85,7 +94,12 @@ parse_password <- function(ecr_token) {
#' @param folder path to store the Dockerfile
#' @param runtime_function name of the runtime function
#' @param runtime_path path to the script containing the runtime function
#' @param dependencies list of dependencies
#' @param renvlock_path path to the renv.lock file (if any)
#' @param dependencies list of dependencies (if any)
#'
#' @details Use either `renvlock_path` or `dependencies` to install required
#' packages, not both. By default, both are `NULL`, so the Docker image will
#' have no additional packages installed.
#'
#' @examples
#' \dontrun{
Expand All @@ -110,7 +124,8 @@ create_lambda_dockerfile <-
function(folder,
runtime_function,
runtime_path,
dependencies) {
renvlock_path = NULL,
dependencies = NULL) {
logger::log_debug("[create_lambda_dockerfile] Validating inputs.")

checkmate::assert_character(
Expand Down Expand Up @@ -155,6 +170,29 @@ create_lambda_dockerfile <-
null.ok = TRUE
)

checkmate::assert_character(
x = renvlock_path,
min.chars = 1,
null.ok = TRUE
)

if (!is.null(renvlock_path)) {
if (!checkmate::test_file_exists(renvlock_path)) {
msg <- glue(
"[create_lambda_dockerfile] Can't access renv.lock file {renvlock_path}." # nolint
)
logger::log_error(msg)
rlang::abort(msg)
}
}

# Can't use both dependencies and renvlock
if (!is.null(renvlock_path) && !is.null(dependencies)) {
msg <- "[create_lambda_dockerfile] Can't use both dependencies and renv."
logger::log_error(msg)
rlang::abort(msg)
}

logger::log_debug(
"[create_lambda_dockerfile] Creating directory with Dockerfile and runtime script." # nolint
)
Expand Down Expand Up @@ -191,6 +229,15 @@ create_lambda_dockerfile <-
)
}

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

runtime_string <- runtime_line(runtime_function)

write(runtime_string,
Expand Down
2 changes: 2 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ on setting credentials, region, profile, etc.
```{r, eval = FALSE}
runtime_function <- "parity"
runtime_path <- system.file("parity.R", package = "r2lambda")
renvlock_path <- system.file("renv.lock", package = "r2lambda")
dependencies <- NULL
# Might take a while, its building a docker image
build_lambda(
tag = "parity1",
runtime_function = runtime_function,
runtime_path = runtime_path,
renvlock_path = renvlock_path,
dependencies = dependencies
)
```
Expand Down
10 changes: 4 additions & 6 deletions inst/lambdr_dockerfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ ENV PATH="${PATH}:/opt/R/${R_VERSION}/bin/"
# System requirements for R packages
RUN yum -y install openssl-devel

RUN Rscript -e "install.packages(c('httr', 'jsonlite', 'logger', 'remotes'), repos = 'https://packagemanager.rstudio.com/all/__linux__/centos7/latest')"
RUN Rscript -e "remotes::install_github('mdneuzerling/lambdr')"

RUN mkdir /lambda
COPY runtime.R /lambda
RUN chmod 755 -R /lambda
RUN R -e 'install.packages(c("remotes", "lambdr", "renv"), repos = "https://packagemanager.rstudio.com/all/__linux__/centos7/latest")'

RUN printf '#!/bin/sh\ncd /lambda\nRscript runtime.R' > /var/runtime/bootstrap \
&& chmod +x /var/runtime/bootstrap

WORKDIR /lambda
COPY runtime.R runtime.R
RUN chmod 755 -R /lambda
127 changes: 127 additions & 0 deletions inst/renv.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"R": {
"Version": "4.4.1",
"Repositories": [
{
"Name": "CRAN",
"URL": "https://cloud.r-project.org"
}
]
},
"Packages": {
"R6": {
"Package": "R6",
"Version": "2.5.1",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R"
],
"Hash": "470851b6d5d0ac559e9d01bb352b4021"
},
"askpass": {
"Package": "askpass",
"Version": "1.2.1",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"sys"
],
"Hash": "c39f4155b3ceb1a9a2799d700fbd4b6a"
},
"curl": {
"Package": "curl",
"Version": "5.2.3",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R"
],
"Hash": "d91263322a58af798f6cf3b13fd56dde"
},
"httr": {
"Package": "httr",
"Version": "1.4.7",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R",
"R6",
"curl",
"jsonlite",
"mime",
"openssl"
],
"Hash": "ac107251d9d9fd72f0ca8049988f1d7f"
},
"jsonlite": {
"Package": "jsonlite",
"Version": "1.8.9",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"methods"
],
"Hash": "4e993b65c2c3ffbffce7bb3e2c6f832b"
},
"lambdr": {
"Package": "lambdr",
"Version": "1.2.5",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"httr",
"jsonlite",
"logger"
],
"Hash": "c5054d8272a53671dd101a17a34419a7"
},
"logger": {
"Package": "logger",
"Version": "0.3.0",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"utils"
],
"Hash": "c145edf05cc128e6ffcfa5d872c46329"
},
"mime": {
"Package": "mime",
"Version": "0.12",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"tools"
],
"Hash": "18e9c28c1d3ca1560ce30658b22ce104"
},
"openssl": {
"Package": "openssl",
"Version": "2.2.2",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"askpass"
],
"Hash": "d413e0fef796c9401a4419485f709ca1"
},
"renv": {
"Package": "renv",
"Version": "1.0.7",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"utils"
],
"Hash": "397b7b2a265bc5a7a06852524dabae20"
},
"sys": {
"Package": "sys",
"Version": "3.4.3",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "de342ebfebdbf40477d0758d05426646"
}
}
}
17 changes: 15 additions & 2 deletions man/build_lambda.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8ebfdbf

Please sign in to comment.