From 972ba095e9b49aef94251557f3cf4eb243606f87 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Mon, 13 Jun 2022 17:07:31 -0700 Subject: [PATCH] Add remote vagrantfile implementation --- lib/vagrant/shared_helpers.rb | 1 + lib/vagrant/vagrantfile.rb | 2 + lib/vagrant/vagrantfile/remote.rb | 67 +++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 lib/vagrant/vagrantfile/remote.rb diff --git a/lib/vagrant/shared_helpers.rb b/lib/vagrant/shared_helpers.rb index 8466dc21a..5dd2250b5 100644 --- a/lib/vagrant/shared_helpers.rb +++ b/lib/vagrant/shared_helpers.rb @@ -270,6 +270,7 @@ module Vagrant ->{ Vagrant::MachineIndex::Entry.extend(Vagrant::MachineIndex::Entry::Remote::ClassMethods) }, ->{ Vagrant::Action::Builtin::MixinSyncedFolders.prepend(Vagrant::Action::Builtin::Remote::MixinSyncedFolders) }, ->{ Vagrant::Action::Builtin::SSHRun.prepend(Vagrant::Action::Builtin::Remote::SSHRun) }, + ->{ Vagrant::Vagrantfile.prepend(Vagrant::Vagrantfile::Remote) }, ->{ Vagrant::Util::SSH.prepend(Vagrant::Util::Remote::SSH) }, ].freeze end diff --git a/lib/vagrant/vagrantfile.rb b/lib/vagrant/vagrantfile.rb index 1a4d5006b..1d9393ea9 100644 --- a/lib/vagrant/vagrantfile.rb +++ b/lib/vagrant/vagrantfile.rb @@ -12,6 +12,8 @@ module Vagrant # loading the configuration of a specific machine/provider combo, # etc. class Vagrantfile + autoload :Remote, "vagrant/vagrantfile/remote" + # This is the configuration loaded as-is given the loader and # keys to #initialize. attr_reader :config diff --git a/lib/vagrant/vagrantfile/remote.rb b/lib/vagrant/vagrantfile/remote.rb new file mode 100644 index 000000000..ee4266843 --- /dev/null +++ b/lib/vagrant/vagrantfile/remote.rb @@ -0,0 +1,67 @@ +# lib/remote.rb + +module Vagrant + class Vagrantfile + module Remote + # Add an attribute reader for the client + # when applied to the Machine class + def self.prepended(klass) + klass.class_eval do + attr_reader :client + end + end + + def initialize(*_, client:) + @client = client + @config = ConfigWrapper.new(client: client) + end + + # @return [Machine] + def machine(name, provider, _, _, _) + client.machine(name, provider) + end + + def machine_names + client.target_names + end + + def machine_config(name, provider, _, _, validate_provider) + client.machine_config(name, provider, validate_provider) + end + end + + class ConfigWrapper + def initialize(client:) + @client = client + @logger = Log4r::Logger.new(self.class.name.downcase) + @root = Vagrant::Config::V2::Root.new(Vagrant.plugin("2").local_manager.config) + end + + def method_missing(*args, **opts, &block) + case args.size + when 1 + namespace = args.first + when 2 + if args.first.to_s != "[]" + raise ArgumentError, + "Expected #[] but received ##{args.first} on config wrapper" + end + namespace = args.last + else + #raise ArgumentError, + @logger.error("Cannot handle wrapped request for: #{args.inspect}") + end + + # TODO: Check args, opts, and block and return error if any are set + @logger.info("config wrapper fetching config value for namespace: #{namespace}") + begin + @client.get_config(namespace) + rescue => err + val = @root.send(*args) + @logger.warn("failed to get config value, reason: #{err} - returning val: #{val}") + val + end + end + end + end +end