Class: OneImageHelper
- Inherits:
-
OpenNebulaHelper::OneHelper
- Object
- OpenNebulaHelper::OneHelper
- OneImageHelper
- 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
['http', 'https', 'ssh', 's3', 'rbd', 'lxd']
- TEMPLATE_OPTIONS =
[ { :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, | 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, | prefix=o.strip.downcase if ['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, | 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 => '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, | 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 => 'Do not check Datastore capacity, only for admins.' }
- FILTERS =
[ { :name => 'backup', :large => '--backup', :description => 'Show only backup type images', :format => String } ]
Instance Attribute Summary
Attributes inherited from OpenNebulaHelper::OneHelper
Class Method Summary collapse
- .conf_file ⇒ Object
- .create_image_template(options) ⇒ Object
- .create_image_variables(options, name) ⇒ Object
- .create_template_options_used?(options, conflicting_opts) ⇒ Boolean
- .rname ⇒ Object
- .state_to_str(id) ⇒ Object
- .type_to_str(id) ⇒ Object
Instance Method Summary collapse
Methods inherited from OpenNebulaHelper::OneHelper
#backup_mode_valid?, #check_orphan, client, #create_resource, #filterflag_to_i, filterflag_to_i_desc, get_client, get_password, #group_name, #initialize, list_layout_help, #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, template_input_help, #to_id, to_id_desc, #user_name
Constructor Details
This class inherits a constructor from OpenNebulaHelper::OneHelper
Class Method Details
.conf_file ⇒ Object
174 175 176 |
# File 'lib/one_helper/oneimage_helper.rb', line 174 def self.conf_file 'oneimage.yaml' end |
.create_image_template(options) ⇒ Object
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
# File 'lib/one_helper/oneimage_helper.rb', line 512 def create_image_template() = TEMPLATE_OPTIONS.map do |o| o[:name].to_sym end << :name template = create_image_variables( , - [:persistent, :dry, :prefix] ) if [:persistent] template << "PERSISTENT=YES\n" end if [:prefix] template << "DEV_PREFIX=\"#{[:prefix]}\"\n" end [0, template] end |
.create_image_variables(options, name) ⇒ Object
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/one_helper/oneimage_helper.rb', line 495 def create_image_variables(, name) if name.is_a?(Array) names = name else names = [name] end t = '' names.each do |n| if [n] t << "#{n.to_s.upcase}=\"#{[n]}\"\n" end end t end |
.create_template_options_used?(options, conflicting_opts) ⇒ Boolean
535 536 537 538 539 540 541 542 543 544 545 |
# File 'lib/one_helper/oneimage_helper.rb', line 535 def (, conflicting_opts) # Get the template options names as symbols, options hash uses symbols =TEMPLATE_OPTIONS.map do |o| o[:name].to_sym end # Check if one at least one of the template options is in options hash conflicting_opts.replace(.keys & ) !conflicting_opts.empty? end |
.rname ⇒ Object
170 171 172 |
# File 'lib/one_helper/oneimage_helper.rb', line 170 def self.rname 'IMAGE' end |
.state_to_str(id) ⇒ Object
178 179 180 181 182 |
# File 'lib/one_helper/oneimage_helper.rb', line 178 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
184 185 186 187 188 |
# File 'lib/one_helper/oneimage_helper.rb', line 184 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_orphans ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/one_helper/oneimage_helper.rb', line 270 def check_orphans orphans = [] xpath = '/VMTEMPLATE_POOL/VMTEMPLATE/TEMPLATE/DISK' pool = factory_pool tmpl_pool = OpenNebula::TemplatePool.new(@client, -2) rc = pool.info return rc if OpenNebula.is_error?(rc) rc = tmpl_pool.info return rc if OpenNebula.is_error?(rc) pool.each do |img| next if img['TYPE'].to_i == 6 # skip backup images 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
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 260 261 262 263 264 265 266 267 268 |
# File 'lib/one_helper/oneimage_helper.rb', line 206 def format_pool() 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, ) end column :GROUP, 'Group of the Image', :left, :size=>10 do |d| helper.group_name(d, ) 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 :MODTIME, 'Last modification time of the Image', :size=>15 do |d| OpenNebulaHelper.time_to_str(d['MODTIME']) 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, , 'M') end default :ID, :USER, :GROUP, :NAME, :DATASTORE, :SIZE, :TYPE, :PERSISTENT, :STAT, :RVMS end end |
#retrieve_snapshot_id(image_id, id) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/one_helper/oneimage_helper.rb', line 190 def retrieve_snapshot_id(image_id, id) return [0, id.to_i] if id =~ /\A\d+\z/ image = retrieve_resource(image_id) image.info ids = image.retrieve_elements( "/IMAGE/SNAPSHOTS/SNAPSHOT[NAME='#{id}']/ID" ) return [-1, "#{id} not found or duplicated"] \ if ids.nil? || ids.size > 1 [0, ids[0].to_i] end |