Class: Chef::Config

Inherits:
Object show all
Extended by:
Mixlib::Config
Defined in:
lib/chef/config.rb

Constant Summary collapse

BACKSLASH =
'\\'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._this_fileObject

Path to this file in the current install.



606
607
608
# File 'lib/chef/config.rb', line 606

def self._this_file
  File.expand_path(__FILE__)
end

.add_formatter(name, file_path = nil) ⇒ Object



96
97
98
# File 'lib/chef/config.rb', line 96

def self.add_formatter(name, file_path=nil)
  formatters << [name, file_path]
end

.derive_path_from_chef_repo_path(child_path) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/chef/config.rb', line 165

def self.derive_path_from_chef_repo_path(child_path)
  if chef_repo_path.kind_of?(String)
    path_join(chef_repo_path, child_path)
  else
    chef_repo_path.map { |path| path_join(path, child_path)}
  end
end

.embedded_dirObject

If installed via an omnibus installer, this gives the path to the “embedded” directory which contains all of the software packaged with omnibus. This is used to locate the cacert.pem file on windows.



595
596
597
598
599
600
601
602
603
# File 'lib/chef/config.rb', line 595

def self.embedded_dir
  Pathname.new(_this_file).ascend do |path|
    if path.basename.to_s == "embedded"
      return path.to_s
    end
  end

  nil
end

.envObject

This provides a hook which rspec can stub so that we can avoid twiddling global state in tests.



547
548
549
# File 'lib/chef/config.rb', line 547

def self.env
  ENV
end

.find_chef_repo_path(cwd) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/chef/config.rb', line 149

def self.find_chef_repo_path(cwd)
  # In local mode, we auto-discover the repo root by looking for a path with "cookbooks" under it.
  # This allows us to run config-free.
  path = cwd
  until File.directory?(path_join(path, "cookbooks"))
    new_path = File.expand_path('..', path)
    if new_path == path
      Chef::Log.warn("No cookbooks directory found at or above current directory.  Assuming #{Dir.pwd}.")
      return Dir.pwd
    end
    path = new_path
  end
  Chef::Log.info("Auto-discovered chef repository at #{path}")
  path
end

.from_string(string, filename) ⇒ Object

Evaluates the given string as config.

filename is used for context in stacktraces, but doesn’t need to be the name of an actual file.



36
37
38
# File 'lib/chef/config.rb', line 36

def self.from_string(string, filename)
  self.instance_eval(string, filename, 1)
end

.inspectObject



57
58
59
# File 'lib/chef/config.rb', line 57

def self.inspect
  configuration.inspect
end

.manage_secret_keyObject

Manages the chef secret session key

Returns

<newkey>

A new or retrieved session key



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chef/config.rb', line 44

def self.manage_secret_key
  newkey = nil
  if Chef::FileCache.has_key?("chef_server_cookie_id")
    newkey = Chef::FileCache.load("chef_server_cookie_id")
  else
    chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
    newkey = ""
    40.times { |i| newkey << chars[rand(chars.size-1)] }
    Chef::FileCache.store("chef_server_cookie_id", newkey)
  end
  newkey
end

.on_windows?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/chef/config.rb', line 61

def self.on_windows?
  RUBY_PLATFORM =~ /mswin|mingw|windows/
end

.path_accessible?(path) ⇒ Boolean

Returns true only if the path exists and is readable and writeable for the user.

Returns:

  • (Boolean)


261
262
263
# File 'lib/chef/config.rb', line 261

def self.path_accessible?(path)
  File.exists?(path) && File.readable?(path) && File.writable?(path)
end

.path_join(*args) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/chef/config.rb', line 75

def self.path_join(*args)
  args = args.flatten
  args.inject do |joined_path, component|
    unless joined_path[-1,1] == platform_path_separator
      joined_path += platform_path_separator
    end
    joined_path += component
  end
end

.platform_path_separatorObject



67
68
69
70
71
72
73
# File 'lib/chef/config.rb', line 67

def self.platform_path_separator
  if on_windows?
    File::ALT_SEPARATOR || BACKSLASH
  else
    File::SEPARATOR
  end
end

.platform_specific_path(path) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/chef/config.rb', line 85

def self.platform_specific_path(path)
  if on_windows?
    # turns /etc/chef/client.rb into C:/chef/client.rb
    system_drive = env['SYSTEMDRIVE'] ? env['SYSTEMDRIVE'] : ""
    path = File.join(system_drive, path.split('/')[2..-1])
    # ensure all forward slashes are backslashes
    path.gsub!(File::SEPARATOR, (File::ALT_SEPARATOR || '\\'))
  end
  path
end

.set_defaults_for_nixObject



525
526
527
528
529
530
531
532
533
534
535
# File 'lib/chef/config.rb', line 525

def self.set_defaults_for_nix
  # Those lists of regular expressions define what chef considers a
  # valid user and group name
  #
  # user/group cannot start with '-', '+' or '~'
  # user/group cannot contain ':', ',' or non-space-whitespace or null byte
  # everything else is allowed (UTF-8, spaces, etc) and we delegate to your O/S useradd program to barf or not
  # copies: http://anonscm.debian.org/viewvc/pkg-shadow/debian/trunk/debian/patches/506_relaxed_usernames?view=markup
  default :user_valid_regex, [ /^[^-+~:,\t\r\n\f\0]+[^:,\t\r\n\f\0]*$/ ]
  default :group_valid_regex, [ /^[^-+~:,\t\r\n\f\0]+[^:,\t\r\n\f\0]*$/ ]
end

.set_defaults_for_windowsObject



514
515
516
517
518
519
520
521
522
523
# File 'lib/chef/config.rb', line 514

def self.set_defaults_for_windows
  # Those lists of regular expressions define what chef considers a
  # valid user and group name
  # From http://technet.microsoft.com/en-us/library/cc776019(WS.10).aspx
  principal_valid_regex_part = '[^"\/\\\\\[\]\:;|=,+*?<>]+'
  default :user_valid_regex, [ /^(#{principal_valid_regex_part}\\)?#{principal_valid_regex_part}$/ ]
  default :group_valid_regex, [ /^(#{principal_valid_regex_part}\\)?#{principal_valid_regex_part}$/ ]

  default :fatal_windows_admin_check, false
end

.windows_home_pathObject



551
552
553
# File 'lib/chef/config.rb', line 551

def self.windows_home_path
  windows_home_path = env['SYSTEMDRIVE'] + env['HOMEPATH'] if env['SYSTEMDRIVE'] && env['HOMEPATH']
end

Instance Method Details

#userObject

Daemonization Settings ## What user should Chef run as?



283
# File 'lib/chef/config.rb', line 283

default :user, nil