Skip to content

Commit

Permalink
working gadget Docker deployments and R scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEdmondson1234 committed Dec 8, 2019
1 parent d719482 commit 346d776
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 23 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export(cr_buildtrigger_list)
export(cr_buildtrigger_make)
export(cr_buildtrigger_run)
export(cr_deploy_docker)
export(cr_deploy_gadget)
export(cr_deploy_pkgdown)
export(cr_deploy_r)
export(cr_deploy_run)
Expand Down
11 changes: 10 additions & 1 deletion R/deploy.R
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,12 @@ cr_deploy_docker <- function(local,
projectId = cr_project_get(),
launch_browser = interactive(),
task_id=NULL){
myMessage("Uploading ", local, " folder for Docker build", level = 3)
assert_that(
dir.exists(local)
)

myMessage("Building ", local, " folder for Docker image: ", image_name, level = 3)

this_job <- FALSE
if(is.null(task_id)){
this_job <- TRUE
Expand All @@ -228,6 +233,10 @@ cr_deploy_docker <- function(local,
rstudio_add_output(task_id, paste("\nConfiguring Dockerfile"))
use_or_create_dockerfile(local, dockerfile = dockerfile)

assert_that(
is.readable(file.path(local, "Dockerfile"))
)

image <- make_image_name(image_name, projectId = projectId)

build_yaml <- cr_build_yaml(
Expand Down
2 changes: 2 additions & 0 deletions R/docker.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ 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)
return(TRUE)
}
# if no dockerfile, attempt to create it
if(is.null(dockerfile)){
myMessage("Creating Dockerfile from ",local, level = 3)
# creates and write a dockerfile to the folder
cr_dockerfile(local)

Expand Down
1 change: 0 additions & 1 deletion R/print_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ print.gar_Build <- function(x, ...){
cat0("substitutions: ", x$substitution)
cat0("tags: ", x$tags)
cat0("secrets: ", x$secrets)
cat0("artifacts: ", x$artifacts)
if(!is.null(x$steps)){
cat("steps:\n")
if(is.data.frame(x$steps)){
Expand Down
88 changes: 67 additions & 21 deletions R/rstudio_gadget.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,40 @@
#' Launch the googleCloudRunner deployment RStudio gadget
#' @import assertthat
#' @export
cr_deploy_gadget <- function(){

assert_that(
cr_project_get() != "",
cr_bucket_get() != "",
cr_region_get() != "",
Sys.getenv("GCE_AUTH_FILE") != ""
)

image_name_helper <- function(dockerImage,
dockerFile,
dockerProject,
dockerTag){
if(dockerImage == ""){
d_image <- basename(dockerFile)
} else {
d_image <- dockerImage
}
image <- paste0("gcr.io/", dockerProject, "/", d_image)
if(!is.null(dockerTag)){
# only for display purposes
image <- paste0(image, ":", dockerTag)
}
image
}

ui <- miniUI::miniPage(
miniUI::gadgetTitleBar("Setup googleCloudRunner Deploy"),
miniUI::miniTabstripPanel(
miniUI::miniTabstripPanel(between = miniUI::miniButtonBlock(
shiny::numericInput("rTimeout",
label = "Build Timeout",
value = 600,
min = 100,
max = 3600, width = "100px")),
miniUI::miniTabPanel("R script", icon = shiny::icon("r-project"),
miniUI::miniContentPanel(
shiny::helpText("Configure arguments passed to cr_deploy_r()"),
Expand All @@ -12,27 +44,24 @@ cr_deploy_gadget <- function(){
shiny::helpText("Set schedule to empty to run immediatly"),
shiny::textInput("rSchedule", label = "cron Schedule",
value = "15 8 * * *"),
shiny::textInput("rImage", label = "Docker image to use",
shiny::textInput("rImage", label = "R docker image",
value = "rocker/verse"),
shiny::radioButtons("rSource", "Data Source",
choices = c("None",
"CloudRepository",
"CloudStorage"),
inline = TRUE),
shiny::uiOutput("rSourceDyn"),
shiny::numericInput("rTimeout", label = "Timeout",
value = 600,
min = 100, max = 3600)
shiny::br()
)
),
miniUI::miniTabPanel("plumber API", icon = shiny::icon("wrench"),
shiny::textOutput("wd"),
miniUI::miniContentPanel(

shiny::helpText("Configure arguments passed to cr_deploy_run()"),
shiny::textInput("apiFile",
label = "Select folder with api.R file",
placeholder = "plumber/"),
shiny::helpText("Working dir: ", getwd()),
shiny::textInput("apiImage", label = "Docker image to build",
placeholder = paste0("gcr.io/",
cr_project_get(),
Expand All @@ -41,26 +70,33 @@ cr_deploy_gadget <- function(){
)
),
miniUI::miniTabPanel("Dockerfile", icon = shiny::icon("docker"),
shiny::textOutput("wd"),
miniUI::miniContentPanel(
shiny::textOutput("wd"),
shiny::helpText("Configure arguments passed to cr_deploy_docker()"),
shiny::textInput("dockerFile",
label = "Select folder with Dockerfile",
placeholder = "docker/"),
shiny::textInput("dockerImage", label = "Docker image to build",
placeholder = paste0("gcr.io/",
cr_project_get(),
"/my-image"))
shiny::helpText("Working dir: ", getwd()),
shiny::textInput("dockerImage", label = "Edit docker basename",
placeholder = "my-image"),
shiny::textInput("dockerProject", label = "GCP project",
value = cr_project_get()),
shiny::textInput("dockerTag", label = "Docker tag",
value = "$BUILD_ID"),
shiny::helpText(shiny::textOutput("dockerGCRIO"))
)
)
)

)

server <- function(input, output, session) {

output$wd <- shiny::renderText({getwd()})
output$dockerGCRIO <- shiny::renderText({
image_name_helper(dockerImage = input$dockerImage,
dockerFile = input$dockerFile,
dockerProject = input$dockerProject,
dockerTag = input$dockerTag)
})

## Your reactive logic goes here.
output$rSourceDyn <- shiny::renderUI({
Expand Down Expand Up @@ -112,24 +148,34 @@ cr_deploy_gadget <- function(){
)
}

if(!is.null(input$apiFile)){
the_file <- input$apiFile
folder <- dirname(the_file$name)
if(input$apiFile != ""){

folder <- input$apiFile

shiny::stopApp(
cr_deploy_run(folder, image_name = input$apiImage)
)
}

if(!is.null(input$dockerFile)){
the_file <- input$dockerFile
folder <- dirname(the_file$datapath)
if(input$dockerFile != ""){

folder <- input$dockerFile

image_name <- image_name_helper(
dockerImage = input$dockerImage,
dockerFile = input$dockerFile,
dockerProject = input$dockerProject,
dockerTag = NULL)

shiny::stopApp(
cr_deploy_docker(folder, image_name = input$dockerImage)
cr_deploy_docker(folder,
image_name = image_name,
tag = input$dockerTag)
)
}

shiny::stopApp()

})
}

Expand Down
11 changes: 11 additions & 0 deletions man/cr_deploy_gadget.Rd

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

0 comments on commit 346d776

Please sign in to comment.