Move to_proto to config classes

This commit is contained in:
sophia 2021-07-27 11:12:19 -05:00 committed by Paul Hinze
parent 5128f604ca
commit a128d98dea
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
6 changed files with 28 additions and 28 deletions

View File

@ -7,5 +7,3 @@ if File.exist?(File.expand_path("../../vagrant-spec", __FILE__))
else
gem 'vagrant-spec', git: "https://github.com/hashicorp/vagrant-spec.git", branch: :main
end
gem "pry-byebug"

12
Vagrantfile vendored
View File

@ -1,11 +1,7 @@
# # This Vagrantfile can be used to develop Vagrant. Note that VirtualBox
# # doesn't run in VirtualBox so you can't actually _run_ Vagrant within
# # the VM created by this Vagrantfile, but you can use it to develop the
# # Ruby, run unit tests, etc.
# Vagrant.configure("2") do |config|
# config.vm.box = "hashicorp/bionic64"
# end
# This Vagrantfile can be used to develop Vagrant. Note that VirtualBox
# doesn't run in VirtualBox so you can't actually _run_ Vagrant within
# the VM created by this Vagrantfile, but you can use it to develop the
# Ruby, run unit tests, etc.
Vagrant.configure("2") do |config|
# vagrant-vbguest plugin options

View File

@ -1,4 +1,7 @@
require "set"
require "vagrant/protobufs/proto/protostructure_pb"
require "vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb"
require "vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_services_pb"
module Vagrant
module Plugin
@ -14,6 +17,8 @@ module Vagrant
# method below will "just work" in many cases.
UNSET_VALUE = Object.new
GENERAL_CONFIG_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::GeneralConfig
# This is called as a last-minute hook that allows the configuration
# object to finalize itself before it will be put into use. This is
# a useful place to do some defaults in the case the user didn't
@ -165,7 +170,7 @@ module Vagrant
end
def to_proto(cfg_cls, type)
def to_proto(type)
protoize = self.instance_variables_hash
protoize.map do |k,v|
# Get embedded default struct
@ -178,7 +183,7 @@ module Vagrant
protoize = clean_up_config_object(protoize)
config_struct = Google::Protobuf::Struct.from_hash(protoize)
config_any = Google::Protobuf::Any.pack(config_struct)
cfg_cls.new(type: type, config: config_any)
GENERAL_CONFIG_CLS.new(type: type, config: config_any)
end
end
end

View File

@ -13,8 +13,6 @@ module VagrantPlugins
prepend Util::HasBroker
prepend Util::ExceptionLogger
CONFIG_VAGRANT_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::ConfigVagrant
GENERAL_CONFIG_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::GeneralConfig
LOG = Log4r::Logger.new("vagrant::command::serve::service::internal")
def get_plugins(req, _unused_call)
@ -45,12 +43,10 @@ module VagrantPlugins
Vagrant::Config::VERSIONS, Vagrant::Config::VERSIONS_ORDER)
config_loader.set(:root, path)
v = Vagrant::Vagrantfile.new(config_loader, [:root])
LOG.debug("loaded vagrantfile")
machine_configs = []
# Get the config for each machine
v.machine_names.each do |mach|
LOG.debug("loading machine config for #{mach}")
machine_info = v.machine_config(mach, nil, nil, false)
root_config = machine_info[:config]
vm_config = root_config.vm
@ -60,16 +56,16 @@ module VagrantPlugins
# A builtin plugin
# TODO: find a better way to check the module
next if config.class.to_s.split("::")[0] == "VagrantPlugins"
plugin_configs << config.to_proto(GENERAL_CONFIG_CLS,name)
plugin_configs << config.to_proto(name)
end
plugin_configs << root_config.ssh.to_proto("ssh")
plugin_configs << root_config.winrm.to_proto("winrm")
plugin_configs << root_config.winssh.to_proto("winssh")
machine_configs << Hashicorp::Vagrant::Sdk::Vagrantfile::MachineConfig.new(
name: mach.to_s,
config_vm: vm_config.to_proto,
config_vagrant: root_config.vagrant.to_proto(CONFIG_VAGRANT_CLS),
config_ssh: root_config.ssh.to_proto(GENERAL_CONFIG_CLS,"ssh"),
config_winrm: root_config.winrm.to_proto(GENERAL_CONFIG_CLS,"winrm"),
config_winssh: root_config.winssh.to_proto(GENERAL_CONFIG_CLS,"winssh"),
config_vagrant: root_config.vagrant.to_proto(),
plugin_configs: plugin_configs
)
end
@ -80,7 +76,6 @@ module VagrantPlugins
current_version: Vagrant::Config::CURRENT_VERSION,
machine_configs: machine_configs,
)
LOG.debug("vagrantfile parsed!")
Hashicorp::Vagrant::ParseVagrantfileResponse.new(
vagrantfile: vagrantfile
)

View File

@ -1,5 +1,11 @@
require "vagrant"
$LOAD_PATH << Vagrant.source_root.join("lib/vagrant/protobufs/proto").to_s
require "vagrant/protobufs/proto/protostructure_pb"
require "vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb"
require "vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_services_pb"
module VagrantPlugins
module Kernel_V2
class VagrantConfig < Vagrant.plugin("2", :config)
@ -9,6 +15,7 @@ module VagrantPlugins
VALID_PLUGIN_KEYS = ["sources", "version", "entry_point"].map(&:freeze).freeze
INVALID_PLUGIN_FORMAT = :invalid_plugin_format
CONFIG_VAGRANT_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::ConfigVagrant
def initialize
@host = UNSET_VALUE
@ -92,8 +99,8 @@ module VagrantPlugins
end
end
def to_proto(cfg_cls)
config_proto = cfg_cls.new()
def to_proto()
config_proto = CONFIG_VAGRANT_CLS.new()
self.instance_variables_hash.each do |k, v|
# Skip config that has not be set
next if v.class == Object

View File

@ -10,12 +10,11 @@ require "vagrant/util/presence"
require "vagrant/util/experimental"
require "vagrant/util/map_command_options"
$LOAD_PATH << Vagrant.source_root.join("lib/vagrant/protobufs").to_s
$LOAD_PATH << Vagrant.source_root.join("lib/vagrant/protobufs/proto").to_s
$LOAD_PATH << Vagrant.source_root.join("lib/vagrant/protobufs/proto/vagrant_plugin_sdk").to_s
require 'vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb'
require 'vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_services_pb'
require "vagrant/protobufs/proto/protostructure_pb"
require "vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb"
require "vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_services_pb"
require File.expand_path("../vm_provisioner", __FILE__)
require File.expand_path("../vm_subvm", __FILE__)