Class: ServerCleanup::CleanupVersions

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/cleanup_versions.rb

Instance Method Summary collapse

Instance Method Details

#backup_cookbook(cb, cb_ver, dir) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/chef/knife/cleanup_versions.rb', line 110

def backup_cookbook(cb,cb_ver,dir)
  ui.msg "\nFist backing up cookbook #{cb} version #{cb_ver}"
  FileUtils.mkdir_p(dir)
  dld = Chef::Knife::CookbookDownload.new
  dld.name_args = [cb, cb_ver]
  dld.config[:download_directory] = dir
  dld.config[:force] = true
  begin
    dld.run
  rescue
    ui.msg "Failed to download cookbook #{cb} version #{cb_ver}... Skipping"
    FileUtils.rm_r(File.join(dir, cb + "-" + cb_ver))
  end
end

#cookbooksObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
96
97
98
99
100
101
102
# File 'lib/chef/knife/cleanup_versions.rb', line 47

def cookbooks
  ui.msg "Searching for unused cookboks versions..."
  all_cookbooks = rest.get_rest("/cookbooks?num_versions=all")
  latest_cookbooks = rest.get_rest("/cookbooks?latest")
  
  # All cookbooks
  cbv = all_cookbooks.inject({}) do |collected, ( cookbook, versions )|
    collected[cookbook] = versions["versions"].map {|v| v['version']}
    collected
  end
  
  # Get the latest cookbooks
  latest = latest_cookbooks.inject({}) do |collected, ( cookbook, versions )|
    collected[cookbook] = versions["versions"].map {|v| v['version']}
    collected
  end
  
  latest.each_key do |cb|
    cbv[cb].delete(latest[cb][0])
  end
  
  # Let see what cookbooks we have in use in all environments
  Chef::Environment.list.each_key do |env_list|
    env = Chef::Environment.load(env_list)
    next unless !env.cookbook_versions.empty?
    env.cookbook_versions.each_key do |cb|
      cb_ver = env.cookbook_versions[cb].split(" ").last
      begin
        cbv[cb].delete(cb_ver)
      rescue
        "Skipping..."
      end
    end
  end
  
  confirm("Do you really want to delete unused cookbook versions from the server")  if config[:delete]
  ui.msg "Cookbook Versions:"
  key_length = cbv.empty? ? 0 : cbv.keys.map {|name| name.size }.max + 2
  cbv.each_key do |cb|
    print "  #{cb.ljust(key_length)}"
    cbv[cb].each do |cb_ver|
      print "#{cb_ver} "
      if config[:delete]
        dir = ".cleanup/#{cb}/"
        backup_cookbook(cb,cb_ver,dir) if config[:backup]
        delete_cookbook(cb, cb_ver)
      end
    end
    print "\n"
  end
  
  if !config[:delete]
    ui.msg "Not deleting unused cookbook versions; use --delete if you want to remove them"
  end
  
end

#delete_cookbook(cb, cb_ver) ⇒ Object



104
105
106
107
108
# File 'lib/chef/knife/cleanup_versions.rb', line 104

def delete_cookbook(cb, cb_ver)
  ui.msg "Deleting cookbook #{cb} version #{cb_ver}"
  rest.delete_rest("cookbooks/#{cb}/#{cb_ver}")
  print ". "
end

#runObject



43
44
45
# File 'lib/chef/knife/cleanup_versions.rb', line 43

def run
  cookbooks
end