Class: OpenNebula::Role

Inherits:
Object
  • Object
show all
Defined in:
lib/models/role.rb

Overview

Service Role class

Constant Summary collapse

SCHEDULE_ACTIONS =

Actions that can be performed on the VMs of a given Role

%w[
    terminate
    terminate-hard
    undeploy
    undeploy-hard
    hold
    release
    stop
    suspend
    resume
    reboot
    reboot-hard
    poweroff
    poweroff-hard
    snapshot-create
    snapshot-revert
    snapshot-delete
    disk-snapshot-create
    disk-snapshot-revert
    disk-snapshot-delete
]
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,
    'HOLD'               => 11
}
STATE_STR =
%w[
    PENDING
    DEPLOYING
    RUNNING
    UNDEPLOYING
    WARNING
    DONE
    FAILED_UNDEPLOYING
    FAILED_DEPLOYING
    SCALING
    FAILED_SCALING
    COOLDOWN
    HOLD
]
RECOVER_DEPLOY_STATES =
%w[
    FAILED_DEPLOYING
    DEPLOYING
    PENDING
]
RECOVER_UNDEPLOY_STATES =
%w[
    FAILED_UNDEPLOYING
    UNDEPLOYING
]
RECOVER_SCALE_STATES =
%w[
    FAILED_SCALING
    SCALING
]
VM_FAILURE_STATES =
%w[
    BOOT_FAILURE
    BOOT_MIGRATE_FAILURE
    PROLOG_MIGRATE_FAILURE
    PROLOG_FAILURE
    EPILOG_FAILURE
    EPILOG_STOP_FAILURE
    EPILOG_UNDEPLOY_FAILURE
    PROLOG_MIGRATE_POWEROFF_FAILURE
    PROLOG_MIGRATE_SUSPEND_FAILURE
    PROLOG_MIGRATE_UNKNOWN_FAILURE
    BOOT_UNDEPLOY_FAILURE
    BOOT_STOPPED_FAILURE
    PROLOG_RESUME_FAILURE
    PROLOG_UNDEPLOY_FAILURE
]
SCALE_WAYS =
{
    'UP'   => 0,
    'DOWN' => 1
}
IMMUTABLE_ATTRS =

List of attributes that can’t be changed in update operation cardinality: this is internal information managed by OneFlow server last_vmname: this is internal information managed by OneFlow server nodes: this is internal information managed by OneFlow server parents: this has only sense in deploy operation state: this is internal information managed by OneFlow server vm_template: this will affect scale operation

%w[
    cardinality
    last_vmname
    nodes
    parents
    state
    vm_template
]
VM_INFO =

VM information to save in document

%w[ID UID GID UNAME GNAME NAME]
LOG_COMP =
'ROL'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, service) ⇒ Role

Returns a new instance of Role.



146
147
148
149
150
151
152
153
# File 'lib/models/role.rb', line 146

def initialize(body, service)
    @body     = body
    @service  = service

    @body['cooldown'] = @@default_cooldown if @body['cooldown'].nil?
    @body['nodes']    ||= []
    @body['on_hold']  = false if @body['on_hold'].nil?
end

Instance Attribute Details

#serviceObject (readonly)

Returns the value of attribute service.



32
33
34
# File 'lib/models/role.rb', line 32

def service
  @service
end

Class Method Details

.init_default_cooldown(default_cooldown) ⇒ Object

rubocop:disable Style/ClassVars



699
700
701
# File 'lib/models/role.rb', line 699

def self.init_default_cooldown(default_cooldown)
    @@default_cooldown = default_cooldown
end

.init_default_shutdown(shutdown_action) ⇒ Object



703
704
705
# File 'lib/models/role.rb', line 703

def self.init_default_shutdown(shutdown_action)
    @@default_shutdown = shutdown_action
end

.init_default_vm_name_template(vm_name_template) ⇒ Object



711
712
713
# File 'lib/models/role.rb', line 711

def self.init_default_vm_name_template(vm_name_template)
    @@vm_name_template = vm_name_template
end

.init_force_deletion(force_deletion) ⇒ Object



707
708
709
# File 'lib/models/role.rb', line 707

def self.init_force_deletion(force_deletion)
    @@force_deletion = force_deletion
end

.vm_failure?(vm_state, lcm_state) ⇒ true, false

Returns true if the VM state is failure

Parameters:

  • vm_state (Integer)

    VM state

  • lcm_state (Integer)

    VM LCM state

Returns:

  • (true, false)

    True if the lcm state is one of *_FAILURE



686
687
688
689
690
691
692
693
694
695
696
# File 'lib/models/role.rb', line 686

def self.vm_failure?(vm_state, lcm_state)
    vm_state_str  = VirtualMachine::VM_STATE[vm_state.to_i]
    lcm_state_str = VirtualMachine::LCM_STATE[lcm_state.to_i]

    if vm_state_str == 'ACTIVE' &&
       VM_FAILURE_STATES.include?(lcm_state_str)
        return true
    end

    false
end

Instance Method Details

#any_parent_on_hold?Boolean

Returns:

  • (Boolean)


211
212
213
214
215
216
217
218
# File 'lib/models/role.rb', line 211

def any_parent_on_hold?
    parents.each do |parent|
        next unless @service.roles[parent]

        return true if @service.roles[parent].on_hold?
    end
    false
end

#batch_action(action, period, vms_per_period, args) ⇒ Object

Schedule the given action on all the VMs that belong to the Role

Parameters:

  • action (String)

    one of the available SCHEDULE_ACTIONS

  • period (Integer)
  • vm_per_period (Integer)
  • action (String)

    arguments



625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'lib/models/role.rb', line 625

def batch_action(action, period, vms_per_period, args)
    vms_id      = []
    error_msgs  = []
    nodes       = @body['nodes']
    now         = Time.now.to_i
    time_offset = 0

    # if role is done, return error
    if state == 5
        return OpenNebula::Error.new("Role #{name} is in DONE state")
    end

    do_offset = (!period.nil? && period.to_i > 0 &&
        !vms_per_period.nil? && vms_per_period.to_i > 0)

    nodes.each_with_index do |node, index|
        vm_id = node['deploy_id']
        vm = OpenNebula::VirtualMachine.new_with_id(vm_id,
                                                    @service.client)

        if do_offset
            offset = (index / vms_per_period.to_i).floor
            time_offset = offset * period.to_i
        end

        tmp_str = 'SCHED_ACTION = ['
        tmp_str << "ACTION = #{action},"
        tmp_str << "ARGS = \"#{args}\"," if args
        tmp_str << "TIME = #{now + time_offset}]"

        rc = vm.sched_action_add(tmp_str)
        if OpenNebula.is_error?(rc)
            msg = "Role #{name} : VM #{vm_id} error scheduling "\
                    "action; #{rc.message}"

            error_msgs << msg

            Log.error LOG_COMP, msg, @service.id

            @service.log_error(msg)
        else
            vms_id << vm.id
        end
    end

    log_msg = "Action:#{action} scheduled on Role:#{name}"\
              "VMs:#{vms_id.join(',')}"

    Log.info LOG_COMP, log_msg, @service.id

    return [true, log_msg] if error_msgs.empty?

    error_msgs << log_msg

    [false, error_msgs.join('\n')]
end

#can_recover_deploy?Boolean

Returns:

  • (Boolean)


165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/models/role.rb', line 165

def can_recover_deploy?
    if state != STATE['PENDING']
        return RECOVER_DEPLOY_STATES.include? STATE_STR[state]
    end

    parents.each do |parent|
        next unless @service.roles[parent]

        return false if @service.roles[parent].state != STATE['RUNNING']
    end

    true
end

#can_recover_scale?Boolean

Returns:

  • (Boolean)


195
196
197
198
199
# File 'lib/models/role.rb', line 195

def can_recover_scale?
    return false unless RECOVER_SCALE_STATES.include? STATE_STR[state]

    true
end

#can_recover_undeploy?Boolean

Returns:

  • (Boolean)


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/models/role.rb', line 179

def can_recover_undeploy?
    if !RECOVER_UNDEPLOY_STATES.include? STATE_STR[state]
        # TODO, check childs if !empty? check if can be undeployed
        @service.roles.each do |role_name, role|
            next if role_name == name

            if role.parents.include?(name) &&
               role.state != STATE['DONE']
                return false
            end
        end
    end

    true
end

#can_release?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/models/role.rb', line 201

def can_release?
    state == STATE['HOLD']
end

#cardinalityInteger

Returns the role cardinality

Returns:

  • (Integer)

    the role cardinality



222
223
224
# File 'lib/models/role.rb', line 222

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

