Sha256: 1674b4dc72f4ca77d209151aca3ac8e64a8dc0d012e04f10191280e23556e0f9

Contents?: true

Size: 1.77 KB

Versions: 196

Compression:

Stored size: 1.77 KB

Contents

require_relative "application"
require_relative "chef_fs/path_utils"
require_relative "http/simple"
require_relative "json_compat"

class Chef
  class ConfigFetcher

    attr_reader :config_location

    def initialize(config_location)
      @config_location = config_location
    end

    def expanded_path
      if config_location.nil? || remote_config?
        config_location
      else
        File.expand_path(config_location)
      end
    end

    def fetch_json
      config_data = read_config
      begin
        Chef::JSONCompat.from_json(config_data)
      rescue Chef::Exceptions::JSON::ParseError => error
        Chef::Application.fatal!("Could not parse the provided JSON file (#{config_location}): " + error.message)
      end
    end

    def read_config
      if remote_config?
        fetch_remote_config
      else
        read_local_config
      end
    end

    def fetch_remote_config
      http.get("")
    rescue SocketError, SystemCallError, Net::HTTPClientException => error
      Chef::Application.fatal!("Cannot fetch config '#{config_location}': '#{error.class}: #{error.message}")
    end

    def read_local_config
      ::File.read(config_location)
    rescue Errno::ENOENT
      Chef::Application.fatal!("Cannot load configuration from #{config_location}")
    rescue Errno::EACCES
      Chef::Application.fatal!("Permissions are incorrect on #{config_location}. Please chmod a+r #{config_location}")
    end

    def config_missing?
      return false if remote_config?

      # Check if the config file exists
      Pathname.new(config_location).realpath.to_s
      false
    rescue Errno::ENOENT
      true
    end

    def http
      Chef::HTTP::Simple.new(config_location)
    end

    def remote_config?
      !!(config_location =~ %r{^(http|https)://})
    end
  end
end

Version data entries

196 entries across 196 versions & 1 rubygems

Version Path
chef-17.9.46 lib/chef/config_fetcher.rb
chef-17.9.42-universal-mingw32 lib/chef/config_fetcher.rb
chef-17.9.42 lib/chef/config_fetcher.rb
chef-16.17.39-universal-mingw32 lib/chef/config_fetcher.rb
chef-16.17.39 lib/chef/config_fetcher.rb
chef-17.9.26-universal-mingw32 lib/chef/config_fetcher.rb
chef-17.9.26 lib/chef/config_fetcher.rb
chef-17.9.18-universal-mingw32 lib/chef/config_fetcher.rb
chef-17.9.18 lib/chef/config_fetcher.rb
chef-17.8.25-universal-mingw32 lib/chef/config_fetcher.rb
chef-17.8.25 lib/chef/config_fetcher.rb
chef-16.17.18-universal-mingw32 lib/chef/config_fetcher.rb
chef-16.17.18 lib/chef/config_fetcher.rb
chef-16.17.4-universal-mingw32 lib/chef/config_fetcher.rb
chef-16.17.4 lib/chef/config_fetcher.rb
chef-17.7.29-universal-mingw32 lib/chef/config_fetcher.rb
chef-17.7.29 lib/chef/config_fetcher.rb
chef-17.7.22-universal-mingw32 lib/chef/config_fetcher.rb
chef-17.7.22 lib/chef/config_fetcher.rb
chef-16.16.13-universal-mingw32 lib/chef/config_fetcher.rb