Class: ChefDK::PolicyfileLock

Inherits:
Object
  • Object
show all
Includes:
ChefDK::Policyfile::StorageConfigDelegation
Defined in:
lib/chef-dk/policyfile_lock.rb

Defined Under Namespace

Classes: InstallReport

Constant Summary collapse

RUN_LIST_ITEM_FORMAT =
/\Arecipe\[[^\s]+::[^\s]+\]\Z/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ChefDK::Policyfile::StorageConfigDelegation

#cache_path, #policyfile_filename, #relative_paths_root

Constructor Details

#initialize(storage_config, ui: nil) ⇒ PolicyfileLock

Returns a new instance of PolicyfileLock.



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/chef-dk/policyfile_lock.rb', line 92

def initialize(storage_config, ui: nil)
  @name = nil
  @run_list = []
  @cookbook_locks = {}
  @relative_paths_root = Dir.pwd
  @storage_config = storage_config
  @ui = ui || UI.null

  @solution_dependencies = Policyfile::SolutionDependencies.new
  @install_report = InstallReport.new(ui: @ui, policyfile_lock: self)
end

Instance Attribute Details

#cookbook_locksObject (readonly)

Returns the value of attribute cookbook_locks.



88
89
90
# File 'lib/chef-dk/policyfile_lock.rb', line 88

def cookbook_locks
  @cookbook_locks
end

#install_reportObject (readonly)

Returns the value of attribute install_report.



90
91
92
# File 'lib/chef-dk/policyfile_lock.rb', line 90

def install_report
  @install_report
end

#nameObject

Returns the value of attribute name.



81
82
83
# File 'lib/chef-dk/policyfile_lock.rb', line 81

def name
  @name
end

#run_listObject

Returns the value of attribute run_list.



82
83
84
# File 'lib/chef-dk/policyfile_lock.rb', line 82

def run_list
  @run_list
end

#solution_dependenciesObject (readonly)

Returns the value of attribute solution_dependencies.



84
85
86
# File 'lib/chef-dk/policyfile_lock.rb', line 84

def solution_dependencies
  @solution_dependencies
end

#storage_configObject (readonly)

Returns the value of attribute storage_config.



86
87
88
# File 'lib/chef-dk/policyfile_lock.rb', line 86

def storage_config
  @storage_config
end

Class Method Details

.build(storage_config) {|lock| ... } ⇒ Object

Yields:

  • (lock)


67
68
69
70
71
# File 'lib/chef-dk/policyfile_lock.rb', line 67

def self.build(storage_config)
  lock = new(storage_config)
  yield lock
  lock
end

.build_from_compiler(compiler, storage_config) ⇒ Object



73
74
75
76
77
# File 'lib/chef-dk/policyfile_lock.rb', line 73

def self.build_from_compiler(compiler, storage_config)
  lock = new(storage_config)
  lock.build_from_compiler(compiler)
  lock
end

Instance Method Details

#build_from_compiler(compiler) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/chef-dk/policyfile_lock.rb', line 168

def build_from_compiler(compiler)
  @name = compiler.name

  @run_list = compiler.normalized_run_list

  compiler.all_cookbook_location_specs.each do |cookbook_name, spec|
    if spec.mirrors_canonical_upstream?
      cached_cookbook(cookbook_name) do |cached_cb|
        cached_cb.cache_key = spec.cache_key
        cached_cb.origin = spec.uri
        cached_cb.source_options = spec.source_options_for_lock
      end
    else
      local_cookbook(cookbook_name) do |local_cb|
        local_cb.source = spec.relative_path
        local_cb.source_options = spec.source_options_for_lock
      end
    end
  end

  @solution_dependencies = compiler.solution_dependencies

  self
end

#build_from_lock_data(lock_data) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/chef-dk/policyfile_lock.rb', line 193

def build_from_lock_data(lock_data)
  set_name_from_lock_data(lock_data)
  set_run_list_from_lock_data(lock_data)
  set_cookbook_locks_from_lock_data(lock_data)
  set_solution_dependencies_from_lock_data(lock_data)
  self
