Class: ChefCLI::PolicyfileServices::UpdateAttributes
  
  
  
  Instance Attribute Summary collapse
  
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  Methods included from Helpers
  #err, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_bin_dir, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_expand_path, #omnibus_install?, #omnibus_root, #package_home, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix
  
  
  
  
  
  
  
  
  
  #cache_path, #policyfile_expanded_path, #policyfile_filename, #policyfile_lock_expanded_path, #relative_paths_root
  Constructor Details
  
    
  
  
    #initialize(policyfile: nil, ui: nil, root_dir: nil, chef_config: nil)  ⇒ UpdateAttributes 
  
  
  
  
    
Returns a new instance of UpdateAttributes.
   
 
  
  
    
      
36
37
38
39
40
41
42
43
44 
     | 
    
      # 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 
     | 
  
 
  
 
  
    Instance Attribute Details
    
      
      
      
  
  
    #chef_config  ⇒ Object  
  
  
  
  
    
Returns the value of attribute chef_config.
   
 
  
  
    
      
34
35
36 
     | 
    
      # File 'lib/chef-cli/policyfile_services/update_attributes.rb', line 34
def chef_config
  @chef_config
end 
     | 
  
 
    
      
      
      
  
  
    #storage_config  ⇒ Object  
  
  
  
  
    
Returns the value of attribute storage_config.
   
 
  
  
    
      
33
34
35 
     | 
    
      # File 'lib/chef-cli/policyfile_services/update_attributes.rb', line 33
def storage_config
  @storage_config
end 
     | 
  
 
    
      
      
      
  
  
    #ui  ⇒ Object  
  
  
  
  
    
Returns the value of attribute ui.
   
 
  
  
    
      
32
33
34 
     | 
    
      # File 'lib/chef-cli/policyfile_services/update_attributes.rb', line 32
def ui
  @ui
end 
     | 
  
 
    
   
  
    Instance Method Details
    
      
  
  
    #assert_policy_and_lock_present!  ⇒ Object 
  
  
  
  
    
      
95
96
97
98
99
100
101
102 
     | 
    
      # 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  ⇒ Object 
  
  
  
  
    
      
80
81
82 
     | 
    
      # 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:, chef_config:)
end 
     | 
  
 
    
      
  
  
    #policyfile_content  ⇒ Object 
  
  
  
  
    
      
76
77
78 
     | 
    
      # File 'lib/chef-cli/policyfile_services/update_attributes.rb', line 76
def policyfile_content
  @policyfile_content ||= IO.read(policyfile_expanded_path)
end 
     | 
  
 
    
      
  
  
    #policyfile_lock  ⇒ Object 
  
  
  
  
    
      
88
89
90
91
92
93 
     | 
    
      # 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:).build_from_lock_data(lock_data)
  end
end
     | 
  
 
    
      
  
  
    #policyfile_lock_content  ⇒ Object 
  
  
  
  
    
      
84
85
86 
     | 
    
      # 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  ⇒ Object 
  
  
  
  
    
      
104
105
106
107
108
109 
     | 
    
      # File 'lib/chef-cli/policyfile_services/update_attributes.rb', line 104
def prepare_constraints
    Policyfile::LockApplier.new(policyfile_lock, policyfile_compiler)
    .with_unlocked_policies(:all)
    .apply!
end
     | 
  
 
    
      
  
  
    #run  ⇒ Object 
  
  
  
  
    
      
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 
     | 
    
      # 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?  ⇒ Boolean 
  
  
  
  
    
      
72
73
74 
     | 
    
      # File 'lib/chef-cli/policyfile_services/update_attributes.rb', line 72
def updated_lock?
  @updated
end 
     |