Skip to content

Instantly share code, notes, and snippets.

@gosuri
Last active August 29, 2015 14:09
Show Gist options
  • Save gosuri/d914479c5965da618b76 to your computer and use it in GitHub Desktop.
Save gosuri/d914479c5965da618b76 to your computer and use it in GitHub Desktop.
Vagrant file to run chef-solo.
# -*- mode: ruby -*-
# vi: set ft=ruby :
HOSTNAME = ENV['BOXHOSTNAME'] || "app-berkshelf"
BASE_BOX = ENV["BASEBOX"] || "ubuntu/trusty64"
DEBUG = ENV["DEBUG"] || false
RUNLIST = ENV['RUNLIST'] || %w(app)
CHEFJSON = ENV['CHEFJSON'] || {
mysql: {
server_root_password: 'rootpass',
server_debian_password: 'debpass',
server_repl_password: 'replpass'
}
}
VAGRANTFILE_API_VERSION = "2"
VAGRANT_REQUIRE_VERSION = ">= 1.5.0"
ERR_MISSINGPLUGIN = "%s plugin not detected. Please install the plugin with using 'vagrant plugin install %s'"
ERR_MISSINGENVFILE = "Missing .env file. Unable to load environment variables from .env file doesn't exits, see https://github.com/gosuri/vagrant-env for more info."
SHELL = <<-BASH
# remove warning stdin: is not a tty
sed -i 's/.*mesg.*/tty -s \&\& mesg/' /root/.profile
if [ ! -f "/usr/local/bin/chef-solo" ]; then
export DEBIAN_FRONTEND=noninteractive
# Upgrade headlessly (this is only safe-ish on vanilla systems)
aptitude update &&
apt-get -o Dpkg::Options::="--force-confnew" --force-yes -fuy dist-upgrade &&
# Install Ruby and Chef
aptitude install -y ruby1.9.1 ruby1.9.1-dev make &&
sudo gem1.9.1 install --no-rdoc --no-ri chef --version 11.16.4
fi
BASH
Vagrant.require_version(VAGRANT_REQUIRE_VERSION)
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = BASE_BOX
with_plugin 'vagrant-env' do
config.env.enable
current_dir = File.expand_path(File.dirname __FILE__)
log(ERR_MISSINGENVFILE, :warning) unless File.exists?(File.join current_dir, ".env")
end
with_plugin 'vagrant-berkshelf' do
config.berkshelf.enabled = true
config.berkshelf.berksfile_path = "Berksfile"
end
config.vm do |vm|
vm.hostname = HOSTNAME
vm.network :private_network, type: "dhcp"
vm.network "forwarded_port", guest: 8080, host: 8080, auto_correct: true
end
config.vm.provider 'virtualbox' do |v|
v.memory = 2042
end
config.vm.provision :shell, inline: SHELL
config.vm.provision 'chef_solo' do |chef|
chef.json = CHEFJSON
chef.run_list = RUNLIST
if DEBUG
chef.log_level = 'debug'
chef.verbose_logging = true
end
end
end
# Fails gracefully if plugin is not found
# yields the block other wise
def with_plugin(plugin, &block)
if Vagrant.has_plugin?(plugin)
yield if block
else
log sprintf(ERR_MISSINGPLUGIN, plugin, plugin), :warning
end
end
def log(msg, type=nil)
msg = "==> #{type}: #{msg}"
$stdout.puts case type
when :info
"\033[1m#{msg}\033[22m" # bold
when :warning
"\033[33m#{msg}\033[0m" # brown
when :error
"\033[31m#{msg}\033[0m" # red
else msg # normal
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment