Created
February 24, 2015 00:45
-
-
Save thiago/842d2fa6eb3a59a9969d to your computer and use it in GitHub Desktop.
Vagrant providers from YAML config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
vagrant: | |
provider: | |
digital_ocean: | |
provider: | |
token: 'YOUR TOKEN' | |
image: 'ubuntu-14-04-x64' | |
region: 'nyc3' | |
size: '1gb' | |
override: | |
ssh: | |
private_key_path: '~/.ssh/id_rsa' | |
vm: | |
box: 'digital_ocean' | |
box_url: "https://github.com/smdahlen/vagrant-digitalocean/raw/master/box/digital_ocean.box" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
VAGRANTFILE_API_VERSION = "2" | |
CURRENT_DIR = File.dirname(File.expand_path(__FILE__)) | |
CONFIG_FILE = "#{CURRENT_DIR}/config.yml" | |
CONFIG_FILE_LOCAL = "#{CURRENT_DIR}/config_local.yml" | |
if File.exist?(CONFIG_FILE) | |
require 'yaml' | |
data = YAML.load_file(CONFIG_FILE) | |
if File.exist?(CONFIG_FILE_LOCAL) | |
class Hash | |
def recursive_merge(h) | |
self.merge!(h) {|key, _old, _new| if _old.class == Hash then _old.recursive_merge(_new) else _new end } | |
end | |
end | |
local_data = YAML::load_file(CONFIG_FILE_LOCAL) | |
data.recursive_merge(local_data) | |
puts "Using config with local file" | |
else | |
puts "Using default config file" | |
end | |
if data['vagrant'] | |
data = data['vagrant'] | |
end | |
end | |
def recursive_set(obj, value, current_key='', separator='.') | |
if value.is_a?(Hash) | |
value.each do |key, val| | |
ckey = current_key.size > 0 ? "#{current_key}#{separator}#{key}" : "#{key}" | |
recursive_set obj, val, ckey | |
end | |
else | |
current_key = current_key ? "#{separator}#{current_key}" : "" | |
if value.is_a?(String) | |
eval "obj#{current_key}='#{value}'" | |
else | |
eval "obj#{current_key}=#{value}" | |
end | |
end | |
end | |
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| | |
# This box was found at https://cloud-images.ubuntu.com/vagrant/ | |
config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box" | |
config.vm.box = "ubuntu1404server" | |
# Providers | |
if data['provider'].is_a?(Hash) | |
data['provider'].each do |provider_key, provider_args| | |
config.vm.provider "#{provider_key}" do |provider, override| | |
recursive_set provider, provider_args['provider'] | |
recursive_set override, provider_args['override'] | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment