2

I am new to Vagrant and Ruby.

Is it possible to get user keyboard input into a ruby script I am triggering from a Vagrantfile thus :

config.trigger.after [:provision] do |trigger|  
  trigger.run = {inline: "C:/HashiCorp/Vagrant/embedded/mingw64/bin/ruby app/scripts/test.rb"}  
end

My host is Windows 10, my guest is bento\ubuntu-18.10.
I am successfully creating a machine running in Virtualbox.

I have simplified test.rb as much as possible.

This code :

#!/usr/bin/ruby  

puts "enter selection"  
puts "xrestore"  

returns this as I expect :

c:\Users\neile\Documents\development\vagrant\bento\ubuntu-18.10-mysql>vagrant provision  
==> vm1: [vagrant-hostsupdater] Checking for host entries  
==> vm1: Running action triggers after provision ...  
==> vm1: Running trigger...  
    vm1: Running local: Inline script  
    vm1: C:/HashiCorp/Vagrant/embedded/mingw64/bin/ruby app/scripts/restore-db.rb  
    vm1: enter selection  
    vm1: xrestore  
c:\Users\neile\Documents\development\vagrant\bento\ubuntu-18.10-mysql>  

This code :

#!/usr/bin/ruby  

puts "enter selection"  
STDOUT.flush()  
xrestore = STDIN.gets.chomp  
puts "xrestore"  

returns this :

>vagrant provision  
==> vm1: [vagrant-hostsupdater] Checking for host entries  
==> vm1: Running action triggers after provision ...  
==> vm1: Running trigger...  
    vm1: Running local: Inline script  
    vm1: C:/HashiCorp/Vagrant/embedded/mingw64/bin/ruby app/scripts/restore-db.rb  
    vm1: enter selection  

There is no prompt to enter a value for variable : xrestore
and processing appears to stop.

To terminate the process I am typing Ctrl-C twice.

If I omit : STDOUT.flush()
I don't even get the prompt "enter selection"

I have tried putting the path to ruby.exe in the System Environment Variable PATH, and removing the path from the trigger.run statement.
This produces the same results as above.

(BTW, I tried doing this using a bash script and I had similar issues with the bash read statement.
Googling, I found others had the same issue with Ruby and Bash, but no solution.)

Is using 'gets' in a ruby script called from my Vagrantfile even possible?

I do have a workaround, which is to place the STDIN.gets in the Vagrantfile, not in the called script.

Neil E
  • 43
  • 6
  • 1
    What you are doing is really more of a provisioner than a trigger, and because of that your solution may lie in that direction. – Matthew Schuchard Mar 19 '19 at 11:04
  • Thanks Matt. Using the provisioner works. I decided on a bash script, instead of Ruby, but either way, the script is now being called, and I can pass parameters. config.vm.provision "optional-name-parameter", type: "shell" do |s| s.path = "app/scripts/restore-db.sh" s.args = "'hello, world!'" end – Neil E Mar 20 '19 at 02:47
  • I couldn't work out how to preserve the formatting in the previous post. – Neil E Mar 20 '19 at 02:48
  • Hi Matt, I would mark your comment as the accepted answer, but I can't see how to. The help offers me this "To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in." but I can't see a check mark. – Neil E Mar 20 '19 at 02:57
  • I did not really give an answer, but rather just made a suggestion that I hoped would be helpful. Glad it was. – Matthew Schuchard Mar 20 '19 at 11:11

0 Answers0