1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-30 22:50:10 +03:00

F #3629: Add oneimage orphans (#3785)

This commit is contained in:
Christian González 2019-10-01 14:20:31 +02:00 committed by Ruben S. Montero
parent 9ed1d12dbb
commit 6855d38460
3 changed files with 56 additions and 0 deletions

View File

@ -798,6 +798,29 @@ EOT
return 0
end
# Check if a resource defined by attributes is referenced in pool
#
# @param pool pool to search in
# @param xpath xpath to search in pool
# @param resource_name name of the resource to search (e.g IMAGE)
# @attributes hash with resource attributes, must contains :id, :name
# and :uname
#
# atributes {uname => ..., name => ..., id => ...}
def check_orphan(pool, xpath, resource_name, attributes)
return false if attributes.empty?
return false unless pool["#{xpath}[#{resource_name}_ID = "\
"#{attributes[:id]}]"].nil?
return false unless pool["#{xpath}[#{resource_name} = "\
"'#{attributes[:name]}' and "\
"#{resource_name}_UNAME = "\
"'#{attributes[:uname]}']"].nil?
true
end
def show_resource(id, options)
resource = retrieve_resource(id)

View File

@ -249,6 +249,29 @@ class OneImageHelper < OpenNebulaHelper::OneHelper
table
end
def check_orphans
orphans = []
xpath = '/VMTEMPLATE_POOL/VMTEMPLATE/TEMPLATE/DISK'
pool = factory_pool
tmpl_pool = OpenNebula::TemplatePool.new(@client, -2)
pool.info
tmpl_pool.info
pool.each do |img|
attrs = { :id => img['ID'],
:name => img['NAME'],
:uname => img['UNAME'] }
orphans << img['ID'] if check_orphan(tmpl_pool,
xpath,
'IMAGE', attrs)
end
orphans
end
private
def factory(id=nil)

View File

@ -438,4 +438,14 @@ CommandParser::CmdParser.new(ARGV) do
i.unlock
end
end
show_desc = <<-EOT.unindent
Shows orphans images (i.e images not referenced in any template).
EOT
command :orphans, show_desc do
puts helper.check_orphans
return 0
end
end