Class: ChefDK::Policyfile::Differ

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-dk/policyfile/differ.rb

Constant Summary collapse

POLICY_SECTIONS =
%w{ revision_id run_list named_run_lists cookbook_locks default_attributes override_attributes }.freeze
LINES_OF_CONTEXT =
3
INITIAL_FILE_LENGTH_DIFFERENCE =
0
FORMAT =
:unified

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(old_name: nil, old_lock: nil, new_name: nil, new_lock: nil, ui: nil) ⇒ Differ

Returns a new instance of Differ.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chef-dk/policyfile/differ.rb', line 39

def initialize(old_name: nil, old_lock: nil, new_name: nil, new_lock: nil, ui: nil)
  @old_lock = old_lock
  @new_lock = new_lock
  @old_name = old_name
  @new_name = new_name
  @ui = ui

  @added_cookbooks    = nil
  @removed_cookbooks  = nil
  @modified_cookbooks = nil
end

Instance Attribute Details

#new_lockObject (readonly)

Returns the value of attribute new_lock.



35
36
37
# File 'lib/chef-dk/policyfile/differ.rb', line 35

def new_lock
  @new_lock
end

#new_nameObject (readonly)

Returns the value of attribute new_name.



36
37
38
# File 'lib/chef-dk/policyfile/differ.rb', line 36

def new_name
  @new_name
end

#old_lockObject (readonly)

Returns the value of attribute old_lock.



33
34
35
# File 'lib/chef-dk/policyfile/differ.rb', line 33

def old_lock
  @old_lock
end

#old_nameObject (readonly)

Returns the value of attribute old_name.



34
35
36
# File 'lib/chef-dk/policyfile/differ.rb', line 34

def old_name
  @old_name
end

#uiObject (readonly)

Returns the value of attribute ui.



37
38
39
# File 'lib/chef-dk/policyfile/differ.rb', line 37

def ui
  @ui
end

Instance Method Details

#added_cookbooksObject



163
164
165
166
# File 'lib/chef-dk/policyfile/differ.rb', line 163

def added_cookbooks
  detect_cookbook_changes if @added_cookbooks.nil?
  @added_cookbooks
end

#different?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/chef-dk/policyfile/differ.rb', line 69

def different?
  !updated_sections.empty?
end

#lock_nameObject



51
52
53
# File 'lib/chef-dk/policyfile/differ.rb', line 51

def lock_name
  old_lock["name"]
end

#modified_cookbooksObject



173
174
175
176
# File 'lib/chef-dk/policyfile/differ.rb', line 173

def modified_cookbooks
  detect_cookbook_changes if @modified_cookbooks.nil?
  @modified_cookbooks
end

#new_cookbook_locksObject



59
60
61
# File 'lib/chef-dk/policyfile/differ.rb', line 59

def new_cookbook_locks
  new_lock["cookbook_locks"]
end

#old_cookbook_locksObject



55
56
57
# File 'lib/chef-dk/policyfile/differ.rb', line 55

def old_cookbook_locks
  old_lock["cookbook_locks"]
end

#removed_cookbooksObject



168
169
170
171
# File 'lib/chef-dk/policyfile/differ.rb', line 168

def removed_cookbooks
  detect_cookbook_changes if @removed_cookbooks.nil?
  @removed_cookbooks
end

#report_added_cookbooksObject



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/chef-dk/policyfile/differ.rb', line 119

def report_added_cookbooks
  return nil if added_cookbooks.empty?
  h1("ADDED COOKBOOKS")
  added_cookbooks.each do |cb_name|
    ui.print("\n")
    old_lock = []
    new_lock = pretty_json(new_cookbook_locks[cb_name])
    h2(cb_name)
    diff_lines(old_lock, new_lock)
  end
end

#report_default_attribute_changesObject



143
144
145
146
147
148
149
150
151
# File 'lib/chef-dk/policyfile/differ.rb', line 143

def report_default_attribute_changes
  return nil unless updated_sections.include?("default_attributes")

  h1("DEFAULT ATTRIBUTES CHANGED")

  old_default = pretty_json(old_lock["default_attributes"])
  new_default = pretty_json(new_lock["default_attributes"])
  diff_lines(old_default, new_default)
end

#report_modified_cookbooksObject



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chef-dk/policyfile/differ.rb', line 131

def report_modified_cookbooks
  return nil if modified_cookbooks.empty?
  h1("MODIFIED COOKBOOKS")
  modified_cookbooks.each do |cb_name|
    ui.print("\n")
    old_lock = pretty_json(old_cookbook_locks[cb_name])
    new_lock = pretty_json(new_cookbook_locks[cb_name])
    h2(cb_name)
    diff_lines(old_lock, new_lock)
  end
end

#report_override_attribute_changesObject



153
154
155
156
157
158
159
160
161
# File 'lib/chef-dk/policyfile/differ.rb', line 153

def report_override_attribute_changes
  return nil unless updated_sections.include?("override_attributes")

  h1("OVERRIDE ATTRIBUTES CHANGED")

  old_override = pretty_json(old_lock["override_attributes"])
  new_override = pretty_json(new_lock["override_attributes"])
  diff_lines(old_override, new_override)
end

#report_removed_cookbooksObject



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/chef-dk/policyfile/differ.rb', line 107

def report_removed_cookbooks
  return nil if removed_cookbooks.empty?
  h1("REMOVED COOKBOOKS")
  removed_cookbooks.each do |cb_name|
    ui.print("\n")
    old_lock = pretty_json(old_cookbook_locks[cb_name])
    new_lock = []
    h2(cb_name)
    diff_lines(old_lock, new_lock)
  end
end

#report_rev_id_changesObject



90
91
92
93
94
95
# File 'lib/chef-dk/policyfile/differ.rb', line 90

def report_rev_id_changes
  h1('REVISION ID CHANGED')
  old_rev = old_lock["revision_id"]
  new_rev = new_lock["revision_id"]
  diff_lines([ old_rev ], [ new_rev ])
end

#report_run_list_changesObject



97
98
99
100
101
102
103
104
105
# File 'lib/chef-dk/policyfile/differ.rb', line 97

def report_run_list_changes
  return nil unless updated_sections.include?("run_list")
  h1("RUN LIST CHANGED")

  old_run_list = old_lock["run_list"]
  new_run_list = new_lock["run_list"]

  diff_lines(old_run_list, new_run_list)
end

#run_reportObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chef-dk/policyfile/differ.rb', line 73

def run_report
  unless different?
    ui.err("No changes for policy lock '#{lock_name}' between '#{old_name}' and '#{new_name}'")
    return true
  end

  ui.print("Policy lock '#{lock_name}' differs between '#{old_name}' and '#{new_name}':\n\n")

  report_rev_id_changes
  report_run_list_changes
  report_added_cookbooks
  report_removed_cookbooks
  report_modified_cookbooks
  report_default_attribute_changes
  report_override_attribute_changes
end

#updated_sectionsObject



63
64
65
66
67
# File 'lib/chef-dk/policyfile/differ.rb', line 63

def updated_sections
  @updated_sections ||= POLICY_SECTIONS.select do |key|
    old_lock[key] != new_lock[key]
  end
end