Class: Chef::Knife::TidyServerClean

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

Instance Method Summary collapse

Methods included from TidyBase

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

Instance Method Details

#all_orgsObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/chef/knife/tidy_server_clean.rb', line 126

def all_orgs
  orgs = []
  report_files.each do |file|
    org = ::File.basename(file).match(/^(.*?)_/).captures[0]
    if org
      orgs.push(org) unless orgs.include?(org)
    end
  end
  orgs
end

#clean_cookbooks(org) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/chef/knife/tidy_server_clean.rb', line 62

def clean_cookbooks(org)
  queue = Chef::Util::ThreadedJobQueue.new
  unused_cookbooks_file = ::File.join(tidy.reports_dir, "#{org}_unused_cookbooks.json")
  return unless ::File.exist?(unused_cookbooks_file)
  ui.info "Cleaning cookbooks for Org: #{org}, using #{unused_cookbooks_file}"
  unused_cookbooks = FFI_Yajl::Parser.parse(::File.read(unused_cookbooks_file), symbolize_names: true)
  unused_cookbooks.keys.each do |cookbook|
    versions = unused_cookbooks[cookbook]
    versions.each do |version|
      queue << lambda { delete_cookbook_job(org, cookbook, version) }
    end
  end
  queue.process(config[:concurrency].to_i)
end

#clean_nodes(org) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/chef/knife/tidy_server_clean.rb', line 90

def clean_nodes(org)
  queue = Chef::Util::ThreadedJobQueue.new
  stale_nodes_file = ::File.join(tidy.reports_dir, "#{org}_stale_nodes.json")
  return unless ::File.exist?(stale_nodes_file)
  ui.info "Cleaning stale nodes for Org: #{org}, using #{stale_nodes_file}"
  stale_nodes = FFI_Yajl::Parser.parse(::File.read(stale_nodes_file), symbolize_names: true)
  stale_nodes[:list].each do |node|
    queue << lambda { delete_node_job(org, node) }
  end
  queue.process(config[:concurrency].to_i)
end

#delete_cookbook_job(org, cookbook, version) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chef/knife/tidy_server_clean.rb', line 77

def delete_cookbook_job(org, cookbook, version)
  path = "/organizations/#{org}/cookbooks/#{cookbook}/#{version}"
  rest.delete(path)
  response = '200'
rescue Net::HTTPServerException
  response = $!.response.code
ensure
  formatted = response == '200' ?
    ui.color(' Deleting  %-20s %-10s %10s', :green) :
    ui.color(' Deleting  %-20s %-10s %10s', :red)
  printf("#{formatted}\n", cookbook, version, response)
end

#delete_node_job(org, node) ⇒ Object



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

def delete_node_job(org, node)
  path = "/organizations/#{org}/nodes/#{node}"
  rest.delete(path)
  response = '200'
rescue Net::HTTPServerException
  response = $!.response.code
ensure
  formatted = response == '200' ?
    ui.color(' Deleting  %-20s %10s', :green) :
    ui.color(' Deleting  %-20s %10s', :red)
  printf("#{formatted}\n", node, response)
end

#ensure_reports_dirObject



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

def ensure_reports_dir
  unless ::File.directory?(tidy.reports_dir)
    ui.error "#{tidy.reports_dir} not found!"
    exit 1
  end
end

#report_filesObject



122
123
124
# File 'lib/chef/knife/tidy_server_clean.rb', line 122

def report_files
  Dir[::File.join(tidy.reports_dir, '**')]
end

#runObject



32
33
34
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
# File 'lib/chef/knife/tidy_server_clean.rb', line 32

def run
  STDOUT.sync = true

  ensure_reports_dir
  ui.info "Reading from #{tidy.reports_dir} directory"

  ui.info "Using thread concurrency #{config[:concurrency]}"
  configure_chef

  if config[:only_cookbooks] && config[:only_nodes]
    ui.error 'Cannot use --only-cookbooks AND --only-nodes'
    exit 1
  end

  ui.confirm('This operation will delete items on Chef Server, continue') unless config[:unattended]

  orgs = if config[:org_list]
           config[:org_list].split(',')
         else
           all_orgs
         end

  orgs.each do |org|
    clean_cookbooks(org) unless config[:only_nodes]
    clean_nodes(org) unless config[:only_cookbooks]
  end

  completion_message
end