Class: OpenNebula::Service

Inherits:
DocumentJSON show all
Defined in:
lib/models/service.rb

Overview

Service class as wrapper of DocumentJSON

Constant Summary collapse

DOCUMENT_TYPE =
100
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
}
STATE_STR =
%w[
    PENDING
    DEPLOYING
    RUNNING
    UNDEPLOYING
    WARNING
    DONE
    FAILED_UNDEPLOYING
    FAILED_DEPLOYING
    SCALING
    FAILED_SCALING
    COOLDOWN
]
TRANSIENT_STATES =
%w[
    DEPLOYING
    UNDEPLOYING
    SCALING
    COOLDOWN
]
FAILED_STATES =
%w[
    FAILED_DEPLOYING
    FAILED_UNDEPLOYING
    FAILED_SCALING
]
RECOVER_DEPLOY_STATES =
%w[
    FAILED_DEPLOYING
    DEPLOYING
    PENDING
]
RECOVER_UNDEPLOY_STATES =
%w[
    FAILED_UNDEPLOYING
    UNDEPLOYING
]
RECOVER_SCALE_STATES =
%w[
    FAILED_SCALING
    SCALING
]
IMMUTABLE_ATTRS =

List of attributes that can’t be changed in update operation

custom_attrs: it only has sense when deploying, not in running custom_attrs_values: it only has sense when deploying, not in running deployment: changing this, changes the undeploy operation log: this is just internal information, no sense to change it name: this has to be changed using rename operation networks: it only has sense when deploying, not in running networks_values: it only has sense when deploying, not in running ready_status_gate: it only has sense when deploying, not in running state: this is internal information managed by OneFlow server start_time: this is internal information managed by OneFlow server

%w[
    custom_attrs
    custom_attrs_values
    deployment
    log
    name
    networks
    networks_values
    ready_status_gate
    state
    start_time
]
LOG_COMP =
'SER'

Constants inherited from DocumentJSON

DocumentJSON::TEMPLATE_TAG

Constants inherited from Document

Document::DOCUMENT_METHODS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DocumentJSON

#allocate_xml, #build_template_xml, #load_body, #template_tag, #to_json

Methods inherited from Document

build_xml, #chmod, #chmod_octet, #clone, #delete, #document_type, #initialize, #owner_id, #public?, #rename

Methods inherited from PoolElement

#id, #name, new_with_id, #replace, #to_str

Methods inherited from XMLElement

#[], #add_element, #attr, build_xml, #delete_element, #each, #each_xpath, #element_xml, #has_elements?, #initialize, #initialize_xml, #name, #retrieve_elements, #retrieve_xmlelements, #set_content, #template_like_str, #template_str, #template_xml, #text, #to_hash, #to_xml, #xml_nil?

Constructor Details

This class inherits a constructor from OpenNebula::Document

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



22
23
24
# File 'lib/models/service.rb', line 22

def client
  @client
end

#rolesObject (readonly)

Returns the value of attribute roles.



22
23
24
# File 'lib/models/service.rb', line 22

def roles
  @roles
end

Instance Method Details

#add_role(template) ⇒ OpenNebula::Role

Adds a role to the service

Parameters:

  • template (Hash)

    Role information

Returns:



339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/models/service.rb', line 339

def add_role(template)
    template['state'] ||= Role::STATE['PENDING']
    role                = Role.new(template, self)

    if @roles[role.name]
        return OpenNebula::Error.new("Role #{role.name} already exists")
    end

    @roles[role.name] = role
    @body['roles'] << template if @body && @body['roles']

    role
end

#all_roles_done?true, false

Returns true if all the nodes are in done state

Returns:

  • (true, false)

    true if all the nodes are correctly deployed



230
231
232
233
234
235
236
237
238
# File 'lib/models/service.rb', line 230

def all_roles_done?
    @roles.each do |_name, role|
        if role.state != Role::STATE['DONE']
            return false
        end
    end

    true
end

#all_roles_running?true, false

Returns true if all the nodes are correctly deployed

Returns:

  • (true, false)

    true if all the nodes are correctly deployed



218
219
220
221
222
223
224
225
226
# File 'lib/models/service.rb', line 218

def all_roles_running?
    @roles.each do |_name, role|
        if role.state != Role::STATE['RUNNING']
            return false
        end
    end

    true
end

#allocate(template_json) ⇒ nil, OpenNebula::Error

Create a new service based on the template provided

Parameters:

  • template_json (String)

Returns:



244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/models/service.rb', line 244

