Class: OneHostHelper
- Inherits:
-
OpenNebulaHelper::OneHelper
- Object
- OpenNebulaHelper::OneHelper
- OneHostHelper
- Defined in:
- lib/one_helper/onehost_helper.rb
Overview
implements onehost command
Constant Summary collapse
- TEMPLATE_XPATH =
'//HOST/TEMPLATE'- HYBRID =
{ :ec2 => { :help => <<-EOT.unindent #----------------------------------------------------------------------- # Supported EC2 AUTH ATTRIBUTTES: # # REGION_NAME = <the name of the ec2 region> # # EC2_ACCESS = <Your ec2 access key id> # EC2_SECRET = <Your ec2 secret key> # # CAPACITY = [ # M1_SMALL = <number of machines m1.small>, # M1_XLARGE = <number of machines m1.xlarge>, # M1_LARGE = <number of machines m1.large> # ] # # You can set any machine type supported by ec2 # See your ec2_driver.conf for more information # #----------------------------------------------------------------------- EOT }, :az => { :help => <<-EOT.unindent #----------------------------------------------------------------------- # Mandatory AZURE ATTRIBUTTES: # # AZ_SUB = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" # AZ_CLIENT = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" # AZ_SECRET = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" # AZ_TENANT = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" # AZ_REGION = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" # # CAPACITY=[ # STANDARD_B1LS =<number of machines Standard_B1ls>, # STANDARD_A1_V2=<number of machines Standard_A1_v2> # ] # # Optional AZURE ATTRIBUTES: # # AZ_RGROUP = "" # # You can set any machine type supported by azure # See your az_driver.conf for more information # #----------------------------------------------------------------------- EOT } }
- VERSION_XPATH =
"#{TEMPLATE_XPATH}/VERSION"- MONITORING =
{ 'FREE_CPU' => 'CAPACITY', 'FREE_MEMORY' => 'CAPACITY', 'USED_CPU' => 'CAPACITY', 'USED_MEMORY' => 'CAPACITY', 'NETRX' => 'SYSTEM', 'NETTX' => 'SYSTEM' }
- NUM_THREADS =
15
Instance Attribute Summary
Attributes inherited from OpenNebulaHelper::OneHelper
Class Method Summary collapse
Instance Method Summary collapse
- #forceupdate(host_ids, options) ⇒ Object
- #format_pool(options) ⇒ Object
- #monitoring(host, attr, options) ⇒ Object
- #set_hybrid(type, path) ⇒ Object
- #sync(host_ids, options) ⇒ Object
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_file ⇒ Object
93 94 95 |
# File 'lib/one_helper/onehost_helper.rb', line 93 def self.conf_file 'onehost.yaml' end |
.rname ⇒ Object
89 90 91 |
# File 'lib/one_helper/onehost_helper.rb', line 89 def self.rname 'HOST' end |
.state_to_str(id) ⇒ Object
97 98 99 100 101 102 |
# File 'lib/one_helper/onehost_helper.rb', line 97 def self.state_to_str(id) id = id.to_i state_str = Host::HOST_STATES[id] Host::SHORT_HOST_STATES[state_str] end |
Instance Method Details
#forceupdate(host_ids, options) ⇒ Object
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 |
# File 'lib/one_helper/onehost_helper.rb', line 408 def forceupdate(host_ids, ) if Process.uid.zero? || Process.gid.zero? STDERR.puts("Cannot run 'onehost forceupdate' as root") exit(-1) end cluster_id = [:cluster] # Get the Host pool filter_flag ||= OpenNebula::Pool::INFO_ALL pool = factory_pool(filter_flag) rc = pool.info return -1, rc. if OpenNebula.is_error?(rc) host_errors = [] pool.each do |host| if host_ids next unless host_ids.include?(host['ID'].to_i) elsif cluster_id next if host['CLUSTER_ID'].to_i != cluster_id end state = host['STATE'] # Skip this host from remote syncing if it's a PUBLIC_CLOUD host next if host['TEMPLATE/PUBLIC_CLOUD'] == 'YES' # Skip this host from remote syncing if it's OFFLINE next if Host::HOST_STATES[state.to_i] == 'OFFLINE' rc = host.forceupdate host_errors << host['NAME'] if OpenNebula.is_error?(rc) end if host_errors.empty? puts 'All hosts updated successfully.' 0 else STDERR.puts 'Failed to update the following hosts:' host_errors.each {|h| STDERR.puts "* #{h}" } -1 end end |
#format_pool(options) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 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 159 160 161 162 163 164 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 195 196 197 198 199 200 201 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 |
# File 'lib/one_helper/onehost_helper.rb', line 104 def format_pool() config_file = self.class.table_conf CLIHelper::ShowTable.new(config_file, self) do column :ID, 'ONE identifier for Host', :size => 4 do |d| d['ID'] end column :NAME, 'Name of the Host', :left, :size => 15 do |d| d['NAME'] end column :CLUSTER, 'Name of the Cluster', :left, :size => 9 do |d| OpenNebulaHelper.cluster_str(d['CLUSTER']) end column :TVM, 'Total Virtual Machines allocated to the Host', :size => 3 do |d| d['HOST_SHARE']['RUNNING_VMS'] || 0 end column :ZVM, 'Number of Virtual Machine zombies', :size => 3 do |d| d['TEMPLATE']['TOTAL_ZOMBIES'] || 0 end column :TCPU, 'Total CPU percentage', :size => 4 do |d| d['HOST_SHARE']['MAX_CPU'] || 0 end column :FCPU, 'Free CPU percentage', :size => 4 do |d| d['HOST_SHARE']['MAX_CPU'].to_i - d['HOST_SHARE']['USED_CPU'].to_i rescue '-' end column :ACPU, 'Available cpu percentage (not reserved by VMs)', :size => 4 do |d| max_cpu = d['HOST_SHARE']['MAX_CPU'].to_i max_cpu = 100 if max_cpu.zero? max_cpu - d['HOST_SHARE']['CPU_USAGE'].to_i end column :TMEM, 'Total Memory', :size => 7 do |d| OpenNebulaHelper.unit_to_str( d['HOST_SHARE']['MAX_MEM'].to_i, ) rescue '-' end column :FMEM, 'Free Memory', :size => 7 do |d| OpenNebulaHelper.unit_to_str( d['HOST_SHARE']['FREE_MEM'].to_i, ) rescue '-' end column :AMEM, 'Available Memory (not reserved by VMs)', :size => 7 do |d| acpu = d['HOST_SHARE']['MAX_MEM'].to_i - d['HOST_SHARE']['MEM_USAGE'].to_i OpenNebulaHelper.unit_to_str(acpu, ) end column :REAL_CPU, 'Real CPU', :size => 18 do |d| max_cpu = d['HOST_SHARE']['MAX_CPU'].to_i if max_cpu != 0 used_cpu = d['HOST_SHARE']['USED_CPU'].to_i ratio = (used_cpu * 100) / max_cpu "#{used_cpu} / #{max_cpu} (#{ratio}%)" else '-' end end column :ALLOCATED_CPU, 'Allocated CPU)', :size => 18 do |d| max_cpu = d['HOST_SHARE']['MAX_CPU'].to_i cpu_usage = d['HOST_SHARE']['CPU_USAGE'].to_i if max_cpu.zero? && cpu_usage.zero? '-' else cpu_usage = d['HOST_SHARE']['CPU_USAGE'].to_i if max_cpu != 0 ratio = (cpu_usage * 100) / max_cpu "#{cpu_usage} / #{max_cpu} (#{ratio}%)" else "#{cpu_usage} / -" end end end column :REAL_MEM, 'Real MEM', :size => 18 do |d| max_mem = d['HOST_SHARE']['MAX_MEM'].to_i if max_mem != 0 used_mem = d['HOST_SHARE']['USED_MEM'].to_i ratio = (used_mem * 100) / max_mem "#{OpenNebulaHelper.unit_to_str(used_mem, )} / "\ "#{OpenNebulaHelper.unit_to_str(max_mem, )} "\ "(#{ratio}%)" else '-' end end column :ALLOCATED_MEM, 'Allocated MEM', :size => 18 do |d| max_mem = d['HOST_SHARE']['MAX_MEM'].to_i mem_usage = d['HOST_SHARE']['MEM_USAGE'].to_i if max_mem.zero? && mem_usage.zero? '-' elsif max_mem != 0 ratio = (mem_usage * 100) / max_mem "#{OpenNebulaHelper.unit_to_str(mem_usage, )} / "\ "#{OpenNebulaHelper.unit_to_str(max_mem, )} "\ "(#{ratio}%)" else "#{OpenNebulaHelper.unit_to_str(mem_usage, )} / -" end end column :STAT, 'Host status', :left, :size => 6 do |d| OneHostHelper.state_to_str(d['STATE']) end default :ID, :NAME, :CLUSTER, :TVM, :ALLOCATED_CPU, :ALLOCATED_MEM, :STAT end end |
#monitoring(host, attr, options) ⇒ Object
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 |
# File 'lib/one_helper/onehost_helper.rb', line 456 def monitoring(host, attr, ) unit = [:unit] || 'G' start_d = [:start] end_d = [:end] n_elems = [:n_elems] || 8 # Different available size units units = %w[K M G T] # Attrs that need units conversion attrs = %w[FREE_MEMORY USED_MEMORY] if unit && !units.include?(unit) STDERR.puts "Invalid unit `#{unit}`" exit(-1) end attr = attr.upcase start_d = Time.parse(start_d) if start_d end_d = Time.parse(end_d) if end_d # Get monitoring data from user path # # 0 -> timestamp # 1 -> data retrieved monitoring_data = host.monitoring(["#{MONITORING[attr]}/#{attr}"]) monitoring_data = monitoring_data["#{MONITORING[attr]}/#{attr}"] if monitoring_data.empty? STDERR.puts 'No monitoring data found' return end # Get data max and min date start_d ||= Time.at(monitoring_data.min {|v| v[0].to_i }[0].to_i) end_d ||= Time.at(monitoring_data.max {|v| v[0].to_i }[0].to_i) # Filter data betwen dates monitoring_data.reject! do |v| v[0].to_i < start_d.to_i || v[0].to_i > end_d.to_i end if monitoring_data.empty? STDERR.puts "No monitoring data found between #{start_d} " \ "and #{end_d}" return end start_d = start_d.strftime('%d/%m/%Y %H:%M') end_d = end_d.strftime('%d/%m/%Y %H:%M') # Parse dcollected data x = monitoring_data.collect {|v| Time.at(v[0].to_i).strftime('%H:%M') } y = monitoring_data.collect do |v| if attrs.include?(attr) # GB is the default unit v = OpenNebulaHelper.bytes_to_unit(v[1].to_i, unit).round(2) "#{v} #{unit}B" else v[1] end end title = '' title << "Host #{host.id} #{attr} " title << "in #{unit}B " if unit && attrs.include?(attr) title << "from #{start_d} to #{end_d}" x = x.last(n_elems) y = y.last(n_elems) if [:table] print_monitoring_table(x, y, title) elsif [:csv] csv = '' csv << "TIME#{[:csv]}VALUE\n" x.zip(y) {|x_v, y_v| csv << "#{x_v}#{[:csv]}#{y_v}\n" } puts csv else puts OpenNebulaHelper.get_plot(x, y, attr, title) end end |
#set_hybrid(type, path) ⇒ Object
235 236 237 238 239 240 241 242 243 |
# File 'lib/one_helper/onehost_helper.rb', line 235 def set_hybrid(type, path) k = type.to_sym return unless HYBRID.key?(k) return OpenNebulaHelper.editor_input(HYBRID[k][:help]) if path.nil? File.read(path) end |
#sync(host_ids, options) ⇒ Object
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 |
# File 'lib/one_helper/onehost_helper.rb', line 246 def sync(host_ids, ) if Process.uid.zero? || Process.gid.zero? STDERR.puts("Cannot run 'onehost sync' as root") exit(-1) end begin current_version = File.read(REMOTES_LOCATION + '/VERSION').strip rescue StandardError STDERR.puts("Could not read #{REMOTES_LOCATION}/VERSION") exit(-1) end if current_version.empty? STDERR.puts 'Remotes version can not be empty' exit(-1) end begin current_version = Gem::Version.new(current_version) rescue StandardError STDERR.puts 'VERSION file is malformed, use semantic versioning.' end cluster_id = [:cluster] rc = OpenNebula::System.new(@client).get_configuration return -1, rc. if OpenNebula.is_error?(rc) conf = rc sync_manager = HostSyncManager.new(conf) # Verify the existence of REMOTES_LOCATION if !File.directory? REMOTES_LOCATION error_msg = "'#{REMOTES_LOCATION}' does not exist. " \ 'This command must be run in the frontend.' return -1, error_msg end # Touch the update file FileUtils.touch(File.join(REMOTES_LOCATION, '.update')) # Get the Host pool filter_flag ||= OpenNebula::Pool::INFO_ALL pool = factory_pool(filter_flag) rc = pool.info return -1, rc. if OpenNebula.is_error?(rc) # Assign hosts to threads queue = [] pool.each do |host| if host_ids next unless host_ids.include?(host['ID'].to_i) elsif cluster_id next if host['CLUSTER_ID'].to_i != cluster_id end vm_mad = host['VM_MAD'].downcase state = host['STATE'] # Skip this host from remote syncing if it's a PUBLIC_CLOUD host next if host['TEMPLATE/PUBLIC_CLOUD'] == 'YES' # Skip this host from remote syncing if it's OFFLINE next if Host::HOST_STATES[state.to_i] == 'OFFLINE' # Skip this host if it is a vCenter cluster next if vm_mad == 'vcenter' host_version = host['TEMPLATE/VERSION'] begin host_version = Gem::Version.new(host_version) rescue StandardError nil end if ![:force] begin next if host_version && host_version >= current_version rescue StandardError STDERR.puts 'Error comparing versions '\ " for host #{host['NAME']}." end end puts "* Adding #{host['NAME']} to upgrade" queue << host end # Run the jobs in threads host_errors = [] queue_lock = Mutex.new error_lock = Mutex.new total = queue.length if total.zero? puts 'No hosts are going to be updated.' exit(0) end ts = (1..NUM_THREADS).map do |_t| Thread.new do loop do host = nil size = 0 queue_lock.synchronize do host = queue.shift size = queue.length end break unless host print_update_info(total - size, total, host['NAME']) retries = 3 begin copy_method = [:ssh] ? :ssh : :rsync rc = sync_manager.update_remotes(host['NAME'], nil, copy_method) rescue IOError # Workaround for broken Ruby 2.5 # https://github.com/OpenNebula/one/issues/3229 if (retries -= 1) > 0 sleep 1 retry end end if rc != 0 error_lock.synchronize do host_errors << host['NAME'] end else update_version(host, current_version) end end end end # Wait for threads to finish ts.each {|t| t.join } puts if host_errors.empty? puts 'All hosts updated successfully.' 0 else STDERR.puts 'Failed to update the following hosts:' host_errors.each {|h| STDERR.puts "* #{h}" } -1 end end |