Skip to content

Commit

Permalink
A function helper for #8 let users specify own Dockerfile in gadget #1
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEdmondson1234 committed Dec 10, 2019
1 parent 9db554f commit 406466a
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 36 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export(cr_deploy_run)
export(cr_dockerfile)
export(cr_email_get)
export(cr_email_set)
export(cr_plumber_pubsub)
export(cr_project_get)
export(cr_project_set)
export(cr_region_get)
Expand Down
32 changes: 22 additions & 10 deletions R/cloudrun.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#' Create a CloudRun service.
#'
#' Deploys an existing gcr.io image. Use \link{cr_deploy_docker} or similar to create image, or \link{cr_deploy_run} to automate building and deploying.
#' Deploys an existing gcr.io image.
#'
#' @seealso \href{https://cloud.google.com/run/}{Google Documentation for Cloud Run}
#' @seealso Use \link{cr_deploy_docker} or similar to create image, or \link{cr_deploy_run} to automate building and deploying.
#'
#' @param image The name of the image to create or use in deployment - \code{gcr.io}
#' @param name Name for deployment on Cloud Run
Expand All @@ -19,7 +20,8 @@
#' @details
#'
#' Uses Cloud Build to deploy an image to Cloud Run
#' https://cloud.google.com/cloud-build/docs/deploying-builds/deploy-cloud-run
#'
#' @seealso \href{https://cloud.google.com/cloud-build/docs/deploying-builds/deploy-cloud-run}{Deploying Cloud Run using Cloud Build}
#'
#' @export
#' @examples
Expand All @@ -43,7 +45,8 @@ cr_run <- function(image,
}

rstudio_add_output(task_id,
paste("\n#> Launching CloudRun image: \n", image))
paste("\n#> Launching CloudRun image: \n",
image))

if(allowUnauthenticated){
auth_calls <- "--allow-unauthenticated"
Expand All @@ -70,7 +73,9 @@ cr_run <- function(image,
timeout = timeout,
launch_browser=launch_browser)

result <- cr_build_wait(build, projectId = projectId, task_id=task_id)
result <- cr_build_wait(build,
projectId = projectId,
task_id=task_id)

if(result$status == "SUCCESS"){
run <- cr_run_get(name, projectId = projectId)
Expand Down Expand Up @@ -114,7 +119,8 @@ make_endpoint <- function(endbit){
paste(endpoints, collapse = " "), " got: ", region)
}

sprintf("https://%s-run.googleapis.com/apis/serving.knative.dev/v1/%s", region, endbit)
sprintf("https://%s-run.googleapis.com/apis/serving.knative.dev/v1/%s",
region, endbit)
}


Expand All @@ -141,10 +147,13 @@ cr_run_list <- function(projectId = cr_project_get(),
)

