to_proto save point

This commit is contained in:
sophia 2021-07-22 15:54:04 -05:00 committed by Paul Hinze
parent 119afb4023
commit 0e40c2eb97
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
11 changed files with 1635 additions and 1579 deletions

View File

@ -7,3 +7,5 @@ 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,7 +1,11 @@
# 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.
# # 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
Vagrant.configure("2") do |config|
# vagrant-vbguest plugin options

File diff suppressed because it is too large Load Diff

View File

@ -137,6 +137,49 @@ module Vagrant
def _finalize!
@__finalized = true
end
def stringify_symbols(m)
m.each do |k,v|
if v.is_a?(Hash)
# All keys need to be strings
v.transform_keys!{|sk| sk.to_s}
stringify_symbols(v)
next
end
if v.is_a?(Array)
v.map!{|sk| sk.is_a?(Symbol) ? sk.to_s : sk}
stringify_symbols(v)
next
end
k = k.to_s if k.is_a?(Symbol)
m[k] = v.to_s if v.is_a?(Symbol)
end
end
def clean_up_config_object(config)
protoize = config
stringify_symbols(protoize)
# Remote variables that are internal
protoize.delete_if{|k,v| k.start_with?("_") }
protoize
end
def to_proto(cfg_cls, type)
protoize = self.instance_variables_hash
protoize.map do |k,v|
# Get embedded default struct
if v.is_a?(Vagrant.plugin("2", :config))
hashed_config = v.instance_variables_hash
hashed_config.delete_if{|k,v| k.start_with?("_") }
protoize[k] = hashed_config
end
end
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)
end
end
end
end

View File

@ -44,6 +44,8 @@ Google::Protobuf::DescriptorPool.generated_pool.build do
end
add_message "hashicorp.vagrant.sdk.Args.TerminalUI" do
optional :stream_id, :uint32, 1
optional :network, :string, 2
optional :target, :string, 3
end
add_message "hashicorp.vagrant.sdk.Args.Logger" do
optional :name, :string, 1
@ -61,15 +63,23 @@ Google::Protobuf::DescriptorPool.generated_pool.build do
end
add_message "hashicorp.vagrant.sdk.Args.Basis" do
optional :stream_id, :uint32, 1
optional :network, :string, 2
optional :target, :string, 3
end
add_message "hashicorp.vagrant.sdk.Args.Project" do
optional :stream_id, :uint32, 1
optional :network, :string, 2
optional :target, :string, 3
end
add_message "hashicorp.vagrant.sdk.Args.Provider" do
optional :stream_id, :uint32, 1
optional :network, :string, 2
optional :target, :string, 3
end
add_message "hashicorp.vagrant.sdk.Args.Target" do
optional :stream_id, :uint32, 1
optional :network, :string, 2
optional :target, :string, 3
end
add_message "hashicorp.vagrant.sdk.Args.Target.State" do
optional :state, :enum, 1, "hashicorp.vagrant.sdk.Args.Target.State.State"
@ -82,6 +92,8 @@ Google::Protobuf::DescriptorPool.generated_pool.build do
end
add_message "hashicorp.vagrant.sdk.Args.Target.Machine" do
optional :stream_id, :uint32, 1
optional :network, :string, 2
optional :target, :string, 3
end
add_message "hashicorp.vagrant.sdk.Args.Target.Machine.Box" do
optional :name, :string, 1
@ -98,9 +110,16 @@ Google::Protobuf::DescriptorPool.generated_pool.build do
end
add_message "hashicorp.vagrant.sdk.Args.StateBag" do
optional :stream_id, :uint32, 1
optional :network, :string, 2
optional :target, :string, 3
end
add_message "hashicorp.vagrant.sdk.Args.Host" do
optional :stream_id, :uint32, 1
optional :network, :string, 2
optional :target, :string, 3
end
add_message "hashicorp.vagrant.sdk.Args.NamedCapability" do
optional :Capability, :string, 1
end
add_message "hashicorp.vagrant.sdk.TargetIndex" do
end
@ -710,6 +729,7 @@ module Hashicorp
Args::Target::Machine::State = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Args.Target.Machine.State").msgclass
Args::StateBag = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Args.StateBag").msgclass
Args::Host = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Args.Host").msgclass
Args::NamedCapability = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Args.NamedCapability").msgclass
TargetIndex = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.TargetIndex").msgclass
TargetIndex::Entry = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.TargetIndex.Entry").msgclass
BoxCollection = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.BoxCollection").msgclass

