diff --git a/lib/vagrant/environment/remote.rb b/lib/vagrant/environment/remote.rb index 158b32d28..97848acf5 100644 --- a/lib/vagrant/environment/remote.rb +++ b/lib/vagrant/environment/remote.rb @@ -12,6 +12,20 @@ module Vagrant super @client = opts[:client] end + + # This returns a machine with the proper provider for this environment. + # The machine named by `name` must be in this environment. + # + # @param [Symbol] name Name of the machine (as configured in the + # Vagrantfile). + # @param [Symbol] provider The provider that this machine should be + # backed by. + # @param [Boolean] refresh If true, then if there is a cached version + # it is reloaded. + # @return [Machine] + def machine(name, provider, refresh=false) + return @client.target(name) + end end end end diff --git a/lib/vagrant/machine/remote.rb b/lib/vagrant/machine/remote.rb index 3ffbbede2..6428e391c 100644 --- a/lib/vagrant/machine/remote.rb +++ b/lib/vagrant/machine/remote.rb @@ -13,7 +13,7 @@ module Vagrant def initialize(name, provider_name, provider_cls, provider_config, provider_options, config, data_dir, box, env, vagrantfile, base=false) @logger = Log4r::Logger.new("vagrant::machine") - @client = VagrantPlugins::CommandServe::Client::Machine.new(name: name) + @client = VagrantPlugins::CommandServe::Client::Machine.new(env: env) @env = env @ui = Vagrant::UI::Prefixed.new(@env.ui, name) @provider_name = provider_name diff --git a/plugins/commands/serve/client.rb b/plugins/commands/serve/client.rb index d32c55bbc..ff161452f 100644 --- a/plugins/commands/serve/client.rb +++ b/plugins/commands/serve/client.rb @@ -7,6 +7,7 @@ module VagrantPlugins ServiceInfo = Service::ServiceInfo 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 end end diff --git a/plugins/commands/serve/client/machine.rb b/plugins/commands/serve/client/machine.rb index 0359d0366..99fa3ad18 100644 --- a/plugins/commands/serve/client/machine.rb +++ b/plugins/commands/serve/client/machine.rb @@ -18,7 +18,6 @@ module VagrantPlugins self.new(conn.to_s) end - def ref SDK::Ref::Machine.new(resource_id: resource_id) end diff --git a/plugins/commands/serve/client/project.rb b/plugins/commands/serve/client/project.rb index de24f994d..ec583eef3 100644 --- a/plugins/commands/serve/client/project.rb +++ b/plugins/commands/serve/client/project.rb @@ -6,15 +6,32 @@ module VagrantPlugins attr_reader :client def initialize(conn) + @logger = Log4r::Logger.new("vagrant::command::serve::client::project") @client = SDK::ProjectService::Stub.new(conn, :this_channel_is_insecure) end - def self.load(raw_project) - m = SDK::Args::Project.decode(raw_project) - conn = Broker.instance.dial(m.stream_id) + def self.load(raw_project, broker:) + p = SDK::Args::Project.decode(raw_project) + conn = broker.dial(p.stream_id) self.new(conn.to_s) end + # Returns a machine client for the given name + def target(name) + @logger.debug("searching for target #{name}") + req = SDK::Project::TargetRequest.new(name: name) + raw_target = @client.target(req) + @logger.debug("got target #{raw_target}") + conn = broker.dial(t.stream_id) + target_service = SDK::TargetService::Stub.new(conn.to_s, :this_channel_is_insecure) + @logger.debug("specializing target") + + machine = target_service.specialize(Google::Protobuf::Any.new) + + m = SDK::Args::Target::Machine.decode(machine.value) + conn = broker.dial(m.stream_id) + return Machine.new(conn) + end end end end diff --git a/plugins/commands/serve/service/command_service.rb b/plugins/commands/serve/service/command_service.rb index 827500774..8e04b3c85 100644 --- a/plugins/commands/serve/service/command_service.rb +++ b/plugins/commands/serve/service/command_service.rb @@ -87,23 +87,31 @@ module VagrantPlugins LOG.debug("machine name: #{machine_service.name(Google::Protobuf::Empty.new).name}") end + begin + arguments = SDK::Command::Arguments.decode(raw_args) + ui_client = Client::Terminal.load(raw_terminal, broker: broker) + env_client = Client::Project.load(raw_project, broker: broker) - arguments = SDK::Command::Arguments.decode(raw_args) - ui_client = Client::Terminal.load(raw_terminal, broker: broker) + ui = Vagrant::UI::RemoteUI.new(ui_client) + env = Vagrant::Environment.new( + {ui: ui, client: env_client} + ) - ui = Vagrant::UI::RemoteUI.new(ui_client) - env = Vagrant::Environment.new(ui: ui) + plugin = Vagrant::Plugin::V2::Plugin.manager.commands[plugin_name.to_sym].to_a.first + if !plugin + raise "Failed to locate command plugin for: #{plugin_name}" + end - plugin = Vagrant::Plugin::V2::Plugin.manager.commands[plugin_name.to_sym].to_a.first - if !plugin - raise "Failed to locate command plugin for: #{plugin_name}" + cmd_klass = plugin.call + cmd_args = req.command_args.to_a[1..] + arguments.args.to_a + cmd = cmd_klass.new(cmd_args, env) + result = cmd.execute + rescue => err + LOG.error(err) + LOG.debug("#{err.class}: #{err}\n#{err.backtrace.join("\n")}") + raise end - cmd_klass = plugin.call - cmd_args = req.command_args.to_a[1..] + arguments.args.to_a - cmd = cmd_klass.new(cmd_args, env) - result = cmd.execute - LOGGER.debug(result) if !result.is_a?(Integer) result = 1