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.