#check_new_template(template) ⇒ Boolean, String

Check that changes values are correct

Parameters:

  • template_json (String)

    New template

Returns:

  • (Boolean, String)

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



758
759
760
761
762
763
764
765
766
# File 'lib/models/role.rb', line 758

def check_new_template(template)
    IMMUTABLE_ATTRS.each do |attr|
        next if template[attr] == @body[attr]

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

    [true, nil]
end

#chown(uid, gid) ⇒ Array<true, nil>, Array<false, String>

Changes the owner/group of all the nodes in this role

Parameters:

  • uid (Integer)

    the new owner id. Set to -1 to leave the current

  • gid (Integer)

    the new group id. Set to -1 to leave the current

Returns:

  • (Array<true, nil>, Array<false, String>)

    true if all the VMs were updated, false and the error reason if there was a problem updating the VMs



552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/models/role.rb', line 552

def chown(uid, gid)
    nodes.each do |node|
        vm_id = node['deploy_id']

        Log.debug LOG_COMP,
                  "Role #{name} : Chown for VM #{vm_id}",
                  @service.id

        vm = OpenNebula::VirtualMachine.new_with_id(vm_id,
                                                    @service.client)
        rc = vm.chown(uid, gid)

        if OpenNebula.is_error?(rc)
            msg = "Role #{name} : Chown failed for VM #{vm_id}; " \
                  "#{rc.message}"

            Log.error LOG_COMP, msg, @service.id
            @service.log_error(msg)

            return [false, rc.message]
        else
            Log.debug LOG_COMP,
                      "Role #{name} : Chown success for VM #{vm_id}",
                      @service.id
        end
    end

    [true, nil]
end

#clean_scale_wayObject



372
373
374
# File 'lib/models/role.rb', line 372

def clean_scale_way
    @body.delete('scale_way')
end

#cooldownObject



330
331
332
# File 'lib/models/role.rb', line 330

def cooldown
    @body['cooldown']
end

#deleteArray<true, nil>

Delete all the nodes in this role

Returns:

  • (Array<true, nil>)

    All the VMs are deleted, and the return ignored



540
541
542
# File 'lib/models/role.rb', line 540

def delete
    raise 'role.delete is not defined'
end

#deployArray<true, nil>, Array<false, String>

Deploys all the nodes in this role were created, false and the error reason if there was a problem creating the VMs

Returns:

  • (Array<true, nil>, Array<false, String>)

    true if all the VMs



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
436
437
438
439
440
441
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/models/role.rb', line 406

