From fdb6efc3b186a157cc257b704c152556695e6cf0 Mon Sep 17 00:00:00 2001 From: hoatle Date: Fri, 20 Jul 2018 11:44:40 +0700 Subject: [PATCH] @ #277 | add configure_plugins --- Vagrantfile | 11 +++--- config_default.yaml | 8 ++-- lib/configurator.rb | 94 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 103 insertions(+), 10 deletions(-) diff --git a/Vagrantfile b/Vagrantfile index a1cb6bcb..b220ae2d 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -45,31 +45,32 @@ end # for example, extension can register hooks -=begin +=begin my_before_configure = Proc.new do |node_config, node| - $logger.debug("my_before_configure") + $logger.debug("my_before_configure: #{node_config}: #{node}") end my_after_configure = Proc.new do |node_config, node| - $logger.debug("my_after_configure") + $logger.debug("my_after_configure: #{node_config}: #{node}") end register_before_configure_hook(&my_before_configure) register_after_configure_hook(&my_after_configure) - =end Vagrant.configure("2") do |config| + configure_plugins(settings['vagrant']['plugins'], config) + settings["nodes"].each do |node_config| primary = node_config["primary"] ||= false autostart = node_config["autostart"] === false ? false : true # NOTE: the next line is async config.vm.define node_config["name"], primary: primary, autostart: autostart do |node| $logger.debug("node_config: #{node_config}") - configure(node_config, node) + configure_node(node_config, node) end end diff --git a/config_default.yaml b/config_default.yaml index 991c56ba..9213cce2 100644 --- a/config_default.yaml +++ b/config_default.yaml @@ -89,8 +89,8 @@ nodes: is_primary: true vm: hostname: "#{NODE_HOSTNAME_PREFIX}-01.local" - - name: "#{node_name_prefix}-02" - autostart: false - vm: - hostname: "#{NODE_HOSTNAME_PREFIX}-02.local" + # - name: "#{node_name_prefix}-02" + # autostart: false + # vm: + # hostname: "#{NODE_HOSTNAME_PREFIX}-02.local" diff --git a/lib/configurator.rb b/lib/configurator.rb index 84b37d4c..b3e6356d 100644 --- a/lib/configurator.rb +++ b/lib/configurator.rb @@ -93,7 +93,7 @@ def register_after_configure_hook(&block) -def configure(node_config, node) +def configure_node(node_config, node) # hooks callback _run_before_configure_hooks(node_config, node) @@ -110,3 +110,95 @@ def configure(node_config, node) # hooks calback _run_after_configure_hooks(node_config, node) end + + +def configure_plugins(plugins_config, config) + $logger.debug("configure_plugins: #{plugins_config}") + plugins_config.each do |plugin| + if plugin['enabled'] == false + $logger.debug("#{plugin['name']} is configured but not enabled") + next + end + + plugin_name = plugin['name'] + if plugin['required'] == true && !Vagrant.has_plugin?(plugin_name) && ARGV[0] != 'plugin' + $logger.info("vagrant plugin install #{plugin_name} && vagrant #{ARGV.join(" ")}") + # thanks to http://matthewcooper.net/2015/01/15/automatically-installing-vagrant-plugin-dependencies/ + exec "vagrant plugin install #{plugin_name} && vagrant #{ARGV.join(" ")}" + + # unless Vagrant.has_plugin?(plugin_name) + # puts red("required: '$ vagrant plugin install #{plugin_name}'") + # exit! + # end + end + + # this is current fixed config, not dynamic plugins config + # FIXME(hoatle): #186 should fix this + 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 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 + # conflicts + 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 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 + # if plugin.key?('config_key') + # config_key = plugin['config_key'] + # if Vagrant.has_plugin?(plugin_name) and !config_key.nil? and !config_key.empty? + # puts red(config[config_key.to_sym]) + # # TODO(hoatle): remove config_key and required keys? + # #config.instance_variable_set("@#{config_key}", plugin) + # # new_config = Vagrant::Config::V2::Root.new({ + # # config_key => plugin + # # }) + # # config.merge(config, new_config) + # end + # end + end +end