vaguerent/plugins/commands/serve/service/config_service.rb
Chris Roberts e958c6183a Adds initial HCP config support
Adds initial basic support for HCP based configuration in vagrant-go.
The initalization process has been updated to remove Vagrantfile parsing
from the client, moving it to the runner using init jobs for the basis
and the project (if there is one). Detection is done on the file based
on extension for Ruby based parsing or HCP based parsing.

Current HCP parsing is extremely simple and currently just a base to
build off. Config components will be able to implement an `Init`
function to handle receiving configuration data from a non-native source
file. This will be extended to include a default approach for injecting
defined data in the future.

Some cleanup was done in the state around validations. Some logging
adjustments were applied on the Ruby side for better behavior
consistency.

VirtualBox provider now caches locale detection to prevent multiple
checks every time the driver is initialized.
2023-09-07 17:26:10 -07:00

130 lines
3.7 KiB
Ruby

# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1
module VagrantPlugins
module CommandServe
module Service
class ConfigService < ProtoService(SDK::ConfigService::Service)
CONFIG_LOCATIONS = [
:config,
:provider_configs,
:provisioner_configs,
:push_configs,
]
def register(req, ctx)
with_plugin(ctx, CONFIG_LOCATIONS, broker: broker) do |plugin|
resp = SDK::Config::RegisterResponse.new
Vagrant.plugin("2").local_manager.registered.each do |plg|
plg.components.configs.each do |scope, registry|
registry.each do |ident, val|
if plugin == val
resp.scope = scope unless scope == :top
resp.identifier = ident
break
end
end
end
end
resp
end
end
def init_spec(*_)
funcspec(
args: [
SDK::Args::ConfigData,
],
result: SDK::Config::InitResponse
)
end
def init(req, ctx)
with_plugin(ctx, :config, broker: broker) do |plugin|
config_data_p = mapper.unfuncspec(req.args.first)
config_data = mapper.map(config_data_p.data)
instance = plugin.new
config_data.each do |key, value|
key = key.downcase.to_sym
if instance.respond_to?("#{key}=".to_sym)
instance.send("#{key}=".to_sym, value)
elsif instance.respond_to?(key)
instance.send(key, value)
else
logger.warn("unknown config key to apply: class: #{plugin} key: #{key}")
end
end
SDK::Config::InitResponse.new(data: mapper.map(instance, to: SDK::Args::ConfigData))
end
end
def struct_spec(*_)
funcspec(
args: [
],
result: SDK::Config::StructResponse
)
end
def struct(req, ctx)
SDK::Config::StructResponse.new(raw: true)
end
def merge_spec(*_)
funcspec(
args: [
SDK::Config::Merge,
],
result: SDK::Args::ConfigData,
)
end
def merge(req, ctx)
with_plugin(ctx, :config, broker: broker) do |plugin|
m = mapper.unfuncspec(req.args.first)
base = mapper.map(m.base, to: plugin)
overlay = mapper.map(m.overlay, to: plugin)
mapper.map(base.merge(overlay), to: SDK::Args::ConfigData)
end
end
def finalize_spec(*_)
funcspec(
args: [
SDK::Config::Finalize,
],
result: SDK::Args::ConfigData
)
end
def finalize(req, ctx)
with_plugin(ctx, CONFIG_LOCATIONS, broker: broker) do |plugin|
# Extract the proto from the funcspec
f = mapper.unfuncspec(req.args.first)
cproto = f.config
# If the config data does not include a source class, we treat
# the request as simply wanting the default finalized data
if cproto.source.name.to_s.empty?
config = plugin.new
else
config = mapper.map(cproto, to: plugin)
end
config.finalize!
# This is just a marker for debugging that we were
# responsible for the finalization
config.instance_variable_set("@__service_finalized", true)
SDK::Config::FinalizeResponse.new(data: mapper.map(config, to: SDK::Args::ConfigData))
end
end
end
end
end
end