class OneImageHelper

CLI helper for oneimage command

Constants

IMAGE
PREFIXES

This list contains prefixes that should skip adding user home to the path This must have the same content as the case $FROM in downloader.sh

TEMPLATE_OPTIONS

Public Class Methods

conf_file() click to toggle source
# File lib/one_helper/oneimage_helper.rb, line 186
def self.conf_file
    'oneimage.yaml'
end
rname() click to toggle source
# File lib/one_helper/oneimage_helper.rb, line 182
def self.rname
    'IMAGE'
end
state_to_str(id) click to toggle source
# File lib/one_helper/oneimage_helper.rb, line 190
def self.state_to_str(id)
    id = id.to_i
    state_str = Image::IMAGE_STATES[id]
    Image::SHORT_IMAGE_STATES[state_str]
end
type_to_str(id) click to toggle source
# File lib/one_helper/oneimage_helper.rb, line 196
def self.type_to_str(id)
    id = id.to_i
    type_str = Image::IMAGE_TYPES[id]
    Image::SHORT_IMAGE_TYPES[type_str]
end

Private Class Methods

create_image_template(options) click to toggle source
# File lib/one_helper/oneimage_helper.rb, line 434
def create_image_template(options)
    template_options = TEMPLATE_OPTIONS.map do |o|
        o[:name].to_sym
    end

    template = create_image_variables(
        options,
        template_options - [:persistent, :dry, :prefix]
    )

    if options[:persistent]
        template << "PERSISTENT=YES\n"
    end

    if options[:prefix]
        template << "DEV_PREFIX=\"#{options[:prefix]}\"\n"
    end

    [0, template]
end
create_image_variables(options, name) click to toggle source
# File lib/one_helper/oneimage_helper.rb, line 417
def create_image_variables(options, name)
    if name.is_a?(Array)
        names = name
    else
        names = [name]
    end

    t = ''
    names.each do |n|
        if options[n]
            t << "#{n.to_s.upcase}=\"#{options[n]}\"\n"
        end
    end

    t
end

Public Instance Methods

check_orphans() click to toggle source
# File lib/one_helper/oneimage_helper.rb, line 261
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
format_pool(options) click to toggle source
# File lib/one_helper/oneimage_helper.rb, line 202
def format_pool(options)
    config_file = self.class.table_conf

    CLIHelper::ShowTable.new(config_file, self) do
        column :ID, 'ONE identifier for the Image', :size=>4 do |d|
            d['ID']
        end

        column :USER, 'Username of the Image owner', :left,
               :size=>10 do |d|
            helper.user_name(d, options)
        end

        column :GROUP, 'Group of the Image', :left,
               :size=>10 do |d|
            helper.group_name(d, options)
        end

        column :NAME, 'Name of the Image', :left, :size=>15 do |d|
            d['NAME']
        end

        column :DATASTORE, 'Name of the Datastore', :left, :size=>10 do |d|
            d['DATASTORE']
        end

        column :TYPE, 'Type of the Image', :left, :size=>4 do |d, _e|
            OneImageHelper.type_to_str(d['TYPE'])
        end

        column :REGTIME, 'Registration time of the Image',
               :size=>15 do |d|
            OpenNebulaHelper.time_to_str(d['REGTIME'])
        end

        column :PERSISTENT, 'Whether the Image is persistent or not',
               :size=>3 do |d|
            OpenNebulaHelper.boolean_to_str(d['PERSISTENT'])
        end

        column :STAT, 'State of the Image', :left, :size=>4 do |d|
            OneImageHelper.state_to_str(d['STATE'])
        end

        column :RVMS, 'Number of VMs currently running from this Image',
               :size=>4 do |d|
            d['RUNNING_VMS']
        end

        column :SIZE, 'Size of the image',
               :size=>7 do |d|
            OpenNebulaHelper.unit_to_str(d['SIZE'].to_i, options, 'M')
        end

        default :ID, :USER, :GROUP, :NAME, :DATASTORE, :SIZE, :TYPE,
                :PERSISTENT, :STAT, :RVMS
    end
end

Private Instance Methods

factory(id = nil) click to toggle source
# File lib/one_helper/oneimage_helper.rb, line 286
def factory(id = nil)
    if id
        OpenNebula::Image.new_with_id(id, @client)
    else
        xml=OpenNebula::Image.build_xml
        OpenNebula::Image.new(xml, @client)
    end
