diff --git a/lib/vagrant/environment/remote.rb b/lib/vagrant/environment/remote.rb index b1695d26e..d4e7ec9d9 100644 --- a/lib/vagrant/environment/remote.rb +++ b/lib/vagrant/environment/remote.rb @@ -1,3 +1,5 @@ +require 'vagrant/machine_index/remote' + module Vagrant class Environment module Remote @@ -9,6 +11,7 @@ module Vagrant end def initialize(opts={}) + @client = opts[:client] super @client = opts[:client] if @client.nil? @@ -24,6 +27,17 @@ module Vagrant def get_target(name) client.target(name) end + + # The {MachineIndex} to store information about the machines. + # + # @return [MachineIndex] + def machine_index + if !@client.nil? + machine_index_client = @client.machine_index + @machine_index ||= Vagrant::MachineIndex.new(client: machine_index_client) + end + @machine_index + end end end end diff --git a/lib/vagrant/machine_index.rb b/lib/vagrant/machine_index.rb index 29788ebaa..a289d622e 100644 --- a/lib/vagrant/machine_index.rb +++ b/lib/vagrant/machine_index.rb @@ -36,6 +36,8 @@ module Vagrant # } # class MachineIndex + autoload :Remote, "vagrant/machine_index/remote" + include Enumerable # Initializes a MachineIndex at the given file location. diff --git a/lib/vagrant/machine_index/remote.rb b/lib/vagrant/machine_index/remote.rb new file mode 100644 index 000000000..0c6b92df5 --- /dev/null +++ b/lib/vagrant/machine_index/remote.rb @@ -0,0 +1,22 @@ +module Vagrant + class MachineIndex + # This module enables the MachineIndex for server mode + module Remote + + # Add an attribute reader for the client + # when applied to the MachineIndex class + def self.prepended(klass) + klass.class_eval do + attr_reader :client + end + end + + # Initializes a MachineIndex + def initialize(*args, **kwargs) + @machines = {} + @machine_locks = {} + @client = kwargs.key("client") + end + end + end +end diff --git a/plugins/commands/serve/client.rb b/plugins/commands/serve/client.rb index ff161452f..54c594768 100644 --- a/plugins/commands/serve/client.rb +++ b/plugins/commands/serve/client.rb @@ -9,6 +9,7 @@ module VagrantPlugins autoload :Machine, Vagrant.source_root.join("plugins/commands/serve/client/machine").to_s autoload :Project, Vagrant.source_root.join("plugins/commands/serve/client/project").to_s autoload :Terminal, Vagrant.source_root.join("plugins/commands/serve/client/terminal").to_s + autoload :MachineIndex, Vagrant.source_root.join("plugins/commands/serve/client/machine_index").to_s end end end diff --git a/plugins/commands/serve/client/machine_index.rb b/plugins/commands/serve/client/machine_index.rb index 3fe84a849..b2fd43a83 100644 --- a/plugins/commands/serve/client/machine_index.rb +++ b/plugins/commands/serve/client/machine_index.rb @@ -5,6 +5,11 @@ module VagrantPlugins attr_reader :client + def self.load(raw, broker:) + conn = broker.dial(raw.stream_id) + self.new(conn.to_s) + end + def initialize(conn) @logger = Log4r::Logger.new("vagrant::command::serve::client::machine") @logger.debug("connecting to target index service on #{conn}") @@ -14,15 +19,19 @@ module VagrantPlugins end def delete(machine) + @logger.debug("deleting machine #{machine} from index") end def get(uuid) + @logger.debug("getting machine with uuid #{uuid} from index") end def include?(uuid) + @logger.debug("checking for machine with uuid #{uuid} in index") end def set(entry) + @logger.debug("setting machine #{entry} in index") end end end diff --git a/plugins/commands/serve/client/project.rb b/plugins/commands/serve/client/project.rb index de4474c7f..e978506ad 100644 --- a/plugins/commands/serve/client/project.rb +++ b/plugins/commands/serve/client/project.rb @@ -40,6 +40,25 @@ module VagrantPlugins conn = @broker.dial(m.stream_id) return Machine.new(conn.to_s) end + + # return [VagrantPlugins::CommandServe::Client::MachineIndex] + def machine_index + @logger.debug("connecting to machine index") + req = Google::Protobuf::Empty.new + begin + raw_target_index = @client.target_index(req) + rescue => error + @logger.debug("target index unreachable") + @logger.debug(error.message) + end + @logger.debug("got response #{raw_target_index}") + # ti = SDK::Args::TargetIndex.decode(raw_target_index) + # @logger.debug("decoded: #{ti}") + @logger.debug("at stream id: #{raw_target_index.stream_id}") + m = MachineIndex.load(raw_target_index, broker: @broker) + @logger.debug("got machine index #{m}") + m + end end end end