Class: OneImageHelper

Inherits:
OpenNebulaHelper::OneHelper show all
Defined in:
lib/one_helper/oneimage_helper.rb

Overview

CLI helper for oneimage command

Constant Summary collapse

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

%w[http https ssh s3 rbd vcenter lxd docker dockerfile]
TEMPLATE_OPTIONS =
{
    :name => 'name',
    :large => '--name name',
    :format => String,
    :description => 'Name of the new image'
},
{
    :name => 'description',
    :large => '--description description',
    :format => String,
    :description => 'Description for the new Image'
},
{
    :name => 'type',
    :large => '--type type',
    :format => String,
    :description => "Type of the new Image: \
    #{Image::IMAGE_TYPES.join(', ')}",

    :proc => lambda do |o, _options|
        type=o.strip.upcase

        if Image::IMAGE_TYPES.include? type
            [0, type]
        else
            [-1, "Type should be: #{Image::IMAGE_TYPES.join(', ')}"]
        end
    end
},
{
    :name => 'persistent',
    :large => '--persistent',
    :description => 'Tells if the image will be persistent'
},
{
    :name => 'prefix',
    :large => '--prefix prefix',
    :description => "Device prefix for the disk (eg. hd, sd, xvd\n"<<
        ' '*31<<'or vd)',
    :format => String,
    :proc => lambda do |o, _options|
        prefix=o.strip.downcase
        if %w[hd sd xvd vd].include? prefix
            [0, prefix]
        else
            [-1, 'The prefix must be hd, sd, xvd or vd']
        end
    end
},
{
    :name => 'target',
    :large => '--target target',
    :description => 'Device the disk will be attached to',
    :format => String
},
{
    :name => 'path',
    :large => '--path path',
    :description => 'Path of the image file',
    :format => String,
    :proc => lambda do |o, _options|
        next [0, o] if o.match(%r{^(#{PREFIXES.join('|')})://})

        if o[0, 1]=='/'
            path=o
        else
            path=Dir.pwd+'/'+o
        end

        [0, path]
    end
},
{
    :name => 'format',
    :large => '--format format',
    :description => 'Format of the image (raw, qcow2, ...)',
    :format => String
},
{
    :name => 'fs',
    :large => '--fs filesystem',
    :description => 'Filesystem to format the image (ext4, xfs, ...)',
    :format => String
},
{
    :name => 'disk_type',
    :large => '--disk_type disk_type',
    :description => "Type of the image \n"<<
        ' ' * 31 << "BLOCK, CDROM, RBD or FILE \n" \
        '(for others, check the documentation) ',
    :format => String
},
{
    :name => 'vcenter_disk_type',
    :large => '--vcenter_disk_type vcenter_disk_type',
    :description => "The vCenter Disk Type of the image \n"<<
        ' ' * 31 <<
        'for vCenter: THIN, THICK, ZEROEDTHICK ' \
        '(for others, check the documentation) ',
    :format => String
},
{
    :name => 'vcenter_adapter_type',
    :large => '--vcenter_adapter_type vcenter_adapter_type',
    :description => 'Controller that will handle this image in ' \
        'vCenter (lsiLogic, ide, busLogic). For other '\
        'values check the documentation',
    :format => String
},
{
    :name => 'source',
    :large => '--source source',
    :description =>
        "Source to be used. Useful for not file-based\n"<<
            ' '*31<<'images',
    :format => String
},
{
    :name => 'size',
    :large => '--size size',
    :description => "Size in MB. \
    Used for DATABLOCK type or SOURCE based images.",

    :format => String,
    :proc => lambda do |o, _options|
        m=o.strip.match(/^(\d+(?:\.\d+)?)(m|mb|g|gb)?$/i)

        if !m
            [-1, 'Size value malformed']
        else
            multiplier=case m[2]
                       when /(g|gb)/i
                           1024
                       else
                           1
                       end

            value=m[1].to_f*multiplier

            [0, value.floor]
        end
    end
},
OpenNebulaHelper::DRY
IMAGE =
{
    :name => 'no_check_capacity',
    :large => '--no_check_capacity',
    :description =>
        'Check Datastore capacity. By Default YES',
    :format => String
}

Instance Attribute Summary

Attributes inherited from OpenNebulaHelper::OneHelper

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OpenNebulaHelper::OneHelper

#check_orphan, client, #create_resource, #filterflag_to_i, filterflag_to_i_desc, get_client, get_password, #group_name, #initialize, #list_pool, #list_pool_format, #list_pool_table, #list_pool_top, #list_pool_xml, #list_to_id, list_to_id_desc, name_to_id, #perform_action, #perform_actions, #print_page, #retrieve_resource, #set_client, set_endpoint, set_password, set_user, #show_resource, #start_pager, #stop_pager, table_conf, #to_id, to_id_desc, #user_name

Constructor Details

This class inherits a constructor from OpenNebulaHelper::OneHelper

Class Method Details

.conf_fileObject



186
187
188
# File 'lib/one_helper/oneimage_helper.rb', line 186

def self.conf_file
    'oneimage.yaml'
end

.create_image_template(options) ⇒ Object



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/one_helper/oneimage_helper.rb', line 495

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) ⇒ Object



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/one_helper/oneimage_helper.rb', line 478

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

.rnameObject



182
183
184
# File 'lib/one_helper/oneimage_helper.rb', line 182

def self.rname
    'IMAGE'
end

.state_to_str(id) ⇒ Object



190
191
192
193
194
# 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) ⇒ Object



196
197
198
199
200
# 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

Instance Method Details

#check_orphansObject



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# 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) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# 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