Attempt to specialize target to machine

This commit is contained in:
sophia 2021-07-01 10:17:38 -05:00 committed by Paul Hinze
parent 37d42bc8ef
commit 87bd08ef12
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
2 changed files with 81 additions and 100 deletions

View File

@ -7,55 +7,17 @@ module VagrantPlugins
attr_reader :resource_id
def initialize(conn)
@client = SDK::MachineService::Stub.new(conn, :this_channel_is_insecure)
@logger = Log4r::Logger.new("vagrant::command::serve::client::machine")
@logger.debug("connecting to target machine service on #{conn}")
@client = SDK::TargetMachineService::Stub.new(conn, :this_channel_is_insecure)
end
def self.load(raw_machine)
m = SDK::Args::Machine.decode(raw_machine)
conn = Broker.instance.dial(m.stream_id)
def self.load(raw_machine, broker:)
m = SDK::Args::Target.decode(raw_machine)
conn = broker.dial(m.stream_id)
self.new(conn.to_s)
end
# Create a new instance
def initialize(name:)
@client = ServiceInfo.client_for(SDK::MachineService)
if name.nil?
@resource_id = ServiceInfo.info.machine
else
load_machine(name)
end
end
# Seed this instance with the resource id for the
# requested machine. If the machine doesn't already
# exist, create it
def load_machine(name)
c = ServiceInfo.client_for(SRV::Vagrant)
machine = SRV::Machine.new(
name: name,
project: SRV::Ref::Project.new(
resource_id: ServiceInfo.info.project
),
state: SDK::Args::MachineState.new(
id: :not_created,
short_description: "not created",
long_description: "Machine not currently created"
)
)
begin
resp = c.find_machine(SRV::FindMachineRequest.new(
machine: machine))
@resource_id = resp.machine.resource_id
return
rescue GRPC::NotFound
# Let this fall through so we create the machine
end
resp = c.upsert_machine(SRV::UpsertMachineRequest.new(
machine: machine))
@resource_id = resp.machine.resource_id
end
def ref
SDK::Ref::Machine.new(resource_id: resource_id)
@ -63,25 +25,20 @@ module VagrantPlugins
# @return [String] machine name
def get_name
req = SDK::Machine::GetNameRequest.new(
machine: ref
)
client.get_name(req).name
req = Google::Protobuf::Empty.new
@client.name(req).name
end
def set_name(name)
req = SDK::Machine::SetNameRequest.new(
machine: ref,
req = SDK::Target::SetNameRequest.new(
name: name
)
client.set_name(req)
@client.set_name(req)
end
def get_id
req = SDK::Machine::GetIDRequest.new(
machine: ref
)
result = client.get_id(req).id
req = Google::Protobuf::Empty.new
result = @client.get_id(req).id
if result.nil?
raise "Failed to get machine ID. REF: #{ref.inspect} - ID WAS NIL"
end
@ -89,18 +46,15 @@ module VagrantPlugins
end
def set_id(new_id)
req = SDK::Machine::SetIDRequest.new(
machine: ref,
req = SDK::Target::SetIDRequest.new(
id: new_id
)
client.set_id(req)
@client.set_id(req)
end
def get_box
req = SDK::Machine::BoxRequest.new(
machine: ref
)
resp = client.box(req)
req = Google::Protobuf::Empty.new
resp = @client.box(req)
Vagrant::Box.new(
resp.box.name,
resp.box.provider.to_sym,
@ -110,55 +64,44 @@ module VagrantPlugins
end
def get_data_dir
req = SDK::Machine::DatadirRequest.new(
machine: ref
)
client.datadir(req).data_dir
req = Google::Protobuf::Empty.new
@client.datadir(req).data_dir
end
# TODO: local data path comes from the project
def get_local_data_path
req = SDK::Machine::LocalDataPathRequest.new(
machine: ref
)
client.localdatapath(req).path
@client.localdatapath(req).path
end
def get_provider
req = SDK::Machine::ProviderRequest.new(
machine: ref
)
client.provider(req)
req = Google::Protobuf::Empty.new
@client.provider(req)
end
def get_vagrantfile_name
req = SDK::Machine::VagrantfileNameRequest.new(
machine: ref
)
resp = client.vagrantfile_name(req)
req = Google::Protobuf::Empty.new
resp = @client.vagrantfile_name(req)
resp.name
end
def get_vagrantfile_path
req = SDK::Machine::VagrantfilePathRequest.new(
machine: ref
)
resp = client.vagrantfile_path(req)
req = Google::Protobuf::Empty.new
resp = @client.vagrantfile_path(req)
Pathname.new(resp.path)
end
def updated_at
req = SDK::Machine::UpdatedAtRequest.new(
machine: ref
)
resp = client.updated_at(req)
req = Google::Protobuf::Empty.new
resp = @client.updated_at(req)
resp.updated_at
end
def get_state
req = SDK::Machine::GetStateRequest.new(
machine: ref
)
resp = client.get_state(req)
req = Google::Protobuf::Empty.new
resp = @client.get_state(req)
Vagrant::MachineState.new(
resp.state.id.to_sym,
resp.state.short_description,
@ -166,27 +109,23 @@ module VagrantPlugins
)
end
# @param [SRV::Operation::PhysicalState] state of the machine
def set_state(state)
req = SDK::Machine::SetStateRequest.new(
machine: ref,
state: SDK::Args::MachineState.new(
id: state.id.to_s,
short_description: state.short_description.to_s,
long_description: state.long_description.to_s
req = SDK::Target::Machine::SetStateRequest.new(
state: SDK::Args::Target::Machine::State.new(
id: state.id,
short_description: state.short_description,
long_description: state.long_description,
)
)
client.set_state(req)
@client.set_state(req)
end
def get_uuid
req = SDK::Machine::GetUUIDRequest.new(
machine: ref
)
client.get_uuid(req).uuid
req = Google::Protobuf::Empty.new
@client.get_uuid(req).uuid
end
end
end
end
end

View File

@ -5,6 +5,7 @@ module VagrantPlugins
prepend Util::HasBroker
prepend Util::ExceptionLogger
LOG = Log4r::Logger.new("vagrant::command::serve::command")
def command_info_spec(*args)
SDK::FuncSpec.new
@ -32,6 +33,10 @@ module VagrantPlugins
type: "hashicorp.vagrant.sdk.Args.Project",
name: "",
),
SDK::FuncSpec::Value.new(
type: "hashicorp.vagrant.sdk.Args.Target",
name: "",
),
SDK::FuncSpec::Value.new(
type: "hashicorp.vagrant.sdk.Command.Arguments",
name: "",
@ -55,6 +60,43 @@ module VagrantPlugins
raw_args = req.spec.args.detect { |a|
a.type == "hashicorp.vagrant.sdk.Command.Arguments"
}&.value&.value
raw_project = req.spec.args.detect { |a|
a.type == "hashicorp.vagrant.sdk.Args.Project"
}&.value&.value
raw_target = req.spec.args.detect { |a|
a.type == "hashicorp.vagrant.sdk.Args.Target"
}&.value&.value
begin
# If a target is specified, specialize into a machine
if !raw_target.nil?
t = SDK::Args::Target.decode(raw_target)
LOG.debug("got a target: #{t}")
conn = broker.dial(t.stream_id)
target_service = SDK::TargetService::Stub.new(conn.to_s, :this_channel_is_insecure)
req = Google::Protobuf::Empty.new
LOG.debug("got target #{target_service.name(req).name}")
LOG.debug("specializing target #{target_service}")
machine = target_service.specialize(
Google::Protobuf::Any.pack(
SDK::SSHInfo.new(
port: "", ssh_command: ""
)
)
)
@logger.debug("target specialized to #{machine}")
m = SDK::Args::MachineTarget.decode(machine)
LOG.debug("got a machine: #{m}")
conn = broker.dial(m.stream_id)
@logger.debug("connecting to target machine service on #{conn}")
machine_service = SDK::TargetMachineService::Stub.new(conn.to_s, :this_channel_is_insecure)
@logger.debug("machine name: #{machine_service.name(Google::Protobuf::Empty.new).name}")
end
rescue => err
LOG.debug("#{err.class}: #{err}\n#{err.backtrace.join("\n")}")
raise
end
arguments = SDK::Command::Arguments.decode(raw_args)
ui_client = Client::Terminal.load(raw_terminal, broker: broker)