def allocate(template_json)
    template = JSON.parse(template_json)
    template['state'] = STATE['PENDING']

    if template['roles']
        template['roles'].each do |elem|
            elem['state'] ||= Role::STATE['PENDING']
        end
    end

    template['start_time'] = Integer(Time.now)

    super(template.to_json, template['name'])
end

#can_recover_deploy?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/models/service.rb', line 159

def can_recover_deploy?
    RECOVER_DEPLOY_STATES.include? STATE_STR[state]
end

#can_recover_scale?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/models/service.rb', line 167

def can_recover_scale?
    RECOVER_SCALE_STATES.include? STATE_STR[state]
end

#can_recover_undeploy?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/models/service.rb', line 163

def can_recover_undeploy?
    RECOVER_UNDEPLOY_STATES.include? STATE_STR[state]
end

#can_scale?Boolean

Returns:

  • (Boolean)


618
619
620
# File 'lib/models/service.rb', line 618

def can_scale?
    state == Service::STATE['RUNNING']
end

#can_undeploy?Boolean

Return true if the service can be undeployed

Returns:

  • (Boolean)

    true if the service can be undeployed, false otherwise



142
143
144
145
146
147
148
149
150
151
# File 'lib/models/service.rb', line 142

def can_undeploy?
    # rubocop:disable Style/IfWithBooleanLiteralBranches
    if (transient_state? && state != Service::STATE['UNDEPLOYING']) ||
        state == Service::STATE['DONE'] || failed_state?
        false
    else
        true
    end
    # rubocop:enable Style/IfWithBooleanLiteralBranches
end

#can_update?Boolean

Return true if the service can be updated

Returns:

  • (Boolean)

    true if the service can be updated, false otherwise



155
156
157
# File 'lib/models/service.rb', line 155

def can_update?
    !transient_state? && !failed_state?
end

#check_new_template(template_json, append) ⇒ Boolean, String

Check that changes values are correct

Parameters:

  • template_json (String)

    New template

  • append (Boolean)

    True to append template to the current

Returns:

  • (Boolean, String)

    True, nil if everything is correct False, attr if attr was changed



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
556
557
558
559
560
561
562
# File 'lib/models/service.rb', line 530

def check_new_template(template_json, append)
    template = JSON.parse(template_json)

    if append
        IMMUTABLE_ATTRS.each do |attr|
            next if template[attr].nil?

            return [false, "service/#{attr}"]
        end
    else
        if template['roles'].size != @roles.size
            return [false, 'service/roles size']
        end

        IMMUTABLE_ATTRS.each do |attr|
            next if template[attr] == @body[attr]

            return [false, "service/#{attr}"]
        end

        template['roles'].each do |role|
            # Role name can't be changed, if it is changed some problems
            # may appear, as name is used to reference roles
            return [false, 'name'] unless @roles[role['name']]

            rc = @roles[role['name']].check_new_template(role)

            return rc unless rc[0]
        end
    end

    [true, nil]
end

#check_role(role) ⇒ Boolean

Check if role is terminated or not

Parameters:

Returns:

  • (Boolean)

    True if the service should be undeployed False otherwise



629
630
631
632
633
634
635
636
637
638
639
# File 'lib/models/service.rb', line 629

def check_role(role)
    return unless @body['automatic_deletion']

    return unless role.nodes.empty?

    ret = true

    @body['roles'].each {|r| ret &= r['nodes'].empty? }

    ret
end

#chown(uid, gid) ⇒ nil, OpenNebula::Error

Changes the owner/group

Parameters:

  • uid (Integer)

    the new owner id. Use -1 to leave the current one

  • gid (Integer)

    the new group id. Use -1 to leave the current one

Returns:



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/models/service.rb', line 403

def chown(uid, gid)
    old_uid = self['UID'].to_i
    old_gid = self['GID'].to_i

    rc = super(uid, gid)

    if OpenNebula.is_error?(rc)
        return rc
    end

    @roles.each do |_name, role|
        rc = role.chown(uid, gid)

        break if rc[0] == false
    end

    if rc[0] == false
        log_error('Chown operation failed, will try to rollback ' \
                  'all VMs to the old user and group')

        update

        super(old_uid, old_gid)

        @roles.each do |_name, role|
            role.chown(old_uid, old_gid)
        end

        return OpenNebula::Error.new(rc[1])
    end

    nil
end

#delete_networksObject



595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/models/service.rb', line 595

def delete_networks
    vnets = @body['networks_values']
    vnets_failed = []

    return if vnets.nil?

    vnets.each do |vnet|
        next unless vnet[vnet.keys[0]].key?('template_id') ||
                    vnet[vnet.keys[0]].key?('reserve_from')

        vnet_id = vnet[vnet.keys[0]]['id'].to_i

        rc = OpenNebula::VirtualNetwork
             .new_with_id(vnet_id, @client).delete

        if OpenNebula.is_error?(rc)
            vnets_failed << vnet_id
        end
    end

    vnets_failed
end

#deploy_networks(deploy = true) ⇒ Object



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/models/service.rb', line 564

def deploy_networks(deploy = true)
    if deploy
        body = JSON.parse(self['TEMPLATE/BODY'])
    else
        body = @body
    end

    return if body['networks_values'].nil?

    body['networks_values'].each do |net|
        rc = create_vnet(net) if net[net.keys[0]].key?('template_id')

        if OpenNebula.is_error?(rc)
            return rc
        end

        rc = reserve(net) if net[net.keys[0]].key?('reserve_from')

        if OpenNebula.is_error?(rc)
            return rc
        end
    end if deploy

    # Replace $attibute by the corresponding value
    resolve_attributes(body)

    # @body = template.to_hash

    update_body(body)
end

#failed_state?Boolean

Return true if the service is in failed state

Returns:

  • (Boolean)

    true if the service is in failed state, false otherwise



136
137
138
# File 'lib/models/service.rb', line 136

def failed_state?
    FAILED_STATES.include? STATE_STR[state]
end

#gidObject



187
188
189
# File 'lib/models/service.rb', line 187

def gid
    self['GID'].to_i
end

#infonil, OpenNebula::Error

Retrieves the information of the Service and all its Nodes.

Returns:



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/models/service.rb', line 315

def info
    rc = super
    if OpenNebula.is_error?(rc)
        return rc
    end

    @roles = {}

    if @body['roles']
        @body['roles'].each do |elem|
            elem['state'] ||= Role::STATE['PENDING']
            role = Role.new(elem, self)
            @roles[role.name] = role
        end
    end

    nil
end

#info_rolesnil, OpenNebula::Error

Retrieves the information of the Service and all its Nodes.

Returns:



368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/models/service.rb', line 368

def info_roles
    @roles = {}

    if @body['roles']
        @body['roles'].each do |elem|
            elem['state'] ||= Role::STATE['PENDING']
            role = Role.new(elem, self)
            @roles[role.name] = role
        end
    end

    nil
end

#log_error(message) ⇒ Object

Add an error message in the service information that will be stored

in OpenNebula

Parameters:

  • message (String)


392
393
394
# File 'lib/models/service.rb', line 392

def log_error(message)
    add_log(Logger::ERROR, message)
end

#log_info(message) ⇒ Object

Add an info message in the service information that will be stored

in OpenNebula

Parameters:

  • message (String)


385
386
387
# File 'lib/models/service.rb', line 385

def log_info(message)
    add_log(Logger::INFO, message)
end

#recovernil, OpenNebula::Error

Recover a failed service.

Returns:



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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/models/service.rb', line 262

def recover
    if [Service::STATE['FAILED_DEPLOYING']].include?(state)
        @roles.each do |_name, role|
            if role.state == Role::STATE['FAILED_DEPLOYING']
                role.set_state(Role::STATE['PENDING'])
            end
        end

        set_state(Service::STATE['DEPLOYING'])

    elsif state == Service::STATE['FAILED_SCALING']
        @roles.each do |_name, role|
            if role.state == Role::STATE['FAILED_SCALING']
                role.set_state(Role::STATE['SCALING'])
            end
        end

        set_state(Service::STATE['SCALING'])

    elsif state == Service::STATE['FAILED_UNDEPLOYING']
        @roles.each do |_name, role|
            if role.state == Role::STATE['FAILED_UNDEPLOYING']
                role.set_state(Role::STATE['RUNNING'])
            end
        end

        set_state(Service::STATE['UNDEPLOYING'])

    elsif state == Service::STATE['COOLDOWN']
        @roles.each do |_name, role|
            if role.state == Role::STATE['COOLDOWN']
                role.set_state(Role::STATE['RUNNING'])
            end
        end

        set_state(Service::STATE['RUNNING'])

    elsif state == Service::STATE['WARNING']
        @roles.each do |_name, role|
            if role.state == Role::STATE['WARNING']
                role.recover_warning
            end
        end
    else
        OpenNebula::Error.new('Action recover: Wrong state' \
                              " #{state_str}")
    end
end

#remove_role(name) ⇒ Object

Removes a role from the service

Parameters:

  • name (String)

    Role name to delete



