-
-
Save anonymous/afd5fa00295872738f8f to your computer and use it in GitHub Desktop.
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
# -*- mode: ruby -*- | |
# vi: set ft=ruby : | |
require 'yaml' | |
require 'ipaddr' | |
settings = YAML.load_file 'vagrant.yml' | |
aws_settings = settings["aws"] | |
vm_settings = settings["vm"] | |
# Read YAML file with box details | |
servers = YAML.load_file('servers.yml') | |
#ARGV | |
skip = 0 | |
while skip < ARGV.length | |
if ARGV[skip].start_with?(arg='--vagrant-namespace=') | |
v = ARGV.delete_at(skip).dup | |
v.slice! arg | |
namespacefilter = v.to_s # set namespace | |
else # skip over "official" vagrant args | |
skip = skip + 1 | |
end | |
end | |
vms = [] | |
ansible_groups = {}; | |
#Assign Children groups for ansible | |
ansible_groups['db:children'] = []; | |
ansible_groups['db:children'] << 'development'; | |
ansible_groups['elastic:children'] = []; | |
ansible_groups['elastic:children'] << 'development'; | |
ansible_groups['elastic:children'] << 'test'; | |
ansible_groups['elastic:children'] << 'staging'; | |
ansible_groups['elastic:children'] << 'production-elastic'; | |
ansible_groups['api:children'] = []; | |
ansible_groups['api:children'] << 'development'; | |
ansible_groups['api:children'] << 'test'; | |
ansible_groups['api:children'] << 'staging'; | |
ansible_groups['api:children'] << 'production-api'; | |
ansible_groups['staging:children'] = []; | |
ansible_groups['staging:children'] << 'staging-release'; | |
ansible_groups['staging:children'] << 'staging-development'; | |
ansible_groups['webserver:children'] = []; | |
ansible_groups['webserver:children'] << 'development'; | |
ansible_groups['webserver:children'] << 'staging'; | |
ansible_groups['webserver:children'] << 'production-webserver'; | |
ansible_groups['prerender:children'] = []; | |
ansible_groups['prerender:children'] << 'production-prerender'; | |
ansible_groups['profiling:children'] = []; | |
ansible_groups['profiling:children'] << 'development'; | |
ansible_groups['profiling:children'] << 'staging'; | |
ansible_groups['analytics:children'] = []; | |
ansible_groups['analytics:children'] << 'development'; | |
ansible_groups['analytics:children'] << 'staging'; | |
servers.each do |servers, i| | |
if servers['virtualbox'] != nil | |
network_obj = IPAddr.new servers['network'] | |
range = network_obj.to_range.to_a | |
cidr = (32-(Math.log(range.length)/Math.log(2))).to_i | |
offset = 100 # start hosts after here | |
# remove reserved values | |
range[0] = '__reserved_network_addr' # network | |
range[1] = '__reserved_router_addr' # router (reserved) | |
end | |
#if namespacefilter != nil && namespacefilter != servers['namespace'] | |
# next | |
#end | |
servers['name'] = servers['namespace'] | |
count = servers['count'] | |
(1..count).each do |i| | |
h = "#{servers['namespace']}-#{i}" | |
server = servers.dup | |
server['name'] = h | |
if servers['virtualbox'] != nil | |
server['ip'] = range[offset+i].to_s | |
end | |
if ansible_groups.has_key?(servers["ansible"]["group"]) == false | |
ansible_groups[servers["ansible"]["group"]] = []; | |
end | |
ansible_groups[servers["ansible"]["group"]] << server['name']; | |
# generate names and add in the defaults | |
vms.push(server) | |
if namespacefilter == servers['namespace'] | |
ARGV << h; | |
end | |
end | |
end | |
print ARGV; | |
Vagrant.configure("2") do |config| | |
vms.each do |vm| | |
config.vm.define vm["name"] do |box| | |
#ENV['VAGRANT_DEFAULT_PROVIDER'] = vm["provider"] | |
box.vm.box = vm["box"] | |
#Virtualbox Provider | |
if vm['virtualbox'] != nil | |
box.vm.network "private_network", ip: vm["ip"] | |
box.vm.synced_folder "./", "/var/www/api.elephone.vb", :owner => "vagrant", :group => "www-data" | |
box.vm.provider :virtualbox do |vb| | |
vb.name = vm["name"] | |
vb.customize [ | |
"modifyvm", :id, | |
"--memory", vm_settings["memory"], | |
"--name", vm["name"], | |
"--natdnshostresolver1", "on", | |
"--cpus", vm_settings["cpus"], | |
] | |
end | |
end | |
#Amazon Webservices Provider | |
if vm['aws'] != nil | |
box.vm.box_url = vm["box_url"] | |
box.vm.synced_folder ".", "/vagrant", type: "rsync", rsync__exclude: ["src/vendor"], disabled: vm["sync"]["disabled"] | |
box.vm.provider :aws do |aws, override| | |
aws.access_key_id = aws_settings['access_key_id'] | |
aws.secret_access_key = aws_settings['secret_access_key'] | |
aws.keypair_name = aws_settings['keypair_name'] | |
override.ssh.private_key_path = aws_settings['private_key_path'] | |
aws.instance_type = vm["aws"]["instance_type"] | |
aws.subnet_id = vm["aws"]["subnet_id"] | |
aws.region = vm["aws"]["region"] | |
aws.availability_zone = vm["aws"]["availability_zone"] | |
aws.ami = vm["aws"]["ami"] | |
override.ssh.username = "ubuntu" | |
aws.elastic_ip = vm["aws"]["elastic_ip"] | |
aws.associate_public_ip = vm["aws"]["associate_public_ip"] | |
aws.tags = { | |
"Name" => vm["name"], | |
"Environment" => vm["aws"]['environment'] | |
} | |
if vm["aws"]["elb"] != nil | |
aws.elb = vm["aws"]["elb"] | |
end | |
aws.security_groups = vm["aws"]["security_groups"] | |
end | |
end | |
box.vm.provision :ansible do |ansible| | |
ansible.playbook = vm["ansible"]["playbook"] | |
ansible.limit = 'all' | |
#ansible.verbose = 'vvvv' | |
ansible.groups = ansible_groups; | |
if vm["ansible"]["vault_password_file"] != nil | |
ansible.vault_password_file = vm["ansible"]["vault_password_file"] | |
end | |
if vm["ansible"]["skip_tags"] != nil | |
ansible.skip_tags = vm["ansible"]["skip_tags"] | |
end | |
ansible.vault_password_file="vault_pass.txt" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment