Skip to content

Commit

Permalink
F OpenNebula#3629: Add oneimage orphans (OpenNebula#3785)
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian González authored and Ruben S. Montero committed Oct 1, 2019
1 parent 9ed1d12 commit 6855d38
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/cli/one_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,29 @@ def list_pool(options, top=false, filter_flag=nil)
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)

Expand Down
23 changes: 23 additions & 0 deletions src/cli/one_helper/oneimage_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,29 @@ def format_pool(options)
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)
Expand Down
10 changes: 10 additions & 0 deletions src/cli/oneimage
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6855d38

Please sign in to comment.