356
357
358
359
360
361
362
# File 'lib/models/service.rb', line 356

def remove_role(name)
    @roles.delete(name)

    @body['roles'].delete_if do |role|
        role['name'] == name
    end
end

#replace_client(owner_client) ⇒ Object

Replaces this object’s client with a new one

Parameters:



193
194
195
# File 'lib/models/service.rb', line 193

def replace_client(owner_client)
    @client = owner_client
end

#report_ready?true, false

Returns the running_status_vm option

Returns:

  • (true, false)

    true if the running_status_vm option is enabled



179
180
181
# File 'lib/models/service.rb', line 179

def report_ready?
    @body['ready_status_gate']
end

#running?Boolean

Return true if the service is running

Returns:

  • (Boolean)

    true if the service is runnning, false otherwise



173
174
175
# File 'lib/models/service.rb', line 173

def running?
    state_str == 'RUNNING'
end

#set_state(state) ⇒ true, false

Sets a new state rubocop:disable Naming/AccessorMethodName

Parameters:

  • the (Integer)

    new state

Returns:

  • (true, false)

    true if the value was changed



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/models/service.rb', line 201

def set_state(state)
    # rubocop:enable Naming/AccessorMethodName
    if state < 0 || state > STATE_STR.size
        return false
    end

    @body['state'] = state.to_i

    msg = "New state: #{STATE_STR[state]}"
    Log.info LOG_COMP, msg, id
    log_info(msg)

    true
end

#shutdown_actionObject



479
480
481
# File 'lib/models/service.rb', line 479

def shutdown_action
    @body['shutdown_action']
end

#stateInteger

Returns the service state

Returns:

  • (Integer)

    the service state



112
113
114
# File 'lib/models/service.rb', line 112

def state
    @body['state'].to_i
end

#state_strObject

Returns the string representation of the service state

Returns:

  • the state string



124
125
126
# File 'lib/models/service.rb', line 124

def state_str
    STATE_STR[state]
end

#strategyString

Returns the service strategy

Returns:

  • (String)

    the service strategy



118
119
120
# File 'lib/models/service.rb', line 118

def strategy
    @body['deployment']
end

#transient_state?Boolean

Returns true if the service is in transient state

Returns:

  • (Boolean)

    true if the service is in transient state, false otherwise



130
131
132
# File 'lib/models/service.rb', line 130

def transient_state?
    TRANSIENT_STATES.include? STATE_STR[state]
end

#unameObject



183
184
185
# File 'lib/models/service.rb', line 183

def uname
    self['UNAME']
end

#update(template_json = nil, append = false) ⇒ nil, OpenNebula::Error

Replaces the template contents

Parameters:

  • template_json (String) (defaults to: nil)

    New template contents

  • append (true, false) (defaults to: false)

    True to append new attributes instead of replace the whole template

Returns:



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/models/service.rb', line 491

def update(template_json = nil, append = false)
    if template_json
        template = JSON.parse(template_json)

        if append
            rc = info

            if OpenNebula.is_error? rc
                return rc
            end

            template = @body.merge(template)
        end

        template_json = template.to_json
    end

    super(template_json, append)
end

#update_raw(template_raw, append = false) ⇒ nil, OpenNebula::Error

Replaces the raw template contents

Parameters:

  • template (String)

    New template contents, in the form KEY = VAL

  • append (true, false) (defaults to: false)

    True to append new attributes instead of replace the whole template

Returns:



519
520
521
# File 'lib/models/service.rb', line 519

def update_raw(template_raw, append = false)
    super(template_raw, append)
end

#update_role(role_name, template_json) ⇒ nil, OpenNebula::Error

Updates a role

Parameters:

  • role_name (String)
  • template_json (String)

Returns:



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/models/service.rb', line 442

def update_role(role_name, template_json)
    if ![Service::STATE['RUNNING'], Service::STATE['WARNING']]
       .include?(state)

        return OpenNebula::Error.new('Update role: Wrong state' \
                                     " #{state_str}")
    end

    template = JSON.parse(template_json)

    # TODO: Validate template?

    role = @roles[role_name]

    if role.nil?
        return OpenNebula::Error.new("ROLE \"#{role_name}\" " \
                                     'does not exist')
    end

    rc = role.update(template)

    if OpenNebula.is_error?(rc)
        return rc
    end

    # TODO: The update may not change the cardinality, only
    # the max and min vms...

    role.set_state(Role::STATE['SCALING'])

    role.set_default_cooldown_duration

    set_state(Service::STATE['SCALING'])

    update
end