Skip to content

Instantly share code, notes, and snippets.

@calavera
Created March 16, 2012 00:31
Show Gist options
  • Save calavera/2047875 to your computer and use it in GitHub Desktop.
Save calavera/2047875 to your computer and use it in GitHub Desktop.
Vagrant vms generator
My Chef recipes behave different if they're used in a single box or within a cluster of boxes.
This script helps me to generate Vagrant configurations for each one of my possible scenarios.
require './vms_init'
Vagrant::Config.run do |config|
@vms.each do |vm|
config.vm.define vm.id do |definition|
definition.vm.box = "lucid64"
definition.vm.network :hostonly, vm.hostname
definition.vm.provision :chef_solo do |chef|
Array(vm.recipes).each do |recipe|
chef.add_recipe recipe
end
chef.json vm.dna
end
end
end
end
require 'erb'
require 'json'
class Vm < Struct.new(:id, :role, :hostname, :recipes)
def initialize(*args)
super
end
def dna_file
"#{role}.json"
end
def dna
JSON.parse(File.read(dna_file))
end
end
# Define the vms that you want to boot
@app_master = Vm.new('app_master', 'app_master', '192.148.100.10', 'base::app_master')
@app_slave = Vm.new('app_slave', 'app', '192.148.100.11', 'base::app')
@db_master = Vm.new('db_master', 'db_master', '192.148.100.12', 'base::db_master')
@vms = [@app_master, @app_slave, @db_master]
@vms.each do |vm|
@node = vm
# Generate a json file per vm role
template = ERB.new File.read('dna.json.erb')
File.open(@node.dna_file, 'w') {|f| f.write template.result}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment