Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

Commit

Permalink
clean CCVs first, CVs later (#55)
Browse files Browse the repository at this point in the history
this allows to remove more CVs, as removing CCVs might mark more CVs as
unused
  • Loading branch information
evgeni authored May 15, 2018
1 parent 30f10ee commit bf46394
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions cvmanager
Original file line number Diff line number Diff line change
Expand Up @@ -156,39 +156,63 @@ end
def clean()
tasks = []
cvs = []
firstcv = true
req = @api.resource(:content_views).call(:index, {:organization_id => @options[:org], :full_results => true})
cvs.concat(req['results'])
while (req['results'].length == req['per_page'].to_i)
req = @api.resource(:content_views).call(:index, {:organization_id => @options[:org], :full_results => true, :per_page => req['per_page'], :page => req['page'].to_i+1})
cvs.concat(req['results'])
end

# Sort the results so that we first process all Composite Content Views
# and then the Content Views. This cleans up CCVs that might reference CVs.
cvs.sort_by! { |cv| cv["composite"] ? 0 : 1 }

# Parse the CV
cvs.each do |cv|
keep = []
puts "Inspecting #{cv['label']}"
# The first Content View we encounter indicates that we processed all Composite Content Views.
# Wait until the CCV tasks are finished.
if not cv["composite"] and firstcv and tasks.any?
puts "First Content View, waiting for the Composite Content View tasks to be completed"
firstcv = false
wait(tasks)
end
cv['versions'].sort_by { |v| v['version'].to_f }.reverse.each do |version|
if not version['environment_ids'].empty?
puts_verbose " #{cv['label']} v#{version['version']} is published to the following environments: #{version['environment_ids']}, skipping."
next
else
puts_verbose " #{cv['label']} v#{version['version']} is not used by any environment."
end
version_details = @api.resource(:content_view_versions).call(:show, {:id => version['id']})
if not version_details['composite_content_view_ids'].empty?
puts_verbose " #{cv['label']} v#{version['version']} is used by the following composite contentviews: #{version_details['composite_content_view_ids']}, skipping."
next
else
puts_verbose " #{cv['label']} v#{version['version']} is not used by any composite contentviews."
end
if keep.length < @options[:keep]
keep.push(version)
puts " keeping #{version['version']}"
else
puts " removing #{version['version']}"
if not @options[:noop]
req = @api.resource(:content_view_versions).call(:destroy, {:id => version['id']})
tasks << req['id']
begin
req = @api.resource(:content_view_versions).call(:destroy, {:id => version['id']})
tasks << req['id']
rescue RestClient::ExceptionWithResponse => err
puts " removal of #{cv['label']}, id #{cv['id']} v#{version['version']} failed. Error message '#{err.response}'"
end
if @options[:sequential] > 0 and tasks.length >= @options[:sequential]
tasks = wait(tasks)
puts " removed content view version with id #{version['id']}"
else
puts " [task enqueued] removed content view version with id #{version['id']}"
end
else
puts " [noop] would delete content view version with id #{version['id']}"
puts " [noop] removed content view version with id #{version['id']}"
end
end
end
Expand Down

0 comments on commit bf46394

Please sign in to comment.