end

#cached_cookbook(name) {|cached_cookbook| ... } ⇒ Object

Yields:



108
109
110
111
112
# File 'lib/chef-dk/policyfile_lock.rb', line 108

def cached_cookbook(name)
  cached_cookbook = Policyfile::CachedCookbook.new(name, storage_config)
  yield cached_cookbook if block_given?
  @cookbook_locks[name] = cached_cookbook
end

#cookbook_locks_for_lockfileObject



133
134
135
136
137
138
139
140
# File 'lib/chef-dk/policyfile_lock.rb', line 133

def cookbook_locks_for_lockfile
  cookbook_locks.inject({}) do |locks_map, (name, location_spec)|
    location_spec.validate!
    location_spec.gather_profile_data
    locks_map[name] = location_spec.to_lock
    locks_map
  end
end

#dependencies {|solution_dependencies| ... } ⇒ Object



120
121
122
# File 'lib/chef-dk/policyfile_lock.rb', line 120

def dependencies
  yield solution_dependencies
end

#ensure_cache_dir_existsObject



211
212
213
214
215
216
# File 'lib/chef-dk/policyfile_lock.rb', line 211

def ensure_cache_dir_exists
  # note: duplicates PolicyfileCompiler#ensure_cache_dir_exists
  unless File.exist?(cache_path)
    FileUtils.mkdir_p(cache_path)
  end
end

#install_cookbooksObject



201
202
203
204
205
206
207
208
209
# File 'lib/chef-dk/policyfile_lock.rb', line 201

def install_cookbooks
  # note: duplicates PolicyfileCompiler#ensure_cache_dir_exists
  ensure_cache_dir_exists

  cookbook_locks.each do |cookbook_name, cookbook_lock|
    install_report.installing_cookbook(cookbook_lock)
    cookbook_lock.install_locked
  end
end

#local_cookbook(name) {|local_cookbook| ... } ⇒ Object

Yields:



114
115
116
117
118
# File 'lib/chef-dk/policyfile_lock.rb', line 114

def local_cookbook(name)
  local_cookbook = Policyfile::LocalCookbook.new(name, storage_config)
  yield local_cookbook if block_given?
  @cookbook_locks[name] = local_cookbook
end

#lock_data_for(cookbook_name) ⇒ Object



104
105
106
# File 'lib/chef-dk/policyfile_lock.rb', line 104

def lock_data_for(cookbook_name)
  @cookbook_locks[cookbook_name]
end

#to_lockObject



124
125
126
127
128
129
130
131
# File 'lib/chef-dk/policyfile_lock.rb', line 124

def to_lock
  {}.tap do |lock|
    lock["name"] = name
    lock["run_list"] = run_list
    lock["cookbook_locks"] = cookbook_locks_for_lockfile
    lock["solution_dependencies"] = solution_dependencies.to_lock
  end
end

#validate_cookbooks!Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/chef-dk/policyfile_lock.rb', line 142

def validate_cookbooks!
  cookbook_locks.each do |name, cookbook_lock|
    cookbook_lock.validate!
    cookbook_lock.refresh!
  end

  # Check that versions and dependencies are still valid. First we need to
  # refresh the dependency info for everything that has changed, then we
  # check that the new versions and dependencies are valid for the working
  # set of cookbooks. We can't do this in a single loop because the user
  # may have modified two cookbooks such that the versions and constraints
  # are only valid when both changes are considered together.
  cookbook_locks.each do |name, cookbook_lock|
    if cookbook_lock.updated?
      solution_dependencies.update_cookbook_dep(name, cookbook_lock.version, cookbook_lock.dependencies)
    end
  end
  cookbook_locks.each do |name, cookbook_lock|
    if cookbook_lock.updated?
      solution_dependencies.test_conflict!(cookbook_lock.name, cookbook_lock.version)
    end
  end

  true
end