class ChefCLI::PolicyfileServices::UpdateAttributes

Attributes

chef_config[R]
storage_config[R]
ui[R]

Public Class Methods

new(policyfile: nil, ui: nil, root_dir: nil, chef_config: nil) click to toggle source
# File lib/chef-cli/policyfile_services/update_attributes.rb, line 36
def initialize(policyfile: nil, ui: nil, root_dir: nil, chef_config: nil)
  @ui = ui

  policyfile_rel_path = policyfile || "Policyfile.rb"
  policyfile_full_path = File.expand_path(policyfile_rel_path, root_dir)
  @storage_config = Policyfile::StorageConfig.new.use_policyfile(policyfile_full_path)
  @updated = false
  @chef_config = chef_config
end

Public Instance Methods

assert_policy_and_lock_present!() click to toggle source
# File lib/chef-cli/policyfile_services/update_attributes.rb, line 95
def assert_policy_and_lock_present!
  unless File.exist?(policyfile_expanded_path)
    raise PolicyfileNotFound, "Policyfile not found at path #{policyfile_expanded_path}"
  end
  unless File.exist?(policyfile_lock_expanded_path)
    raise LockfileNotFound, "Policyfile lock not found at path #{policyfile_lock_expanded_path}"
  end
end
policyfile_compiler() click to toggle source
# File lib/chef-cli/policyfile_services/update_attributes.rb, line 80
def policyfile_compiler
  @policyfile_compiler ||= ChefCLI::PolicyfileCompiler.evaluate(policyfile_content, policyfile_expanded_path, ui: ui, chef_config: chef_config)
end
policyfile_content() click to toggle source
# File lib/chef-cli/policyfile_services/update_attributes.rb, line 76
def policyfile_content
  @policyfile_content ||= IO.read(policyfile_expanded_path)
end
policyfile_lock() click to toggle source
# File lib/chef-cli/policyfile_services/update_attributes.rb, line 88
def policyfile_lock
  @policyfile_lock ||= begin
    lock_data = FFI_Yajl::Parser.new.parse(policyfile_lock_content)
    PolicyfileLock.new(storage_config, ui: ui).build_from_lock_data(lock_data)
  end
end
policyfile_lock_content() click to toggle source
# File lib/chef-cli/policyfile_services/update_attributes.rb, line 84
def policyfile_lock_content
  @policyfile_lock_content ||= IO.read(policyfile_lock_expanded_path)
end
prepare_constraints() click to toggle source
# File lib/chef-cli/policyfile_services/update_attributes.rb, line 104
def prepare_constraints
  # Ensure we recompute policies from their (possibly updated) source
  Policyfile::LockApplier.new(policyfile_lock, policyfile_compiler)
    .with_unlocked_policies(:all)
    .apply!
end
run() click to toggle source
# File lib/chef-cli/policyfile_services/update_attributes.rb, line 46
def run
  assert_policy_and_lock_present!
  prepare_constraints

  if policyfile_compiler.default_attributes != policyfile_lock.default_attributes
    policyfile_lock.default_attributes = policyfile_compiler.default_attributes
    @updated = true
  end

  if policyfile_compiler.override_attributes != policyfile_lock.override_attributes
    policyfile_lock.override_attributes = policyfile_compiler.override_attributes
    @updated = true
  end

  if updated_lock?
    with_file(policyfile_lock_expanded_path) do |f|
      f.print(FFI_Yajl::Encoder.encode(policyfile_lock.to_lock, pretty: true ))
    end
    ui.msg("Updated attributes in #{policyfile_lock_expanded_path}")
  else
    ui.msg("Attributes already up to date")
  end
rescue => error
  raise PolicyfileUpdateError.new("Failed to update Policyfile lock", error)
end
updated_lock?() click to toggle source
# File lib/chef-cli/policyfile_services/update_attributes.rb, line 72
def updated_lock?
  @updated
end