Class: Chef::Knife::TidyBackupClean

Inherits:
Chef::Knife show all
Includes:
TidyBase
Defined in:
lib/chef/knife/tidy_backup_clean.rb

Instance Method Summary collapse

Methods included from TidyBase

#completion_message, included, #rest, #server, #tidy

Instance Method Details

#action_needed(msg) ⇒ Object



309
310
311
312
313
# File 'lib/chef/knife/tidy_backup_clean.rb', line 309

def action_needed(msg)
  ::File.open(action_needed_file_path, 'a') do |f|
    f.write(msg + "\n")
  end
end

#action_needed_file_pathObject



305
306
307
# File 'lib/chef/knife/tidy_backup_clean.rb', line 305

def action_needed_file_path
  ::File.expand_path('knife-tidy-actions-needed.txt')
end

#add_cookbook_name_to_metadata(cookbook_name, rb_path) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/chef/knife/tidy_backup_clean.rb', line 127

def (cookbook_name, rb_path)
  puts "REPAIRING: Correcting `name` in #{rb_path}"
  content = IO.readlines(rb_path)
  new_content = content.reject { |line| line =~ /^name .*/ }
  name_field = "name '#{cookbook_name}'\n"
  IO.write rb_path, name_field + new_content.join('')
end

#broken_cookooks_add(org, cookbook) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/chef/knife/tidy_backup_clean.rb', line 174

def broken_cookooks_add(org, cookbook)
  broken_path = ::File.join(tidy.org_path(org), 'cookbooks.broken')
  FileUtils.mkdir(broken_path) unless ::File.directory?(broken_path)
  Dir[::File.join(tidy.cookbooks_path(org), "#{cookbook}*")].each do |cb|
    FileUtils.mv(cb, broken_path, :verbose => true, :force => true)
  end
end

#create_minimal_metadata(cookbook_path) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/chef/knife/tidy_backup_clean.rb', line 253

def (cookbook_path)
  name = tidy.cookbook_name_from_path(cookbook_path)
  components = cookbook_path.split(File::SEPARATOR)
  name_version = components[components.index('cookbooks')+1]
  version = name_version.match(/\d+\.\d+\.\d+/).to_s
   = {}
  ['name'] = name
  ['version'] = version
  ['description'] = 'the description'
  ['long_description'] = 'the long description'
  ['maintainer'] = 'the maintainer'
  ['maintainer_email'] = 'the maintainer email'
  rb_file = ::File.join(cookbook_path, 'metadata.rb')
  puts "REPAIRING: no metadata files exist for #{cookbook_path}, creating #{rb_file}"
  ::File.open(rb_file, 'w') do |f|
    .each_pair do |key, value|
      f.write("#{key} '#{value}'\n")
    end
  end
end

#fix_cookbook_names(org) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/chef/knife/tidy_backup_clean.rb', line 135

