Class: ChefDK::PolicyfileServices::UpdateAttributes

Inherits:
Object
  • Object
show all
Includes:
Helpers, ChefDK::Policyfile::StorageConfigDelegation
Defined in:
lib/chef-dk/policyfile_services/update_attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#chefdk_home, #err, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_chefdk_location, #omnibus_embedded_bin_dir, #omnibus_install?, #omnibus_root, #stderr, #stdout, #system_command

Methods included from ChefDK::Policyfile::StorageConfigDelegation

#cache_path, #policyfile_expanded_path, #policyfile_filename, #policyfile_lock_expanded_path, #relative_paths_root

Constructor Details

#initialize(policyfile: nil, ui: nil, root_dir: nil) ⇒ UpdateAttributes

Returns a new instance of UpdateAttributes.



34
35
36
37
38
39
40
41
# File 'lib/chef-dk/policyfile_services/update_attributes.rb', line 34

def initialize(policyfile: nil, ui: nil, root_dir: 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
end

Instance Attribute Details

#storage_configObject (readonly)

Returns the value of attribute storage_config.



32
33
34
# File 'lib/chef-dk/policyfile_services/update_attributes.rb', line 32

def storage_config
  @storage_config
end

#uiObject (readonly)

Returns the value of attribute ui.



31
32
33
# File 'lib/chef-dk/policyfile_services/update_attributes.rb', line 31

def ui
  @ui
end

Instance Method Details

#assert_policy_and_lock_present!Object



92
93
94
95
96
97
98
99
# File 'lib/chef-dk/policyfile_services/update_attributes.rb', line 92

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_compilerObject



76
77
78
# File 'lib/chef-dk/policyfile_services/update_attributes.rb', line 76

def policyfile_compiler
  @policyfile_compiler ||= ChefDK::PolicyfileCompiler.evaluate(policyfile_content, policyfile_expanded_path, ui: ui)
end

#policyfile_contentObject



72
73
74
# File 'lib/chef-dk/policyfile_services/update_attributes.rb', line 72

def policyfile_content
  @policyfile_content ||= IO.read(policyfile_expanded_path)
end

#policyfile_lockObject



84
85
86
87
88
89
# File 'lib/chef-dk/policyfile_services/update_attributes.rb', line 84

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_contentObject



80
81
82
# File 'lib/chef-dk/policyfile_services/update_attributes.rb', line 80

def policyfile_lock_content
  @policyfile_lock_content ||= IO.read(policyfile_lock_expanded_path)
end

#runObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chef-dk/policyfile_services/update_attributes.rb', line 43

def run
  assert_policy_and_lock_present!

  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?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/chef-dk/policyfile_services/update_attributes.rb', line 68

def updated_lock?
  @updated
end