diff --git a/plugins/commands/serve/client/machine_index.rb b/plugins/commands/serve/client/machine_index.rb deleted file mode 100644 index eb0abd7c1..000000000 --- a/plugins/commands/serve/client/machine_index.rb +++ /dev/null @@ -1,137 +0,0 @@ -module VagrantPlugins - module CommandServe - module Client - class MachineIndex - - attr_reader :client - attr_reader :broker - - def self.load(raw, broker:) - conn = broker.dial(raw.stream_id) - self.new(conn.to_s, broker) - end - - def initialize(conn, broker=nil) - @logger = Log4r::Logger.new("vagrant::command::serve::client::machineindex") - @logger.debug("connecting to target index service on #{conn}") - if !conn.nil? - @client = SDK::TargetIndexService::Stub.new(conn, :this_channel_is_insecure) - end - @broker = broker - end - - # @param [string] - # @return [Boolean] true if delete is successful - def delete(uuid) - @logger.debug("deleting machine with id #{uuid} from index") - ref = Hashicorp::Vagrant::Sdk::TargetIndex::TargetIdentifier.new( - id: uuid - ) - @client.delete(ref) - true - end - - # @param [string] - # @return [MachineIndex::Entry] - def get(uuid) - @logger.debug("getting machine with id #{uuid} from index") - ref = Hashicorp::Vagrant::Sdk::TargetIndex::TargetIdentifier.new( - id: uuid - ) - resp = @client.get(ref) - return machine_to_entry(resp) - end - - # @param [string] - # @return [Boolean] - def include?(uuid) - @logger.debug("checking for machine with id #{uuid} in index") - ref = Hashicorp::Vagrant::Sdk::TargetIndex::TargetIdentifier.new( - id: uuid - ) - @client.includes(ref).exists - end - - ## @param [Hashicorp::Vagrant::Sdk::Args::Target] target - # - # @param [MachineIndex::Entry] - # @return [MachineIndex::Entry] - def set(entry) - @logger.debug("setting machine #{entry} in index") - ref = Hashicorp::Vagrant::Sdk::TargetIndex::TargetIdentifier.new( - id: entry.id - ) - if entry.id.nil? - raise "Entry id should not be nil!" - end - machine = machine_arg_to_machine_client(@client.get(ref)) - machine.set_name(entry.name) - machine.set_state(entry.full_state) - machine_client_to_entry(machine) - end - - # Get all targets - # @return [Array] - def all() - @logger.debug("getting all machines") - req = Google::Protobuf::Empty.new - resp = @client.all(req) - arg_machines = resp.targets - machine_entries = [] - arg_machines.each do |m| - machine_entries << machine_to_entry(m) - end - machine_entries - end - - protected - - # Converts a Args::Target to a machine client - # - # @param [Hashicorp::Vagrant::Sdk::Args::Target] - # @return [VagrantPlugins::CommandServe::Client::Machine] - def machine_arg_to_machine_client(machine) - @logger.debug("transforming machine #{machine}") - conn = @broker.dial(machine.stream_id) - target_service = Hashicorp::Vagrant::Sdk::TargetService::Stub.new(conn.to_s, :this_channel_is_insecure) - machine = target_service.specialize(Google::Protobuf::Any.new) - m = Hashicorp::Vagrant::Sdk::Args::Target::Machine.decode(machine.value) - conn = @broker.dial(m.stream_id) - VagrantPlugins::CommandServe::Client::Machine.new(conn.to_s) - end - - # Converts a machine client to a machine index entry - # - # @param [VagrantPlugins::CommandServe::Client::Machine] - # @return [Vagrant::MachineIndex::Entry] - def machine_client_to_entry(machine_client) - state = machine_client.get_state() - raw = { - "name" => machine_client.get_name(), - "local_data_path" => machine_client.get_data_dir(), - # TODO: get the provider! - "provider" => "virtualbox", - "full_state" => state, - "state" => state.id, - "vagrantfile_name" => machine_client.get_vagrantfile_name(), - "vagrantfile_path" => machine_client.get_vagrantfile_path(), - } - id = machine_client.get_id() - @logger.debug("machine id: #{id}") - Vagrant::MachineIndex::Entry.new( - id=id, raw=raw - ) - end - - # Converts a machine to a machine index entry - # - # @param [Hashicorp::Vagrant::Sdk::Args::Target] - # @return [Vagrant::MachineIndex::Entry] - def machine_to_entry(machine) - machine_client = machine_arg_to_machine_client(machine) - machine_client_to_entry(machine_client) - end - end - end - end -end diff --git a/plugins/commands/serve/client/target_index.rb b/plugins/commands/serve/client/target_index.rb new file mode 100644 index 000000000..3308d697a --- /dev/null +++ b/plugins/commands/serve/client/target_index.rb @@ -0,0 +1,96 @@ +module VagrantPlugins + module CommandServe + module Client + class TargetIndex + + extend Util::Connector + + attr_reader :client + attr_reader :broker + + def initialize(conn, broker=nil) + @logger = Log4r::Logger.new("vagrant::command::serve::client::targetindex") + @logger.debug("connecting to target index service on #{conn}") + @client = SDK::TargetIndexService::Stub.new(conn, :this_channel_is_insecure) + @broker = broker + end + + def self.load(raw_index, broker:) + m = raw_index.is_a?(String) ? SDK::Args::TargetIndex.decode(raw_index) : raw_index + self.new(connect(proto: m, broker: broker), broker) + end + + # @param [string] + # @return [Boolean] true if delete is successful + def delete(ident) + @logger.debug("deleting machine with id #{ident} from index") + client.delete( + SDK::TargetIndex::TargetIdentifier.new( + id: ident + ) + ) + true + end + + # @param [string] + # @return [MachineIndex::Entry] + def get(ident) + @logger.debug("getting machine with id #{ident} from index") + begin + resp = client.get( + SDK::TargetIndex::TargetIdentifier.new( + id: ident + ) + ) + machine = Target.load(resp.target, broker: broker).to_machine + Entry.load(machine) + rescue GRPC::NotFound + nil + end + end + + # @param [string] + # @return [Boolean] + def include?(ident) + @logger.debug("checking for machine with id #{ident} in index") + client.includes( + SDK::TargetIndex::TargetIndexIdentifier.new( + id: ident + ) + ).exists + end + + ## @param [Hashicorp::Vagrant::Sdk::Args::Target] target + # + # @param [MachineIndex::Entry] + # @return [MachineIndex::Entry] + def set(entry) + @logger.debug("setting machine #{entry} in index") + if entry.id.to_s.empty? + raise ArgumentError, + "Entry id must be set" + end + resp = client.get( + SDK::TargetIndex::TargetIdentifier.new( + id: ident + ) + ) + machine = Target.load(resp.target, broker: broker).to_machine + machine.set_name(entry.name) + machine.set_state(entry.full_state) + Entry.load(machine) + end + + # Get all targets + # @return [Array] + def all + @logger.debug("getting all machines") + client.all(Empty.new).targets.map do |t_ref| + machine = Target.load(t_ref, broker: broker).to_machine + Entry.load(machine) + end + end + end + end + end +end