Add remote host module

This commit is contained in:
sophia 2021-08-27 15:27:50 -05:00 committed by Paul Hinze
parent eeb950b987
commit 54c0b0528d
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
4 changed files with 68 additions and 0 deletions

View File

@ -68,6 +68,14 @@ module Vagrant
hook(:environment_load, runner: Action::Runner.new(env: self))
end
# Returns the host object associated with this environment.
#
# @return [Class]
def host
h = @client.host
Vagrant::Host.new(h, nil, nil, self)
end
# Gets a target (machine) by name
#
# @param [String] machine name

View File

@ -9,6 +9,8 @@ module Vagrant
class Host
include CapabilityHost
autoload :Remote, "vagrant/host/remote"
def initialize(host, hosts, capabilities, env)
initialize_capabilities!(host, hosts, capabilities, env)
end

View File

@ -0,0 +1,57 @@
module Vagrant
class Host
# This module enables Host for server mode
module Remote
# Add an attribute accesor for the client
# when applied to the Guest class
def self.prepended(klass)
klass.class_eval do
attr_accessor :client
end
end
# @param [] host client
# @param hosts - unused
# @param capabilities - unused
# @param [Vagrant::Environment]
def initialize(host, hosts, capabilities, env)
@env = env
@client = host
@logger = Log4r::Logger.new("vagrant::host")
end
def initialize_capabilities!(host, hosts, capabilities, *args)
# no-op
end
# Executes the capability with the given name, optionally passing more
# arguments onwards to the capability. If the capability returns a value,
# it will be returned.
#
# @param [Symbol] cap_name Name of the capability
def capability(cap_name, *args)
@logger.debug("running remote host capability #{cap_name} with args #{args}")
if !client.has_capability?(cap_name)
raise Errors::GuestCapabilityNotFound,
cap: cap_name.to_s,
guest: name
end
client.capability(cap_name, @env.to_proto, *args)
end
# Tests whether the given capability is possible.
#
# @param [Symbol] cap_name Capability name
# @return [Boolean]
def capability?(cap_name)
@logger.debug("checking for remote host capability #{cap_name}")
client.has_capability?(cap_name)
end
def to_proto
client.proto
end
end
end
end

View File

@ -242,6 +242,7 @@ module Vagrant
SERVER_MODE_CALLBACKS = [
->{ Vagrant::Guest.prepend(Vagrant::Guest::Remote) },
->{ Vagrant::Host.prepend(Vagrant::Host::Remote) },
->{ Vagrant::Machine.prepend(Vagrant::Machine::Remote) },
->{ Vagrant::Environment.prepend(Vagrant::Environment::Remote) },
->{ Vagrant::MachineIndex.prepend(Vagrant::MachineIndex::Remote) },