Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I interact with a Vagrant shell provisioning script?

Tags:

vagrant

heroku

I have a shell provisioning script that invokes a command that requires user input - but when I run vagrant provision, the process hangs at that point in the script, as the command is waiting for my input, but there is nowhere to give it. Is there any way around this - i.e. to force the script to run in some interactive mode?

The specifics are that I creating a clean Ubuntu VM, and then invoking the Heroku CLI to download a database backup (this is in my provisioning script):

curl -o /tmp/db.backup `heroku pgbackups:url -a myapp`

However, because this is a clean VM, and therefore this is the first time that I have run an Heroku CLI command, I am prompted for my login credentials. Because the script is being managed by Vagrant, there is no interactive shell attached, and so the script just hangs there.

like image 205
Hugo Rodger-Brown Avatar asked Jan 18 '13 18:01

Hugo Rodger-Brown


2 Answers

If you want to pass temporary input or variables to a Vagrant script, you can have them enter their credentials as temporary environment variables for that command by placing them first on the same line:

username=x password=x vagrant provision

and access them from within Vagrantfile as

$u = ENV['username']
$p = ENV['password']

Then you can pass them as an argument to your bash script:

config.vm.provision "shell" do |s|
    s.inline: "echo username: $1, password: $2"
    s.args: [$u, $p]
end

You can install something like expect in the vm to handle passing those variables to the curl command.

like image 107
Richard Avatar answered Oct 22 '22 01:10

Richard


I'm assuming you don't want to hard code your credentials in plain text thus trying to force an interactive mode. Thing is just as you I don't see such option in vagrant provision doc ( http://docs.vagrantup.com/v1/docs/provisioners/shell.html ) so one way or another you need to embed the authentication within your script. Have you thought about using something like getting a token and use the heroku REST Api instead of the CLI? https://devcenter.heroku.com/articles/authentication

like image 42
Jaime Gago Avatar answered Oct 22 '22 01:10

Jaime Gago