Skip to content

Commit

Permalink
Merge pull request #29 from discindo/feat/noref/multiple-files
Browse files Browse the repository at this point in the history
feat: allow support files (multi-file project)
  • Loading branch information
teofiln authored Oct 17, 2024
2 parents 58c27e2 + 95f9949 commit 8180779
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
10 changes: 7 additions & 3 deletions R/lambda-deploy.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
#' @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 renvlock_path path to the renv.lock file (if any)
#' @param dependencies list of dependencies (if any)
#' @param support_path path to the support files (if any). Either NULL
#' (the default) if all needed code is in the same `runtime_path` script, or a
#' character vector of paths to additional files needed by the runtime script.
#' @param renvlock_path path to the renv.lock file (if any). Default is NULL.
#' @param dependencies list of dependencies (if any). Default is NULL.
#'
#' @details Use either `renvlock_path` or `dependencies` to install required
#' packages, not both. By default, both are `NULL`, so the Docker image will
Expand All @@ -13,7 +16,7 @@
#' @importFrom glue glue glue_collapse single_quote double_quote
#' @export
build_lambda <- function(
tag, runtime_function, runtime_path,
tag, runtime_function, runtime_path, support_path = NULL,
renvlock_path = NULL, dependencies = NULL) {

logger::log_info("[build_lambda] Checking system dependencies.")
Expand All @@ -31,6 +34,7 @@ build_lambda <- function(
runtime_function = runtime_function,
runtime_path = runtime_path,
renvlock_path = renvlock_path,
support_path = support_path,
dependencies = dependencies
)
},
Expand Down
47 changes: 45 additions & 2 deletions R/lambda-util.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ runtime_line <- function(runtime) {
glue("CMD [{rt}]")
}

#' support script line
#' @noRd
support_line <- function(support) {
checkmate::assert_character(support)
glue("COPY {support} {support}")
}

#' renv_line
#' @noRd
renv_line <- function(renvlock_path) {
Expand All @@ -93,7 +100,9 @@ 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 runtime_path path to the script containing the runtime function
#' @param support_path path to the supporting code (if any). Can be `NULL`
#' (default) or a character vector of paths to scripts.
#' @param renvlock_path path to the renv.lock file (if any)
#' @param dependencies list of dependencies (if any)
#'
Expand Down Expand Up @@ -124,6 +133,7 @@ create_lambda_dockerfile <-
function(folder,
runtime_function,
runtime_path,
support_path = NULL,
renvlock_path = NULL,
dependencies = NULL) {
logger::log_debug("[create_lambda_dockerfile] Validating inputs.")
Expand Down Expand Up @@ -152,7 +162,6 @@ create_lambda_dockerfile <-
checkmate::assert_character(
x = runtime_path,
min.chars = 1,
len = 1,
any.missing = FALSE
)

Expand All @@ -164,6 +173,24 @@ create_lambda_dockerfile <-
rlang::abort(msg)
}

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

if (!is.null(support_path)) {
lapply(support_path, function(x) {
if (!checkmate::test_file_exists(x)) {
msg <- glue(
"[create_lambda_dockerfile] Can't access support script {x}."
)
logger::log_error(msg)
rlang::abort(msg)
}
})
}

checkmate::assert_character(
x = dependencies,
min.chars = 1,
Expand Down Expand Up @@ -211,6 +238,12 @@ create_lambda_dockerfile <-
to = file.path(folder, "runtime.R")
)

if (!is.null(support_path)) {
lapply(support_path, function(x) {
file.copy(x, folder)
})
}

file.copy(docker_template, folder, recursive = FALSE)
file.rename(
from = file.path(folder, basename(docker_template)),
Expand Down Expand Up @@ -245,6 +278,16 @@ create_lambda_dockerfile <-
append = TRUE
)

if (!is.null(support_path)) {
lapply(support_path, function(x) {
support_string <- support_line(basename(x))
write(support_string,
file = file.path(folder, "Dockerfile"),
append = TRUE
)
})
}

logger::log_debug("[create_lambda_dockerfile] Done.")
}

Expand Down
9 changes: 7 additions & 2 deletions man/build_lambda.Rd

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

0 comments on commit 8180779

Please sign in to comment.