Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/#248 use hostmanager instead of hostsupdater plugin deps #246 #249

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,6 @@ home/.docker/*

nodes/*.json

# https://github.com/teracyhq/dev/issues/246
vagrant_config_override.json
Vagrantfile-ext.rb
123 changes: 81 additions & 42 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

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)

# 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
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)
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"]

Expand Down Expand Up @@ -210,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 plugin['time_format'].nil? or plugin['time_format'].empty?
config.gatling.time_format = plugin['time_format']
unless options['rsync_on_startup'].nil?
config.gatling.rsync_on_startup = options['rsync_on_startup']
end

unless plugin['rsync_on_startup'].nil?
config.gatling.rsync_on_startup = plugin['rsync_on_startup']
elsif 'hostmanager' == config_key
if Vagrant.has_plugin?('vagrant-hostsupdater')
puts red('recommended: $ vagrant plugin uninstall vagrant-hostsupdater')
end

elsif 'hostsupdater' == config_key
unless plugin['aliases'].nil? or plugin['aliases'].empty?
config.hostsupdater.aliases = plugin['aliases']
unless options['enabled'].nil?
config.hostmanager.enabled = options['enabled']
end

unless plugin['remove_on_suspend'].nil? or plugin['remove_on_suspend'].empty?
config.hostsupdater.remove_on_suspend = plugin['remove_on_suspend']
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 options['ignore_private_ip'].nil?
config.hostmanager.ignore_private_ip = options['ignore_private_ip']
end

unless options['include_offline'].nil?
config.hostmanager.include_offline = options['include_offline']
end

unless options['aliases'].nil?
config.hostmanager.aliases = options['aliases']
end

# 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
Expand Down Expand Up @@ -370,3 +392,20 @@ Vagrant.configure("2") do |config|
end
end
end

begin
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)
end
23 changes: 23 additions & 0 deletions lib/utility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,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
33 changes: 27 additions & 6 deletions vagrant_config.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -149,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
}
}]
}