Search code examples
gruntjsvagrantchef-infrachef-solovagrantfile

Display Grunt Output from Vagrant


Updated with solution. Many thanks @Sneal

I have a Vagrant box that I've setup and the final Chef Solo command in my recipes is a grunt watch command that, when used normally from the command line, displays output of the tasks that grunt watch performs (i.e. less compiled to css, jslinting errors, etc.) but when my vagrant box comes up, I don't get the output piped through Vagrant. Everything else seems to working fine, including the LiveReload grunt performs via the Chrome LiveReload extension. This is what my Vagrantfile looks like:

VAGRANTFILE_API_VERSION = "2"
PORT = 9090

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.vm.box = "precise-server-cloudimg-i386-vagrant-disk1"
    config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-i386-vagrant-disk1.box"

    config.vm.network "forwarded_port", guest: PORT, host: 8080
    config.vm.network "forwarded_port", guest: 35729, host: 35729

    config.ssh.forward_agent = true

    config.vm.synced_folder "../", "/vagrant", owner: "vagrant", group: "vagrant", mount_options: ["dmode=775", "fmode=775"]

    config.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--memory", "2048"]
    end

    config.vm.provision :shell, :inline => "gem install chef --no-rdoc --no-ri --conservative"

    config.vm.provision "chef_solo" do |chef|
        chef.custom_config_path = "config.chef"
        chef.json = {
            :app => {
                :name => "website",
                :web_root => "/vagrant/app"
            },
            :user => {
                :name => "vagrant",
                :group => "vagrant"
            },
            :nginx => {
                :dir => "/etc/nginx",
                :port => PORT
            },
            :mysql => {
                :server_root_password => "reallyAwesomePassword",
                :allow_remote_root => false
            }
        }
        chef.run_list = [
            "recipe[apt]",
            "recipe[git]",
            "recipe[php5_ppa::from_ondrej]",
            "recipe[php]",
            "recipe[php5-fpm::install]",
            "recipe[nginx]",
            "recipe[mysql::client]",
            "recipe[mysql::server]",
            "recipe[composer]",
            "recipe[nodejs]",
            "recipe[website]"
        ]
    end
    # replaces the commented section below
    config.vm.provision :shell, :inline => "cd /vagrant && grunt watch"
end

I've added an extra log message after the grunt command that doesn't even get displayed. Here's my website recipe:

execute 'sudo gem install compass' do
    cwd '/vagrant'
    user 'vagrant'
    action :run
    environment ({'HOME' => '/home/vagrant', 'USER' => 'vagrant'})
end

execute 'sudo gem install sass' do
    cwd '/vagrant'
    user 'vagrant'
    action :run
    environment ({'HOME' => '/home/vagrant', 'USER' => 'vagrant'})
end

execute 'sudo npm install -g yo phantomjs' do
    cwd '/vagrant'
    user 'vagrant'
    action :run
    environment ({'HOME' => '/home/vagrant', 'USER' => 'vagrant'})
end

directory node.app.web_root do
    owner node.user.name
    mode "0755"
    recursive true
end

template "#{node.nginx.dir}/sites-available/#{node.app.name}.conf" do
    source "nginx.conf.erb"
    mode "0644"
end

nginx_site "#{node.app.name}.conf"

cookbook_file "#{node.app.web_root}/info.php" do
    source "info.php"
    mode "0755"
    owner node.user.name
end

execute 'bower install' do
    cwd '/vagrant'
    user 'vagrant'
    action :run
    environment ({'HOME' => '/home/vagrant', 'USER' => 'vagrant'})
end

execute 'sudo npm install' do
    cwd '/vagrant'
    user 'vagrant'
    action :run
    environment ({'HOME' => '/home/vagrant', 'USER' => 'vagrant'})
end

execute 'composer --verbose -v install' do
    cwd '/vagrant'
    user 'vagrant'
    action :run
    environment ({'HOME' => '/home/vagrant', 'USER' => 'vagrant'})
end

# execute 'grunt watch' do
#     cwd '/vagrant'
#     user 'vagrant'
#     action :run
#     environment ({'HOME' => '/home/vagrant', 'USER' => 'vagrant'})
# end

log "message" do
  message "All Done!"
  level :info
end

Is there something else I need to do to get the grunt output to display in the chef output? Still new to Vagrant and Chef so any advice is appreciated.


Solution

  • I would use Chef to configure my infrastructure and tools, but then use a separate Vagrant shell provisioner that actually executes the grunt command. The Vagrant shell provisioner will display stdout.