def fix_cookbook_names(org)
  for_each_cookbook_path(org) do |cookbook_path|
    rb_path = ::File.join(cookbook_path, 'metadata.rb')
    json_path = ::File.join(cookbook_path, 'metadata.json')
    # next unless ::File.exist?(rb_path)
    cookbook_name = tidy.cookbook_name_from_path(cookbook_path)
    if ::File.exist?(rb_path)
      lines = ::File.readlines(rb_path).select { |line| line =~ /^name.*['"]#{cookbook_name}['"]/ }
      (cookbook_name, rb_path) if lines.empty?
    else
      if ::File.exist?(json_path)
         = FFI_Yajl::Parser.parse(::File.read(json_path), symbolize_names: false)
        if ['name'] != cookbook_name
          ['name'] = cookbook_name
          puts "REPAIRING: Correcting `name` in #{json_path}`"
          ::File.open(json_path, 'w') do |f|
            f.write(Chef::JSONCompat.to_json_pretty())
          end
        end
      end
    end
  end
end

#fix_metadata_fields(cookbook_path) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/chef/knife/tidy_backup_clean.rb', line 204

def (cookbook_path)
  json_path = ::File.join(cookbook_path, 'metadata.json')
   = FFI_Yajl::Parser.parse(::File.read(json_path), symbolize_names: false)
  md = .dup
  .each_pair do |key, value|
    if value.nil?
      puts "REPAIRING: Fixing null value for key #{key} in #{json_path}"
      md[key] = 'default value'
    end
  end
  if .has_key?('platforms')
    ['platforms'].each_pair do |key, value|
      if value.kind_of?(Array) && value.empty?
        puts "REPAIRING: Fixing empty platform key for for key #{key} in #{json_path}"
        md['platforms'][key] = '>= 0.0.0'
      end
    end
  end
  ::File.open(json_path, 'w') do |f|
    f.write(Chef::JSONCompat.to_json_pretty(md))
  end
end

#fix_org_object(org) ⇒ Object

In Chef Server 12 an org object should have exactly 3 keys: name, full_name and guid The existence of anything else will cause a restore to fail EC11 backups will contain org objects with 6 extra fields including org_type, billing_plan, assigned_at, etc



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/chef/knife/tidy_backup_clean.rb', line 100

def fix_org_object(org)
  puts "INFO: Validating org object for #{org}"
  org_object = load_org_object(org)

  unless org_object.keys.count == 3  # cheapo, maybe expect the exact names?
    puts "REPAIRING: org object for #{org} contains extra/missing fields. Fixing that for you"
    # quick/dirty attempt at fixing any of the required fields in case they're nil
    good_name = org_object['name'] || org
    good_full_name = org_object['full_name'] || org
    good_guid = org_object['guid'] || SecureRandom.uuid.gsub('-','')
    fixed_org_object = { name: good_name, full_name: good_full_name, guid: good_guid }

    write_org_object(org, fixed_org_object)
  end
end

#fix_self_dependencies(org) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/chef/knife/tidy_backup_clean.rb', line 189

def fix_self_dependencies(org)
  for_each_cookbook_path(org) do |cookbook_path|
    name = tidy.cookbook_name_from_path(cookbook_path)
    md_path = ::File.join(cookbook_path, 'metadata.rb')
    unless ::File.exist?(md_path)
      puts "INFO: No metadata.rb in #{cookbook_path} - skipping"
      next
    end
    Chef::TidySubstitutions.new.sub_in_file(
      ::File.join(cookbook_path, 'metadata.rb'),
      Regexp.new("^depends +['\"]#{name}['\"]"),
      "# depends '#{name}' # knife-tidy was here")
  end
end

#for_each_cookbook_basename(org) ⇒ Object



288
289
290
291
292
293
294
295
296
297
# File 'lib/chef/knife/tidy_backup_clean.rb', line 288

def for_each_cookbook_basename(org)
  cookbooks_seen = []
  Dir[::File.join(tidy.cookbooks_path(org), '**-**')].each do |cookbook|
    name = tidy.cookbook_name_from_path(cookbook)
    unless cookbooks_seen.include?(name)
      cookbooks_seen.push(name)
      yield name
    end
  end
end

#for_each_cookbook_path(org) ⇒ Object



299
300
301
302
303
# File 'lib/chef/knife/tidy_backup_clean.rb', line 299

def for_each_cookbook_path(org)
  Dir[::File.join(tidy.cookbooks_path(org), '**')].each do |cookbook|
    yield cookbook
  end
end

#for_each_role(org) ⇒ Object



321
322
323
324
325
# File 'lib/chef/knife/tidy_backup_clean.rb', line 321

def for_each_role(org)
  Dir[::File.join(tidy.roles_path(org), '*.json')].each do |role|
    yield role
  end
end

#generate_metadata_from_file(cookbook, path) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/chef/knife/tidy_backup_clean.rb', line 227

def (cookbook, path)
  md_path = ::File.join(path, 'metadata.rb')
  json_path = ::File.join(path, 'metadata.json')
  if !::File.exist?(md_path) && !::File.exist?(json_path)
    (path)
  end
  unless ::File.exist?(md_path)
    puts "INFO: No metadata.rb in #{path} - skipping"
    return
  end
  puts "INFO: Generating new metadata.json for #{path}"
  md = Chef::Cookbook::.new
  md.name(cookbook)
  md.from_file(md_path)
  json_file = ::File.join(path, 'metadata.json')
  ::File.open(json_file, 'w') do |f|
    f.write(Chef::JSONCompat.to_json_pretty(md))
  end
rescue Exceptions::ObsoleteDependencySyntax, Exceptions::InvalidVersionConstraint => e
  ui.stderr.puts "ERROR: The cookbook '#{cookbook}' contains invalid or obsolete metadata syntax."
  ui.stderr.puts "in #{file}:"
  ui.stderr.puts
  ui.stderr.puts e.message
  exit 1
end

#generate_new_metadata(org) ⇒ Object



182
183
184
185
186
187
# File 'lib/chef/knife/tidy_backup_clean.rb', line 182

def (org)
  for_each_cookbook_path(org) do |cookbook_path|
    (tidy.cookbook_name_from_path(cookbook_path), cookbook_path)
    (cookbook_path)
  end
end

#load_cookbooks(org) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/chef/knife/tidy_backup_clean.rb', line 159

def load_cookbooks(org)
  cl = Chef::CookbookLoader.new(tidy.cookbooks_path(org))
  for_each_cookbook_basename(org) do |cookbook|
    puts "INFO: Loading #{cookbook}"
    ret = cl.load_cookbook(cookbook)
    if ret.nil?
      action_needed("ACTION NEEDED: Something's wrong with the #{cookbook} cookbook in org #{org} - cannot load it! Moving to cookbooks.broken folder.")
      broken_cookooks_add(org, cookbook)
    end
  end
rescue LoadError => e
  ui.error e
  exit 1
end

#load_org_object(org) ⇒ Object



116
117
118
119
120
121
# File 'lib/chef/knife/tidy_backup_clean.rb', line 116

def load_org_object(org)
  JSON.parse(File.read(File.join(tidy.org_path(org), 'org.json')))
rescue Errno::ENOENT, JSON::ParserError
  puts "REPAIRING: org object for organization #{org} is missing or corrupt. Generating a new one"
  return { name: org, full_name: org, guid: SecureRandom.uuid.gsub('-','') }
end

#orgsObject



280
281
282
283
284
285
286
# File 'lib/chef/knife/tidy_backup_clean.rb', line 280

def orgs
  @orgs ||= if config[:org_list]
              config[:org_list].split(',')
            else
              Dir[::File.join(tidy.backup_path, 'organizations', '*')].map { |dir| ::File.basename(dir) }
            end
end

#repair_role_run_lists(role_path) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/chef/knife/tidy_backup_clean.rb', line 327

def repair_role_run_lists(role_path)
  # rubocop:disable MethodLength
  the_role = FFI_Yajl::Parser.parse(::File.read(role_path), symbolize_names: false)
  new_role = the_role.clone
  rl = Chef::RunList.new
  new_role['run_list'] = []
  the_role['run_list'].each do |item|
    begin
      rl << item
      new_role['run_list'].push(item)
    rescue ArgumentError
      puts "REPAIRING: Invalid Recipe Item: #{item} in run_list from #{role_path}"
    end
  end
  if the_role.has_key?('env_run_lists')
    the_role['env_run_lists'].each_pair do |key, value|
      new_role['env_run_lists'][key] = []
      value.each do |item|
        begin
          rl << item
          new_role['env_run_lists'][key].push(item)
        rescue ArgumentError
          puts "REPAIRING: Invalid Recipe Item: #{item} in env_run_lists #{key} from #{role_path}"
        end
      end
    end
  end
  write_role(role_path, new_role)
  # rubocop:enable MethodLength
end

#runObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chef/knife/tidy_backup_clean.rb', line 35

def run
  FileUtils.rm_f(action_needed_file_path)

  if config[:gen_gsub]
    Chef::TidySubstitutions.new().boiler_plate
    exit
  end

  unless config[:backup_path] && ::File.directory?(config[:backup_path])
    ui.error 'Must specify valid --backup-path'
    exit 1
  end

  Chef::TidySubstitutions.new(substitutions_file, tidy).run_substitutions if config[:gsub_file]

  validate_user_emails

  orgs.each do |org|
    fix_org_object(org)
    validate_roles(org)
    org_acls = Chef::TidyOrgAcls.new(tidy, org)
    org_acls.validate_acls
    org_acls.validate_user_acls
    fix_self_dependencies(org)
    fix_cookbook_names(org)
    (org)
    load_cookbooks(org)
  end

  completion_message

  puts "\nWARNING: ** Unrepairable Items **\nPlease see #{action_needed_file_path}\n" if ::File.exist?(action_needed_file_path)
end

#substitutions_fileObject



274
275
276
277
278
# File 'lib/chef/knife/tidy_backup_clean.rb', line 274

def substitutions_file
  sub_file_path = ::File.expand_path(config[:gsub_file])
  ui.error "Subtitutions file #{sub_file_path} does not exist!" unless ::File.exist?(sub_file_path)
  @substitutions_file ||= sub_file_path
end

#validate_roles(org) ⇒ Object



358
359
360
361
362
363
364
365
366
367
# File 'lib/chef/knife/tidy_backup_clean.rb', line 358

def validate_roles(org)
  for_each_role(org) do |role_path|
    puts "INFO: Validating Role at #{role_path}"
    begin
      Chef::Role.from_hash(FFI_Yajl::Parser.parse(::File.read(role_path), symbolize_names: false))
    rescue ArgumentError
      repair_role_run_lists(role_path)
    end
  end
end

#validate_user_emailsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chef/knife/tidy_backup_clean.rb', line 69

def validate_user_emails
  emails_seen = []
  tidy.global_user_names.each do |user|
    email = ''
    puts "INFO: Validating #{user}"
    the_user = FFI_Yajl::Parser.parse(::File.read(::File.join(tidy.users_path, "#{user}.json")), symbolize_names: false)
    if the_user.has_key?('email') && the_user['email'].match(/\A[^@\s]+@[^@\s]+\z/)
      if emails_seen.include?(the_user['email'])
        puts "REPAIRING: Already saw #{user}'s email, creating a unique one."
        email = tidy.unique_email
        new_user = the_user.dup
        new_user['email'] = email
        tidy.save_user(new_user)
        emails_seen.push(email)
      else
        emails_seen.push(the_user['email'])
      end
    else
      puts "REPAIRING: User #{user} does not have a valid email, creating a unique one."
      email = tidy.unique_email
      new_user = the_user.dup
      new_user['email'] = email
      tidy.save_user(new_user)
      emails_seen.push(email)
    end
  end
end

#write_org_object(org, org_object) ⇒ Object



123
124
125
# File 'lib/chef/knife/tidy_backup_clean.rb', line 123

def write_org_object(org, org_object)
  File.write(File.join(tidy.org_path(org), 'org.json') , JSON.pretty_generate(org_object))
end

#write_role(path, role) ⇒ Object



315
316
317
318
319
# File 'lib/chef/knife/tidy_backup_clean.rb', line 315

def write_role(path, role)
  ::File.open(path, 'w') do |f|
    f.write(Chef::JSONCompat.to_json_pretty(role))
  end
end