diff --git a/Vagrantfile b/Vagrantfile index fb69e9d3b..09bb8af9a 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -20,6 +20,6 @@ Vagrant.configure("2") do |config| end config.vm.provision "shell", inline: "echo hello world" - + config.vm.provision "idontexistinruby", key: "val", foo: "bar", communicator_required: false end diff --git a/lib/vagrant/config/v2/dummy_config.rb b/lib/vagrant/config/v2/dummy_config.rb index 149b66b7a..cc53a1998 100644 --- a/lib/vagrant/config/v2/dummy_config.rb +++ b/lib/vagrant/config/v2/dummy_config.rb @@ -7,6 +7,20 @@ module Vagrant def method_missing(name, *args, &block) DummyConfig.new end + + def set_options(options) + options.each do |key, value| + var_name = "@#{key.to_s}" + self.instance_variable_set(var_name, value) + end + end + + def instance_variables_hash + instance_variables.inject({}) do |acc, iv| + acc[iv.to_s[1..-1]] = instance_variable_get(iv) + acc + end + end end end end diff --git a/parse_vagrantfile.rb b/parse_vagrantfile.rb index a402fdacd..720136499 100644 --- a/parse_vagrantfile.rb +++ b/parse_vagrantfile.rb @@ -20,22 +20,22 @@ def parse_vagrantfile(path) machine_configs = [] # Get the config for each machine v.machine_names.each do |mach| - machine_info = v.machine_config(mach, nil, nil) + machine_info = v.machine_config(mach, nil, nil, false) root_config = machine_info[:config] vm_config = root_config.vm provisioners = [] vm_config.provisioners.each do |p| - config_struct = Google::Protobuf::Struct.from_hash(p.config.instance_variables_hash) - config_any = Google::Protobuf::Any.pack(config_struct) - provisioners << Hashicorp::Vagrant::VagrantfileComponents::Provisioner.new( - name: p.name, - type: p.type.to_s, - before: p.before, - after: p.after, - communicator_required: p.communicator_required, - config: config_any, - ) - end + config_struct = Google::Protobuf::Struct.from_hash(p.config.instance_variables_hash) + config_any = Google::Protobuf::Any.pack(config_struct) + provisioners << Hashicorp::Vagrant::VagrantfileComponents::Provisioner.new( + name: p.name, + type: p.type.to_s, + before: p.before, + after: p.after, + communicator_required: p.communicator_required, + config: config_any, + ) + end machine_configs << Hashicorp::Vagrant::VagrantfileComponents::MachineConfig.new( name: mach.to_s, config_vm: Hashicorp::Vagrant::VagrantfileComponents::ConfigVM.new( @@ -76,21 +76,27 @@ end def proto_to_provisioner(vagrantfile_proto) # Just grab the first provisioner - p = vagrantfile_proto.machine_configs[0].config_vm.provisioners[0] - plugin = Vagrant.plugin("2").manager.provisioners[p.type.to_sym] - plugin_config = Vagrant.plugin("2").manager.provisioner_configs[p.type.to_sym] - # Create a new config - config = plugin_config.new - # Unpack the config from the proto - raw_config = p.config.unpack( Google::Protobuf::Struct).to_h - # Set config - config.set_options(raw_config) - # Ensure config is valid - config.validate("machine") - # Create new provisioner - provisioner = plugin.new("machine", config) + vagrantfile_proto.machine_configs[0].config_vm.provisioners.each do |p| + plugin = Vagrant.plugin("2").manager.provisioners[p.type.to_sym] + raw_config = p.config.unpack( Google::Protobuf::Struct).to_h + puts raw_config - puts provisioner + # TODO: fetch this config + # if it doesn't exist, then pass in generic config + plugin_config = Vagrant.plugin("2").manager.provisioner_configs[p.type.to_sym] + # Create a new config + config = plugin_config.new + # Unpack the config from the proto + raw_config = p.config.unpack( Google::Protobuf::Struct).to_h + # Set config + config.set_options(raw_config) + # Ensure config is valid + config.validate("machine") + # Create new provisioner + provisioner = plugin.new("machine", config) + + puts provisioner + end end parse_vagrantifle_response = parse_vagrantfile(vagrantfile_path) diff --git a/plugins/kernel_v2/config/vm_provisioner.rb b/plugins/kernel_v2/config/vm_provisioner.rb index a99e143d0..94c511302 100644 --- a/plugins/kernel_v2/config/vm_provisioner.rb +++ b/plugins/kernel_v2/config/vm_provisioner.rb @@ -100,7 +100,10 @@ module VagrantPlugins end def add_config(**options, &block) - return if invalid? + if invalid? + add_invalid_config(**options, &block) + return + end current = @config_class.new current.set_options(options) if options @@ -109,6 +112,14 @@ module VagrantPlugins @config = current end + def add_invalid_config(**options, &block) + current = @config_class.new + current.set_options(options) if options + block.call(current) if block + current = @config.merge(current) if @config + @config = current + end + def finalize! return if invalid?