# -*- mode: ruby -*- # vi: set ft=ruby : # Time-stamp: ########################################################################################### # __ __ _ __ _ _ # \ \ / /_ _ __ _ _ __ __ _ _ __ | |_ / _(_) | ___ # \ \ / / _` |/ _` | '__/ _` | '_ \| __| |_| | |/ _ \ # \ V / (_| | (_| | | | (_| | | | | |_| _| | | __/ # \_/ \__,_|\__, |_| \__,_|_| |_|\__|_| |_|_|\___| # |___/ ########################################################################################### require 'yaml' require 'ipaddr' require 'deep_merge' require 'pp' require 'erb' ###### Expected Vagrant plugins detection ###### # For more information on the below plugins: # - https://github.com/oscar-stack/vagrant-hosts # - https://github.com/dotless-de/vagrant-vbguest # - https://github.com/emyl/vagrant-triggers # - https://github.com/fgrehm/vagrant-cachier # Terminal-table is a nice ruby gem for automatically print tables with nice layout ### [ 'vagrant-hosts', 'vagrant-vbguest', 'vagrant-triggers', 'vagrant-cachier', 'terminal-table' ].each do |plugin| abort "Install the '#{plugin}' plugin with 'vagrant plugin install #{plugin}'" unless Vagrant.has_plugin?("#{plugin}") end require 'terminal-table' ### Global variables ### # Vagrantfile API/syntax version. Don't touch unless you know what you're doing! VAGRANTFILE_API_VERSION = "2" # Eventually a local YAML configuration for the deployment TOP_SRCDIR = File.expand_path File.dirname(__FILE__) TOP_CONFDIR = File.join(TOP_SRCDIR, 'vagrant') config_file = File.join(TOP_CONFDIR, 'config.yaml') #SHARED_DIR = File.join('vagrant', 'shared') ### Default settings ### DEFAULT_SETTINGS = { # Default images settings :defaults => { :os => :<%= config[:os] %>, :ram => <%= config[:ram] %>, :vcpus => <%= config[:vcpus] %>, :vbguest_auto_update => true, # :nodes => 1, }, # Default domain settings :network => { :domain => '<%= config[:domain] %>', :range => '<%= config[:range] %>', :client_ip_start_offset => 100, }, # Default Boxes :boxes => { :centos7 => '<%= config[:boxes][:centos7] %>', :debian8 => '<%= config[:boxes][:debian8] %>', :ubuntu14 => '<%= config[:boxes][:ubuntu14] %>' }, # virtual images to deploy # : # :hostname: # :desc: # :os: # from the configured boxes # :ram: # :vcpus: # :role: # supported: [ 'controller', 'frontend' ] :vms => { # IF in single mode, below is the definition of the box to deploy 'default' => { :hostname => 'vm', :desc => 'Testing Vagrant box', }, }, } # List of default provisioning scripts DEFAULT_PROVISIONING_SCRIPTS = [ "vagrant/bootstrap.sh" ] # Load the settings (eventually overwritten using values from the yaml file 'config/vagrant.yaml') settings = DEFAULT_SETTINGS.clone if File.exist?(config_file) config = YAML::load_file config_file #puts config.to_yaml settings.deep_merge!( config ) if config end #puts settings.to_yaml # abort 'end' #pp settings abort "Undefined settings" if settings.nil? ############################################################ # Complete configuration of the boxes to deploy defaults = settings[:defaults] network = settings[:network] ############################################################ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| ### Common configs shared by all VMs ### # Cache plugin -- Supports local cache, so you don't wast bandwitdh # vagrant plugin install vagrant-cachier # see https://github.com/fgrehm/vagrant-cachier config.cache.auto_detect = true if Vagrant.has_plugin?("vagrant-cachier") # check if VirtualBox Guest Additions are up to date if Vagrant.has_plugin?("vagrant-vbguest") # set auto_update to false, if you do NOT want to check the correct # additions version when booting these boxes config.vbguest.auto_update = defaults[:vbguest_auto_update] end # Shell provisioner, to bootstrap each box with the minimal settings/packages DEFAULT_PROVISIONING_SCRIPTS.each do |script| config.vm.provision "shell", path: "#{script}", keep_color: true end config.vm.synced_folder ".", "/vagrant", type: "virtualbox" ## Uncomment the below part if you want and additional shared directory # config.vm.synced_folder "vagrant/shared", "/shared", mount_options: ['dmode=777','fmode=777'], # type: "virtualbox" # Shared directory for users # network settings ipaddr = IPAddr.new network[:range] ip_range = ipaddr.to_range.to_a ip_index = 2 # cosmetics for the post-up message __table = { :title => "Virtual environment deployed on Vagrant", :headings => [ 'Name', 'Hostname', 'OS', 'vCPU/RAM', 'Description', 'IP' ], :rows => [], } #__________________________________ settings[:vms].each do |name, node| hostname = node[:hostname] ? node[:hostname] : name domain = network[:domain] fqdn = "#{hostname}.#{domain}" os = node[:os] ? node[:os].to_sym : defaults[:os].to_sym ram = node[:ram] ? node[:ram] : defaults[:ram] vcpus = node[:vcpus] ? node[:vcpus] : defaults[:vcpus] desc = node[:desc] ? node[:desc] : 'n/a' abort "Non-existing box OS '#{os}' for the VM '#{name}'" if settings[:boxes][os.to_sym].nil? abort "Empty IP address range" if ip_range.empty? ip = ip_range[ ip_index.to_i ].to_s ip_index += 1 # increment index for the next VM of this type config.vm.define "#{name}" do |c| c.vm.box = settings[:boxes][os.to_sym] c.vm.hostname = "#{fqdn}" c.vm.network :private_network, :ip => ip c.vm.provision :hosts, :sync_hosts => true c.vm.provider "virtualbox" do |v| v.customize [ 'modifyvm', :id, '--name', hostname, '--memory', ram.to_s ] v.customize [ 'modifyvm', :id, '--cpus', vcpus.to_s ] if vcpus.to_i > 1 #v.customize [ 'setextradata', :id, 'VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root', '1'] end end __table[:rows] << [ name, fqdn, os.to_sym, "#{vcpus}/#{ram}", desc, ip] end # settings config.trigger.after :up do puts Terminal::Table.new __table end end