R/harbor_ovh.R

Defines functions docker_cmd.ovh_instance docker_build

Documented in docker_build docker_cmd.ovh_instance

#' Docker S3 method for use with harbor package
#'
#' @param host The OVH instance
#' @param cmd The command to pass to docker
#' @param args arguments to the command
#' @param docker_opts options for docker
#' @param capture_text whether to return the output
#' @param ... other arguments passed to \link{cloud_ssh}
#'
#' @details
#'
#' Instances launched in the \code{ovh-containers} image family automatically add your user to the docker group,
#'   but for others you will need to run \code{sudo usermod -a -G docker ${USER}} and log out and back in.
#' @author Mark Edmondson
#' @export
docker_cmd.ovh_instance <- function(host, cmd = NULL, args = NULL,
                                    docker_opts = NULL, capture_text = FALSE, ...) {

  cmd_string <- paste(c("sudo docker", cmd, docker_opts, args), collapse = " ")

  cloud_ssh(host, ..., cmd_string, capture_text = capture_text)

}

#' Build image on an instance from a local Dockerfile
#'
#' Uploads a folder with a \code{Dockerfile} and supporting files to an instance and builds it
#'
#' @inheritParams docker_cmd
#' @param dockerfolder Local location of build directory including valid \code{Dockerfile}
#' @param new_image Name of the new image
#' @param folder Where on host to build dockerfile
#' @param wait Whether to block R console until finished build
#'
#' @details
#'
#' Dockerfiles are best practice when creating your own docker images,
#' rather than logging into a Docker container, making changes and committing.
#'
#' @seealso \href{https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/}{Best practices for writing Dockerfiles}
#'
#' An example Dockerfile for \href{https://hub.docker.com/r/rocker/ropensci/~/dockerfile/}{rOpensci}
#'
#' General R Docker images found at \href{https://github.com/rocker-org}{rocker-org}
#'
#' @examples
#' \dontrun{
#' docker_build(localhost, "/home/stuff/dockerfolder" ,"new_image", wait = TRUE)
#' docker_run(localhost, "new_image")
#' }
#' @return A table of active images on the instance
#' @author Mark Edmondson
#' @export
docker_build <- function(host = localhost, dockerfolder, new_image, folder = "buildimage",  wait = FALSE, ...) {

  stopifnot(file.exists(dockerfolder))

  cloud_ssh(host, paste0("mkdir -p -m 0755 ", folder), ...)
  cloud_ssh_upload(host, dockerfolder, folder, ...)

  docker_cmd(host,
             "build",
             args = c(new_image, paste0(folder,"/",basename(dockerfolder))),
             docker_opts = "-t",
             wait = wait,
             ...)

  ## list images
  docker_cmd(host, "images", ..., capture_text = TRUE)
}
voltek62/RsparkleR documentation built on May 19, 2019, 1:48 a.m.