Using Composer with Vagrant & Puppet
Using Composer in your Vagrant / Puppet setup is as simple as this:
exec { 'composer-run':
command => "/usr/local/bin/composer.phar install --dev",
cwd => '/vagrant/',
user => root,
group => root,
timeout => 0,
require => [Package['git'], Package['php5-cli'], File['composer.phar']]
}
However, as I have written before we are in need to wrap the Composer call with the expect command to handle the HTTP Basic Auth that protects our private Satis repository. Obviously I want Puppet to run without interaction so I somehow need to pass the username and password for the Satis repo to Vagrant which needs to pass it Puppet which in turn needs to pass it to expect. Long story short this is super simple, although it is not the perfect solution I have to admit. The downside of the story is that you need to expose your username and password as an environment variable.
This is how your Vagrantfile needs to look like:
Vagrant.configure("2") do |config|
config.vm.define "vm" do |vm_cfg|
vm_cfg.vm.box = 'debian-wheezy-64'
vm_cfg.vm.box_url = 'https://dl.dropboxusercontent.com/u/86066173/debian-wheezy.box'
vm_cfg.vm.hostname = "vm"
vm_cfg.vm.synced_folder ".", "/vagrant", owner: "www-data", group: "vagrant"
vm_cfg.vm.provision "puppet" do |puppet|
# access the environment variables
COMPOSER_USER=ENV['COMPOSER_USER']
COMPOSER_PASS=ENV['COMPOSER_PASS']
# pass the variables to facter
puppet.facter = {
"composer_user" => "#{COMPOSER_USER}",
"composer_pass" => "#{COMPOSER_PASS}",
}
puppet.manifests_path = "puppet/manifests"
puppet.module_path = "puppet/modules"
puppet.manifest_file = "init.pp"
puppet.options = ['--verbose --debug']
end
end
end
As you can see you are able to access the environment variables via ENV[]. In our puppet script we can now use the variables set by facter and feed them to the expect call like this:
exec { 'composer-run':
command => "expect -c '
set timeout -1
spawn /usr/local/bin/composer.phar install --dev
expect {
-re { Username: } {
send \"${::composer_user}\r\"
exp_continue
}
-re { Password: } {
send \"${::composer_pass}\r\"
exp_continue
}
-re {^Generating autoload files }
}'",
cwd => '/vagrant/',
user => root,
group => root,
timeout => 0,
require => [Package['git'], Package['expect'], Package['php5-cli'], File['composer.phar']]
}
Now wen calling vagrant up or vagrant provision the environment variables will be picked up by Vagrant and passed to Puppet. When running the Puppet scripts from command line you need to pass the facter variables manually to make sure the Composer command will run fine.