Skip to content

Commit

Permalink
declare imports from glue formally
Browse files Browse the repository at this point in the history
  • Loading branch information
teofiln committed Oct 16, 2024
1 parent aeec46d commit b00a047
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export(deploy_lambda)
export(invoke_lambda)
export(schedule_lambda)
export(test_lambda)
importFrom(glue,double_quote)
importFrom(glue,glue)
importFrom(glue,glue_collapse)
importFrom(glue,single_quote)
importFrom(paws,ecr)
importFrom(paws,eventbridge)
importFrom(paws,iam)
Expand Down
20 changes: 10 additions & 10 deletions R/lambda-deploy.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ test_lambda <- function(tag, payload) {
tag_exists <- repo_tag %in% tags

if (!tag_exists) {
msg <- glue::glue("[test_lambda] Image tagged {repo_tag} not found.")
msg <- glue("[test_lambda] Image tagged {repo_tag} not found.")
logger::log_error(msg)
message("Available images:\n", glue::glue_collapse(sep = "\n", tags))
message("Available images:\n", glue_collapse(sep = "\n", tags))
rlang::abort(msg)
}

uid <- uuid::UUIDgenerate(1)
logger::log_info(glue::glue("[test_lambda] Starting lambda container with name {uid}."))
logger::log_info(glue("[test_lambda] Starting lambda container with name {uid}."))
docker_cli$container$run(
image = repo_tag,
port = "9000:8080",
Expand Down Expand Up @@ -221,16 +221,16 @@ deploy_lambda <-
)

logger::log_warn("[deploy_lambda] Lambda function created successfully.")
logger::log_warn(glue::glue(
logger::log_warn(glue(
"[deploy_lambda] Pushed docker image to ECR with URI `{ecr_image_uri}`"
))
logger::log_warn(
glue::glue(
glue(
"[deploy_lambda] Created Lambda execution role with ARN `{iam_lambda_role$Role$Arn}`"
)
)
logger::log_warn(
glue::glue(
glue(
"[deploy_lambda] Created Lambda function `{lambda$FunctionName}` with ARN `{lambda$FunctionArn}`"
)
)
Expand Down Expand Up @@ -289,7 +289,7 @@ invoke_lambda <-
), error = function(e) e$message)

} else {
logger::log_info(glue::glue("[invoke_lambda] Failed to invoke the function due to {state} state."))
logger::log_info(glue("[invoke_lambda] Failed to invoke the function due to {state} state."))
logger::log_info("[invoke_lambda] Please try again shortly if the reported state was `Pending`.")
}

Expand Down Expand Up @@ -329,7 +329,7 @@ schedule_lambda <- function(lambda_function, execution_rate) {
lambda_names <- sapply(fun_list$Functions, "[[", "FunctionName")

if (!lambda_function %in% lambda_names) {
msg <- glue::glue("[schedule_lambda] Cannot find deployed lambda function {lambda_function}")
msg <- glue("[schedule_lambda] Cannot find deployed lambda function {lambda_function}")
logger::log_error(msg)
rlang::abort(msg)
}
Expand All @@ -339,7 +339,7 @@ schedule_lambda <- function(lambda_function, execution_rate) {
lambda_function_arn <- fun_list$Functions[[lambda_index]]$FunctionArn

rate_clean <- gsub("\\(|\\)| |\\*|\\?", "_", execution_rate)
rule_name <- glue::glue("schedule_rule_{rate_clean}_{lambda_function}")
rule_name <- glue("schedule_rule_{rate_clean}_{lambda_function}")
logger::log_info("[schedule_lambda] Creating event schedule rule with name {rule_name}")

rule_arn <- tryCatch(
Expand All @@ -352,7 +352,7 @@ schedule_lambda <- function(lambda_function, execution_rate) {
}
)

event_name <- glue::glue("schedule_event_{rate_clean}_{lambda_function}")
event_name <- glue("schedule_event_{rate_clean}_{lambda_function}")
logger::log_info("[schedule_lambda] Adding permission to execute lambda to event with name {event_name}")
tryCatch(
expr = {
Expand Down
19 changes: 10 additions & 9 deletions R/lambda-util.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ check_system_dependencies <- function() {
#' \dontrun{
#' aws_connect("lambda")
#' }
#' @importFrom glue glue glue_collapse single_quote double_quote
#' @export
aws_connect <- function(service) {
logger::log_debug("[aws_connect] Checking requested service.")

if (!service %in% getNamespaceExports("paws")) {
msg <- glue::glue("The service `{service}` does not appear to be available in `paws`.")
msg <- glue("The service `{service}` does not appear to be available in `paws`.")
logger::log_error(msg)
rlang::abort(msg)
}
Expand All @@ -50,17 +51,17 @@ aws_connect <- function(service) {
install_deps_line <- function(deps) {
checkmate::assert_character(deps)

repo <- glue::single_quote("https://packagemanager.rstudio.com/all/__linux__/centos7/latest")
glued <- glue::glue_collapse(glue::single_quote(deps), sep = ", ")
glue::glue('RUN Rscript -e "install.packages(c({glued}), repos = {repo})"')
repo <- single_quote("https://packagemanager.rstudio.com/all/__linux__/centos7/latest")
glued <- glue_collapse(single_quote(deps), sep = ", ")
glue('RUN Rscript -e "install.packages(c({glued}), repos = {repo})"')
}

#' runtime_line
#' @noRd
runtime_line <- function(runtime) {
checkmate::assert_character(runtime)
rt <- glue::double_quote(runtime)
glue::glue("CMD [{rt}]")
rt <- double_quote(runtime)
glue("CMD [{rt}]")
}

#' parse password from ecr token
Expand Down Expand Up @@ -112,7 +113,7 @@ create_lambda_dockerfile <-
)

if (checkmate::test_directory_exists(folder)) {
msg <- glue::glue("[create_lambda_dockerfile] Directory {folder} exists. Please choose another name.")
msg <- glue("[create_lambda_dockerfile] Directory {folder} exists. Please choose another name.")
logger::log_error(msg)
rlang::abort(msg)
}
Expand All @@ -132,7 +133,7 @@ create_lambda_dockerfile <-
)

if (!checkmate::test_file_exists(runtime_path)) {
msg <- glue::glue("[create_lambda_dockerfile] Can't access runtime script file {runtime_path}.")
msg <- glue("[create_lambda_dockerfile] Can't access runtime script file {runtime_path}.")
logger::log_error(msg)
rlang::abort(msg)
}
Expand Down Expand Up @@ -374,7 +375,7 @@ create_lambda_function <-
lambda_service <- aws_connect("lambda")
lambda <- lambda_service$create_function(
FunctionName = tag,
Code = list(ImageUri = glue::glue("{ecr_image_uri}:latest")),
Code = list(ImageUri = glue("{ecr_image_uri}:latest")),
PackageType = "Image",
Role = lambda_role_arn,
Environment = envvar_list,
Expand Down

0 comments on commit b00a047

Please sign in to comment.