diff --git a/flow/lcm/LifeCycleManager.rb b/flow/lcm/LifeCycleManager.rb index 0557a0ae33..334e14a2d6 100644 --- a/flow/lcm/LifeCycleManager.rb +++ b/flow/lcm/LifeCycleManager.rb @@ -74,6 +74,21 @@ class ServiceLCM if !service.all_roles_running? service.set_state(Service::STATE['UNKNOWN']) + elsif service.scale_up? + + Log.debug "ELAS", "Service #{service.name} to scale up" + Log.debug "ELAS", "Scale up", service.id() + + # TODO: create a new state SCALING, SCALING_UP? + + # TODO: changing state stops the elasticity rules + # check until current deployments end. + service.set_state(Service::STATE['DEPLOYING']) + + rc = strategy.boot_step(service) + if !rc[0] + service.set_state(Service::STATE['FAILED_DEPLOYING']) + end end when Service::STATE['UNKNOWN'] strategy.monitor_step(service) diff --git a/flow/models/Role.rb b/flow/models/Role.rb index 87904cf029..d2be07aa24 100644 --- a/flow/models/Role.rb +++ b/flow/models/Role.rb @@ -74,6 +74,28 @@ module OpenNebula return card end + # Sets a new cardinality for this role + # @param [Integer] the new cardinality + def set_cardinality(cardinality) + @body['cardinality'] = cardinality.to_i + end + + # Returns the role max cardinality + # @return [Integer] the role cardinality + def max_cardinality + max = nil + + if !@body['elasticity_policy'].nil? + max = @body['elasticity_policy']['max_vms'] + end + + if max.nil? + max = cardinality() + end + + return max.to_i + end + # Returns the string representation of the service state # @return [String] the state string def state_str diff --git a/flow/models/Service.rb b/flow/models/Service.rb index da539be259..4c74de163f 100644 --- a/flow/models/Service.rb +++ b/flow/models/Service.rb @@ -139,6 +139,50 @@ module OpenNebula return false end + # If any role scalability rule is triggered, the role is set to DEPLOYING + # and the cardinality is updated + # + # @return true if any role is going to scale up + def scale_up?() + scale = false + + @roles.each { |name, role| + if role_scale_up?(role) + role.set_cardinality( role.cardinality + 1 ) + role.set_state(Role::STATE['PENDING']) + + scale = true + end + } + + return scale + end + + # Method stub, returns true if any VM has an attribute SCALE_UP=YES + def role_scale_up?(role) + role.get_nodes.each { |node| + if node && node['vm_info'] + scale_up = node['vm_info']['VM']['USER_TEMPLATE']['SCALE_UP'] + + if scale_up == "YES" + Log.debug "ELAS", "Role #{role.name} to scale up" + + if role.cardinality() < role.max_cardinality() + # Clean vm template + `echo "" > /tmp/empty.txt` + `onevm update #{node['deploy_id']} /tmp/empty.txt` + + return true + else + Log.debug "ELAS", "Role #{role.name} has reached its VM limit" + end + end + end + } + + return false + end + # Create a new service based on the template provided # @param [String] template_json # @return [nil, OpenNebula::Error] nil in case of success, Error diff --git a/flow/models/ServiceTemplate.rb b/flow/models/ServiceTemplate.rb index 000932d33e..7037fce86a 100644 --- a/flow/models/ServiceTemplate.rb +++ b/flow/models/ServiceTemplate.rb @@ -37,6 +37,39 @@ module OpenNebula :items => { :type => :string } + }, + 'elasticity_policy' => { + :type => :object, + :properties => { + 'type' => { + :type => :string, + :required => true + }, + 'min_vms' => { + :type => :integer, + :default => 1 + }, + 'max_vms' => { + :type => :integer, + :required => true + }, + 'up_expr' => { + :type => :string, + :required => true + }, + 'down_expr' => { + :type => :string, + :required => true + }, + 'period_number' => { + :type => :integer, + :default => 1 + }, + 'period_duration' => { + :type => :integer, + :required => true + } + } } } }