Collect config for Vagrantfile config element that does not exist in Ruby

This commit is contained in:
sophia 2021-05-18 17:33:30 -05:00 committed by Paul Hinze
parent ee062c655f
commit 0923f1edf2
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
4 changed files with 59 additions and 28 deletions

2
Vagrantfile vendored
View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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?