Class: Chef::Provider::ChefMirror

Inherits:
LWRPBase
  • Object
show all
Defined in:
lib/chef/provider/chef_mirror.rb

Defined Under Namespace

Classes: CopyListener

Instance Method Summary collapse

Instance Method Details

#copy_to(src_root, dest_root) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chef/provider/chef_mirror.rb', line 47

def copy_to(src_root, dest_root)
  if new_resource.concurrency && new_resource.concurrency <= 0
    raise "chef_mirror.concurrency must be above 0!  Was set to #{new_resource.concurrency}"
  end
  # Honor concurrency
  Chef::ChefFS::Parallelizer.threads = (new_resource.concurrency || 10) - 1

  # We don't let the user pass absolute paths; we want to reserve those for
  # multi-org support (/organizations/foo).
  if new_resource.path[0] == '/'
    raise "Absolute paths in chef_mirror not yet supported."
  end
  # Copy!
  path = Chef::ChefFS::FilePattern.new("/#{new_resource.path}")
  ui = CopyListener.new(self)
  error = Chef::ChefFS::FileSystem.copy_to(path, src_root, dest_root, nil, options, ui, proc { |p| p.path })

  if error
    raise "Errors while copying:#{ui.errors.map { |e| "#{e}\n" }.join('')}"
  end
end

#load_current_resourceObject



135
136
# File 'lib/chef/provider/chef_mirror.rb', line 135

def load_current_resource
end

#local_fsObject



69
70
71
72
73
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
101
102
103
104
105
# File 'lib/chef/provider/chef_mirror.rb', line 69

def local_fs
  # If chef_repo_path is set to a string, put it in the form it usually is in
  # chef config (:chef_repo_path, :node_path, etc.)
  path_config = new_resource.chef_repo_path
  if path_config.is_a?(Hash)
    chef_repo_path = path_config.delete(:chef_repo_path)
  elsif path_config
    chef_repo_path = path_config
    path_config = {}
  else
    chef_repo_path = Chef::Config.chef_repo_path
    path_config = Chef::Config
  end
  chef_repo_path = Array(chef_repo_path).flatten

  # Go through the expected object paths and figure out the local paths for each.
  case repo_mode
  when 'hosted_everything'
    object_names = %w(acls clients cookbooks containers data_bags environments groups nodes roles)
  else
    object_names = %w(clients cookbooks data_bags environments nodes roles users)
  end

  object_paths = {}
  object_names.each do |object_name|
    variable_name = "#{object_name[0..-2]}_path" # cookbooks -> cookbook_path
    if path_config[variable_name.to_sym]
      paths = Array(path_config[variable_name.to_sym]).flatten
    else
      paths = chef_repo_path.map { |path| ::File.join(path, object_name) }
    end
    object_paths[object_name] = paths.map { |path| ::File.expand_path(path) }
  end

  # Set up the root dir
  Chef::ChefFS::FileSystem::ChefRepositoryFileSystemRootDir.new(object_paths)
end

#optionsObject



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/chef/provider/chef_mirror.rb', line 122

def options
  result = {
    :purge => new_resource.purge,
    :freeze => new_resource.freeze,
    :diff => new_resource.no_diff,
    :dry_run => whyrun_mode?
  }
  result[:diff] = !result[:diff]
  result[:repo_mode] = repo_mode
  result[:concurrency] = new_resource.concurrency if new_resource.concurrency
  result
end

#remote_fsObject



107
108
109
110
111
112
113
114
115
116
# File 'lib/chef/provider/chef_mirror.rb', line 107

def remote_fs
  config = {
    :chef_server_url => new_resource.chef_server[:chef_server_url],
    :node_name => new_resource.chef_server[:options][:client_name],
    :client_key => new_resource.chef_server[:options][:client_key],
    :repo_mode => repo_mode,
    :versioned_cookbooks => Chef::Config.versioned_cookbooks
  }
  Chef::ChefFS::FileSystem::ChefServerRootDir.new("remote", config)
end

#repo_modeObject



118
119
120
# File 'lib/chef/provider/chef_mirror.rb', line 118

def repo_mode
  new_resource.chef_server[:chef_server_url] =~ /\/organizations\// ? 'hosted_everything' : 'everything'
end

#whyrun_supported?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/chef/provider/chef_mirror.rb', line 10

def whyrun_supported?
  true
end

#with_modified_configObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chef/provider/chef_mirror.rb', line 26

def with_modified_config
  # pre-Chef-12 ChefFS reads versioned_cookbooks out of Chef::Config instead of
  # taking it as an input, so we need to modify it for the duration of copy_to
  @old_versioned_cookbooks = Chef::Config.versioned_cookbooks
  # If versioned_cookbooks is explicitly set, set it.
  if !new_resource.versioned_cookbooks.nil?
    Chef::Config.versioned_cookbooks = new_resource.versioned_cookbooks

  # If new_resource.chef_repo_path is set, versioned_cookbooks defaults to true.
  # Otherwise, it stays at its current Chef::Config value.
  elsif new_resource.chef_repo_path
    Chef::Config.versioned_cookbooks = true
  end

  begin
    yield
  ensure
    Chef::Config.versioned_cookbooks = @old_versioned_cookbooks
  end
end