Class: Chef::ConfigFetcher

Inherits:
Object show all
Defined in:
lib/chef/config_fetcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_location) ⇒ ConfigFetcher

Returns a new instance of ConfigFetcher.



11
12
13
# File 'lib/chef/config_fetcher.rb', line 11

def initialize(config_location)
  @config_location = config_location
end

Instance Attribute Details

#config_locationObject (readonly)

Returns the value of attribute config_location.



9
10
11
# File 'lib/chef/config_fetcher.rb', line 9

def config_location
  @config_location
end

Instance Method Details

#config_missing?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
# File 'lib/chef/config_fetcher.rb', line 46

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
  return true
end

#fetch_jsonObject



15
16
17
18
19
20
21
22
# File 'lib/chef/config_fetcher.rb', line 15

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, 2)
  end
end

#fetch_remote_configObject



32
33
34
35
36
# File 'lib/chef/config_fetcher.rb', line 32

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

#httpObject



56
57
58
# File 'lib/chef/config_fetcher.rb', line 56

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

#read_configObject



24
25
26
27
28
29
30
# File 'lib/chef/config_fetcher.rb', line 24

def read_config
  if remote_config?
    fetch_remote_config
  else
    read_local_config
  end
end

#read_local_configObject



38
39
40
41
42
43
44
# File 'lib/chef/config_fetcher.rb', line 38

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

#remote_config?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/chef/config_fetcher.rb', line 60

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