Class: Chef::ChefFS::Config

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

Overview

Helpers to take Chef::Config and create chef_fs and local_fs from it

Constant Summary collapse

PATH_VARIABLES =
%w(acl_path client_path cookbook_path container_path data_bag_path environment_path group_path node_path role_path user_path)

Instance Method Summary collapse

Constructor Details

#initialize(chef_config = Chef::Config, cwd = Dir.pwd) ⇒ Config

Returns a new instance of Config.



28
29
30
31
32
# File 'lib/chef/chef_fs/config.rb', line 28

def initialize(chef_config = Chef::Config, cwd = Dir.pwd)
  @chef_config = chef_config
  @cwd = cwd
  configure_repo_paths
end

Instance Method Details

#base_pathObject

The current directory, relative to server root



103
104
105
106
107
108
109
110
111
# File 'lib/chef/chef_fs/config.rb', line 103

def base_path
  @base_path ||= begin
    if @chef_config[:chef_repo_path]
      server_path(File.expand_path(@cwd))
    else
      nil
    end
  end
end

#chef_fsObject



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

def chef_fs
  @chef_fs ||= create_chef_fs
end

#create_chef_fsObject



40
41
42
43
# File 'lib/chef/chef_fs/config.rb', line 40

def create_chef_fs
  require 'chef/chef_fs/file_system/chef_server_root_dir'
  Chef::ChefFS::FileSystem::ChefServerRootDir.new("remote", @chef_config)
end

#create_local_fsObject



49
50
51
52
# File 'lib/chef/chef_fs/config.rb', line 49

def create_local_fs
  require 'chef/chef_fs/file_system/chef_repository_file_system_root_dir'
  Chef::ChefFS::FileSystem::ChefRepositoryFileSystemRootDir.new(object_paths)
end

#format_path(entry) ⇒ Object

Print the given server path, relative to the current directory



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/chef/chef_fs/config.rb', line 114

def format_path(entry)
  server_path = entry.path
  if base_path && server_path[0,base_path.length] == base_path
    if server_path == base_path
      return "."
    elsif server_path[base_path.length,1] == "/"
      return server_path[base_path.length + 1, server_path.length - base_path.length - 1]
    elsif base_path == "/" && server_path[0,1] == "/"
      return server_path[1, server_path.length - 1]
    end
  end
  server_path
end

#local_fsObject



45
46
47
# File 'lib/chef/chef_fs/config.rb', line 45

def local_fs
  @local_fs ||= create_local_fs
end

#require_chef_repo_pathObject



128
129
130
131
132
133
# File 'lib/chef/chef_fs/config.rb', line 128

def require_chef_repo_path
  if !@chef_config[:chef_repo_path]
    Chef::Log.error("Must specify either chef_repo_path or cookbook_path in Chef config file")
    exit(1)
  end
end

#server_path(file_path) ⇒ Object

Returns the given real path’s location relative to the server root.

If chef_repo is /home/jkeiser/chef_repo, and pwd is /home/jkeiser/chef_repo/cookbooks, server_path(‘blah’) == ‘/cookbooks/blah’ server_path(‘../roles/blah.json’) == ‘/roles/blah’ server_path(‘../../readme.txt’) == nil server_path(‘*/ab’) == ‘/cookbooks/*/ab’ server_path(‘/home/jkeiser/chef_repo/cookbooks/blah’) == ‘/cookbooks/blah’ server_path(‘/home/*/chef_repo/cookbooks/blah’) == nil

If there are multiple paths (cookbooks, roles, data bags, etc. can all have separate paths), and cwd+the path reaches into one of them, we will return a path relative to that. Otherwise we will return a path to chef_repo.

Globs are allowed as well, but globs outside server paths are NOT (presently) supported. See above examples. TODO support that.

If the path does not reach into ANY specified directory, nil is returned.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/chef/chef_fs/config.rb', line 74

def server_path(file_path)
  pwd = File.expand_path(Dir.pwd)
  absolute_path = Chef::ChefFS::PathUtils.realest_path(File.expand_path(file_path, pwd))

  # Check all object paths (cookbooks_dir, data_bags_dir, etc.)
  object_paths.each_pair do |name, paths|
    paths.each do |path|
      realest_path = Chef::ChefFS::PathUtils.realest_path(path)
      if absolute_path[0,realest_path.length] == realest_path &&
        (absolute_path.length == realest_path.length ||
          absolute_path[realest_path.length,1] =~ /#{PathUtils.regexp_path_separator}/)
        relative_path = Chef::ChefFS::PathUtils::relative_to(absolute_path, realest_path)
        return relative_path == '.' ? "/#{name}" : "/#{name}/#{relative_path}"
      end
    end
  end

  # Check chef_repo_path
  Array(@chef_config[:chef_repo_path]).flatten.each do |chef_repo_path|
    realest_chef_repo_path = Chef::ChefFS::PathUtils.realest_path(chef_repo_path)
    if absolute_path == realest_chef_repo_path
      return '/'
    end
  end

  nil
end