end
factory_pool(user_flag = -2) click to toggle source
# File lib/one_helper/oneimage_helper.rb, line 295
def factory_pool(user_flag = -2)
    OpenNebula::ImagePool.new(@client, user_flag)
end
format_resource(image, _options = {}) click to toggle source
# File lib/one_helper/oneimage_helper.rb, line 299
def format_resource(image, _options = {})
    str='%-15s: %-20s'
    str_h1='%-80s'

    path = image['PATH']
    iformat = image['FORMAT']

    size = OpenNebulaHelper.unit_to_str(image['SIZE'].to_i, {}, 'M')
    lock = OpenNebulaHelper.level_lock_to_str(image['LOCK/LOCKED'])
    regtime = OpenNebulaHelper.time_to_str(image['REGTIME'])
    pers = OpenNebulaHelper.boolean_to_str(image['PERSISTENT'])

    CLIHelper.print_header(str_h1 % "IMAGE #{image['ID']} INFORMATION")
    puts format(str, 'ID', image.id.to_s)
    puts format(str, 'NAME', image.name)
    puts format(str, 'USER', image['UNAME'])
    puts format(str, 'GROUP', image['GNAME'])
    puts format(str, 'LOCK', lock)
    puts format(str, 'DATASTORE', image['DATASTORE'])
    puts format(str, 'TYPE', image.type_str)
    puts format(str, 'REGISTER TIME', regtime)
    puts format(str, 'PERSISTENT', pers)
    puts format(str, 'SOURCE', image['SOURCE'])
    puts format(str, 'PATH', path) if path && !path.empty?
    puts format(str, 'FORMAT', iformat) if iformat && !iformat.empty?
    puts format(str, 'SIZE', size)
    puts format(str, 'STATE', image.short_state_str)
    puts format(str, 'RUNNING_VMS', image['RUNNING_VMS'])
    puts

    CLIHelper.print_header(str_h1 % 'PERMISSIONS', false)

    %w[OWNER GROUP OTHER].each do |e|
        mask = '---'
        mask[0] = 'u' if image["PERMISSIONS/#{e}_U"] == '1'
        mask[1] = 'm' if image["PERMISSIONS/#{e}_M"] == '1'
        mask[2] = 'a' if image["PERMISSIONS/#{e}_A"] == '1'

        puts format(str, e, mask)
    end

    if image.has_elements?('/IMAGE/SNAPSHOTS/SNAPSHOT')
        puts
        CLIHelper.print_header(str_h1 % 'IMAGE SNAPSHOTS', false)
        format_snapshots(image)
    end

    puts

    CLIHelper.print_header(str_h1 % 'IMAGE TEMPLATE', false)
    puts image.template_str

    puts
    CLIHelper.print_header('VIRTUAL MACHINES', false)
    puts

    vms=image.retrieve_elements('VMS/ID')

    return unless vms

    vms.map! {|e| e.to_i }
    onevm_helper=OneVMHelper.new
    onevm_helper.client=@client
    onevm_helper.list_pool({ :ids=>vms, :no_pager => true }, false)
end
format_snapshots(image) click to toggle source
# File lib/one_helper/oneimage_helper.rb, line 365
def format_snapshots(image)
    table=CLIHelper::ShowTable.new(nil, self) do
        column :AC, 'Is active', :left, :size => 2 do |d|
            if d['ACTIVE'] == 'YES'
                '=>'
            else
                ''
            end
        end
        column :ID, 'Snapshot ID', :size=>3 do |d|
            d['ID']
        end

        column :PARENT, 'Snapshot Parent ID', :size=>6 do |d|
            d['PARENT']
        end

        column :CHILDREN, 'Snapshot Children IDs', :size=>10 do |d|
            d['CHILDREN']
        end

        column :SIZE, '', :left, :size=>8 do |d|
            if d['SIZE']
                OpenNebulaHelper.unit_to_str(
                    d['SIZE'].to_i,
                    {},
                    'M'
                )
            else
                '-'
            end
        end

        column :NAME, 'Snapshot Name', :left, :size=>37 do |d|
            d['NAME']
        end

        column :DATE, 'Snapshot creation date', :size=>15 do |d|
            OpenNebulaHelper.time_to_str(d['DATE'])
        end

        default :AC, :ID, :PARENT, :DATE, :SIZE, :NAME
    end

    # Convert snapshot data to an array
    image_hash = image.to_hash
    image_snapshots = [image_hash['IMAGE']['SNAPSHOTS']['SNAPSHOT']].flatten
    table.show(image_snapshots)
end