class OneFlowHelper

Oneflow command helper

Public Class Methods

adjust_str(policy) click to toggle source

Get policy adjust information in str format

@param policy [Hash] Policy information

# File lib/one_helper/oneflow_helper.rb, line 185
def self.adjust_str(policy)
    policy['adjust'].to_i >= 0 ? sign = '+' : sign = '-'
    adjust = policy['adjust'].to_i.abs

    case policy['type']
    when 'CARDINALITY'
        "= #{adjust}"
    when 'PERCENTAGE_CHANGE'
        st = "#{sign} #{adjust} %"

        if policy['min_adjust_step']
            st << " (#{policy['min_adjust_step']})"
        end

        st
    else
        "#{sign} #{adjust}"
    end
end
conf_file() click to toggle source

Configuration file

# File lib/one_helper/oneflow_helper.rb, line 23
def self.conf_file
    'oneflow.yaml'
end

Public Instance Methods

client(options) click to toggle source

Get client to make request

@options [Hash] CLI options

# File lib/one_helper/oneflow_helper.rb, line 30
def client(options)
    Service::Client.new(
        :username => options[:username],
        :password => options[:password],
        :url => options[:server],
        :user_agent => USER_AGENT
    )
end
format_resource(client, service, options) click to toggle source

Show service detailed information

@param client [Service::Client] Petition client @param service [Integer] Service ID @param options [Hash] CLI options

# File lib/one_helper/oneflow_helper.rb, line 145
def format_resource(client, service, options)
    response = client.get("#{RESOURCE_PATH}/#{service}")

    if CloudClient.is_error?(response)
        [response.code.to_i, response.to_s]
    else
        if options[:json]
            [0, response.body]
        elsif options[:yaml]
            [0, JSON.parse(response.body).to_yaml(:indent => 4)]
        else
            str_h1   = '%-80s'
            document = JSON.parse(response.body)['DOCUMENT']
            template = document['TEMPLATE']['BODY']

            CLIHelper.print_header(
                str_h1 % "SERVICE #{document['ID']} INFORMATION"
            )

            print_service_info(document)

            print_roles_info(template['roles'])

            return 0 unless template['log']

            CLIHelper.print_header(str_h1 % 'LOG MESSAGES', false)

            template['log'].each do |log|
                t = Time.at(log['timestamp']).strftime('%m/%d/%y %H:%M')
                puts "#{t} [#{log['severity']}] #{log['message']}"
            end

            0
        end
    end
end
format_service_pool() click to toggle source

Get service pool table

# File lib/one_helper/oneflow_helper.rb, line 40
def format_service_pool
    config_file = self.class.table_conf

    CLIHelper::ShowTable.new(config_file, self) do
        column :ID, 'ID', :size => 10 do |d|
            d['ID']
        end

        column :USER, 'Username', :left, :size => 15 do |d|
            d['UNAME']
        end

        column :GROUP, 'Group', :left, :size => 15 do |d|
            d['GNAME']
        end

        column :NAME, 'Name', :expand => true, :left => true do |d|
            d['NAME']
        end

        column :STARTTIME, 'Start time of the Service', :size => 15 do |d|
            d.extend(CLIHelper::HashWithSearch)
            d = d.dsearch('TEMPLATE/BODY')

            OpenNebulaHelper.time_to_str(d['start_time'])
        end

        column :STAT, 'State', :size => 11, :left => true do |d|
            Service.state_str(d['TEMPLATE']['BODY']['state'])
        end

        default :ID, :USER, :GROUP, :NAME, :STARTTIME, :STAT
    end
end
list_service_pool(client, options) click to toggle source

List service pool

@param client [Service::Client] Petition client @param options [Hash] CLI options

# File lib/one_helper/oneflow_helper.rb, line 79
def list_service_pool(client, options)
    response = client.get(RESOURCE_PATH)

    if CloudClient.is_error?(response)
        [response.code.to_i, response.to_s]
    elsif options[:yaml]
        [0, JSON.parse(response.body).to_yaml(:indent => 4)]
    else
        array_list = JSON.parse(response.body)
        array_list = array_list['DOCUMENT_POOL']['DOCUMENT']

        array_list = [] if array_list.nil?

        unless options.key? :done
            # remove from list flows in DONE state
            array_list.reject! do |value|
                value['TEMPLATE']['BODY']['state'] == 5
            end
        end

        if options[:json]
            if array_list.empty?
                0
            else
                [0, JSON.pretty_generate(array_list)]
            end
        elsif options[:yaml]
            [0, array_list.to_yaml(:indent => 4)]
        else
            format_service_pool.show(array_list)

            0
        end
    end
end
top_service_pool(client, options) click to toggle source

List service pool continiously

@param client [Service::Client] Petition client @param options [Hash] CLI options

# File lib/one_helper/oneflow_helper.rb, line 119
def top_service_pool(client, options)
    # TODO: make default delay configurable
    options[:delay] ? delay = options[:delay] : delay = 4

    begin
        loop do
            CLIHelper.scr_cls
            CLIHelper.scr_move(0, 0)

            list_service_pool(client, options)

            sleep delay
        end
    rescue StandardError => e
        STDERR.puts e.message
        exit(-1)
    end

    0
end

Private Instance Methods

format_node_pool() click to toggle source

Get nodes pool table

# File lib/one_helper/oneflow_helper.rb, line 208
def format_node_pool
    # TODO: config file
    CLIHelper::ShowTable.new(nil, self) do
        column :VM_ID,
               'ONE identifier for Virtual Machine',
               :size => 6 do |d|
            st = ''

            if d['scale_up']
                st << '\u2191 '
            elsif d['disposed']
                st << '\u2193 '
            end

            if d['vm_info'].nil?
                st << d['deploy_id'].to_s
            else
                st << d['vm_info']['VM']['ID']
            end

            st
        end

        column :NAME,
               'Name of the Virtual Machine',
               :left,
               :size => 24 do |d|
            if !d['vm_info'].nil?
                if d['vm_info']['VM']['RESCHED'] == '1'
                    "*#{d['NAME']}"
                else
                    d['vm_info']['VM']['NAME']
                end
            else
                ''
            end
        end

        column :USER,
               'Username of the Virtual Machine owner',
               :left,
               :size => 15 do |d|
            if !d['vm_info'].nil?
                d['vm_info']['VM']['UNAME']
            else
                ''
            end
        end

        column :GROUP,
               'Group of the Virtual Machine',
               :left,
               :size => 15 do |d|
            if !d['vm_info'].nil?
                d['vm_info']['VM']['GNAME']
            else
                ''
            end
        end

        default :VM_ID, :NAME, :USER, :GROUP
    end
end
print_elasticity_info(role) click to toggle source

Print role elasticity info

@param role [OpenNebula::Role] Role information

print_roles_info(roles) click to toggle source

Print service roles information

@param roles [Array] Service roles information

print_scheduled_info(role) click to toggle source

Print role schedule info

@param role [OpenNebula::Role] Role information

print_service_info(document) click to toggle source

Print service information

@param document [Hash] Service document information