How can I automatically run Bundler within the project directory when Vagrant is provisioning?

295 views Asked by At

I'd like to Bundler to install Rails project dependencies when Vagrant is provisioning a new VM. This is what I currently have, but I get this line printed ==> default: Could not locate Gemfile or .bundle/ directory. The project directory does contain a Gemfile.

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.require_version ">= 1.9.1"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "bento/ubuntu-16.04"

  # Configurate the virtual machine to use 2GB of RAM
  config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id, "--memory", "2048"]
    vb.name = "Nuggets_API_Env"
  end

  # Forward the Rails server default port to the host
  config.vm.network :forwarded_port, guest: 3000, host: 3000

  # Use Chef Solo to provision our virtual machine
  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = ["cookbooks"]

    chef.add_recipe "apt"
    chef.add_recipe "build-essential"
    chef.add_recipe "nodejs"
    chef.add_recipe "openssl"
    chef.add_recipe "ruby_build"
    chef.add_recipe "ruby_rbenv::user"
    chef.add_recipe "vim"
    chef.add_recipe "postgresql::config_initdb"
    chef.add_recipe "postgresql::server"
    chef.add_recipe "postgresql::client"

    chef.json = {
      postgresql: {
        apt_pgdg_postgresql: true,
        version: "9.5",
        password: {
          postgres: "test1234",
        }
      },
      rbenv: {
        user_installs: [{
          user: 'vagrant',
          rubies: ["2.4.0"],
          global: "2.4.0",
          gems: {
            "2.4.0" => [
              { name: "bundler" }
            ]
          }
        }]
      },
    }
  end

  config.vm.provision "shell" do |s|
    s.path = "VM_Local_Setup.sh"
    s.upload_path = "/vagrant/VM_Local_Setup.sh"
    s.privileged = false
  end

end

Cheffile

site "https://supermarket.getchef.com/api/v1"

cookbook 'apt'
cookbook 'build-essential'
cookbook 'nodejs'
cookbook 'openssl'
cookbook 'postgresql'
cookbook 'ruby_build'
cookbook 'ruby_rbenv'
cookbook 'vim'

VM_Local_Setup.sh

#!/bin/bash

sudo apt-get update
sudo apt-get upgrade -y
bundler install
rbenv rehash
1

There are 1 answers

0
StringsOnFire On

Provisioning isn't done in the project directory (/vagrant), and that's where my Gemfile is.

Adding cd /vagrant solves this