From a128d98dea3362df8452db0c064cf4cbf8e28fcb Mon Sep 17 00:00:00 2001 From: sophia Date: Tue, 27 Jul 2021 11:12:19 -0500 Subject: [PATCH] Move to_proto to config classes --- Gemfile | 2 -- Vagrantfile | 12 ++++-------- lib/vagrant/plugin/v2/config.rb | 9 +++++++-- .../commands/serve/service/internal_service.rb | 15 +++++---------- plugins/kernel_v2/config/vagrant.rb | 11 +++++++++-- plugins/kernel_v2/config/vm.rb | 7 +++---- 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/Gemfile b/Gemfile index 69495bdbb..565ca7959 100644 --- a/Gemfile +++ b/Gemfile @@ -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" diff --git a/Vagrantfile b/Vagrantfile index a2dab7c95..ff5ef335b 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -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 diff --git a/lib/vagrant/plugin/v2/config.rb b/lib/vagrant/plugin/v2/config.rb index 693ebacc9..1c1ca0fd2 100644 --- a/lib/vagrant/plugin/v2/config.rb +++ b/lib/vagrant/plugin/v2/config.rb @@ -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 diff --git a/plugins/commands/serve/service/internal_service.rb b/plugins/commands/serve/service/internal_service.rb index a1424705d..88b54a43c 100644 --- a/plugins/commands/serve/service/internal_service.rb +++ b/plugins/commands/serve/service/internal_service.rb @@ -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 ) diff --git a/plugins/kernel_v2/config/vagrant.rb b/plugins/kernel_v2/config/vagrant.rb index 24d4648bc..3da5cd22d 100644 --- a/plugins/kernel_v2/config/vagrant.rb +++ b/plugins/kernel_v2/config/vagrant.rb @@ -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 diff --git a/plugins/kernel_v2/config/vm.rb b/plugins/kernel_v2/config/vm.rb index b754698dd..4d05e0221 100644 --- a/plugins/kernel_v2/config/vm.rb +++ b/plugins/kernel_v2/config/vm.rb @@ -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__)