Class: OneZoneHelper

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

Overview

Helper class for Zone commands

Constant Summary collapse

SERVER_NAME =
{
    :name => 'server_name',
    :short => '-n server_name',
    :large => '--name',
    :format => String,
    :description => 'Zone server name'
}
SERVER_ENDPOINT =
{
    :name => 'server_rpc',
    :short => '-r xml-rpc endpoint',
    :large => '--rpc',
    :format => String,
    :description => 'Zone server XML-RPC endpoint'
}
SERVER_ENDPOINT_GRPC =
{
    :name => 'server_grpc',
    :short => '-g gRPC endpoint',
    :large => '--grpc-endpoint',
    :format => String,
    :description => 'Zone server gRPC endpoint'
}

Instance Attribute Summary

Attributes inherited from OpenNebulaHelper::OneHelper

#client

Class Method Summary collapse

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, #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_fileObject



497
498
499
# File 'lib/one_helper/onezone_helper.rb', line 497

def self.conf_file
    'onezone.yaml'
end

.rnameObject



493
494
495
# File 'lib/one_helper/onezone_helper.rb', line 493

def self.rname
    'ZONE'
end

.state_to_str(id) ⇒ Object



501
502
503
504
505
506
# File 'lib/one_helper/onezone_helper.rb', line 501

def self.state_to_str(id)
    id        = id.to_i
    state_str = Zone::ZONE_STATES[id]

    Zone::SHORT_ZONE_STATES[state_str]
end

Instance Method Details

#format_pool(_options) ⇒ Object



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
# File 'lib/one_helper/onezone_helper.rb', line 524

def format_pool(_options)
    config_file = self.class.table_conf

    CLIHelper::ShowTable.new(config_file, self) do
        column :CURRENT, 'Active Zone', :size=>1 do |d|
            '*' if helper.client.one_endpoint.strip ==
                   d['TEMPLATE']['ENDPOINT'].strip
        end

        column :ID, 'ONE identifier for the Zone', :size=>5 do |d|
            d['ID']
        end

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

        column :ENDPOINT, 'Endpoint of the Zone', :left, :size=>45 do |d|
            d['TEMPLATE']['ENDPOINT']
        end

        column :FED_INDEX, 'Federation index', :left, :size=>10 do |d|
            helper.get_fed_index(d['TEMPLATE']['ENDPOINT'])
        end

        column :STAT, 'Zone status', :left, :size => 6 do |d|
            OneZoneHelper.state_to_str(d['STATE'])
        end

        default :CURRENT, :ID, :NAME, :ENDPOINT, :FED_INDEX, :STAT
    end
end

#get_fed_index(endpoint) ⇒ Object



557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/one_helper/onezone_helper.rb', line 557

def get_fed_index(endpoint)
    client = OpenNebula::Client.new(nil, endpoint, :timeout => 5)
    xml    = client.call('zone.raftstatus')

    return '-' if OpenNebula.is_error?(xml)

    xml    = Nokogiri::XML(xml)

    if xml.xpath('RAFT/FEDLOG_INDEX')
        xml.xpath('RAFT/FEDLOG_INDEX').text
    else
        '-'
    end
end

#retrieve_server_id(zone_id, id) ⇒ Object



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/one_helper/onezone_helper.rb', line 508

def retrieve_server_id(zone_id, id)
    return [0, id.to_i] if id =~ /\A\d+\z/

    zone = retrieve_resource(zone_id)
    zone.info

    ids = zone.retrieve_elements(
        "/ZONE/SERVER_POOL/SERVER[NAME='#{id}']/ID"
    )

    return [-1, "#{id} not found or duplicated"] \
            if ids.nil? || ids.size > 1

    [0, ids[0].to_i]
end

#set_zone(zone_id, temporary_zone) ⇒ Object



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
# File 'lib/one_helper/onezone_helper.rb', line 572

def set_zone(zone_id, temporary_zone)
    zone = factory(zone_id)
    rc = zone.info

    if OpenNebula.is_error?(rc)
        return -1, rc.message
    end

    if !zone['TEMPLATE/ENDPOINT']
        return -1, "No Endpoint defined for Zone #{zone_id}"
    end

    if temporary_zone
        puts "Type: export ONE_XMLRPC=#{zone['TEMPLATE/ENDPOINT']}"
    else
        File.open(Dir.home+'/.one/one_endpoint', 'w') do |f|
            f.puts zone['TEMPLATE/ENDPOINT']
        end
        puts "Endpoint changed to \"#{zone['TEMPLATE/ENDPOINT']}\" in " <<
            "#{Dir.home}/.one/one_endpoint"
    end

    return 0 unless zone['TEMPLATE/ONEFLOW_ENDPOINT']

    # Set ONEFLOW_ENDPOINT
    if temporary_zone
        puts "Type: export ONEFLOW_URL=#{zone['TEMPLATE/ONEFLOW_ENDPOINT']}"
    else
        File.open(Dir.home + '/.one/oneflow_endpoint', 'w') do |f|
            f.puts zone['TEMPLATE/ONEFLOW_ENDPOINT']
        end

        puts 'OneFlow Endpoint changed to ' \
             "\"#{zone['TEMPLATE/ONEFLOW_ENDPOINT']}\" in " <<
             "#{Dir.home}/.one/oneflow_endpoint"
    end

    0
end

#show_resource(id, options) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/one_helper/onezone_helper.rb', line 468

def show_resource(id, options)
    resource = retrieve_resource(id)

    rc = resource.info_extended
    return -1, rc.message if OpenNebula.is_error?(rc)

    if options[:xml]
        return 0, resource.to_xml(true)
    elsif options[:json]
        # If body is set, the resource contains a JSON inside
        if options[:body]
            return 0, check_resource_xsd(resource)
        else
            return 0, ::JSON.pretty_generate(
                check_resource_xsd(resource)
            )
        end
    elsif options[:yaml]
        return 0, check_resource_xsd(resource).to_yaml(:indent => 4)
    else
        format_resource(resource, options)
        return 0
    end
end