View File

@ -13,19 +13,9 @@ module VagrantPlugins
prepend Util::HasBroker
prepend Util::ExceptionLogger
LOG = Log4r::Logger.new("vagrant::command::serve::service::internal")
PROVIDER_PROTO_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::Provider
PROVISION_PROTO_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::Provisioner
SYNCED_FOLDER_PROTO_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::SyncedFolder
NETWORK_PROTO_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::Network
GENERAL_CONFIG_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::GeneralConfig
CONFIG_VM_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::ConfigVM
CONFIG_SSH_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::ConfigSSH
CONFIG_WINRM_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::ConfigWinRM
CONFIG_WINSSH_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::ConfigWinssh
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)
plugins = []
@ -65,50 +55,21 @@ module VagrantPlugins
root_config = machine_info[:config]
vm_config = root_config.vm
LOG.debug("extracting config.ssh")
# Get config.ssh.*
config_ssh_proto = extract_communicator(CONFIG_SSH_CLS, root_config.ssh)
LOG.debug("extracting config.winrm")
# Get config.winrm.*
config_winrm_proto = extract_communicator(CONFIG_WINRM_CLS, root_config.winrm)
LOG.debug("extracting config.winssh")
#Get config.winssh.*
config_winssh_proto = extract_communicator(CONFIG_WINSSH_CLS, root_config.winssh)
LOG.debug("extracting config.vagrant")
# Get config.vagrant.*
config_vagrant_proto = extract_config_component(CONFIG_VAGRANT_CLS, root_config.vagrant)
LOG.debug("extracting config.vm")
# Get's config.vm.*
config_vm_proto = extract_config_component(CONFIG_VM_CLS, vm_config)
LOG.debug("extracting provisioners")
extract_provisioners(config_vm_proto.provisioners, vm_config.provisioners)
LOG.debug("extracting network")
extract_network(config_vm_proto.networks, vm_config.networks)
LOG.debug("extracting synced folder")
extract_synced_folders(config_vm_proto.synced_folders, vm_config.synced_folders)
LOG.debug("extracting provider")
extract_provider(config_vm_proto.providers, vm_config)
LOG.debug("done loading machine config for #{mach}")
plugin_configs = []
root_config.__internal_state["keys"].each do |name, config|
# A builtin plugin
# TODO: find a better way to check the module
next if config.class.to_s.split("::")[0] == "VagrantPlugins"
plugin_configs << extract_plugin_config(name, config)
plugin_configs << config.to_proto(GENERAL_CONFIG_CLS,name)
end
machine_configs << Hashicorp::Vagrant::Sdk::Vagrantfile::MachineConfig.new(
name: mach.to_s,
config_vm: config_vm_proto,
config_vagrant: config_vagrant_proto,
config_ssh: config_ssh_proto,
config_winrm: config_winrm_proto,
config_winssh: config_winssh_proto,
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"),
plugin_configs: plugin_configs
)
end
@ -124,202 +85,6 @@ module VagrantPlugins
vagrantfile: vagrantfile
)
end
def stringify_symbols(m)
m.each do |k,v|
if v.is_a?(Hash)
# All keys need to be strings
v.transform_keys!{|sk| sk.to_s}
stringify_symbols(v)
next
end
if v.is_a?(Array)
v.map!{|sk| sk.is_a?(Symbol) ? sk.to_s : sk}
stringify_symbols(v)
next
end
k = k.to_s if k.is_a?(Symbol)
m[k] = v.to_s if v.is_a?(Symbol)
end
end
def clean_up_config_object(config)
protoize = config
stringify_symbols(protoize)
# Remove variables that are internal
protoize.delete_if{|k,v| k.start_with?("_") }
protoize
end
def extract_provisioners(target, provisioners)
provisioners.each do |c|
proto = PROVISION_PROTO_CLS.new()
c.instance_variables_hash.each do |k, v|
begin
if k == "config"
protoize = clean_up_config_object(c.config.instance_variables_hash)
config_struct = Google::Protobuf::Struct.from_hash(protoize)
config_any = Google::Protobuf::Any.pack(config_struct)
proto.config = config_any
next
end
if !v.nil?
v = v.to_s if v.is_a?(Symbol)
proto.send("#{k}=", v)
end
rescue NoMethodError
# this is ok
end
end
target << proto
end
end
# Network configs take the form
# [
# [:type, {:id=>"tcp8080", ...}], ...
# ]
def extract_network(target, networks)
networks.each do |n|
type = n[0]
opts = n[1]
network_proto = NETWORK_PROTO_CLS.new(type: type, id: opts.fetch(:id, ""))
opts.delete(:id)
opts.transform_keys!(&:to_s)
config_struct = Google::Protobuf::Struct.from_hash(opts)
config_any = Google::Protobuf::Any.pack(config_struct)
network_proto.config = config_any
target << network_proto
end
end
# Providers take the form
# {
# :type=> #<VagrantPlugins::PluginClass::Config:Object>, ...
# }
def extract_provider(target, vm_config)
#WARNING: hacks ahead
vm_config.define_singleton_method(:compiled_provider_configs) do
return @__compiled_provider_configs
end
vm_config.compiled_provider_configs.each do |type, config|
c = clean_up_config_object(config.instance_variables_hash)
provider_proto = PROVIDER_PROTO_CLS.new(type: type)
config_struct = Google::Protobuf::Struct.from_hash(c)
config_any = Google::Protobuf::Any.pack(config_struct)
provider_proto.config = config_any
target << provider_proto
end
end
# Synced folders take the form of a hash map
# {
# "name"=>{:type=>:rsync, ...}, ...
# },
def extract_synced_folders(target, synced_folders)
synced_folders.each do |k,v|
sf_proto = SYNCED_FOLDER_PROTO_CLS.new()
# Need to set source and destination since they don't exactly map
sf_proto.source = v[:hostpath]
sf_proto.destination = v[:guestpath]
# config_opts keep track of the config options specific to the synced
# folder type. They are in the form `type`__`option`
config_opts = {}
v.each do |opt, val|
# already accounted for above
next if ["guestpath", "hostpath"].include?(opt.to_s)
# match the synced folder specific options and store them in the
# config_opts
if opt.to_s.match(/#{v[:type]}__/)
config_opts[opt.to_s.split("__")[1]] = val
next
end
sf_proto.send("#{opt.to_s}=", val)
end
config_struct = Google::Protobuf::Struct.from_hash(config_opts)
config_any = Google::Protobuf::Any.pack(config_struct)
sf_proto.config = config_any
target << sf_proto
end
end
def extract_config_component(config_cls, config)
config_proto = config_cls.new()
config.instance_variables_hash.each do |k, v|
# Skip config that has not be set
next if v.class == Object
# Going to deal with these seperately because they are more involved
next if ["provisioners", "networks", "synced_folders", "disks", "cloud_init_configs"].include?(k)
# Skip all variables that are internal
next if k.start_with?("_")
if v.nil?
# If v is nil, set it to the default value defined by the proto
v = config_proto.send(k)
end
if v.is_a?(Range)
v = v.to_a
end
if v.is_a?(Hash)
m = config_proto.send(k)
v.each do |k2,v2|
m[k2] = v2
end
v = m
end
if v.is_a?(Array)
m = config_proto.send(k)
v.each do |v2|
m << v2
end
v = m
end
begin
config_proto.send("#{k}=", v)
rescue NoMethodError
# Reach here when Hashicorp::Vagrant::VagrantfileComponents::ConfigVM does not
# have a config variable for one of the instance methods. This is ok.
end
end
config_proto
end
def extract_communicator(config_cls, config)
protoize = config.instance_variables_hash
protoize.map do |k,v|
if v.is_a?(Vagrant.plugin("2", :config))
# Get embedded default struct
hashed_config = v.instance_variables_hash
hashed_config.delete_if{|k,v| k.start_with?("_") }
protoize[k] = hashed_config
end
end
protoize = clean_up_config_object(protoize)
config_struct = Google::Protobuf::Struct.from_hash(protoize)
config_any = Google::Protobuf::Any.pack(config_struct)
config_cls.new(config: config_any)
end
def extract_plugin_config(type, config)
protoize = config.instance_variables_hash
protoize = clean_up_config_object(protoize)
config_struct = Google::Protobuf::Struct.from_hash(protoize)
config_any = Google::Protobuf::Any.pack(config_struct)
GENERAL_CONFIG_CLS.new(type: type, config: config_any)
end
end
end
end

View File

@ -128,6 +128,10 @@ module VagrantPlugins
def to_s
"cloud_init config"
end
def to_proto
#TODO
end
end
end
end

View File

@ -216,6 +216,10 @@ module VagrantPlugins
def to_s
"disk config"
end
def to_proto
# TODO
end
end
end
end

View File

@ -91,6 +91,50 @@ module VagrantPlugins
INVALID_PLUGIN_FORMAT
end
end
def to_proto(cfg_cls)
config_proto = cfg_cls.new()
self.instance_variables_hash.each do |k, v|
# Skip config that has not be set
next if v.class == Object
# Skip all variables that are internal
next if k.start_with?("_")
if v.nil?
# If v is nil, set it to the default value defined by the proto
v = config_proto.send(k)
end
if v.is_a?(Range)
v = v.to_a
end
if v.is_a?(Hash)
m = config_proto.send(k)
v.each do |k2,v2|
m[k2] = v2
end
v = m
end
if v.is_a?(Array)
m = config_proto.send(k)
v.each do |v2|
m << v2
end
v = m
end
begin
config_proto.send("#{k}=", v)
rescue NoMethodError
# Reach here when Hashicorp::Vagrant::VagrantfileComponents::ConfigVM does not
# have a config variable for one of the instance methods. This is ok.
end
end
config_proto
end
end
end
end

View File

@ -10,11 +10,24 @@ 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 File.expand_path("../vm_provisioner", __FILE__)
require File.expand_path("../vm_subvm", __FILE__)
require File.expand_path("../disk", __FILE__)
require File.expand_path("../cloud_init", __FILE__)
CONFIG_VM_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::ConfigVM
PROVIDER_PROTO_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::Provider
PROVISION_PROTO_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::Provisioner
SYNCED_FOLDER_PROTO_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::SyncedFolder
NETWORK_PROTO_CLS = Hashicorp::Vagrant::Sdk::Vagrantfile::Network
module VagrantPlugins
module Kernel_V2
class VMConfig < Vagrant.plugin("2", :config)
@ -1043,6 +1056,139 @@ module VagrantPlugins
def __providers
@__provider_order
end
# Providers take the form
# {
# :type=> #<VagrantPlugins::PluginClass::Config:Object>, ...
# }
def extract_provider(target, vm_config)
#WARNING: hacks ahead
vm_config.define_singleton_method(:compiled_provider_configs) do
return @__compiled_provider_configs
end
vm_config.compiled_provider_configs.each do |type, config|
c = clean_up_config_object(config.instance_variables_hash)
provider_proto = PROVIDER_PROTO_CLS.new(type: type)
config_struct = Google::Protobuf::Struct.from_hash(c)
config_any = Google::Protobuf::Any.pack(config_struct)
provider_proto.config = config_any
target << provider_proto
end
end
# Network configs take the form
# [
# [:type, {:id=>"tcp8080", ...}], ...
# ]
def extract_network(target, networks)
networks.each do |n|
type = n[0]
opts = n[1]
network_proto = NETWORK_PROTO_CLS.new(type: type, id: opts.fetch(:id, ""))
opts.delete(:id)
opts.transform_keys!(&:to_s)
config_struct = Google::Protobuf::Struct.from_hash(opts)
config_any = Google::Protobuf::Any.pack(config_struct)
network_proto.config = config_any
target << network_proto
end
end
# Synced folders take the form of a hash map
# {
# "name"=>{:type=>:rsync, ...}, ...
# },
def extract_synced_folders(target, synced_folders)
synced_folders.each do |k,v|
sf_proto = SYNCED_FOLDER_PROTO_CLS.new()
# Need to set source and destination since they don't exactly map
sf_proto.source = v[:hostpath]
sf_proto.destination = v[:guestpath]
# config_opts keep track of the config options specific to the synced
# folder type. They are in the form `type`__`option`
config_opts = {}
v.each do |opt, val|
# already accounted for above
next if ["guestpath", "hostpath"].include?(opt.to_s)
# match the synced folder specific options and store them in the
# config_opts
if opt.to_s.match(/#{v[:type]}__/)
config_opts[opt.to_s.split("__")[1]] = val
next
end
sf_proto.send("#{opt.to_s}=", val)
end
config_struct = Google::Protobuf::Struct.from_hash(config_opts)
config_any = Google::Protobuf::Any.pack(config_struct)
sf_proto.config = config_any
target << sf_proto
end
end
def to_proto
config_proto = CONFIG_VM_CLS.new()
self.instance_variables_hash.each do |k, v|
# Skip config that has not be set
next if v.class == Object
# Going to deal with these seperately because they are a little different
next if ["networks", "synced_folders"].include?(k)
# Extract a proto!
if ["provisioners", "disks", "cloud_init_configs"].include?(k)
v.each do |el|
config_proto[k] << el.to_proto
end
next
end
# Skip all variables that are internal
next if k.start_with?("_")
if v.nil?
# If v is nil, set it to the default value defined by the proto
v = config_proto.send(k)
end
if v.is_a?(Range)
v = v.to_a
end
if v.is_a?(Hash)
m = config_proto.send(k)
v.each do |k2,v2|
m[k2] = v2
end
v = m
end
if v.is_a?(Array)
m = config_proto.send(k)
v.each do |v2|
m << v2
end
v = m
end
begin
config_proto.send("#{k}=", v)
rescue NoMethodError
# Reach here when Hashicorp::Vagrant::VagrantfileComponents::ConfigVM does not
# have a config variable for one of the instance methods. This is ok.
end
end
extract_provider(config_proto.providers, self)
extract_network(config_proto.networks, self.networks)
extract_synced_folders(config_proto.synced_folders, self.synced_folders)
config_proto
end
end
end
end

View File

@ -194,6 +194,28 @@ module VagrantPlugins
def invalid?
@invalid
end
def to_proto
proto = Hashicorp::Vagrant::Sdk::Vagrantfile::Provisioner.new()
self.instance_variables_hash.each do |k, v|
begin
if k == "config"
protoize = clean_up_config_object(c.config.instance_variables_hash)
config_struct = Google::Protobuf::Struct.from_hash(protoize)
config_any = Google::Protobuf::Any.pack(config_struct)
proto.config = config_any
next
end
if !v.nil?
v = v.to_s if v.is_a?(Symbol)
proto.send("#{k}=", v)
end
rescue NoMethodError
# this is ok
end
end
proto
end
end
end
end