Class: OneFlowHelper

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

Overview

Oneflow command helper

Class Method Summary collapse

Instance Method Summary collapse

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

.adjust_str(policy) ⇒ Object

Get policy adjust information in str format



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# 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_fileObject

Configuration file



23
24
25
# File 'lib/one_helper/oneflow_helper.rb', line 23

def self.conf_file
    'oneflow.yaml'
end

Instance Method Details

#client(options) ⇒ Object

Get client to make request



30
31
32
33
34
35
36
37
# 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) ⇒ Object

Show service detailed information



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
# 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_poolObject

Get service pool table



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object

List service pool



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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) ⇒ Object

List service pool continiously



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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