Class: ChefDK::PolicyfileServices::CleanPolicyCookbooks

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-dk/policyfile_services/clean_policy_cookbooks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: nil, ui: nil) ⇒ CleanPolicyCookbooks

Returns a new instance of CleanPolicyCookbooks.



32
33
34
35
36
37
38
39
# File 'lib/chef-dk/policyfile_services/clean_policy_cookbooks.rb', line 32

def initialize(config: nil, ui: nil)
  @chef_config = config
  @ui = ui

  @all_cookbooks = nil
  @active_cookbooks = nil
  @all_policies = nil
end

Instance Attribute Details

#chef_configObject (readonly)

Returns the value of attribute chef_config.



28
29
30
# File 'lib/chef-dk/policyfile_services/clean_policy_cookbooks.rb', line 28

def chef_config
  @chef_config
end

#uiObject (readonly)

Returns the value of attribute ui.



30
31
32
# File 'lib/chef-dk/policyfile_services/clean_policy_cookbooks.rb', line 30

def ui
  @ui
end

Instance Method Details

#active_cookbooksObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chef-dk/policyfile_services/clean_policy_cookbooks.rb', line 70

def active_cookbooks
  policy_revisions_by_name.inject({}) do |cb_map, (policy_name, revision_ids)|
    revision_ids.each do |revision_id|
      cookbook_revisions_in_policy(policy_name, revision_id).each do |cb_name, identifier|
        cb_map[cb_name] ||= Set.new
        cb_map[cb_name] << identifier
      end
    end
    cb_map
  end
end

#all_cookbooksObject



62
63
64
65
66
67
68
# File 'lib/chef-dk/policyfile_services/clean_policy_cookbooks.rb', line 62

def all_cookbooks
  cookbook_list = http_client.get("/cookbook_artifacts")
  cookbook_list.inject({}) do |cb_map, (name, cb_info)|
    cb_map[name] = cb_info["versions"].map { |v| v["identifier"] }
    cb_map
  end
end

#cookbook_revisions_in_policy(name, revision_id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



104
105
106
107
108
109
110
111
# File 'lib/chef-dk/policyfile_services/clean_policy_cookbooks.rb', line 104

def cookbook_revisions_in_policy(name, revision_id)
  policy_revision_data = http_client.get("/policies/#{name}/revisions/#{revision_id}")

  policy_revision_data["cookbook_locks"].inject({}) do |cb_map, (cb_name, lock_info)|
    cb_map[cb_name] = lock_info["identifier"]
    cb_map
  end
end

#cookbooks_to_cleanObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chef-dk/policyfile_services/clean_policy_cookbooks.rb', line 82

def cookbooks_to_clean
  active_cbs = active_cookbooks

  all_cookbooks.inject({}) do |cb_map, (cb_name, revisions)|
    active_revs = active_cbs[cb_name] || Set.new
    inactive_revs = Set.new(revisions) - active_revs
    cb_map[cb_name] = inactive_revs unless inactive_revs.empty?

    cb_map
  end
end

#gc_cookbooksObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chef-dk/policyfile_services/clean_policy_cookbooks.rb', line 47

def gc_cookbooks
  cookbooks = cookbooks_to_clean

  if cookbooks.empty?
    ui.msg("No cookbooks deleted.")
  end

  cookbooks.each do |name, identifiers|
    identifiers.each do |identifier|
      http_client.delete("/cookbook_artifacts/#{name}/#{identifier}")
      ui.msg("DELETE #{name} #{identifier}")
    end
  end
end

#http_clientObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

An instance of Chef::ServerAPI configured with the user’s server URL and credentials.



116
117
118
119
120
# File 'lib/chef-dk/policyfile_services/clean_policy_cookbooks.rb', line 116

def http_client
  @http_client ||= Chef::ServerAPI.new(chef_config.chef_server_url,
    signing_key_filename: chef_config.client_key,
    client_name: chef_config.node_name)
end

#policy_revisions_by_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
98
99
100
101
# File 'lib/chef-dk/policyfile_services/clean_policy_cookbooks.rb', line 95

def policy_revisions_by_name
  policies_list = http_client.get("/policies")
  policies_list.inject({}) do |policies_map, (name, policy_info)|
    policies_map[name] = policy_info["revisions"].keys
    policies_map
  end
end

#runObject



41
42
43
44
45
# File 'lib/chef-dk/policyfile_services/clean_policy_cookbooks.rb', line 41

def run
  gc_cookbooks
rescue => e
  raise PolicyCookbookCleanError.new("Failed to cleanup policy cookbooks", e)
end