From 108165414bf3dcc5fffd1258d2a1d4a89b529ed7 Mon Sep 17 00:00:00 2001 From: hoatle Date: Thu, 9 Feb 2017 16:21:00 +0700 Subject: [PATCH 1/4] @ #246 | should allow extend Vagrantfile with Vagrantfile-ext.rb --- .gitignore | 2 ++ Vagrantfile | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index c37c7519..4e7896c2 100644 --- a/.gitignore +++ b/.gitignore @@ -161,4 +161,6 @@ home/.docker/* nodes/*.json +# https://github.com/teracyhq/dev/issues/246 vagrant_config_override.json +Vagrantfile-ext.rb diff --git a/Vagrantfile b/Vagrantfile index 5d34cff0..34102a22 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -1,16 +1,11 @@ # -*- mode: ruby -*- # vi: set ft=ruby : +require 'json' +load File.dirname(__FILE__) + '/lib/utility.rb' +load File.dirname(__FILE__) + '/lib/provisioner.rb' Vagrant.configure("2") do |config| - - # TODO(hoatle): support this json configuration - # config.omnibus.chef_version = :latest - - require 'json' - load File.dirname(__FILE__) + '/lib/utility.rb' - load File.dirname(__FILE__) + '/lib/provisioner.rb' - # Load default setting file = File.read(File.dirname(__FILE__) + '/vagrant_config.json') data_hash = JSON.parse(file) @@ -370,3 +365,12 @@ Vagrant.configure("2") do |config| end end end + +begin + ext_file_path = File.dirname(__FILE__) + '/Vagrantfile-ext.rb' + if File.file?(ext_file_path) + load ext_file_path + end +rescue Exception => msg + puts red(msg) +end From b03d4e277cb3c24676cf8944fa688d9e6d6923ba Mon Sep 17 00:00:00 2001 From: hoatle Date: Thu, 9 Feb 2017 21:57:12 +0700 Subject: [PATCH 2/4] @ #246 | should allow extend Vagrantfile with Vagrantfile-ext.rb: support to load more extension files --- Vagrantfile | 55 ++++++++++++++++++++++++++------------------- vagrant_config.json | 6 +++++ 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/Vagrantfile b/Vagrantfile index 34102a22..bc25fb27 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -4,28 +4,29 @@ require 'json' load File.dirname(__FILE__) + '/lib/utility.rb' load File.dirname(__FILE__) + '/lib/provisioner.rb' -Vagrant.configure("2") do |config| - - # Load default setting - file = File.read(File.dirname(__FILE__) + '/vagrant_config.json') - data_hash = JSON.parse(file) - - # Check and override if exist any match JSON object from vagrant_config_override.json - if File.exist? (File.dirname(__FILE__) + '/vagrant_config_override.json') - override_file = File.read(File.dirname(__FILE__) + '/vagrant_config_override.json') - - begin - data_hash = overrides(data_hash, JSON.parse(override_file)) - rescue Exception => msg - puts red(msg) - puts red('from vagrant_config_override.json') - ans = prompt yellow("some errors have occured and 'vagrant_config_override.json' file will not be used, do you want to continue? [y/N]: ") - if ans.downcase != 'y' - exit! - end +# Load default setting +file = File.read(File.dirname(__FILE__) + '/vagrant_config.json') +data_hash = JSON.parse(file) +override_hash = nil + +# Check and override if exist any match JSON object from vagrant_config_override.json +if File.exist? (File.dirname(__FILE__) + '/vagrant_config_override.json') + override_file = File.read(File.dirname(__FILE__) + '/vagrant_config_override.json') + + begin + override_hash = JSON.parse(override_file) + data_hash = overrides(data_hash, override_hash) + rescue Exception => msg + puts red(msg) + puts red('from vagrant_config_override.json') + ans = prompt yellow("some errors have occured and 'vagrant_config_override.json' file will not be used, do you want to continue? [y/N]: ") + if ans.downcase != 'y' + exit! end - end +end + +Vagrant.configure("2") do |config| vm_hash = data_hash["vm"] @@ -367,9 +368,17 @@ Vagrant.configure("2") do |config| end begin - ext_file_path = File.dirname(__FILE__) + '/Vagrantfile-ext.rb' - if File.file?(ext_file_path) - load ext_file_path + extension_paths = data_hash['vagrant']['extension_paths'] + extension_paths.each do |path| + ext_file_path = File.dirname(__FILE__) + '/' + path + if File.file?(ext_file_path) + load ext_file_path + else + # warnings if override vagrant:extension_paths + if !override_hash.nil? and !override_hash['vagrant'].nil? and !override_hash['vagrant']['extension_paths'].nil? + puts red(ext_file_path + ' is missing!') + end + end end rescue Exception => msg puts red(msg) diff --git a/vagrant_config.json b/vagrant_config.json index 26ade0f1..6db4d754 100644 --- a/vagrant_config.json +++ b/vagrant_config.json @@ -1,4 +1,10 @@ { + "vagrant": { + "extension_paths": [ // add paths of Vagrantfile-ext.rb files to be loaded + // the path must be relative to the Vagrantfile + "Vagrantfile-ext.rb" + ] + }, "vm": { "hostname": "teracy.dev", "box": "bento/ubuntu-16.04", From 5070c29d5fff41ed8cf08e780b9fdf65a8fe7210 Mon Sep 17 00:00:00 2001 From: hoatle Date: Thu, 9 Feb 2017 22:01:55 +0700 Subject: [PATCH 3/4] @ #248 | should support to use vagrant-hostmanager instead of vagrant-hostsupdater plugin --- Vagrantfile | 52 +++++++++++++++++++++++++++++++++------------ lib/utility.rb | 25 ++++++++++++++++++++++ vagrant_config.json | 27 +++++++++++++++++------ 3 files changed, 85 insertions(+), 19 deletions(-) diff --git a/Vagrantfile b/Vagrantfile index bc25fb27..4f9006aa 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -206,30 +206,56 @@ Vagrant.configure("2") do |config| # this is current fixed config, not dynamic plugins config # FIXME(hoatle): #186 should fix this - - if Vagrant.has_plugin?(plugin_name) and plugin.key?('config_key') + if Vagrant.has_plugin?(plugin_name) and plugin['enabled'] == true and plugin.key?('config_key') config_key = plugin['config_key'] + options = plugin['options'] if 'gatling' == config_key - unless plugin['latency'].nil? - config.gatling.latency = plugin['latency'] + unless options['latency'].nil? + config.gatling.latency = options['latency'] + end + + unless options['time_format'].nil? or options['time_format'].empty? + config.gatling.time_format = options['time_format'] + end + + unless options['rsync_on_startup'].nil? + config.gatling.rsync_on_startup = options['rsync_on_startup'] + end + + elsif 'hostmanager' == config_key + if Vagrant.has_plugin?('vagrant-hostsupdater') + puts red('recommended: $ vagrant plugin uninstall vagrant-hostsupdater') + end + + unless options['enabled'].nil? + config.hostmanager.enabled = options['enabled'] + end + + unless options['manage_host'].nil? + config.hostmanager.manage_host = options['manage_host'] + end + + unless options['manage_guest'].nil? + config.hostmanager.manage_guest = options['manage_guest'] end - unless plugin['time_format'].nil? or plugin['time_format'].empty? - config.gatling.time_format = plugin['time_format'] + unless options['ignore_private_ip'].nil? + config.hostmanager.ignore_private_ip = options['ignore_private_ip'] end - unless plugin['rsync_on_startup'].nil? - config.gatling.rsync_on_startup = plugin['rsync_on_startup'] + unless options['include_offline'].nil? + config.hostmanager.include_offline = options['include_offline'] end - elsif 'hostsupdater' == config_key - unless plugin['aliases'].nil? or plugin['aliases'].empty? - config.hostsupdater.aliases = plugin['aliases'] + unless options['aliases'].nil? + config.hostmanager.aliases = options['aliases'] end - unless plugin['remove_on_suspend'].nil? or plugin['remove_on_suspend'].empty? - config.hostsupdater.remove_on_suspend = plugin['remove_on_suspend'] + # workaround for :public_network + # maybe this will not work with :private_network + config.hostmanager.ip_resolver = proc do |vm, resolving_vm| + read_ip_address(vm) end end end diff --git a/lib/utility.rb b/lib/utility.rb index d02e77a1..8b2b24fc 100644 --- a/lib/utility.rb +++ b/lib/utility.rb @@ -1,3 +1,5 @@ +$logger = Log4r::Logger.new('lib/utility') + # Utility functions def overrides(obj1, obj2) obj2.each do |key, value| @@ -89,3 +91,26 @@ def prompt(message) print message return STDIN.gets.chomp end + +# thanks to https://github.com/devopsgroup-io/vagrant-hostmanager/issues/121#issuecomment-69050265 + +def read_ip_address(machine) + command = "LANG=en ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1 }'" + result = "" + + $logger.info "Processing #{ machine.name } ... " + + begin + # sudo is needed for ifconfig + machine.communicate.sudo(command) do |type, data| + result << data if type == :stdout + end + $logger.info "Processing #{ machine.name } ... success" + rescue + result = "# NOT-UP" + $logger.info "Processing #{ machine.name } ... not running" + end + + # the second inet is more accurate + result.chomp.split("\n").last +end diff --git a/vagrant_config.json b/vagrant_config.json index 6db4d754..01489b70 100644 --- a/vagrant_config.json +++ b/vagrant_config.json @@ -155,14 +155,29 @@ "name": "vagrant-gatling-rsync", "config_key": "gatling", "required": true, - // auto rsync watch when up/ reload done - // should try to reduce rsync latency to 0.5s instead of 1s #173 - "latency": 0.5, //0.5s - "time_format": "%H:%M:%S", - "rsync_on_startup": true + "enabled": true, + "options": { + // auto rsync watch when up/ reload done + // should try to reduce rsync latency to 0.5s instead of 1s #173 + "latency": 0.5, //0.5s + "time_format": "%H:%M:%S", + "rsync_on_startup": true + } }, { "_id": "1", "name": "vagrant-rsync-back", - "required": true + "required": true, + "enabled": true + }, { + "_id" : "2", + "name" : "vagrant-hostmanager", + "config_key" : "hostmanager", + "required" : true, + "enabled" : true, + "options" : { + "enabled" : true, + "manage_host" : true, + "manage_guest" : true + } }] } From fcd7d6387522390b744e75b7191efe8071dd750b Mon Sep 17 00:00:00 2001 From: hoatle Date: Thu, 9 Feb 2017 22:15:11 +0700 Subject: [PATCH 4/4] @ #248 | should support to use vagrant-hostmanager instead of vagrant-hostsupdater plugin: fix build failed --- lib/utility.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/utility.rb b/lib/utility.rb index 8b2b24fc..49ac3028 100644 --- a/lib/utility.rb +++ b/lib/utility.rb @@ -1,5 +1,3 @@ -$logger = Log4r::Logger.new('lib/utility') - # Utility functions def overrides(obj1, obj2) obj2.each do |key, value| @@ -98,17 +96,17 @@ def read_ip_address(machine) command = "LANG=en ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1 }'" result = "" - $logger.info "Processing #{ machine.name } ... " + # $logger.info "Processing #{ machine.name } ... " begin # sudo is needed for ifconfig machine.communicate.sudo(command) do |type, data| result << data if type == :stdout end - $logger.info "Processing #{ machine.name } ... success" + # $logger.info "Processing #{ machine.name } ... success" rescue result = "# NOT-UP" - $logger.info "Processing #{ machine.name } ... not running" + # $logger.info "Processing #{ machine.name } ... not running" end # the second inet is more accurate