1

Running commands or script lines with simple tests, like -e, in vagrant using the ssh subcommand works fine (e.g.

vagrant ssh -c 'if ! [ -e file.deb ] ; then wget http://a.b/file.deb; fi'

as soon as string comparison and command execution bash quoting gets involved, complexity immediately rises to the sky. I tried

  • escaping strings, but that doesn't seem to work in general (complexity of escaping of escaping of escaping ... in bash is just inacceptable)
  • writing everything in a script file with cat <<EOF > file [code]EOF idiom, e.g.

    vagrant ssh -c 'cat <<EOF > fetch.sh \
    #!/bin/bash \
    if [ "$(md5sum file.deb | cut -d \" \" -f 1)" != "d757d51819f8f945ae7715b532ab8d2e" ] ; then wget http://a/b/file.deb; fi \
    EOF'
    vagrant ssh -c 'bash ./fetch.sh'
    

    causes ./fetch.sh: line 1: syntax error near unexpected token `if'

  • the failure of the cat EOF idiom implies failure of any sort of script created in vagrant (python, groovy, etc. would suffer from the same incapacity of bash to provide usable string quoting/be usable at all)

Is there any way to execute script code with complex statements in tests inside a vagrant box? Do I seriously need to transfer the script via the localhost interface (using FTP or something similar)?

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177

1 Answers1

0

With knowledge of How to use SSH to run a shell script on a remote machine? it's possible to connect to vagrant using

ssh -p [port] -i [keyfile] vagrant@localhost 'bash -s' < script.sh

(with [port] and [keyfile] retrieved from vagrant ssh-config) and the script code put in script.sh.

The port can change if multiple vagrant instances are running (each starting its own SSH server), so you might need to parse vagrant ssh-config.

Community
  • 1
  • 1
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177