Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mskyttner opened this issue Dec 9, 2020 · 0 comments

Comments

@mskyttner
Copy link

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant