Class: OneQuotaHelper
- Inherits:
-
Object
- Object
- OneQuotaHelper
- Defined in:
- lib/one_helper/onequota_helper.rb
Overview
Helper class for Quota commands
Constant Summary collapse
- LIMIT_DEFAULT =
'-1'- LIMIT_UNLIMITED =
'-2'- DEFAULT_VM_QUOTA =
{ 'VMS' => LIMIT_DEFAULT, 'VMS_USED' => '0', 'CPU' => LIMIT_DEFAULT, 'CPU_USED' => '0', 'PCI_DEV' => LIMIT_DEFAULT, 'PCI_DEV_USED' => '0', 'PCI_NIC' => LIMIT_DEFAULT, 'PCI_NIC_USED' => '0', 'MEMORY' => LIMIT_DEFAULT, 'MEMORY_USED' => '0', 'RUNNING_VMS' => LIMIT_DEFAULT, 'RUNNING_VMS_USED' => '0', 'RUNNING_CPU' => LIMIT_DEFAULT, 'RUNNING_CPU_USED' => '0', 'RUNNING_MEMORY' => LIMIT_DEFAULT, 'RUNNING_MEMORY_USED' => '0', 'RUNNING_PCI_DEV' => LIMIT_DEFAULT, 'RUNNING_PCI_DEV_USED' => '0', 'RUNNING_PCI_NIC' => LIMIT_DEFAULT, 'RUNNING_PCI_NIC_USED' => '0', 'SYSTEM_DISK_SIZE' => LIMIT_DEFAULT, 'SYSTEM_DISK_SIZE_USED' => '0' }
- EDITOR_PATH =
'/usr/bin/vi'- HELP_QUOTA =
<<-EOT.unindent #----------------------------------------------------------------------- # Supported quota limits: # # DATASTORE = [ # ID = <ID of the datastore> # IMAGES = <Max. number of images in the datastore> # SIZE = <Max. storage capacity (Mb) used in the datastore> # ] # # VM = [ # VMS = <Max. number of VMs> # RUNNING_VMS = <Max. number of running VMs> # MEMORY = <Max. allocated memory (MB)> # RUNNING_MEMORY = <Max. running memory (MB)> # CPU = <Max. allocated CPU> # RUNNING_CPU = <Max. running CPU> # PCI_DEV = <Max. allocated PCI> # RUNNING_PCI_DEV = <Max. running PCI> # PCI_NIC = <Max. allocated network PCI> # RUNNING_PCI_NIC = <Max. running network PCI> # SYSTEM_DISK_SIZE = <Max. allocated system disk (MB)> # ] # # NETWORK = [ # ID = <ID of the network> # LEASES = <Max. number of IP leases from the network> # ] # # IMAGE = [ # ID = <ID of the image> # RVMS = <Max. number of VMs using the image> # ] EOT
- HELP_QUOTA_FOOTER =
<<-EOT.unindent # # In any quota: # -1 means use the default limit (set with the 'defaultquota' command) # -2 means unlimited. # # The usage counters "*_USED" are shown for information # purposes and will NOT be modified. #----------------------------------------------------------------------- EOT
- HELP_DEFAULT_QUOTA_FOOTER =
<<-EOT.unindent # # In any quota: # -2 means unlimited. # # The usage counters "*_USED" will always be 0 for the default # quotas, and can be ignored. #----------------------------------------------------------------------- EOT
Class Method Summary collapse
-
.get_batch_quota(path) ⇒ Object
Retrieves a clean quota template, without any existing resource information @param path [String] path to the new contents.
-
.merge_quota(resource, str) ⇒ Object
Edits the quota template of a resource, adding the quotas set in str @param resource [PoolElement] to get the current info from @param str [String] quota template, created by get_batch_quota() @return [String, OpenNebula::Error] merged contents of the new quotas on success, Error if the user info could not be retrieved.
-
.set_quota(resource, path, is_default = false) ⇒ Object
Edits the quota template of a resource @param [XMLElement] resource to get the current info from @param [String] path to the new contents.
Instance Method Summary collapse
-
#format_quota(qh, default_quotas, resource_id) ⇒ Object
Outputs formated quota information to stdout @param qh [Hash] with the quotas for a given resource @param default_quotas_hash [XMLElement] with the default quota limits @param resource_id [Integer] user/group ID.
- #get_default_limit(limit, xpath) ⇒ Object
-
#initialize(client = nil) ⇒ OneQuotaHelper
constructor
A new instance of OneQuotaHelper.
Constructor Details
#initialize(client = nil) ⇒ OneQuotaHelper
Returns a new instance of OneQuotaHelper.
108 109 110 |
# File 'lib/one_helper/onequota_helper.rb', line 108 def initialize(client = nil) @client=client end |
Class Method Details
.get_batch_quota(path) ⇒ Object
Retrieves a clean quota template, without any existing resource
information
@param path [String] path to the new contents. If nil a editor will be
used
@return [String] contents of the new quotas
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/one_helper/onequota_helper.rb', line 165 def self.get_batch_quota(path) str = '' if path.nil? require 'tempfile' tmp = Tempfile.new('one-cli') path = tmp.path tmp << HELP_QUOTA << "\n" tmp.close editor_path = ENV['EDITOR'] ? ENV['EDITOR'] : EDITOR_PATH system("#{editor_path} #{path}") unless $CHILD_STATUS.exitstatus == 0 puts 'Editor not defined' exit(-1) end str = File.read(path) File.unlink(path) else str = File.read(path) end str end |
.merge_quota(resource, str) ⇒ Object
Edits the quota template of a resource, adding the quotas set in str
@param resource [PoolElement] to get the current info from
@param str [String] quota template, created by get_batch_quota()
@return [String, OpenNebula::Error] merged contents of the new quotas on
success, Error if the user info could not be retrieved
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/one_helper/onequota_helper.rb', line 201 def self.merge_quota(resource, str) rc = resource.info if OpenNebula.is_error?(rc) return rc end # Instead of parsing the existing quotas, and deleting the ones that # conflict with the batch quota string, the new quotas are placed at # the end of the template sent to opennebula. This relies on the core # reading them in order and replacing the quotas with each new # appearance tmp_str = '' tmp_str << resource.template_like_str('DATASTORE_QUOTA') << "\n" tmp_str << resource.template_like_str('VM_QUOTA') << "\n" tmp_str << resource.template_like_str('NETWORK_QUOTA') << "\n" tmp_str << resource.template_like_str('IMAGE_QUOTA') << "\n" tmp_str << str return tmp_str end |
.set_quota(resource, path, is_default = false) ⇒ Object
Edits the quota template of a resource
@param [XMLElement] resource to get the current info from
@param [String] path to the new contents. If nil a editor will be
used
@param [True|False] is_default To change the help text
@return [String] contents of the new quotas
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/one_helper/onequota_helper.rb', line 118 def self.set_quota(resource, path, is_default = false) str = '' if path.nil? require 'tempfile' tmp = Tempfile.new('one-cli') path = tmp.path tmp << HELP_QUOTA if is_default tmp << HELP_DEFAULT_QUOTA_FOOTER else tmp << HELP_QUOTA_FOOTER end tmp << resource.template_like_str('DATASTORE_QUOTA') << "\n" tmp << resource.template_like_str('VM_QUOTA') << "\n" tmp << resource.template_like_str('NETWORK_QUOTA') << "\n" tmp << resource.template_like_str('IMAGE_QUOTA') << "\n" tmp.close editor_path = ENV['EDITOR'] ? ENV['EDITOR'] : EDITOR_PATH system("#{editor_path} #{path}") unless $CHILD_STATUS.exitstatus == 0 puts 'Editor not defined' exit(-1) end str = File.read(path) File.unlink(path) else str = File.read(path) end str end |
Instance Method Details
#format_quota(qh, default_quotas, resource_id) ⇒ Object
Outputs formated quota information to stdout
@param qh [Hash] with the quotas for a given resource
@param default_quotas_hash [XMLElement] with the default quota limits
@param resource_id [Integer] user/group ID
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 269 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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 |
# File 'lib/one_helper/onequota_helper.rb', line 231 def format_quota(qh, default_quotas, resource_id) str_h1='%-80s' puts CLIHelper.print_header(str_h1 % 'VMS USAGE & QUOTAS', false) puts @default_quotas = default_quotas vm_quotas = [qh['VM_QUOTA']['VM']].flatten generic_q = generic_quotas # This initializes the VM quotas for users/groups that don't have any # resource usage yet. It not applied to oneadmin if vm_quotas[0].nil? && resource_id.to_i != 0 limit = LIMIT_DEFAULT vm_quotas = [{ 'VMS' => limit, 'VMS_USED' => '0', 'CPU' => limit, 'CPU_USED' => '0', 'MEMORY' => limit, 'MEMORY_USED' => '0', 'PCI_DEV' => limit, 'PCI_DEV_USED' => '0', 'PCI_NIC' => limit, 'PCI_NIC_USED' => '0', 'RUNNING_VMS' => limit, 'RUNNING_VMS_USED' => '0', 'RUNNING_CPU' => limit, 'RUNNING_CPU_USED' => '0', 'RUNNING_MEMORY' => limit, 'RUNNING_MEMORY_USED' => '0', 'RUNNING_PCI_DEV' => limit, 'RUNNING_PCI_DEV_USED' => '0', 'RUNNING_PCI_NIC' => limit, 'RUNNING_PCI_NIC_USED' => '0', 'SYSTEM_DISK_SIZE' => limit, 'SYSTEM_DISK_SIZE_USED' => '0' }] generic_q.each do |q| vm_quotas[0][q] = limit vm_quotas[0]["#{q}_USED"] = '0' vm_quotas[0]["RUNNING_#{q}"] = limit vm_quotas[0]["RUNNING_#{q}_USED"] = '0' end end if !vm_quotas[0].nil? CLIHelper::ShowTable.new(nil, self) do column :CLUSTERS, '', :right, :size=>8 do |d| if !d.nil? d['CLUSTER_IDS'] end end column :VMS, '', :right, :size=>9 do |d| if !d.nil? elem = 'VMS' limit = d[elem] limit = helper.get_default_limit( limit, "VM_QUOTA/VM/#{elem}" ) if limit == LIMIT_UNLIMITED format('%<used>3d / -', :used => d['VMS_USED']) else format('%<used>3d / %<limit>3d', :used => d['VMS_USED'], :limit => limit) end end end column :MEMORY, '', :right, :size=>15 do |d| if !d.nil? elem = 'MEMORY' limit = d[elem] limit = helper.get_default_limit( limit, "VM_QUOTA/VM/#{elem}" ) if limit == LIMIT_UNLIMITED format('%<used>6s / -', :used => OpenNebulaHelper.unit_to_str(d['MEMORY_USED'].to_i, {}, 'M')) else format('%<used>6s / %<limit>6s', :used => OpenNebulaHelper.unit_to_str(d['MEMORY_USED'].to_i, {}, 'M'), :limit => OpenNebulaHelper.unit_to_str(limit.to_i, {}, 'M')) end end end column :CPU, '', :right, :size=>15 do |d| if !d.nil? elem = 'CPU' limit = d[elem] limit = helper.get_default_limit( limit, "VM_QUOTA/VM/#{elem}" ) if limit == LIMIT_UNLIMITED format('%<used>6.1f / -', :used => d['CPU_USED']) else format('%<used>6.1f / %<limit>6.2f', :used => d['CPU_USED'], :limit => limit) end end end column :"PCI DEV", '', :right, :size=>9 do |d| if !d.nil? elem = 'PCI_DEV' limit = d[elem] limit = helper.get_default_limit( limit, "VM_QUOTA/VM/#{elem}" ) if limit == LIMIT_UNLIMITED format('%<used>3d / -', :used => d['PCI_DEV_USED']) else format('%<used>3d / %<limit>3d', :used => d['PCI_DEV_USED'], :limit => limit) end end end column :"PCI NIC", '', :right, :size=>9 do |d| if !d.nil? elem = 'PCI_NIC' limit = d[elem] limit = helper.get_default_limit( limit, "VM_QUOTA/VM/#{elem}" ) if limit == LIMIT_UNLIMITED format('%<used>3d / -', :used => d['PCI_NIC_USED']) else format('%<used>3d / %<limit>3d', :used => d['PCI_NIC_USED'], :limit => limit) end end end column :"DISK SIZE", '', :right, :size=>15 do |d| if !d.nil? elem = 'SYSTEM_DISK_SIZE' limit = d[elem] limit = helper.get_default_limit( limit, "VM_QUOTA/VM/#{elem}" ) if limit == LIMIT_UNLIMITED format('%<used>6s / -', :used => OpenNebulaHelper.unit_to_str( d['SYSTEM_DISK_SIZE_USED'].to_i, {}, 'M' )) else format('%<used>6s / %<limit>6s', :used => OpenNebulaHelper.unit_to_str( d['SYSTEM_DISK_SIZE_USED'].to_i, {}, 'M' ), :limit => OpenNebulaHelper.unit_to_str( limit.to_i, {}, 'M' )) end end end end.show(vm_quotas, {}) puts end CLIHelper.print_header(str_h1 % 'VMS USAGE & QUOTAS - RUNNING', false) puts if !vm_quotas[0].nil? CLIHelper::ShowTable.new(nil, self) do column :CLUSTERS, '', :right, :size=>8 do |d| if !d.nil? d['CLUSTER_IDS'] end end column :"RUN VMS", '', :right, :size=>9 do |d| if !d.nil? elem = 'RUNNING_VMS' limit = d[elem] || LIMIT_UNLIMITED limit = helper.get_default_limit( limit, "VM_QUOTA/VM/#{elem}" ) if d['RUNNING_VMS_USED'].nil? d['RUNNING_VMS_USED'] = 0 end if limit == LIMIT_UNLIMITED format('%<used>3d / -', :used => d['RUNNING_VMS_USED']) else format('%<used>3d / %<limit>3d', :used => d['RUNNING_VMS_USED'], :limit => limit) end end end column :"RUN MEMORY", '', :right, :size=>15 do |d| if !d.nil? elem = 'RUNNING_MEMORY' limit = d[elem] || LIMIT_UNLIMITED limit = helper.get_default_limit( limit, "VM_QUOTA/VM/#{elem}" ) if d['RUNNING_MEMORY_USED'].nil? d['RUNNING_MEMORY_USED'] = 0 end if limit == LIMIT_UNLIMITED format('%<used>6s / -', :used => OpenNebulaHelper.unit_to_str( d['RUNNING_MEMORY_USED'].to_i, {}, 'M' )) else format('%<used>6s / %<limit>6s', :used => OpenNebulaHelper.unit_to_str( d['RUNNING_MEMORY_USED'].to_i, {}, 'M' ), :limit => OpenNebulaHelper.unit_to_str( limit.to_i, {}, 'M' )) end end end column :"RUN CPU", '', :right, :size=>15 do |d| if !d.nil? elem = 'RUNNING_CPU' limit = d[elem] || LIMIT_UNLIMITED limit = helper.get_default_limit( limit, "VM_QUOTA/VM/#{elem}" ) if d['RUNNING_CPU_USED'].nil? d['RUNNING_CPU_USED'] = 0 end if limit == LIMIT_UNLIMITED format('%<used>6.1f / -', :used => d['RUNNING_CPU_USED']) else format('%<used>6.1f / %<limit>6.1f', :used => d['RUNNING_CPU_USED'], :limit => limit) end end end column :"RUN PCI", '', :right, :size=>12 do |d| if !d.nil? elem = 'RUNNING_PCI_DEV' limit = d[elem] || LIMIT_UNLIMITED limit = helper.get_default_limit( limit, "VM_QUOTA/VM/#{elem}" ) value = d['RUNNING_PCI_DEV_USED'] || 0 if limit == LIMIT_UNLIMITED format('%<used>3s / -', :used => value) else format('%<used>3s / %<limit>3s', :used => value, :limit => limit) end end end column :"RUN PCI NIC", '', :right, :size=>12 do |d| if !d.nil? elem = 'RUNNING_PCI_NIC' limit = d[elem] || LIMIT_UNLIMITED limit = helper.get_default_limit( limit, "VM_QUOTA/VM/#{elem}" ) value = d['RUNNING_PCI_NIC_USED'] || 0 if limit == LIMIT_UNLIMITED format('%<used>3s / -', :used => value) else format('%<used>3s / %<limit>3s', :used => value, :limit => limit) end end end end.show(vm_quotas, {}) puts end if !generic_q.empty? && !vm_quotas[0].nil? CLIHelper.print_header(str_h1 % 'VMS GENERIC QUOTAS', false) size = [80 / generic_q.length - 1, 18].min CLIHelper::ShowTable.new(nil, self) do generic_q.each do |elem| column elem.to_sym, '', :right, :size=>size do |d| if !d.nil? limit = d[elem] limit = helper.get_default_limit( limit, "VM_QUOTA/VM/#{elem}" ) if limit == LIMIT_UNLIMITED format('%<used>6s / -', :used => d["#{elem}_USED"]) else format('%<used>6s / %<limit>6s', :used => d["#{elem}_USED"], :limit => limit) end end end end end.show(vm_quotas, {}) puts CLIHelper.print_header(str_h1 % 'VMS GENERIC RUNNING QUOTAS', false) size = [80 / generic_q.length - 1, 18].min CLIHelper::ShowTable.new(nil, self) do generic_q.each do |q| elem = "RUNNING_#{q}" column elem.to_sym, '', :right, :size=>size do |d| if !d.nil? limit = d[elem] limit = helper.get_default_limit( limit, "VM_QUOTA/VM/#{elem}" ) if limit == LIMIT_UNLIMITED format('%<used>6s / -', :used => d["#{elem}_USED"]) else format('%<used>6s / %<limit>6s', :used => d["#{elem}_USED"], :limit => limit) end end end end end.show(vm_quotas, {}) puts end CLIHelper.print_header(str_h1 % 'DATASTORE USAGE & QUOTAS', false) puts ds_quotas = [qh['DATASTORE_QUOTA']['DATASTORE']].flatten if !ds_quotas[0].nil? CLIHelper::ShowTable.new(nil, self) do column :ID, '', :size=>12 do |d| d['ID'] unless d.nil? end column :IMAGES, '', :right, :size=>20 do |d| if !d.nil? elem = 'IMAGES' limit = d[elem] limit = helper.get_default_limit( limit, "DATASTORE_QUOTA/DATASTORE[ID=#{d['ID']}]/#{elem}" ) if limit == LIMIT_UNLIMITED format('%<used>8d / -', :used => d['IMAGES_USED']) else format('%<used>8d / %<limit>8d', :used => d['IMAGES_USED'], :limit => limit) end end end column :SIZE, '', :right, :size=>19 do |d| if !d.nil? elem = 'SIZE' limit = d[elem] limit = helper.get_default_limit( limit, "DATASTORE_QUOTA/DATASTORE[ID=#{d['ID']}]/#{elem}" ) if limit == LIMIT_UNLIMITED format('%<used>8s / -', :used => OpenNebulaHelper.unit_to_str( d['SIZE_USED'].to_i, {}, 'M' )) else format('%<used>8s / %<limit>8s', :used => OpenNebulaHelper.unit_to_str( d['SIZE_USED'].to_i, {}, 'M' ), :limit => OpenNebulaHelper.unit_to_str( limit.to_i, {}, 'M' )) end end end end.show(ds_quotas, {}) puts end CLIHelper.print_header(str_h1 % 'NETWORK USAGE & QUOTAS', false) puts net_quotas = [qh['NETWORK_QUOTA']['NETWORK']].flatten if !net_quotas[0].nil? CLIHelper::ShowTable.new(nil, self) do column :ID, '', :size=>12 do |d| d['ID'] unless d.nil? end column :LEASES, '', :right, :size=>20 do |d| if !d.nil? elem = 'LEASES' limit = d[elem] limit = helper.get_default_limit( limit, "NETWORK_QUOTA/NETWORK[ID=#{d['ID']}]/#{elem}" ) if limit == LIMIT_UNLIMITED format('%<used>8d / -', :used => d['LEASES_USED']) else format('%<used>8d / %<limit>8d', :used => d['LEASES_USED'], :limit => limit) end end end end.show(net_quotas, {}) puts end CLIHelper.print_header(str_h1 % 'IMAGE USAGE & QUOTAS', false) puts image_quotas = [qh['IMAGE_QUOTA']['IMAGE']].flatten if !image_quotas[0].nil? CLIHelper::ShowTable.new(nil, self) do column :ID, '', :size=>12 do |d| d['ID'] unless d.nil? end column :"RUNNING VMS", '', :right, :size=>20 do |d| if !d.nil? elem = 'RVMS' limit = d[elem] limit = helper.get_default_limit( limit, "IMAGE_QUOTA/IMAGE[ID=#{d['ID']}]/RVMS" ) if limit == LIMIT_UNLIMITED format('%<used>8d / -', :used => d['RVMS_USED']) else format('%<used>8d / %<limit>8d', :used => d['RVMS_USED'], :limit => limit) end end end end.show(image_quotas, {}) end end |
#get_default_limit(limit, xpath) ⇒ Object
715 716 717 718 719 720 721 722 723 724 725 726 727 |
# File 'lib/one_helper/onequota_helper.rb', line 715 def get_default_limit(limit, xpath) if limit == LIMIT_DEFAULT if !@default_quotas.nil? limit = @default_quotas[xpath] limit = LIMIT_UNLIMITED if limit.nil? || limit == '' else limit = LIMIT_UNLIMITED end end return limit end |