Extract network info from Vagrantfile into proto
This commit is contained in:
parent
ace5f0db69
commit
5467b9134c
5
Vagrantfile
vendored
5
Vagrantfile
vendored
@ -14,7 +14,10 @@ Vagrant.configure("2") do |config|
|
||||
["a", "b"].each do |m|
|
||||
config.vm.define m do |c|
|
||||
c.vm.box = "hashicorp/bionic64"
|
||||
c.vm.network "private_network", ip: "192.168.50.4"
|
||||
c.vm.network "forwarded_port", guest: 80, host: 8080
|
||||
|
||||
c.vm.network "private_network", ip: "192.168.50.4", thing: "what"
|
||||
c.vm.network "public_network"
|
||||
c.vm.synced_folder "../tm", "/tm", type: "rsync", rsync__exclude: ".git/"
|
||||
end
|
||||
end
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -124,49 +124,8 @@ message VagrantfileComponents {
|
||||
|
||||
message Network {
|
||||
string type = 1;
|
||||
oneof config {
|
||||
ForwardedPort forwarded_port = 2;
|
||||
PrivateNetwork private_network = 3;
|
||||
PublicNetwork public_network = 4;
|
||||
}
|
||||
}
|
||||
|
||||
message ForwardedPort {
|
||||
bool auto_correct = 1;
|
||||
int32 guest = 2;
|
||||
string guest_ip = 3;
|
||||
int32 host = 4;
|
||||
string host_ip = 5;
|
||||
string id = 6;
|
||||
string protocol = 7;
|
||||
}
|
||||
|
||||
message PrivateNetwork {
|
||||
string type = 1;
|
||||
bool auto_config = 2;
|
||||
oneof config {
|
||||
DHCP dhcp = 3;
|
||||
StaticIp static_ip = 4;
|
||||
}
|
||||
}
|
||||
|
||||
message PublicNetwork {
|
||||
string type = 1;
|
||||
bool auto_config = 2;
|
||||
repeated string bridge = 3;
|
||||
oneof config {
|
||||
DHCP dhcp = 4;
|
||||
StaticIp static_ip = 5;
|
||||
}
|
||||
}
|
||||
|
||||
message DHCP {
|
||||
bool use_dhcp_assigned_default_route = 1;
|
||||
}
|
||||
|
||||
message StaticIp {
|
||||
string ip = 1;
|
||||
string netmask = 2;
|
||||
string id = 2;
|
||||
google.protobuf.Any config = 3;
|
||||
}
|
||||
|
||||
message SyncedFolder {
|
||||
|
||||
@ -78,44 +78,8 @@ Google::Protobuf::DescriptorPool.generated_pool.build do
|
||||
end
|
||||
add_message "hashicorp.vagrant.VagrantfileComponents.Network" do
|
||||
optional :type, :string, 1
|
||||
oneof :config do
|
||||
optional :forwarded_port, :message, 2, "hashicorp.vagrant.VagrantfileComponents.ForwardedPort"
|
||||
optional :private_network, :message, 3, "hashicorp.vagrant.VagrantfileComponents.PrivateNetwork"
|
||||
optional :public_network, :message, 4, "hashicorp.vagrant.VagrantfileComponents.PublicNetwork"
|
||||
end
|
||||
end
|
||||
add_message "hashicorp.vagrant.VagrantfileComponents.ForwardedPort" do
|
||||
optional :auto_correct, :bool, 1
|
||||
optional :guest, :int32, 2
|
||||
optional :guest_ip, :string, 3
|
||||
optional :host, :int32, 4
|
||||
optional :host_ip, :string, 5
|
||||
optional :id, :string, 6
|
||||
optional :protocol, :string, 7
|
||||
end
|
||||
add_message "hashicorp.vagrant.VagrantfileComponents.PrivateNetwork" do
|
||||
optional :type, :string, 1
|
||||
optional :auto_config, :bool, 2
|
||||
oneof :config do
|
||||
optional :dhcp, :message, 3, "hashicorp.vagrant.VagrantfileComponents.DHCP"
|
||||
optional :static_ip, :message, 4, "hashicorp.vagrant.VagrantfileComponents.StaticIp"
|
||||
end
|
||||
end
|
||||
add_message "hashicorp.vagrant.VagrantfileComponents.PublicNetwork" do
|
||||
optional :type, :string, 1
|
||||
optional :auto_config, :bool, 2
|
||||
repeated :bridge, :string, 3
|
||||
oneof :config do
|
||||
optional :dhcp, :message, 4, "hashicorp.vagrant.VagrantfileComponents.DHCP"
|
||||
optional :static_ip, :message, 5, "hashicorp.vagrant.VagrantfileComponents.StaticIp"
|
||||
end
|
||||
end
|
||||
add_message "hashicorp.vagrant.VagrantfileComponents.DHCP" do
|
||||
optional :use_dhcp_assigned_default_route, :bool, 1
|
||||
end
|
||||
add_message "hashicorp.vagrant.VagrantfileComponents.StaticIp" do
|
||||
optional :ip, :string, 1
|
||||
optional :netmask, :string, 2
|
||||
optional :id, :string, 2
|
||||
optional :config, :message, 3, "google.protobuf.Any"
|
||||
end
|
||||
add_message "hashicorp.vagrant.VagrantfileComponents.SyncedFolder" do
|
||||
optional :source, :string, 1
|
||||
@ -155,11 +119,6 @@ module Hashicorp
|
||||
VagrantfileComponents::Provisioner = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.VagrantfileComponents.Provisioner").msgclass
|
||||
VagrantfileComponents::Provider = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.VagrantfileComponents.Provider").msgclass
|
||||
VagrantfileComponents::Network = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.VagrantfileComponents.Network").msgclass
|
||||
VagrantfileComponents::ForwardedPort = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.VagrantfileComponents.ForwardedPort").msgclass
|
||||
VagrantfileComponents::PrivateNetwork = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.VagrantfileComponents.PrivateNetwork").msgclass
|
||||
VagrantfileComponents::PublicNetwork = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.VagrantfileComponents.PublicNetwork").msgclass
|
||||
VagrantfileComponents::DHCP = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.VagrantfileComponents.DHCP").msgclass
|
||||
VagrantfileComponents::StaticIp = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.VagrantfileComponents.StaticIp").msgclass
|
||||
VagrantfileComponents::SyncedFolder = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.VagrantfileComponents.SyncedFolder").msgclass
|
||||
VagrantfileComponents::Vagrantfile = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.VagrantfileComponents.Vagrantfile").msgclass
|
||||
end
|
||||
|
||||
@ -10,12 +10,10 @@ require_relative "./plugins/commands/serve/command"
|
||||
|
||||
vagrantfile_path = "/Users/sophia/project/vagrant-ruby/Vagrantfile"
|
||||
|
||||
PROVIDER_PROTO_CLS = Hashicorp::Vagrant::VagrantfileComponents::Provisioner
|
||||
PROVISION_PROTO_CLS = Hashicorp::Vagrant::VagrantfileComponents::Provisioner
|
||||
SYNCED_FOLDER_PROTO_CLS = Hashicorp::Vagrant::VagrantfileComponents::SyncedFolder
|
||||
NETWORK_PROTO_CLS = Hashicorp::Vagrant::VagrantfileComponents::Network
|
||||
NETWORK_FORWARDED_PORT_PROTO_CLS = Hashicorp::Vagrant::VagrantfileComponents::ForwardedPort
|
||||
NETWORK_PRIVATE_PROTO_CLS = Hashicorp::Vagrant::VagrantfileComponents::PrivateNetwork
|
||||
NETWORK_PUBLIC_PROTO_CLS = Hashicorp::Vagrant::VagrantfileComponents::PrivateNetwork
|
||||
|
||||
def extract_component(target_cls, target, vagrant_config)
|
||||
vagrant_config.each do |c|
|
||||
@ -40,22 +38,28 @@ def extract_component(target_cls, target, vagrant_config)
|
||||
end
|
||||
end
|
||||
|
||||
# Network configs take the form
|
||||
# [
|
||||
# [:type, {:id=>"tcp8080", ...}], ...
|
||||
# ]
|
||||
def extract_network(target, networks)
|
||||
networks.each do |n|
|
||||
network_proto = NETWORK_PROTO_CLS.new()
|
||||
network_proto.type = n[0]
|
||||
case n[0]
|
||||
when :forwarded_port
|
||||
network_proto.forwarded_port = NETWORK_FORWARDED_PORT_PROTO_CLS.new(n[1])
|
||||
when :private
|
||||
network_proto.private_network = NETWORK_PRIVATE_PROTO_CLS.new(n[1])
|
||||
when :public
|
||||
network_proto.public_network = NETWORK_PUBLIC_PROTO_CLS.new(n[1])
|
||||
end
|
||||
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()
|
||||
@ -121,6 +125,7 @@ def parse_vagrantfile(path)
|
||||
# have a config variable for one of the instance methods. This is ok.
|
||||
end
|
||||
end
|
||||
|
||||
extract_component(PROVISION_PROTO_CLS, config_vm_proto.provisioners, vm_config.provisioners)
|
||||
extract_network(config_vm_proto.networks, vm_config.networks)
|
||||
extract_synced_folders(config_vm_proto.synced_folders, vm_config.synced_folders)
|
||||
@ -130,7 +135,7 @@ def parse_vagrantfile(path)
|
||||
config_vm: config_vm_proto,
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
vagrantfile = Hashicorp::Vagrant::VagrantfileComponents::Vagrantfile.new(
|
||||
path: path,
|
||||
# raw: raw,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user