Class: Chef::ConfigFetcher

Inherits:
Object
  • 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)


54
55
56
57
58
59
60
61
62
# File 'lib/chef/config_fetcher.rb', line 54

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

#expanded_pathObject



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

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

#fetch_jsonObject



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

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

#fetch_remote_configObject



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

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

#httpObject



64
65
66
# File 'lib/chef/config_fetcher.rb', line 64

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

#read_configObject



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

def read_config
  if remote_config?
    fetch_remote_config
  else
    read_local_config
  end
end

#read_local_configObject



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

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

#remote_config?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/chef/config_fetcher.rb', line 68

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