class OneZoneHelper

Constants

SERVER_ENDPOINT
SERVER_NAME

Public Class Methods

conf_file() click to toggle source
# File lib/one_helper/onezone_helper.rb, line 498
def self.conf_file
    "onezone.yaml"
end
rname() click to toggle source
# File lib/one_helper/onezone_helper.rb, line 494
def self.rname
    "ZONE"
end
state_to_str(id) click to toggle source
# File lib/one_helper/onezone_helper.rb, line 502
def self.state_to_str(id)
    id        = id.to_i
    state_str = Zone::ZONE_STATES[id]

    Zone::SHORT_ZONE_STATES[state_str]
end

Public Instance Methods

format_pool(options) click to toggle source
# File lib/one_helper/onezone_helper.rb, line 509
def format_pool(options)
    config_file = self.class.table_conf

    table = 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

    table
end
get_fed_index(endpoint) click to toggle source
# File lib/one_helper/onezone_helper.rb, line 544
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
set_zone(zone_id, temporary_zone) click to toggle source
# File lib/one_helper/onezone_helper.rb, line 559
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(ENV['HOME']+"/.one/one_endpoint", 'w'){|f|
            f.puts zone['TEMPLATE/ENDPOINT']
        }
        puts "Endpoint changed to \"#{zone['TEMPLATE/ENDPOINT']}\" in " <<
            "#{ENV['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(ENV['HOME'] + '/.one/oneflow_endpoint', 'w') do |f|
            f.puts zone['TEMPLATE/ONEFLOW_ENDPOINT']
        end

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

    0
end
show_resource(id, options) click to toggle source
# File lib/one_helper/onezone_helper.rb, line 469
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

Private Instance Methods

factory(id=nil) click to toggle source
# File lib/one_helper/onezone_helper.rb, line 601
def factory(id=nil)
    if id
        OpenNebula::Zone.new_with_id(id, @client)
    else
        xml=OpenNebula::Zone.build_xml
        OpenNebula::Zone.new(xml, @client)
    end
end
factory_pool(user_flag=-2) click to toggle source
# File lib/one_helper/onezone_helper.rb, line 610
def factory_pool(user_flag=-2)
    OpenNebula::ZonePool.new(@client)
end
format_resource(zone, options = {}) click to toggle source
# File lib/one_helper/onezone_helper.rb, line 614
def format_resource(zone, options = {})
    str="%-18s: %-20s"
    str_h1="%-80s"

    CLIHelper.print_header(str_h1 % "ZONE #{zone['ID']} INFORMATION")
    puts str % ["ID",   zone.id.to_s]
    puts str % ["NAME", zone.name]
    puts str % ["STATE",zone.state_str]
    puts

    zone_hash=zone.to_hash

    if zone.has_elements?("/ZONE/SERVER_POOL/SERVER")

        puts
        CLIHelper.print_header(str_h1 % "ZONE SERVERS",false)

        CLIHelper::ShowTable.new(nil, self) do

            column :"ID", "", :size=>2 do |d|
                d["ID"] if !d.nil?
            end

            column :"NAME", "", :left, :size=>15 do |d|
                d["NAME"] if !d.nil?
            end

            column :"ENDPOINT", "", :left, :size=>63 do |d|
                d["ENDPOINT"] if !d.nil?
            end
        end.show([zone_hash['ZONE']['SERVER_POOL']['SERVER']].flatten, {})

        puts
        CLIHelper.print_header(str_h1 % "HA & FEDERATION SYNC STATUS",false)

        CLIHelper::ShowTable.new(nil, self) do

            column :"ID", "", :size=>2 do |d|
                d["ID"] if !d.nil?
            end

            column :"NAME", "", :left, :size=>15 do |d|
                d["NAME"] if !d.nil?
            end

            column :"STATE", "", :left, :size=>10 do |d|
                d["STATE"] = case d["STATE"]
                    when "0" then "solo"
                    when "1" then "candidate"
                    when "2" then "follower"
                    when "3" then "leader"
                    else "error"
                end
                d["STATE"] if !d.nil?
            end

            column :"TERM", "", :left, :size=>10 do |d|
                d["TERM"] if !d.nil?
            end

            column :"INDEX", "", :left, :size=>10 do |d|
                d["LOG_INDEX"] if !d.nil?
            end

            column :"COMMIT", "", :left, :size=>10 do |d|
                d["COMMIT"] if !d.nil?
            end

            column :"VOTE", "", :left, :size=>5 do |d|
                d["VOTEDFOR"] if !d.nil?
            end

            column :"FED_INDEX", "", :left, :size=>10 do |d|
                d["FEDLOG_INDEX"] if !d.nil?
            end

        end.show([zone_hash['ZONE']['SERVER_POOL']['SERVER']].flatten, {})
    end

    puts

    CLIHelper.print_header(str_h1 % "ZONE TEMPLATE", false)
    puts zone.template_str
end