Module: Service

Defined in:
lib/opennebula/oneflow_client.rb

Defined Under Namespace

Classes: Client

Constant Summary collapse

STATE =
{
    'PENDING'                 => 0,
    'DEPLOYING'               => 1,
    'RUNNING'                 => 2,
    'UNDEPLOYING'             => 3,
    'WARNING'                 => 4,
    'DONE'                    => 5,
    'FAILED_UNDEPLOYING'      => 6,
    'FAILED_DEPLOYING'        => 7,
    'SCALING'                 => 8,
    'FAILED_SCALING'          => 9,
    'COOLDOWN'                => 10,
    'DEPLOYING_NETS'          => 11,
    'UNDEPLOYING_NETS'        => 12,
    'FAILED_DEPLOYING_NETS'   => 13,
    'FAILED_UNDEPLOYING_NETS' => 14,
    'HOLD'                    => 15
}
STATE_STR =
[
    'PENDING',
    'DEPLOYING',
    'RUNNING',
    'UNDEPLOYING',
    'WARNING',
    'DONE',
    'FAILED_UNDEPLOYING',
    'FAILED_DEPLOYING',
    'SCALING',
    'FAILED_SCALING',
    'COOLDOWN',
    'DEPLOYING_NETS',
    'UNDEPLOYING_NETS',
    'FAILED_DEPLOYING_NETS',
    'FAILED_UNDEPLOYING_NETS',
    'HOLD'
]
DEFAULT_OPTIONS =

CLI options

[
    ENDPOINT = {
        :name => "server",
        :short => "-s url",
        :large => "--server url",
        :format => String,
        :description => "Service endpoint"
    },
    USERNAME={
        :name => "username",
        :short => "-u name",
        :large => "--username name",
        :format => String,
        :description => "User name"
    },
    PASSWORD={
        :name => "password",
        :short => "-p pass",
        :large => "--password pass",
        :format => String,
        :description => "User password"
    }
]
JSON_FORMAT =
{
    :name => "json",
    :short => "-j",
    :large => "--json",
    :description => "Print the resource in JSON"
}
TOP =
{
    :name => "top",
    :short => "-t",
    :large => "--top",
    :description => "Top for the command"
}
PERIOD =
{
    :name => "period",
    :short => "-p x",
    :large => "--period x",
    :format => Integer,
    :description => "Seconds between each group of actions"
}
NUMBER =
{
    :name => "number",
    :short => "-n x",
    :large => "--number x",
    :format => Integer,
    :description => "Number of VMs to apply the action to each period"
}
FORCE =
{
    :name => "force",
    :short => "-f",
    :large => "--force",
    :description => "Force the new cardinality even if it is outside the limits"
}

Class Method Summary collapse

Class Method Details

.build_json_action(perform, params = nil) ⇒ String

Build a json specifying an action

Parameters:

  • perform (String)

    action to be performed (e.g.: shutdown)

  • params (Hash, nil) (defaults to: nil)

    contains the params for the action

Returns:

  • (String)

    json representing the action



136
137
138
139
140
141
142
143
144
145
# File 'lib/opennebula/oneflow_client.rb', line 136

def self.build_json_action(perform, params=nil)
    body = Hash.new
    body['perform'] = perform
    body['params']  = params if params

    action = Hash.new
    action['action'] = body

    JSON.pretty_generate action
end

.list_to_id(names, poolname) ⇒ Object



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
# File 'lib/opennebula/oneflow_client.rb', line 258

def self.list_to_id(names, poolname)

    client = Service::Client.new()

    resource_path = case poolname
    when "SERVICE"          then "/service"
    when "SERVICE TEMPLATE" then "/service_template"
    end

    response = client.get(resource_path)

    if CloudClient::is_error?(response)
        return -1, "OpenNebula #{poolname} name not found," <<
                   " use the ID instead"
    end

    pool = JSON.parse(response.body)

    result = names.split(',').collect { |name|
        if name.match(/^[0123456789]+$/)
            name.to_i
        else
            rc = name_to_id(name, pool, poolname)

            if rc.first == -1
                return rc[0], rc[1]
            end

            rc[1]
        end
    }

    return 0, result
end

.list_to_id_desc(poolname) ⇒ Object



293
294
295
# File 'lib/opennebula/oneflow_client.rb', line 293

def self.list_to_id_desc(poolname)
    "Comma-separated list of OpenNebula #{poolname} names or ids"
end

.name_to_id(name, pool, ename) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/opennebula/oneflow_client.rb', line 238

def self.name_to_id(name, pool, ename)
    if pool['DOCUMENT_POOL']['DOCUMENT'].nil?
        return -1, "#{ename} named #{name} not found."
    end

    objects = pool['DOCUMENT_POOL']['DOCUMENT'].select {|object| object['NAME'] == name }

    if objects.length>0
        if objects.length>1
            return -1, "There are multiple #{ename}s with name #{name}."
        else
            result = objects.first['ID']
        end
    else
        return -1, "#{ename} named #{name} not found."
    end

    return 0, result
end

.perform_action(id, &block) ⇒ Integer

Perform an action on a resource

Parameters:

  • id (Integer)

    resource id

  • block (Block)

    action to be performed

Returns:

  • (Integer)

    exit_code



320
321
322
323
324
325
326
327
328
329
330
# File 'lib/opennebula/oneflow_client.rb', line 320

def self.perform_action(id, &block)
    exit_code = 0
    response  = block.call(id) if block_given?

    if CloudClient::is_error?(response)
        puts response.to_s
        exit_code = response.code.to_i
    end

    exit_code
end

.perform_actions(ids, &block) ⇒ Integer

Perform an action on several resources

Parameters:

  • ids (Array)

    resources ids

  • block (Block)

    action to be performed

Returns:

  • (Integer)

    exit_code



301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/opennebula/oneflow_client.rb', line 301

def self.perform_actions(ids, &block)
    exit_code = 0

    ids.each do |id|
        response = block.call(id) if block_given?

        if CloudClient::is_error?(response)
            puts response.to_s
            exit_code = response.code.to_i
        end
    end

    exit_code
end

.rname_to_id(name, poolname) ⇒ Object

def self.rname_to_id(name, poolname, options)



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/opennebula/oneflow_client.rb', line 213

def self.rname_to_id(name, poolname)
    return 0, name.to_i if name.match(/^[0123456789]+$/)

    client = Service::Client.new()

    resource_path = case poolname
    when "SERVICE"          then "/service"
    when "SERVICE TEMPLATE" then "/service_template"
    end

    response = client.get(resource_path)

    if CloudClient::is_error?(response)
        return -1, "OpenNebula #{poolname} name not found," <<
                   " use the ID instead"
    end

    pool = JSON.parse(response.body)
    name_to_id(name, pool, poolname)
end

.rname_to_id_desc(poolname) ⇒ Object



234
235
236
# File 'lib/opennebula/oneflow_client.rb', line 234

def self.rname_to_id_desc(poolname)
    "OpenNebula #{poolname} name or id"
end

.state_str(state_number) ⇒ Object

Returns the string representation of the service state

Parameters:

  • state (String)

    String number representing the state

Returns:

  • the state string



128
129
130
# File 'lib/opennebula/oneflow_client.rb', line 128

def self.state_str(state_number)
    return STATE_STR[state_number.to_i]
end