0

Let's take following shinyproxy yaml as example:

proxy:
  title: Open Analytics Shiny Proxy
  logo-url: http://www.openanalytics.eu/sites/www.openanalytics.eu/themes/oa/logo.png
  landing-page: /
  heartbeat-rate: 10000
  heartbeat-timeout: 60000
  port: 8080
  authentication: simple
  admin-groups: scientists
  # Example: 'simple' authentication configuration
  users:
  - name: jack
    password: password
    groups: scientists
  - name: jeff
    password: password
    groups: mathematicians
  # Example: 'ldap' authentication configuration
  ldap:
    url: ldap://ldap.forumsys.com:389/dc=example,dc=com
    user-dn-pattern: uid={0}
    group-search-base:
    group-search-filter: (uniqueMember={0})
    manager-dn: cn=read-only-admin,dc=example,dc=com
    manager-password: password
  # Docker configuration
  docker:
    cert-path: /home/none
    url: http://localhost:2375
    port-range-start: 20000
  specs:
  - id: 01_hello
    display-name: Hello Application
    description: Application which demonstrates the basics of a Shiny app
    container-cmd: ["R", "-e", "shinyproxy::run_01_hello()"]
    container-image: openanalytics/shinyproxy-demo
    access-groups: [scientists, mathematicians]
  - id: 06_tabsets
    container-cmd: ["R", "-e", "shinyproxy::run_06_tabsets()"]
    container-image: openanalytics/shinyproxy-demo
    access-groups: scientists

logging:
  file:
    shinyproxy.log

How can I parse ["R", "-e", "shinyproxy::run_06_tabsets()"] to normal exec form or directly run this command without parsing in python? Is there any lib for that?

EDIT:

I want to run that command in subprocess and kill it after timeout is reached or error occurred when running Shiny app:

MWE:

from subprocess import Popen, PIPE
from threading import Timer

cmd_from_yaml = ['R', '-e', 'shinyproxy::run_06_tabsets()']
docker_cmd = "docker run -i {0} {1}".format(vol, img)
docker_cmd = docker_cmd.split()
docker_cmd.extend(cmd_from_yaml)
timeout_sec = 10
print("Running command: " + str(" ".join(docker_cmd)))
proc = Popen(docker_cmd, stdout=PIPE, stderr=PIPE, shell=False)

timer = Timer(timeout_sec, proc.kill)
try:
    timer.start()
    stdout, stderr = proc.communicate()
finally:
    timer.cancel()

if stderr:
    proc.kill()
    raise Exception("Error: " + str(stderr))

Terminal output:

>>> from subprocess import Popen, PIPE
... from threading import Timer
... 
... cmd_from_yaml = ['R', '-e', 'shinyproxy::run_06_tabsets()']
... docker_cmd = "docker run -i {0} {1}".format(vol, img)
... docker_cmd = docker_cmd.split()
... docker_cmd.extend(cmd_from_yaml)
... timeout_sec = 10
... print("Running command: " + str(" ".join(docker_cmd)))
... proc = Popen(docker_cmd, stdout=PIPE, stderr=PIPE, shell=False)
... 
... timer = Timer(timeout_sec, proc.kill)
... try:
...     timer.start()
...     stdout, stderr = proc.communicate()
... finally:
...     timer.cancel()
... 
... if stderr:
...     proc.kill()
...     raise Exception("Error: " + str(stderr))
... 
Running command: docker run -i openanalytics/shinyproxy-demo R -e shinyproxy::run_06_tabsets()
Traceback (most recent call last):
  File "<input>", line 21, in <module>
Exception: Error: b'Loading required package: shiny\n\nListening on http://0.0.0.0:3838\n'

Note command docker run -i openanalytics/shinyproxy-demo R -e "shinyproxy::run_06_tabsets()" will work because of adding missing quotations - but my point in this question is if there is any way to do this automatically without manually adding missing quotations as it is done in docker compose.

Taz
  • 5,755
  • 6
  • 26
  • 63
  • Which part of it are you having trouble with? Do you have some code you're working on that you can include in the question? – David Maze Jan 23 '19 at 11:20
  • @DavidMaze Iv've just updated my question. Please have a look ;) – Taz Jan 23 '19 at 11:38
  • @Taz Have you tried to run that command from the commandline and checked the return value of that command? I also suggest you drop the `.split()` and construct `docker_cmd` as a list of strings right from the start (just in case `vol` or `img` contain whitespace) – Anthon Jan 23 '19 at 11:44
  • ...so the container ran and exited, and your script is upset that it printed something to its stderr. You might just run the `docker run` command outside the script and try to debug that in isolation, if you're not expecting it to exit. Also, the way you're constructing `docker_cmd` is dangerous and can be used to root the system, and it would be safer to construct it only in array form. – David Maze Jan 23 '19 at 11:44

0 Answers0