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

Add renv and multifile support #27

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 6 additions & 2 deletions R/lambda-deploy.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#' @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 renv_path path containing `renv.lock` and `renv` folder to use for dependencies.
#' @param additional_script_paths path to additional scripts to bundle in the image
#'
#' @export
build_lambda <- function(tag, runtime_function, runtime_path, dependencies) {
build_lambda <- function(tag, runtime_function, runtime_path, dependencies, renv_path = NULL, additional_script_paths = NULL) {

logger::log_info("[build_lambda] Checking system dependencies.")
check_system_dependencies()
Expand All @@ -22,7 +24,9 @@ build_lambda <- function(tag, runtime_function, runtime_path, dependencies) {
folder = folder,
runtime_function = runtime_function,
runtime_path = runtime_path,
dependencies = dependencies
dependencies = dependencies,
renv_path = renv_path,
additional_script_paths = additional_script_paths
)
},
error = function(e) {
Expand Down
101 changes: 100 additions & 1 deletion R/lambda-util.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ parse_password <- function(ecr_token) {

#' create_lambda_dockerfile
#'
#' Changes will be proposed here
#'
#' @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 renv_path path containing `renv.lock` and `renv` folder to use for dependencies.
#' @param additional_R_script_path path to an R folder containing additional scripts to bundle in the docker image.
#'
#' @examples
#' \dontrun{
Expand All @@ -102,7 +106,9 @@ create_lambda_dockerfile <-
function(folder,
runtime_function,
runtime_path,
dependencies) {
dependencies,
renv_path = NULL,
additional_R_script_path = NULL) {
logger::log_debug("[create_lambda_dockerfile] Validating inputs.")

checkmate::assert_character(
Expand Down Expand Up @@ -143,6 +149,52 @@ create_lambda_dockerfile <-
null.ok = TRUE
)

if (!is.null(renv_path)){
checkmate::assert_character(
x = renv_path,
min.chars = 1,
null.ok = TRUE
)

if (!checkmate::test_file_exists(file.path(renv_path, ".Rprofile"))) {
msg <- glue::glue("[create_lambda_dockerfile] Can't access .Rprofile file in {renv_path}.")
logger::log_error(msg)
rlang::abort(msg)
}

if (!checkmate::test_file_exists(file.path(renv_path, "renv.lock"))) {
msg <- glue::glue("[create_lambda_dockerfile] Can't access renv.lock file in {renv_path}.")
logger::log_error(msg)
rlang::abort(msg)
}

if (!checkmate::test_file_exists(file.path(renv_path, "renv", "activate.R"))) {
msg <- glue::glue("[create_lambda_dockerfile] Can't access renv/activate.R file inside {renv_path}.")
logger::log_error(msg)
rlang::abort(msg)
}

if (!checkmate::test_file_exists(file.path(renv_path, "renv", "settings.json"))) {
msg <- glue::glue("[create_lambda_dockerfile] Can't access renv/settings.json file inside {renv_path}.")
logger::log_error(msg)
rlang::abort(msg)
}
}

if (!is.null(additional_R_script_path)) {
checkmate::assert_character(
x = additional_R_script_path,
min.chars = 1,
null.ok = TRUE
)

if (!checkmate::test_directory_exists(additional_R_script_path)) {
msg <- glue::glue("[create_lambda_dockerfile] Can't access additional R script folder {additional_R_script_path}.")
logger::log_error(msg)
rlang::abort(msg)
}
}

logger::log_debug("[create_lambda_dockerfile] Creating directory with Dockerfile and runtime script.")

docker_template <- system.file("lambdr_dockerfile.txt", package = "r2lambda")
Expand Down Expand Up @@ -172,6 +224,53 @@ create_lambda_dockerfile <-
)
}

if (!is.null(renv_path)){
renv_lock_path <- file.path(renv_path, "renv.lock")
file.copy(renv_lock_path, folder)

renv_profile_path <- file.path(renv_path, ".Rprofile")
file.copy(renv_profile_path, folder)

dir.create(file.path(folder, "renv"))
dir.exists(file.path(folder, "renv"))

renv_activate_path <- file.path(renv_path, "renv", "activate.R")
file.copy(renv_activate_path, file.path(folder, "renv"))

renv_settings_path <- file.path(renv_path, "renv", "settings.json")
file.copy(renv_settings_path, file.path(folder, "renv"))

renv_string <- '
COPY .Rprofile /lambda
COPY renv.lock /lambda

RUN mkdir /lambda/renv
COPY settings.json /lambda/renv
COPY activate.R /lambda/renv

RUN chmod 755 -R /lambda

RUN Rscript -e "renv::restore()"
'
write(renv_string,
file = file.path(folder, "Dockerfile"),
append = TRUE
)
}

if (!is.null(additional_R_script_path)) {
additional_R_scripts <- dir(additional_R_script_path, pattern="*\\.R", full.names = TRUE, recursive = FALSE, include.dirs = FALSE)

dir.create(file.path(folder, "R"))
for (path in additional_R_scripts){
file.copy(path, file.path(folder, "R"))
}

additional_r_script_names <- basename(additional_R_scripts)
additional_R_scripts_string <-
glue::glue("COPY R/{additional_r_script_names} /lambda/R/{additional_r_script_names}", .sep = "\n")
}

runtime_string <- runtime_line(runtime_function)

write(runtime_string,
Expand Down
1 change: 1 addition & 0 deletions inst/multifile_renv/.Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source("renv/activate.R")
3 changes: 3 additions & 0 deletions inst/multifile_renv/R/hello_big_world.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hello_big_world <- function(number) {
list(message = "Hello big world!")
}
6 changes: 6 additions & 0 deletions inst/multifile_renv/R/hello_world.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
hello_world <- function(number) {
if (number > 10) {
hello_big_world()
}
list(message = "Hello world!")
}
5 changes: 5 additions & 0 deletions inst/multifile_renv/multifile_renv.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library(tidyverse)

source(dir("R"))

lambdr::start_lambda()
16 changes: 16 additions & 0 deletions inst/multifile_renv/multifile_renv.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
Loading