url <- make_endpoint(sprintf("namespaces/%s/services", projectId))
myMessage("Cloud Run services in region: ", .cr_env$region, level = 3)
myMessage("Cloud Run services in region: ",
.cr_env$region, level = 3)
# run.namespaces.services.list
#TODO: paging
pars <- list(labelSelector = labelSelector, continue = NULL, limit = limit)
pars <- list(labelSelector = labelSelector,
continue = NULL,
limit = limit)
f <- gar_api_generator(url,
"GET",
pars_args = rmNullObs(pars),
Expand Down Expand Up @@ -175,7 +184,8 @@ parse_service_list_post <- function(x){

data.frame(
name = x$metadata$name,
container = unlist(lapply(x$spec$template$spec$containers, function(x) x$image)),
container = unlist(lapply(x$spec$template$spec$containers,
function(x) x$image)),
url = x$status$url,
stringsAsFactors = FALSE
)
Expand All @@ -197,10 +207,12 @@ parse_service_list_post <- function(x){
#' @export
cr_run_get <- function(name, projectId = cr_project_get()) {

url <- make_endpoint(sprintf("namespaces/%s/services/%s", projectId, name))
url <- make_endpoint(sprintf("namespaces/%s/services/%s",
projectId, name))

# run.namespaces.services.get
f <- gar_api_generator(url, "GET", data_parse_function = parse_service_get,
f <- gar_api_generator(url, "GET",
data_parse_function = parse_service_get,
checkTrailingSlash = FALSE)
f()

Expand Down
3 changes: 2 additions & 1 deletion R/docker.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ cr_dockerfile <- function(deploy_folder, ...){
}

use_or_create_dockerfile <- function(local, dockerfile){

local_files <- list.files(local)
if("Dockerfile" %in% local_files){
myMessage("Dockerfile found in ",local, level = 3)
Expand All @@ -59,7 +60,7 @@ use_or_create_dockerfile <- function(local, dockerfile){

} else {
assert_that(
is.readable(file.path(local, dockerfile))
is.readable(dockerfile)
)
myMessage("Copying Dockerfile from ", dockerfile," to ",local, level = 3)
file.copy(dockerfile, file.path(local, "Dockerfile"))
Expand Down
52 changes: 52 additions & 0 deletions R/plumber.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#' Plumber - Pub/Sub parser
#'
#' A function to use in plumber scripts to accept Pub/Sub messages
#'
#' @param message The pubsub message
#' @param pass_f An R function that will work with the data parsed out of the pubsub \code{message$data} field.
#'
#' @details
#'
#' This function is intended to be used within \link[plumber]{plumb} API scripts. It needs to be annotated with a \code{@post} URL route and a \code{@param message The pubsub message} as per the plumber documentation.
#'
#' \code{pass_f} should be a function you create that accepts one argument, the data from the pubsub \code{message$data} field. It is unencoded for you.
#'
#' The Docker container for the API will need to include \code{googleCloudRunner} installed in its R environment to run this function. This is available in the public \code{gcr.io/gcer-public/cloudrunner} image.
#'
#'
#' @export
#' @examples
#'
#' \dontrun{
#'
#' # within a plumber api.R script:
#'
#' # example function echos back pubsub message
#' pub <- function(x){
#' paste("Echo:", x)
#' }
#'
#' #' Recieve pub/sub message
#' #' @post /pubsub
#' #' @param message a pub/sub message
#' function(message=NULL){
#' googleCloudRunner::cr_plumber_pubsub(message, pub)
#' }
#'
#' }
#' @seealso \href{https://cloud.google.com/run/docs/tutorials/pubsub}{Google Pub/Sub tutorial for Cloud Run}
#' @family Cloud Run functions
cr_run_pubsub <- function(message=NULL,
pass_f=function(x) x){
#
if(is.null(message)) stop("pub/sub message not found")
stopifnot(
is.list(message),
!is.null(message$data)
)

the_data <- rawToChar(jsonlite::base64_dec(message$data))

pass_f(the_data)

}
22 changes: 18 additions & 4 deletions R/rstudio_gadget.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#' Launch the googleCloudRunner deployment RStudio gadget
#'
#' You can assign a hotkey to the addin via Tools > Addins > Browse Addins > Keyboard shortcuts. CTRL+SHIFT+D is a suggested hotkey.
#' @import assertthat
#' @export
cr_deploy_gadget <- function(){
Expand Down Expand Up @@ -63,13 +65,18 @@ cr_deploy_gadget <- function(){
miniUI::miniContentPanel(
shiny::h2("Configure cr_deploy_run()"),
shiny::textInput("apiFile",
label = "Select folder with api.R file and Dockerfile",
placeholder = "plumber/"),
label = "Select folder with api.R file and Dockerfile",
placeholder = "plumber/"),
shiny::helpText("Working dir: ", getwd()),
shiny::textInput("apiDockerfile",
label = "Dockerfile to build from",
value = ""),
shiny::helpText("Leave Dockerfile blank to attempt autodetection"),
# shiny::textInput("apiName", label = "Cloud Run service name",
# placeholder = "api-name"),
shiny::textInput("apiImage", label = "Edit docker basename",
placeholder = "my-image"),
shiny::textInput("apiImage",
label = "Edit docker basename",
placeholder = "my-image"),
shiny::textInput("apiTag", label = "Docker tag",
value = "$BUILD_ID"),
shiny::helpText(shiny::textOutput("apiGCRIO"))
Expand Down Expand Up @@ -175,9 +182,16 @@ cr_deploy_gadget <- function(){
# apiName <- input$apiName
# }

if(input$apiDockerfile == ""){
dockerfile <- NULL
} else {
dockerfile <- input$apiDockerfile
}

shiny::stopApp(
cr_deploy_run(folder,
# remote = apiName,
dockerfile = dockerfile,
image_name = image_name,
tag = input$apiTag,
launch_browser=input$interactive)
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ reference:
contents:
- matches("cr_run")
- cr_deploy_run
- cr_plumber_pubsub
- title: "Cloud Scheduler functions"
desc: >
Functions for working with Cloud Scheduler
Expand Down
4 changes: 2 additions & 2 deletions inst/example/Dockerfile_bak
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM trestletech/plumber
FROM gcr.io/gcer-public/cloudrunner:master
LABEL maintainer="cloudRunner"

COPY [".", "./"]

ENTRYPOINT ["R", "-e", "pr <- plumber::plumb(commandArgs()[4]); pr$run(host='0.0.0.0', port=as.numeric(Sys.getenv('PORT')))"]
CMD ["api.R"]
CMD ["api.R"]
12 changes: 2 additions & 10 deletions inst/example/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,8 @@ function(spec){
#' @post /pubsub
#' @param message a pub/sub message
function(message=NULL){
# https://cloud.google.com/run/docs/tutorials/pubsub
if(is.null(message)) stop("pub/sub message not found")
stopifnot(
is.list(message),
!is.null(message$data)
)
str(message)

the_data <- rawToChar(jsonlite::base64_dec(message$data))

the_data
pub <- function(x){paste("Echo:", x)}
googleCloudRunner::cr_plumber_pubsub(message, pub)

}
2 changes: 1 addition & 1 deletion man/cr_deploy_gadget.Rd

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

50 changes: 50 additions & 0 deletions man/cr_plumber_pubsub.Rd

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

11 changes: 7 additions & 4 deletions man/cr_run.Rd

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

4 changes: 2 additions & 2 deletions man/cr_run_get.Rd

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

4 changes: 2 additions & 2 deletions man/cr_run_list.Rd

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

0 comments on commit 406466a

Please sign in to comment.