Class: PagerDuty::ChefServer::Sync
- Inherits:
-
Object
- Object
- PagerDuty::ChefServer::Sync
show all
- Includes:
- SyncHelper
- Defined in:
- lib/pagerduty/chef_server/sync.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from SyncHelper
#chef_repo_dir, #converge_by, #create_databag, #data_bag_from_file, #data_bag_from_hash, #databag_dir, #delete_cookbook, #environment_dir, #ignore_file, #ignored?, #knife, #role_dir, #upload_all_cookbooks, #upload_cookbook, #upload_cookbooks, #upload_databags, #upload_environments, #upload_roles
Constructor Details
#initialize(opts = {}) ⇒ Sync
Returns a new instance of Sync.
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/pagerduty/chef_server/sync.rb', line 30
def initialize(opts={})
require 'tempfile'
require 'json'
require 'mixlib/shellout'
require 'chef/cookbook_version'
require 'chef/data_bag_item'
require 'chef/data_bag'
require 'berkshelf'
require 'berkshelf/berksfile'
@cookbook_dir = opts[:vendor_dir]
@why_run = opts[:why_run]
@ui = opts[:ui] || Chef::Knife.ui
if File.exist?(ignore_file)
@ignore_patterns = File.read(ignore_file).lines.map{|l| File.join(chef_repo_dir, l).strip}
else
ui.info('.pd-ignore absent, nothing will be ignored')
@ignore_patterns = false
end
end
|
Instance Attribute Details
#cookbook_dir ⇒ Object
Returns the value of attribute cookbook_dir.
28
29
30
|
# File 'lib/pagerduty/chef_server/sync.rb', line 28
def cookbook_dir
@cookbook_dir
end
|
#ignore_patterns ⇒ Object
Returns the value of attribute ignore_patterns.
28
29
30
|
# File 'lib/pagerduty/chef_server/sync.rb', line 28
def ignore_patterns
@ignore_patterns
end
|
#ui ⇒ Object
Returns the value of attribute ui.
28
29
30
|
# File 'lib/pagerduty/chef_server/sync.rb', line 28
def ui
@ui
end
|
#why_run ⇒ Object
Returns the value of attribute why_run.
28
29
30
|
# File 'lib/pagerduty/chef_server/sync.rb', line 28
def why_run
@why_run
end
|
Instance Method Details
#berkshelf_install ⇒ Object
79
80
81
82
83
84
85
|
# File 'lib/pagerduty/chef_server/sync.rb', line 79
def berkshelf_install
path = File.expand_path(File.join(chef_repo_dir, 'Berksfile'))
ui.info(ui.color("using Berksfile: #{path} for berkshelf install", :yellow))
berksfile = Berkshelf::Berksfile.from_file(path, { except: 'tests' } )
FileUtils.rm_rf(cookbook_dir)
berksfile.vendor(cookbook_dir)
end
|
#cookbook_segments ⇒ Object
105
106
107
|
# File 'lib/pagerduty/chef_server/sync.rb', line 105
def cookbook_segments
Chef::CookbookVersion::COOKBOOK_SEGMENTS
end
|
#diff(mf1, mf2) ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/pagerduty/chef_server/sync.rb', line 162
def diff(mf1, mf2)
diffs = Hash.new{|h, k| h[k]= []}
mf2 = {} if mf2.nil?
mf1 = {} if mf1.nil?
segments = (mf1.keys + mf2.keys).sort.uniq
different_parts = segments.select{|segment| mf1[segment]!= mf2[segment]}
different_parts.each do |segment|
files = (Array(mf1[segment]) + Array(mf2[segment])).map{|f| f['name']}.uniq
files.each do |file|
f1 = Array(mf1[segment]).detect{|f|f['name'] == file} || {}
f2 = Array(mf2[segment]).detect{|f|f['name'] == file} || {}
unless f1['checksum'] == f2['checksum']
diffs[segment] << file end
end
end
diffs
end
|
#different_cookbook?(cb) ⇒ Boolean
181
182
183
|
# File 'lib/pagerduty/chef_server/sync.rb', line 181
def different_cookbook?(cb)
!diff(local_checksums[cb], remote_checksums[cb]).empty?
end
|
#local_checksums ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/pagerduty/chef_server/sync.rb', line 130
def local_checksums
@local_checksums ||= begin
c = {}
cbl = Chef::CookbookLoader.new(Array(cookbook_dir))
cbl.load_cookbooks
cbl_sort = cbl.values.map(&:name).map(&:to_s).sort
cbl_sort.each do |cb|
print "#{cb} => "
c[cb] = {}
cookbook_segments.each do |m|
cbm_sort = cbl[cb].manifest[m].sort { |x, y| x['name'] <=> y['name'] }
cbm_sort = cbm_sort.sort { |x, y| x['checksum'] <=> y['checksum'] }
cbm_sort.each do |file|
file.delete(:path)
file.delete(:specificity)
end
c[cb][m] = cbm_sort
end
diff = diff(c[cb], remote_checksums[cb])
if diff.empty?
ui.info(ui.color( 'match', :green))
else
ui.info(ui.color( 'mismatch', :yellow))
ui.output(diff(c[cb], remote_checksums[cb]))
end
sleep 0.1 end
c
end
end
|
#local_cookbooks ⇒ Object
87
88
89
|
# File 'lib/pagerduty/chef_server/sync.rb', line 87
def local_cookbooks
local_checksums.keys.sort
end
|
#new_cookbooks ⇒ Object
185
186
187
|
# File 'lib/pagerduty/chef_server/sync.rb', line 185
def new_cookbooks
local_cookbooks - remote_cookbooks
end
|
#remote_checksums ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/pagerduty/chef_server/sync.rb', line 109
def remote_checksums
@remote_checksums ||= begin
c = {}
remote_cookbooks.each do |cb|
c[cb] = {}
cbm = Chef::CookbookVersion.load(cb).manifest
cookbook_segments.each do |m|
cbm_sort = cbm[m].sort { |x, y| x['name'] <=> y['name'] }
cbm_sort = cbm_sort.sort { |x, y| x['checksum'] <=> y['checksum'] }
cbm_sort.each do |file|
file.delete(:url)
file.delete(:path)
file.delete(:specificity)
end
c[cb][m] = cbm_sort
end
end
c
end
end
|
#remote_commit ⇒ Object
95
96
97
98
99
100
101
102
103
|
# File 'lib/pagerduty/chef_server/sync.rb', line 95
def remote_commit
@remote_commit ||= begin
if Chef::DataBag.list.keys.include?('metadata')
Chef::DataBagItem.load('metadata', 'commit').raw_data['commit']
else
{}
end
end
end
|
#remote_cookbooks ⇒ Object
91
92
93
|
# File 'lib/pagerduty/chef_server/sync.rb', line 91
def remote_cookbooks
@remote_cookbooks ||= Chef::CookbookVersion.list.keys.sort
end
|
#replace_cookbook(cb) ⇒ Object
199
200
201
202
203
204
|
# File 'lib/pagerduty/chef_server/sync.rb', line 199
def replace_cookbook(cb)
converge_by "Replace cookbook #{cb}" do
delete_cookbook(cb)
upload_cookbook(cb)
end
end
|
#run ⇒ Object
50
51
52
53
54
55
56
|
# File 'lib/pagerduty/chef_server/sync.rb', line 50
def run
berkshelf_install
sync_cookbooks
upload_databags
upload_environments
upload_roles
end
|
#stale_cookbooks ⇒ Object
189
190
191
|
# File 'lib/pagerduty/chef_server/sync.rb', line 189
def stale_cookbooks
remote_cookbooks - local_cookbooks
end
|
#sync_cookbooks ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/pagerduty/chef_server/sync.rb', line 58
def sync_cookbooks
altered_cookbooks = Hash.new{|h, k| h[k] = []}
if remote_cookbooks.empty?
upload_all_cookbooks
else
unless new_cookbooks.empty?
upload_cookbooks(new_cookbooks)
altered_cookbooks[:added] = new_cookbooks
end
stale_cookbooks.each do |cb|
delete_cookbook(cb)
altered_cookbooks[:deleted] << cb
end
updated_cookbooks.each do |cb|
replace_cookbook(cb)
altered_cookbooks[:updated] << cb
end
end
end
|
#updated_cookbooks ⇒ Object
193
194
195
196
197
|
# File 'lib/pagerduty/chef_server/sync.rb', line 193
def updated_cookbooks
(local_cookbooks & remote_cookbooks).select do |cb|
different_cookbook?(cb)
end
end
|