<commit_before># -*- mode: ruby -*-
# vi: set ft=ruby :
# Time-stamp: <Fri 2018-04-27 15:13 svarrette>
###########################################################################################
#             __     __                          _    __ _ _
#             \ \   / /_ _  __ _ _ __ __ _ _ __ | |_ / _(_) | ___
#              \ \ / / _` |/ _` | '__/ _` | '_ \| __| |_| | |/ _ \
#               \ 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
  # <name>:
  #   :hostname: <hostname>
  #   :desc: <VM-description>
  #   :os: <os>       # from the configured boxes
  #   :ram: <ram>
  #   :vcpus: <vcpus>
  #   :role: <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 = {
    :frontend     => 2,
    :controller   => 11,
    :node         => 101,
    :easybuild    => 1,
  }

  # 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 = name
    domain   = network[:domain]
    fqdn  =    "#{hostname}.#{domain}"
    os    =    defaults[:os].to_sym
    ram   =    defaults[:ram]
    vcpus =    defaults[:vcpus]
    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?
    abort "Unknown role '#{role}' for the VM '#{name}'" unless ip_index[role.to_sym]
    ip = ip_range[ ip_index[role.to_sym].to_i ].to_s
    ip_index[role.to_sym] += 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
<commit_msg>typo<commit_after># -*- mode: ruby -*-
# vi: set ft=ruby :
# Time-stamp: <Fri 2018-04-27 15:16 svarrette>
###########################################################################################
#             __     __                          _    __ _ _
#             \ \   / /_ _  __ _ _ __ __ _ _ __ | |_ / _(_) | ___
#              \ \ / / _` |/ _` | '__/ _` | '_ \| __| |_| | |/ _ \
#               \ 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
  # <name>:
  #   :hostname: <hostname>
  #   :desc: <VM-description>
  #   :os: <os>       # from the configured boxes
  #   :ram: <ram>
  #   :vcpus: <vcpus>
  #   :role: <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 = {
    :frontend     => 2,
    :controller   => 11,
    :node         => 101,
    :easybuild    => 1,
  }

  # 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 = name
    domain   = network[:domain]
    fqdn  =    "#{hostname}.#{domain}"
    os    =    defaults[:os].to_sym
    ram   =    defaults[:ram]
    vcpus =    defaults[:vcpus]
    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?
    abort "Unknown role '#{role}' for the VM '#{name}'" unless ip_index[role.to_sym]
    ip = ip_range[ ip_index[role.to_sym].to_i ].to_s
    ip_index[role.to_sym] += 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

{'fraction_non_alphanumeric': 0.19384449244060475, 'fraction_numerical': 0.0064794816414686825, 'mean_word_length': 3.1944354577806533, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 91, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2627555', 'n_tokens_mistral': 4491, 'n_tokens_neox': 4363, 'n_words': 1016}