def deploy
    deployed_nodes = []
    n_nodes = cardinality - nodes.size

    return [deployed_nodes, nil] if n_nodes == 0

    @body['last_vmname'] ||= 0

    template_id = @body['vm_template']
    template    = OpenNebula::Template.new_with_id(template_id,
                                                   @service.client)

    if @body['vm_template_contents']
        extra_template = @body['vm_template_contents'].dup

        # If the extra_template contains APPEND="<attr1>,<attr2>", it
        # will add the attributes that already exist in the template,
        # instead of replacing them.
        append = extra_template
                 .match(/^\s*APPEND=\"?(.*?)\"?\s*$/)[1]
                 .split(',') rescue nil

        if append && !append.empty?
            rc = template.info

            if OpenNebula.is_error?(rc)
                msg = "Role #{name} : Info template #{template_id};" \
                       " #{rc.message}"

                Log.error LOG_COMP, msg, @service.id
                @service.log_error(msg)

                return [false, 'Error fetching Info to instantiate' \
                               " VM Template #{template_id} in Role " \
                               "#{name}: #{rc.message}"]
            end

            et = template.template_like_str('TEMPLATE',
                                            true,
                                            append.join('|'))

            et = et << "\n" << extra_template

            extra_template = et
        end
    else
        extra_template = ''
    end

    extra_template << "\nSERVICE_ID = #{@service.id}"
    extra_template << "\nROLE_NAME = \"#{@body['name']}\""

    n_nodes.times do
        vm_name = @@vm_name_template
                  .gsub('$SERVICE_ID', @service.id.to_s)
                  .gsub('$SERVICE_NAME', @service.name.to_s)
                  .gsub('$ROLE_NAME', name.to_s)
                  .gsub('$VM_NUMBER', @body['last_vmname'].to_s)

        @body['last_vmname'] += 1

        Log.debug LOG_COMP,
                  "Role #{name} : Trying to instantiate " \
                  "template #{template_id}, with name #{vm_name}",
                  @service.id

        vm_id = template.instantiate(vm_name, on_hold?, extra_template)

        deployed_nodes << vm_id

        if OpenNebula.is_error?(vm_id)
            msg = "Role #{name} : Instantiate failed for template " \
                  "#{template_id}; #{vm_id.message}"

            Log.error LOG_COMP, msg, @service.id
            @service.log_error(msg)

            return [false, 'Error trying to instantiate the VM ' \
                           "Template #{template_id} in Role " \
                           "#{name}: #{vm_id.message}"]
        end

        Log.debug LOG_COMP, "Role #{name} : Instantiate success," \
                            " VM ID #{vm_id}", @service.id
        node = {
            'deploy_id' => vm_id
        }

        vm = OpenNebula::VirtualMachine.new_with_id(vm_id,
                                                    @service.client)
        rc = vm.info

        if OpenNebula.is_error?(rc)
            node['vm_info'] = nil
        else
            hash_vm       = vm.to_hash['VM']
            vm_info       = {}
            vm_info['VM'] = hash_vm.select {|v| VM_INFO.include?(v) }

            node['vm_info'] = vm_info
        end

        @body['nodes'] << node
    end

    [deployed_nodes, nil]
end

#elasticity_policiesObject



314
315
316
# File 'lib/models/role.rb', line 314

def elasticity_policies
    @body['elasticity_policies']
end

#hold(hold) ⇒ Object

Set the on_hold vm option to true



389
390
391
# File 'lib/models/role.rb', line 389

def hold(hold)
    @body['on_hold'] = hold
end

#infonil, OpenNebula::Error

Retrieves the VM information for each Node in this Role. If a Node is to be disposed and it is found in DONE, it will be cleaned

Returns:



398
399
400
# File 'lib/models/role.rb', line 398

def info
    raise 'role.info is not defined'
end

#info_nodes(vm_pool) ⇒ Object



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
# File 'lib/models/role.rb', line 279

def info_nodes(vm_pool)
    ret = []

    monitoring = vm_pool[:monitoring]
    vm_pool    = vm_pool[:vm_pool]

    @body['nodes'].each do |node|
        id = node['deploy_id']
        vm = vm_pool.retrieve_xmlelements("/VM_POOL/VM[ID=#{id}]")[0]

        if vm.nil?
            Log.error LOG_COMP,
                      "Error getting VM #{id}",
                      @service.id
        else
            obj = {}
            obj['deploy_id'] = node['deploy_id']

            hash     = vm.to_hash
            vm_monit = monitoring.select {|v| v['ID'].to_i == id }[0]

            hash['VM']['MONITORING'] = vm_monit if vm_monit
            obj['vm_info']           = hash

            ret << obj
        end
    end

    ret
end

#max_cardinalityInteger?

Returns the role max cardinality

Returns:

  • (Integer, nil)

    the role cardinality or nil if it isn’t defined



249
250
251
252
253
254
255
# File 'lib/models/role.rb', line 249

def max_cardinality
    max = @body['max_vms']

    return if max.nil?

    max.to_i
end

#min_cardinalityInteger?

Returns the role min cardinality

Returns:

  • (Integer, nil)

    the role cardinality or nil if it isn’t defined



259
260
261
262
263
264
265
# File 'lib/models/role.rb', line 259

def min_cardinality
    min = @body['min_vms']

    return if min.nil?

    min.to_i
end

#nameObject



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

def name
    @body['name']
end

#nodesArray

Returns the nodes of the role

Returns:

  • (Array)

    the nodes



275
276
277
# File 'lib/models/role.rb', line 275

def nodes
    @body['nodes']
end

#nodes_idsObject



310
311
312
# File 'lib/models/role.rb', line 310

def nodes_ids
    @body['nodes'].map {|node| node['deploy_id'] }
end

#on_hold?true, false

Returns the on_hold role option

Returns:

  • (true, false)

    true if the on_hold option is enabled



378
379
380
# File 'lib/models/role.rb', line 378

def on_hold?
    @body['on_hold']
end

#parentsArray

Returns the role parents

Returns:

  • (Array)

    the role parents



207
208
209
# File 'lib/models/role.rb', line 207

def parents
    @body['parents'] || []
end

#recover_deploy(report) ⇒ Object

Recover



772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
# File 'lib/models/role.rb', line 772

def recover_deploy(report)
    nodes = @body['nodes']
    deployed_nodes = []

    nodes.each do |node|
        vm_id = node['deploy_id']

        vm = OpenNebula::VirtualMachine.new_with_id(vm_id,
                                                    @service.client)

        rc = vm.info

        if OpenNebula.is_error?(rc)
            msg = "Role #{name} : Retry failed for VM "\
                  "#{vm_id}; #{rc.message}"
            Log.error LOG_COMP, msg, @service.id

            next true
        end

        vm_state = vm.state
        lcm_state = vm.lcm_state

        # ACTIVE/RUNNING
        next false if vm_state == 3 && lcm_state == 3 && !report

        next true if vm_state == '6' # Delete DONE nodes

        if Role.vm_failure?(vm_state, lcm_state)
            rc = vm.recover(2)

            if OpenNebula.is_error?(rc)
                msg = "Role #{name} : Retry failed for VM "\
                      "#{vm_id}; #{rc.message}"

                Log.error LOG_COMP, msg, @service.id
                @service.log_error(msg)
            else
                deployed_nodes << vm_id
            end
        else
            vm.resume

            deployed_nodes << vm_id
        end
    end

    rc = deploy

    deployed_nodes.concat(rc[0]) if rc[1].nil?

    deployed_nodes
end

#recover_scale(report) ⇒ Object

def recover_warning end



839
840
841
842
843
844
845
846
847
848
849
# File 'lib/models/role.rb', line 839

def recover_scale(report)
    rc = nil

    if @body['scale_way'] == SCALE_WAYS['UP']
        rc = [recover_deploy(report), true]
    elsif @body['scale_way'] == SCALE_WAYS['DOWN']
        rc = [recover_undeploy, false]
    end

    rc
end

#recover_undeployObject



826
827
828
829
830
831
832
833
834
# File 'lib/models/role.rb', line 826

def recover_undeploy
    undeployed_nodes = []

    rc = shutdown(true)

    undeployed_nodes.concat(rc[0]) if rc[1].nil?

    undeployed_nodes
end

#releaseArray, Bool

Release all the nodes in this role

Returns:

  • (Array, Bool)

    true if all the VMs were released, false otherwise and Array with VMs released



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# File 'lib/models/role.rb', line 585

def release
    release_nodes = []
    success       = true

    # Release all vms in the role
    nodes.each do |node|
        vm_id = node['deploy_id']

        Log.debug(LOG_COMP,
                  "Role #{name}: Releasing VM #{vm_id}",
                  @service.id)

        vm = OpenNebula::VirtualMachine.new_with_id(vm_id,
                                                    @service.client)
        rc = vm.release

        if OpenNebula.is_error?(rc)
            msg = "Role #{name}: Release failed for VM #{vm_id}, " \
                  "#{rc.message}"

            Log.error(LOG_COMP, msg, @service.id)
            @service.log_error(msg)
            success = false
        else
            Log.debug(LOG_COMP,
                      "Role #{name}: Release success for VM #{vm_id}",
                      @service.id)

            release_nodes << vm_id
        end
    end

    [release_nodes, success]
end

#scale?(vm_pool) ⇒ Array<Integer>

Returns a positive, 0, or negative number of nodes to adjust,

according to the elasticity and scheduled policies

Returns:

  • (Array<Integer>)

    positive, 0, or negative number of nodes to adjust, plus the cooldown period duration



855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
# File 'lib/models/role.rb', line 855

def scale?(vm_pool)
    elasticity_pol = @body['elasticity_policies']
    scheduled_pol  = @body['scheduled_policies']

    elasticity_pol ||= []
    scheduled_pol  ||= []

    scheduled_pol.each do |policy|
        diff, cooldown_duration = scale_time?(policy)

        return [diff, cooldown_duration] if diff != 0
    end

    elasticity_pol.each do |policy|
        diff, cooldown_duration = scale_attributes?(policy, vm_pool)

        next if diff == 0

        cooldown_duration = @body['cooldown'] if cooldown_duration.nil?
        cooldown_duration = @@default_cooldown if cooldown_duration.nil?

        return [diff, cooldown_duration]
    end

    # Implicit rule that scales up to maintain the min_cardinality, with
    # no cooldown period
    if cardinality < min_cardinality.to_i
        return [min_cardinality.to_i - cardinality, 0]
    end

    [0, 0]
end

#scale_way(way) ⇒ Object



368
369
370
# File 'lib/models/role.rb', line 368

def scale_way(way)
    @body['scale_way'] = SCALE_WAYS[way]
end

#scheduled_policiesObject



322
323
324
# File 'lib/models/role.rb', line 322

def scheduled_policies
    @body['scheduled_policies']
end

#service_on_hold?true, false

Returns the on_hold service option

Returns:

  • (true, false)

    true if the on_hold option is enabled



384
385
386
# File 'lib/models/role.rb', line 384

def service_on_hold?
    @service.on_hold?
end

#set_cardinality(target_cardinality) ⇒ Object

Sets a new cardinality for this role rubocop:disable Naming/AccessorMethodName

Parameters:

  • the (Integer)

    new cardinality



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/models/role.rb', line 229

def set_cardinality(target_cardinality)
    # rubocop:enable Naming/AccessorMethodName
    if target_cardinality > cardinality
        dir = 'up'
    else
        dir = 'down'
    end

    msg = "Role #{name} scaling #{dir} from #{cardinality} to " \
          "#{target_cardinality} nodes"

    Log.info LOG_COMP, msg, @service.id

    @service.log_info(msg)

    @body['cardinality'] = target_cardinality.to_i
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



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/models/role.rb', line 342

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

    @body['state'] = state.to_i

    if state == STATE['SCALING']

        elasticity_pol = @body['elasticity_policies']

        if !elasticity_pol.nil?
            elasticity_pol.each do |policy|
                policy.delete('true_evals')
            end
        end
    end

    Log.info LOG_COMP,
             "Role #{name} new state: #{STATE_STR[state]}",
             @service.id

    true
end

#shutdown(recover) ⇒ Array<true, nil>, Array<false, String>

Terminate all the nodes in this role

were terminated, false and the error reason if there was a problem shutting down the VMs

Parameters:

  • scale_down (true, false)

    true to terminate and dispose the number of VMs needed to get down to cardinality nodes

Returns:

  • (Array<true, nil>, Array<false, String>)

    true if all the VMs



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/models/role.rb', line 521

def shutdown(recover)
    if nodes.size != cardinality
        n_nodes = nodes.size - cardinality
    else
        n_nodes = nodes.size
    end

    rc = shutdown_nodes(nodes, n_nodes, recover)

    unless rc[0]
        return [false, "Error undeploying nodes for role `#{name}`"]
    end

    [rc[1], nil]
end

#stateInteger

Returns the role state

Returns:

  • (Integer)

    the role state



161
162
163
# File 'lib/models/role.rb', line 161

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

#state_strString

Returns the string representation of the service state

Returns:

  • (String)

    the state string



269
270
271
# File 'lib/models/role.rb', line 269

def state_str
    STATE_STR[state]
end

#update(template) ⇒ nil, OpenNebula::Error

Updates the role

Parameters:

Returns:



724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
# File 'lib/models/role.rb', line 724

def update(template)
    force = template['force'] == true
    new_cardinality = template['cardinality']

    return if new_cardinality.nil?

    new_cardinality = new_cardinality.to_i

    if !force
        if new_cardinality < min_cardinality.to_i
            return OpenNebula::Error.new(
                "Minimum cardinality is #{min_cardinality}"
            )

        elsif !max_cardinality.nil? &&
              new_cardinality > max_cardinality.to_i
            return OpenNebula::Error.new(
                "Maximum cardinality is #{max_cardinality}"
            )

        end
    end

    set_cardinality(new_cardinality)

    nil
end

#update_cooldown(new_cooldown) ⇒ Object



334
335
336
# File 'lib/models/role.rb', line 334

def update_cooldown(new_cooldown)
    @body['cooldown'] = new_cooldown unless new_cooldown.nil?
end

#update_elasticity_policies(new_policies) ⇒ Object



318
319
320
# File 'lib/models/role.rb', line 318

def update_elasticity_policies(new_policies)
    @body['elasticity_policies'] = new_policies
end

#update_scheduled_policies(new_policies) ⇒ Object



326
327
328
# File 'lib/models/role.rb', line 326

def update_scheduled_policies(new_policies)
    @body['scheduled_policies'] = new_policies
end