From 7aad66c0ee2d4f636a6c426a7c4a3c9eb402d0fb Mon Sep 17 00:00:00 2001 From: Daniele Palumbo Date: Wed, 9 May 2018 14:05:00 +0200 Subject: [PATCH] clean CCVs first, CVs later this allows to remove more CVs, as removing CCVs might mark more CVs as unused --- cvmanager | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/cvmanager b/cvmanager index 5e336c8..342420c 100755 --- a/cvmanager +++ b/cvmanager @@ -156,6 +156,7 @@ 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) @@ -163,18 +164,34 @@ def clean() 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) @@ -182,13 +199,20 @@ def clean() else puts " removing #{version['version']}" if not @options[:noop] - req = @api.resource(:content_view_versions).call(:destroy, {:id => version['id']}) + begin + req = @api.resource(:content_view_versions).call(:destroy, {:id => version['id']}) + rescue RestClient::ExceptionWithResponse => err + puts " removal of #{cv['label']}, id #{cv['id']} v#{version['version']} failed. Error message '#{err.response}'" + end tasks << req['id'] 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