Fill in provider ruby service
This commit is contained in:
parent
5fda271283
commit
75e6081e6d
@ -1,76 +1,170 @@
|
|||||||
require 'vagrant/machine'
|
|
||||||
require 'vagrant/batch_action'
|
|
||||||
require 'vagrant/ui'
|
|
||||||
|
|
||||||
module VagrantPlugins
|
module VagrantPlugins
|
||||||
module CommandServe
|
module CommandServe
|
||||||
module Service
|
module Service
|
||||||
class ProviderService < SDK::ProviderService::Service
|
class ProviderService < SDK::ProviderService::Service
|
||||||
include Util::ServiceInfo
|
|
||||||
include Util::HasSeeds::Service
|
include CapabilityPlatformService
|
||||||
|
|
||||||
prepend Util::HasMapper
|
def initialize(*args, **opts, &block)
|
||||||
prepend Util::HasBroker
|
caps = Vagrant.plugin("2").local_manager.provider_capabilities
|
||||||
prepend Util::HasLogger
|
default_args = {
|
||||||
include Util::ExceptionTransformer
|
Client::Target => SDK::FuncSpec::Value.new(
|
||||||
|
type: "hashicorp.vagrant.sdk.Args.Target",
|
||||||
def usable(req, _unused_call)
|
name: "",
|
||||||
nil
|
),
|
||||||
|
}
|
||||||
|
initialize_capability_platform!(caps, default_args)
|
||||||
end
|
end
|
||||||
|
|
||||||
def usable_spec(req, _unused_call)
|
def usable_spec(*_)
|
||||||
nil
|
SDK::FuncSpec.new(
|
||||||
|
name: "usable_spec",
|
||||||
|
args: [],
|
||||||
|
result: [
|
||||||
|
SDK::FuncSpec::Value.new(
|
||||||
|
type: "hashicorp.vagrant.sdk.Provider.UsableResp",
|
||||||
|
name: "",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def installed(req, _unused_call)
|
def usable(req, ctx)
|
||||||
nil
|
plugins = Vagrant.plugin("2").local_manager.providers
|
||||||
end
|
with_plugin(ctx, plugins, broker: broker) do |plugin|
|
||||||
|
is_usable = plugin.usable?
|
||||||
def installed_spec(req, _unused_call)
|
SDK::Provider::UsableResp.new(
|
||||||
nil
|
is_usable: is_usable,
|
||||||
end
|
)
|
||||||
|
|
||||||
def init(req, _unused_call)
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def init_spec(req, _unused_call)
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def action_up(req, ctx)
|
|
||||||
with_info(ctx, broker: broker) do |info|
|
|
||||||
plugin_name = info.plugin_name
|
|
||||||
ui, machine = mapper.funcspec_map(req.spec, expect: [Vagrant::UI::Remote, Vagrant::Machine])
|
|
||||||
|
|
||||||
machine = Client::Target::Machine.load(raw_machine, ui)
|
|
||||||
machine.ui.warn("hello from vagrant")
|
|
||||||
SDK::Provider::ActionResp.new(success: true)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def action_up_spec(req, _unused_call)
|
def installed_spec(*_)
|
||||||
args = [
|
SDK::FuncSpec.new(
|
||||||
Hashicorp::Vagrant::Sdk::FuncSpec::Value.new(
|
name: "installed_spec",
|
||||||
type: "hashicorp.vagrant.sdk.Args.TerminalUI",
|
args: [],
|
||||||
name: ""
|
result: [
|
||||||
),
|
SDK::FuncSpec::Value.new(
|
||||||
Hashicorp::Vagrant::Sdk::FuncSpec::Value.new(
|
type: "hashicorp.vagrant.sdk.Provider.InstalledResp",
|
||||||
type: "hashicorp.vagrant.sdk.Args.Machine",
|
name: "",
|
||||||
name: ""
|
),
|
||||||
),
|
],
|
||||||
]
|
|
||||||
result = [
|
|
||||||
Hashicorp::Vagrant::Sdk::FuncSpec::Value.new(
|
|
||||||
type: "hashicorp.vagrant.sdk.Provider.ActionResp",
|
|
||||||
name: ""
|
|
||||||
),
|
|
||||||
]
|
|
||||||
Hashicorp::Vagrant::Sdk::FuncSpec.new(
|
|
||||||
args: args,
|
|
||||||
result: result
|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def installed(*_)
|
||||||
|
plugins = Vagrant.plugin("2").local_manager.providers
|
||||||
|
with_plugin(ctx, plugins, broker: broker) do |plugin|
|
||||||
|
is_installed = plugin.installed?
|
||||||
|
SDK::Provider::InstalledResp.new(
|
||||||
|
is_installed: is_installed,
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def init_spec(*_)
|
||||||
|
# TODO
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def init(req, ctx)
|
||||||
|
# TODO
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def action_spec(req, _unused_call)
|
||||||
|
SDK::FuncSpec.new(
|
||||||
|
name: "capability_spec",
|
||||||
|
args: [
|
||||||
|
SDK::FuncSpec::Value.new(
|
||||||
|
type: "hashicorp.vagrant.sdk.Args.Direct",
|
||||||
|
name: "",
|
||||||
|
)
|
||||||
|
],
|
||||||
|
result: []
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def action_up(req, ctx)
|
||||||
|
# TODO
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def machine_id_changed_spec(*_)
|
||||||
|
SDK::FuncSpec.new(
|
||||||
|
name: "machine_id_changed_spec",
|
||||||
|
args: [],
|
||||||
|
result: [],
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def machine_id_changed(req, ctx)
|
||||||
|
plugins = Vagrant.plugin("2").local_manager.providers
|
||||||
|
with_plugin(ctx, plugins, broker: broker) do |plugin|
|
||||||
|
provider = plugin.new
|
||||||
|
# TODO: does this provider need to be initialized?
|
||||||
|
# provider.initialize(machine)?
|
||||||
|
provider.machine_id_changed
|
||||||
|
end
|
||||||
|
Empty.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def ssh_info_spec(*_)
|
||||||
|
SDK::FuncSpec.new(
|
||||||
|
name: "ssh_info_spec",
|
||||||
|
args: [],
|
||||||
|
result: [
|
||||||
|
SDK::FuncSpec::Value.new(
|
||||||
|
type: "hashicorp.vagrant.sdk.SSHInfo",
|
||||||
|
name: "",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def ssh_info(req, ctx)
|
||||||
|
plugins = Vagrant.plugin("2").local_manager.providers
|
||||||
|
with_plugin(ctx, plugins, broker: broker) do |plugin|
|
||||||
|
provider = plugin.new
|
||||||
|
# TODO: does this provider need to be initialized?
|
||||||
|
# provider.initialize(machine)?
|
||||||
|
info = provider.ssh_info
|
||||||
|
return SDK::SSHInfo.new(
|
||||||
|
host: info[:host],
|
||||||
|
port: info[:port],
|
||||||
|
username: info[:username],
|
||||||
|
private_key_path: info[:private_key_path]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def state_spec(*_)
|
||||||
|
SDK::FuncSpec.new(
|
||||||
|
name: "ssh_info_spec",
|
||||||
|
args: [],
|
||||||
|
result: [
|
||||||
|
SDK::FuncSpec::Value.new(
|
||||||
|
type: "hashicorp.vagrant.sdk.Args.Target.Machine.State",
|
||||||
|
name: "",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def state(req, ctx)
|
||||||
|
plugins = Vagrant.plugin("2").local_manager.providers
|
||||||
|
with_plugin(ctx, plugins, broker: broker) do |plugin|
|
||||||
|
provider = plugin.new
|
||||||
|
# TODO: does this provider need to be initialized?
|
||||||
|
# provider.initialize(machine)?
|
||||||
|
machine_state = provider.state
|
||||||
|
return SDK::Args::Target::Machine::State.new(
|
||||||
|
id: machine_state.id,
|
||||||
|
short_description: machine_state.short_description,
|
||||||
|
long_description: machine_state.long_description,
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -31,6 +31,41 @@ module VagrantPlugins
|
|||||||
end
|
end
|
||||||
Thread.current.thread_variable_set(:service_info, nil)
|
Thread.current.thread_variable_set(:service_info, nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def with_plugin(context, plugins, broker:, &block)
|
||||||
|
if !context.metadata["plugin_name"]
|
||||||
|
raise KeyError,
|
||||||
|
"plugin name not defined (metadata content: #{context.metadata.inspect})"
|
||||||
|
end
|
||||||
|
|
||||||
|
info = Service::ServiceInfo.new(
|
||||||
|
plugin_name: context.metadata["plugin_name"],
|
||||||
|
broker: broker
|
||||||
|
)
|
||||||
|
if context.metadata["plugin_manager"] && info.broker
|
||||||
|
Service::ServiceInfo.manager_tracker.activate do
|
||||||
|
info.plugin_manager = Client::PluginManager.load(
|
||||||
|
context.metadata["plugin_manager"],
|
||||||
|
broker: info.broker
|
||||||
|
)
|
||||||
|
Vagrant.plugin("2").enable_remote_manager
|
||||||
|
end
|
||||||
|
end
|
||||||
|
Thread.current.thread_variable_set(:service_info, info)
|
||||||
|
return if !block_given?
|
||||||
|
plugin_name = info.plugin_name
|
||||||
|
plugin = plugins[plugin_name.to_s.to_sym].to_a.first
|
||||||
|
if !plugin
|
||||||
|
logger.debug("Failed to locate plugin for: #{plugin_name} in list: #{plugins.keys}")
|
||||||
|
raise "Failed to locate plugin for: #{plugin_name.inspect}"
|
||||||
|
end
|
||||||
|
yield plugin
|
||||||
|
ensure
|
||||||
|
Service::ServiceInfo.manager_tracker.deactivate do
|
||||||
|
Vagrant.plugin("2").disable_remote_manager
|
||||||
|
end
|
||||||
|
Thread.current.thread_variable_set(:service_info, nil)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user