Skip to content

(bypassing?) command splitter when running commands on ephemeral containers and returning results #56

Description

@mskyttner

The use case for either having a "command splitter" or being able to bypass "command splitting" is when using an ephemeral container to run a single command line invocation and capture the output.

Are there any recommendation when it comes to using stevedore to "shell out" to run a command (which may involve pipes) and capture the output?

Below is an illustration/attempt using "jq" which is available in the "docker.io/endeveit/docker-jq" image. It looks like stevedore allows running commands that does not contain quotation marks.

stevedore_runc <- function(container_slug, command, use_split = FALSE, rm = TRUE) {
  
  stopifnot(stevedore::docker_available())

  docker <- stevedore::docker_client()
  
  # capture output from command
  output <- textConnection(tempfile(), "w")
  on.exit(close(output))
  
  # any command needs to be split
  split_cmd <- function(x)
    unlist(strsplit(x, "\\s+"))
  
  cli <- command
  if(use_split) cli <- split_cmd(command)
  
  message("CLI command is:", cli)
  str(cli)
  
  # stream output from command and return it
  invisible(docker$container$run(
    container_slug, 
    cmd = cli, 
    rm = TRUE, 
    stream = output)
  )
  
  # a leading "O> " gets inserted, strip it out
  res <- textConnectionValue(output)
  gsub("^O> ", "", res)
}

# works ok if no quotes
stevedore_runc("docker.io/endeveit/docker-jq", 
  command = "jq --help", use_split = TRUE)

stevedore_runc("docker.io/endeveit/docker-jq", 
  command = I("jq --help"), use_split = FALSE)

# quotes are causing problems and cannot easily bypass the "command splitter"?
stevedore_runc("docker.io/endeveit/docker-jq", 
  command = I(c('echo "hello"')), use_split = FALSE)

stevedore_runc("docker.io/endeveit/docker-jq", 
  command = I(c("echo 'hello'")), use_split = FALSE)

stevedore_runc("docker.io/endeveit/docker-jq", 
  command = I('jq --version # "no, does not seem so"'), use_split = FALSE)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions