R/add_dockerfile.R

Defines functions check_file_exist cat_green_tick cat_red_bullet cat_created alert_build

#' @importFrom yesno yesno
# Definindo algumas funções utilitárias:
check_file_exist <- function(file){
  res <- TRUE
  if (file.exists(file)){
    res <- yesno("This file already exists, override?")
  }
  return(res)
}

#' @importFrom cli cat_bullet
cat_green_tick <- function(...){
  cat_bullet(
    ..., 
    bullet = "tick", 
    bullet_col = "green"
  )
}

#' @importFrom cli cat_bullet
cat_red_bullet <- function(...){
  cat_bullet(
    ..., 
    bullet = "bullet",
    bullet_col = "red"
  )
}

cat_created <- function(
  where, 
  file = "File"
){
  cat_green_tick(
    sprintf(
      "%s created at %s",
      file, 
      where
    )
  )
}

alert_build <- function(
  path, 
  output, 
  build_golem_from_source
){
  cat_created(output, "Dockerfile")
  if ( ! build_golem_from_source ){
    cat_red_bullet(
      sprintf(
        "Be sure to keep your %s_%s.tar.gz file (generated using `devtools::build()` ) in the same folder as the %s file generated", 
        read.dcf(path)[1], 
        read.dcf(path)[1,][['Version']], 
        basename(output)
      )
    )
  }
}

#' @importFrom golem get_golem_wd
#' @importFrom usethis use_build_ignore
# Sobrescrevendo a função golem::add_dockerfile(), porque preciso de alguns comandos a mais:
add_dockerfile <- function (input = "DESCRIPTION",
                            output = "Dockerfile",
                            pkg = get_golem_wd(),
                            from = paste0("rocker/tidyverse:",
                                          R.Version()$major,
                                          ".", 
                                          R.Version()$minor),
                            as = NULL,
                            port = 80,
                            host = "0.0.0.0") 
{
  # Comandos originais do package:
  #   - Pega o file.path do diretório de output:
  where <- file.path(pkg, output)
  
  #   - Verifica se o diretório existe e dá erro caso não exista:
  if (!check_file_exist(where))
    return(invisible(FALSE))
  
  #   - Adiciona o output no Rbuildignore:
  use_build_ignore(basename(where))
  
  #   - Comando do dockerfiler pra criar o Dockerfile a partir do DESCRIPTION:
  dock <- dock_from_desc(input, FROM = from, AS = as)
  
  #   - Linhas adicionadas pro package golem funcionar:
  dock$EXPOSE(port)
  dock$CMD(glue("R -e \"options('shiny.port'={port},shiny.host='{host}');{read.dcf(input)[1]}::run_app()\""))
  
  #   - Linhas que adicionei pro meu aplicativo buildar direitinho no GCP:
  dock$add_after(after = 1,
                 cmd = paste0("
RUN sudo apt-get install -y default-jre
RUN sudo apt-get install -y default-jdk
RUN sudo R CMD javareconf
RUN sudo apt-get install -y r-cran-rjava
RUN sudo apt-get install -y libv8-dev
RUN R -e 'Sys.setenv(G_APP_PASS = \"",    Sys.getenv("G_APP_PASS"),    "\")'
RUN R -e 'Sys.setenv(EMAIL_PESSOAL = \"", Sys.getenv("EMAIL_PESSOAL"), "\")'
"))
  
  #   - Exportando o Dockerfile:
  dock$write(output)
  
  #   - Alertas do golem sobre o processo da função:
  alert_build(input, output, TRUE)
}

#' @importFrom golem get_golem_wd
#' @importFrom usethis use_build_ignore
#' @importFrom glue glue
#' @importFrom cli cat_rule cat_line
#' @import dockerfiler
add_dockerfile_heroku <- function (input = "DESCRIPTION", output = "Dockerfile", pkg = get_golem_wd(), 
                                   from = paste0("rocker/tidyverse:", R.Version()$major, ".", 
                                                 R.Version()$minor), as = NULL) 
{
  where <- file.path(pkg, output)
  if (!check_file_exist(where)) {
    return(invisible(FALSE))
  }
  use_build_ignore(basename(where))
  dock <- dock_from_desc(input, FROM = from, AS = as)
  dock$CMD(glue("R -e \"options('shiny.port'=$PORT,shiny.host='0.0.0.0');{read.dcf(input)[1]}::run_app()\""))
  dock$add_after(after = 1,
                 cmd = paste0("
RUN sudo apt-get install -y default-jre
RUN sudo apt-get install -y default-jdk
RUN sudo R CMD javareconf
RUN sudo apt-get install -y r-cran-rjava
RUN sudo apt-get install -y libv8-dev
RUN R -e 'Sys.setenv(G_APP_PASS = \"",    Sys.getenv("G_APP_PASS"),    "\")'
RUN R -e 'Sys.setenv(EMAIL_PESSOAL = \"", Sys.getenv("EMAIL_PESSOAL"), "\")'
"))
  dock$write(output)
  alert_build(input, output, TRUE)
  apps_h <- gsub("\\.", "-", glue("{read.dcf(input)[1]}-{read.dcf('DESCRIPTION')[1,][['Version']]}"))
  cat_rule("From your command line, run:")
  cat_line("heroku container:login")
  cat_line(glue("heroku create {apps_h}"))
  cat_line(glue("heroku container:push web --app {apps_h}"))
  cat_line(glue("heroku container:release web --app {apps_h}"))
  cat_line(glue("heroku open --app {apps_h}"))
  cat_red_bullet("Be sure to have the heroku CLI installed.")
  cat_red_bullet(glue("You can replace {apps_h} with another app name."))
  use_build_ignore(files = output)
  invisible(output)
}
guarastats/GuaraDash documentation built on March 31, 2020, 3:37 p.m.