Class: OpenNebula::VMRole

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

Overview

Service Role class

Constant Summary

Constants inherited from Role

Role::FAILURE_STATES, Role::IMMUTABLE_ATTRS, Role::LOG_COMP, Role::RECOVER_DEPLOY_STATES, Role::RECOVER_SCALE_STATES, Role::RECOVER_UNDEPLOY_STATES, Role::SCALE_WAYS, Role::SCHEDULE_ACTIONS, Role::STATE, Role::STATE_STR, Role::VM_INFO

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Role

#any_parent_on_hold?, #can_recover_deploy?, #can_recover_scale?, #can_recover_undeploy?, #can_release?, #cardinality, #cardinality=, #check_new_template, for, #info_nodes, init_default_cooldown, init_default_shutdown, init_default_vm_name_template, init_default_vr_name_template, init_force_deletion, #name, #nodes, #nodes_ids, #on_hold=, #on_hold?, #parents, #release, #scheduled_policies, #service_on_hold?, #shutdown, #state, #state_str, #update_scheduled_policies, vm_failure?

Constructor Details

#initialize(body, service) ⇒ VMRole

Returns a new instance of VMRole.



24
25
26
27
28
# File 'lib/models/vmrole.rb', line 24

def initialize(body, service)
    super(body, service)

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

Instance Attribute Details

#serviceObject (readonly)

Returns the value of attribute service.



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

def service
  @service
end

Instance Method Details

#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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
181
182
183
# File 'lib/models/vmrole.rb', line 128

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

#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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/models/vmrole.rb', line 57

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



266
267
268
# File 'lib/models/vmrole.rb', line 266

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

#cooldownObject



254
255
256
# File 'lib/models/vmrole.rb', line 254

def cooldown
    @body['cooldown']
end

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

Deploys all the nodes in this role

Returns:

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

    true if all the VMs were created, false and the error reason if there was a problem creating the VMs



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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/models/vmrole.rb', line 279

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

    return [deployed_nodes, nil] if n_nodes == 0

    template_id, template, extra_template = init_template_attributes

    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} : Instantiate template #{template_id}, name #{vm_name}",
            @service.id
        )

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

        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 instantiating 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
        )

        # Once deployed, save VM info in role node body
        deployed_nodes << vm_id
        fill_node_info(vm_id)
    end

    [deployed_nodes, nil]
end

#elasticity_policiesObject



246
247
248
# File 'lib/models/vmrole.rb', line 246

def elasticity_policies
    @body['elasticity_policies']
end

#max_cardinalityInteger?

Returns the role max cardinality

Returns:

  • (Integer, nil)

    the role cardinality or nil if it isn’t defined



191
192
193
194
195
196
197
# File 'lib/models/vmrole.rb', line 191

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



201
202
203
204
205
206
207
# File 'lib/models/vmrole.rb', line 201

def min_cardinality
    min = @body['min_vms']

    return if min.nil?

    min.to_i
end

#recover_deploy(report) ⇒ Object

Recover



335
336
337
338
339
340
341
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/models/vmrole.rb', line 335

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

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

    deployed_nodes.concat(rc[0])

    deployed_nodes
end

#recover_scale(report) ⇒ Object



405
406
407
408
409
410
411
412
413
414
415
# File 'lib/models/vmrole.rb', line 405

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



393
394
395
396
397
398
399
400
401
402
403
# File 'lib/models/vmrole.rb', line 393

def recover_undeploy
    undeployed_nodes = []

    rc = shutdown(true)

    return false unless rc[0]

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

    undeployed_nodes
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



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/models/vmrole.rb', line 213

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



262
263
264
# File 'lib/models/vmrole.rb', line 262

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

#state=(state) ⇒ Object

Sets a new state

Parameters:

  • the (Integer)

    new state



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/models/vmrole.rb', line 32

def state=(state)
    super(state)
    return unless state == STATE['SCALING']

    elasticity_pol = @body['elasticity_policies']

    return if elasticity_pol.nil?

    elasticity_pol.each do |policy|
        policy.delete('true_evals')
    end
end

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

Updates the role

Parameters:

Returns:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/models/vmrole.rb', line 91

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

    self.cardinality = new_cardinality

    nil
end

#update_cooldown(new_cooldown) ⇒ Object



258
259
260
# File 'lib/models/vmrole.rb', line 258

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

#update_elasticity_policies(new_policies) ⇒ Object



250
251
252
# File 'lib/models/vmrole.rb', line 250

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