From ed48186831835be81b436a160bcf190a7756d473 Mon Sep 17 00:00:00 2001 From: Marc Chamberland Date: Sat, 9 May 2020 22:58:18 -0400 Subject: move dist implementation into chef-utils Signed-off-by: Marc Chamberland Signed-off-by: Lance Albertson --- lib/chef/train_transport.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/chef/train_transport.rb') diff --git a/lib/chef/train_transport.rb b/lib/chef/train_transport.rb index a4f311fc51..ba935f637f 100644 --- a/lib/chef/train_transport.rb +++ b/lib/chef/train_transport.rb @@ -17,7 +17,7 @@ require "chef-config/mixin/credentials" autoload :Train, "train" -require_relative "dist" +require "chef-utils" class Chef class TrainTransport @@ -77,8 +77,8 @@ class Chef credentials_file = if tm_config.credentials_file && File.exist?(tm_config.credentials_file) tm_config.credentials_file - elsif File.exist?(Chef::Config.platform_specific_path("#{Chef::Dist::CONF_DIR}/#{profile}/credentials")) - Chef::Config.platform_specific_path("#{Chef::Dist::CONF_DIR}/#{profile}/credentials") + elsif File.exist?("#{ChefConfig::Config.etc_chef_dir}/#{profile}/credentials") + "#{ChefConfig::Config.etc_chef_dir}/#{profile}/credentials" else super end @@ -102,7 +102,7 @@ class Chef tm_config = Chef::Config.target_mode protocol = tm_config.protocol train_config = tm_config.to_hash.select { |k| Train.options(protocol).key?(k) } - Chef::Log.trace("Using target mode options from #{Chef::Dist::PRODUCT} config file: #{train_config.keys.join(", ")}") if train_config + Chef::Log.trace("Using target mode options from #{ChefUtils::Dist::Infra::PRODUCT} config file: #{train_config.keys.join(", ")}") if train_config # Load the credentials file, and place any valid settings into the train configuration credentials = load_credentials(tm_config.host) -- cgit v1.2.1 From 215dfd4834cf59e508c26b45dbddbc698c57fd7b Mon Sep 17 00:00:00 2001 From: Marc Chamberland Date: Sun, 10 May 2020 09:49:03 -0400 Subject: tighten all require statements for dist implementation Signed-off-by: Marc Chamberland Signed-off-by: Lance Albertson --- lib/chef/train_transport.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/chef/train_transport.rb') diff --git a/lib/chef/train_transport.rb b/lib/chef/train_transport.rb index ba935f637f..cea51d76c3 100644 --- a/lib/chef/train_transport.rb +++ b/lib/chef/train_transport.rb @@ -17,7 +17,7 @@ require "chef-config/mixin/credentials" autoload :Train, "train" -require "chef-utils" +require "chef-utils/dist/infra" class Chef class TrainTransport -- cgit v1.2.1 From 1633da85e2aa3c8b8d8e1bd284ea8c82966d68a0 Mon Sep 17 00:00:00 2001 From: Marc Chamberland Date: Mon, 11 May 2020 19:14:01 -0400 Subject: merge all dist files back into dist.rb Signed-off-by: Marc Chamberland Signed-off-by: Lance Albertson --- lib/chef/train_transport.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/chef/train_transport.rb') diff --git a/lib/chef/train_transport.rb b/lib/chef/train_transport.rb index cea51d76c3..fc9ad98fc3 100644 --- a/lib/chef/train_transport.rb +++ b/lib/chef/train_transport.rb @@ -17,7 +17,7 @@ require "chef-config/mixin/credentials" autoload :Train, "train" -require "chef-utils/dist/infra" +require "chef-utils/dist" class Chef class TrainTransport -- cgit v1.2.1 From 9ade086b6e0f64ae0610e3ecd2fb1d99b6d91962 Mon Sep 17 00:00:00 2001 From: Lance Albertson Date: Mon, 31 Aug 2020 12:09:27 -0700 Subject: Workaround rubygems slow requires by only running require for chef-utils/dist Signed-off-by: Lance Albertson --- lib/chef/train_transport.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/chef/train_transport.rb') diff --git a/lib/chef/train_transport.rb b/lib/chef/train_transport.rb index fc9ad98fc3..f2ab69921c 100644 --- a/lib/chef/train_transport.rb +++ b/lib/chef/train_transport.rb @@ -17,7 +17,7 @@ require "chef-config/mixin/credentials" autoload :Train, "train" -require "chef-utils/dist" +require "chef-utils/dist" unless defined?(ChefUtils::Dist) class Chef class TrainTransport -- cgit v1.2.1 From abbaf5a19db7c58b8d0fcabbb285fe390ec4be3d Mon Sep 17 00:00:00 2001 From: Lance Albertson Date: Mon, 14 Sep 2020 13:16:31 -0700 Subject: Revert change that removed use of Chef::Config.platform_specific_path Signed-off-by: Lance Albertson --- lib/chef/train_transport.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/chef/train_transport.rb') diff --git a/lib/chef/train_transport.rb b/lib/chef/train_transport.rb index f2ab69921c..7d6539b416 100644 --- a/lib/chef/train_transport.rb +++ b/lib/chef/train_transport.rb @@ -77,8 +77,8 @@ class Chef credentials_file = if tm_config.credentials_file && File.exist?(tm_config.credentials_file) tm_config.credentials_file - elsif File.exist?("#{ChefConfig::Config.etc_chef_dir}/#{profile}/credentials") - "#{ChefConfig::Config.etc_chef_dir}/#{profile}/credentials" + elsif File.exist?(Chef::Config.platform_specific_path("#{ChefConfig::Config.etc_chef_dir}/#{profile}/credentials")) + Chef::Config.platform_specific_path("#{ChefConfig::Config.etc_chef_dir}/#{profile}/credentials") else super end -- cgit v1.2.1 From b597635a194f0f238692b0c69ffcf7d8958986bc Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Tue, 15 Sep 2020 17:33:20 -0700 Subject: Move TrainTransport to ChefConfig Also pull in some helpers from the Ohai work so that Ohai can use them later. This is code rearrangement to support the remote ohai work but does not change any existing behavior yet. Signed-off-by: Lamont Granquist --- lib/chef/train_transport.rb | 109 ++------------------------------------------ 1 file changed, 5 insertions(+), 104 deletions(-) (limited to 'lib/chef/train_transport.rb') diff --git a/lib/chef/train_transport.rb b/lib/chef/train_transport.rb index 7d6539b416..4fe1fcadec 100644 --- a/lib/chef/train_transport.rb +++ b/lib/chef/train_transport.rb @@ -15,114 +15,15 @@ # limitations under the License. # -require "chef-config/mixin/credentials" -autoload :Train, "train" -require "chef-utils/dist" unless defined?(ChefUtils::Dist) +require "chef-config/mixin/train_transport" unless defined?(ChefConfig::Mixin::TrainTransport) class Chef class TrainTransport - extend ChefConfig::Mixin::Credentials + include ChefConfig::Mixin::TrainTransport - # - # Returns a RFC099 credentials profile as a hash - # - def self.load_credentials(profile) - # Tomlrb.load_file returns a hash with keys as strings - credentials = parse_credentials_file - if contains_split_fqdn?(credentials, profile) - Chef::Log.warn("Credentials file #{credentials_file_path} contains target '#{profile}' as a Hash, expected a string.") - Chef::Log.warn("Hostnames must be surrounded by single quotes, e.g. ['host.example.org']") - end - - # host names must be specified in credentials file as ['foo.example.org'] with quotes - if !credentials.nil? && !credentials[profile].nil? - credentials[profile].map { |k, v| [k.to_sym, v] }.to_h # return symbolized keys to match Train.options() - else - nil - end - end - - # Toml creates hashes when a key is separated by periods, e.g. - # [host.example.org] => { host: { example: { org: {} } } } - # - # Returns true if the above example is true - # - # A hostname has to be specified as ['host.example.org'] - # This will be a common mistake so we should catch it - # - def self.contains_split_fqdn?(hash, fqdn) - fqdn.split(".").reduce(hash) do |h, k| - v = h[k] - if Hash === v - v - else - break false - end - end - end - - # ChefConfig::Mixin::Credentials.credentials_file_path is designed around knife, - # overriding it here. - # - # Credentials file preference: - # - # 1) target_mode.credentials_file - # 2) /etc/chef/TARGET_MODE_HOST/credentials - # 3) #credentials_file_path from parent ($HOME/.chef/credentials) - # - def self.credentials_file_path - tm_config = Chef::Config.target_mode - profile = tm_config.host - - credentials_file = - if tm_config.credentials_file && File.exist?(tm_config.credentials_file) - tm_config.credentials_file - elsif File.exist?(Chef::Config.platform_specific_path("#{ChefConfig::Config.etc_chef_dir}/#{profile}/credentials")) - Chef::Config.platform_specific_path("#{ChefConfig::Config.etc_chef_dir}/#{profile}/credentials") - else - super - end - - raise ArgumentError, "No credentials file found for target '#{profile}'" unless credentials_file - raise ArgumentError, "Credentials file specified for target mode does not exist: '#{credentials_file}'" unless File.exist?(credentials_file) - - Chef::Log.debug("Loading credentials file '#{credentials_file}' for target '#{profile}'") - - credentials_file - end - - def self.build_transport(logger = Chef::Log.with_child(subsystem: "transport")) - return nil unless Chef::Config.target_mode? - - # TODO: Consider supporting parsing the protocol from a URI passed to `--target` - # - train_config = {} - - # Load the target_mode config context from Chef::Config, and place any valid settings into the train configuration - tm_config = Chef::Config.target_mode - protocol = tm_config.protocol - train_config = tm_config.to_hash.select { |k| Train.options(protocol).key?(k) } - Chef::Log.trace("Using target mode options from #{ChefUtils::Dist::Infra::PRODUCT} config file: #{train_config.keys.join(", ")}") if train_config - - # Load the credentials file, and place any valid settings into the train configuration - credentials = load_credentials(tm_config.host) - if credentials - valid_settings = credentials.select { |k| Train.options(protocol).key?(k) } - valid_settings[:enable_password] = credentials[:enable_password] if credentials.key?(:enable_password) - train_config.merge!(valid_settings) - Chef::Log.trace("Using target mode options from credentials file: #{valid_settings.keys.join(", ")}") if valid_settings - end - - train_config[:logger] = logger - - # Train handles connection retries for us - Train.create(protocol, train_config) - rescue SocketError => e # likely a dns failure, not caught by train - e.message.replace "Error connecting to #{train_config[:target]} - #{e.message}" - raise e - rescue Train::PluginLoadError - logger.error("Invalid target mode protocol: #{protocol}") - exit(false) + def config + require "chef/config" unless defined?(Chef::Config) + Chef::Config end end end -- cgit v1.2.1