Class: OpenNebula::Service
- Inherits:
-
DocumentJSON
- Object
- XMLElement
- PoolElement
- Document
- DocumentJSON
- OpenNebula::Service
- 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
Constants inherited from Document
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#roles ⇒ Object
readonly
Returns the value of attribute roles.
Instance Method Summary collapse
-
#all_roles_done? ⇒ true, false
Returns true if all the nodes are in done state.
-
#all_roles_running? ⇒ true, false
Returns true if all the nodes are correctly deployed.
-
#allocate(template_json) ⇒ nil, OpenNebula::Error
Create a new service based on the template provided.
- #can_recover_deploy? ⇒ Boolean
- #can_recover_scale? ⇒ Boolean
- #can_recover_undeploy? ⇒ Boolean
- #can_scale? ⇒ Boolean
-
#can_undeploy? ⇒ Boolean
Return true if the service can be undeployed.
-
#can_update? ⇒ Boolean
Return true if the service can be updated.
-
#check_new_template(template_json) ⇒ Boolean, String
Check that changes values are correct.
-
#check_role(role) ⇒ Boolean
Check if role is terminated or not.
-
#chown(uid, gid) ⇒ nil, OpenNebula::Error
Changes the owner/group.
- #delete_networks ⇒ Object
- #deploy_networks ⇒ Object
-
#failed_state? ⇒ Boolean
Return true if the service is in failed state.
- #gid ⇒ Object
-
#info ⇒ nil, OpenNebula::Error
Retrieves the information of the Service and all its Nodes.
-
#info_roles ⇒ nil, OpenNebula::Error
Retrieves the information of the Service and all its Nodes.
-
#log_error(message) ⇒ Object
Add an error message in the service information that will be stored in OpenNebula.
-
#log_info(message) ⇒ Object
Add an info message in the service information that will be stored in OpenNebula.
-
#recover ⇒ nil, OpenNebula::Error
Recover a failed service.
-
#replace_client(owner_client) ⇒ Object
Replaces this object’s client with a new one.
-
#report_ready? ⇒ true, false
Returns the running_status_vm option.
-
#set_state(state) ⇒ true, false
Sets a new state rubocop:disable Naming/AccessorMethodName.
- #shutdown_action ⇒ Object
-
#state ⇒ Integer
Returns the service state.
-
#state_str ⇒ Object
Returns the string representation of the service state.
-
#strategy ⇒ String
Returns the service strategy.
-
#transient_state? ⇒ Boolean
Returns true if the service is in transient state.
- #uname ⇒ Object
-
#update(template_json = nil, append = false) ⇒ nil, OpenNebula::Error
Replaces the template contents.
-
#update_raw(template_raw, append = false) ⇒ nil, OpenNebula::Error
Replaces the raw template contents.
-
#update_role(role_name, template_json) ⇒ nil, OpenNebula::Error
Updates a role.
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
#client ⇒ Object (readonly)
Returns the value of attribute client.
22 23 24 |
# File 'lib/models/service.rb', line 22 def client @client end |
#roles ⇒ Object (readonly)
Returns the value of attribute roles.
22 23 24 |
# File 'lib/models/service.rb', line 22 def roles @roles end |
Instance Method Details
#all_roles_done? ⇒ true, false
Returns true if all the nodes are in done state
222 223 224 225 226 227 228 229 230 |
# File 'lib/models/service.rb', line 222 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
210 211 212 213 214 215 216 217 218 |
# File 'lib/models/service.rb', line 210 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
236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/models/service.rb', line 236 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
157 158 159 |
# File 'lib/models/service.rb', line 157 def can_recover_deploy? RECOVER_DEPLOY_STATES.include? STATE_STR[state] end |
#can_recover_scale? ⇒ Boolean
165 166 167 |
# File 'lib/models/service.rb', line 165 def can_recover_scale? RECOVER_SCALE_STATES.include? STATE_STR[state] end |
#can_recover_undeploy? ⇒ Boolean
161 162 163 |
# File 'lib/models/service.rb', line 161 def can_recover_undeploy? RECOVER_UNDEPLOY_STATES.include? STATE_STR[state] end |
#can_scale? ⇒ Boolean
567 568 569 |
# File 'lib/models/service.rb', line 567 def can_scale? state == Service::STATE['RUNNING'] end |
#can_undeploy? ⇒ Boolean
Return true if the service can be undeployed
142 143 144 145 146 147 148 149 |
# File 'lib/models/service.rb', line 142 def can_undeploy? if (transient_state? && state != Service::STATE['UNDEPLOYING']) || state == Service::STATE['DONE'] || failed_state? false else true end end |
#can_update? ⇒ Boolean
Return true if the service can be updated
153 154 155 |
# File 'lib/models/service.rb', line 153 def can_update? !transient_state? && !failed_state? end |
#check_new_template(template_json) ⇒ Boolean, String
Check that changes values are correct
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'lib/models/service.rb', line 491 def check_new_template(template_json) template = JSON.parse(template_json) 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 [true, nil] end |
#check_role(role) ⇒ Boolean
Check if role is terminated or not
578 579 580 581 582 583 584 585 586 587 588 |
# File 'lib/models/service.rb', line 578 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
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 392 393 394 395 396 397 |
# File 'lib/models/service.rb', line 365 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_networks ⇒ Object
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
# File 'lib/models/service.rb', line 544 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 ⇒ Object
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
# File 'lib/models/service.rb', line 517 def deploy_networks body = JSON.parse(self['TEMPLATE/BODY']) 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 # 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
136 137 138 |
# File 'lib/models/service.rb', line 136 def failed_state? FAILED_STATES.include? STATE_STR[state] end |
#gid ⇒ Object
179 180 181 |
# File 'lib/models/service.rb', line 179 def gid self['GID'].to_i end |
#info ⇒ nil, OpenNebula::Error
Retrieves the information of the Service and all its Nodes.
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/models/service.rb', line 307 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_roles ⇒ nil, OpenNebula::Error
Retrieves the information of the Service and all its Nodes.
330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/models/service.rb', line 330 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
354 355 356 |
# File 'lib/models/service.rb', line 354 def log_error() add_log(Logger::ERROR, ) end |
#log_info(message) ⇒ Object
Add an info message in the service information that will be stored
in OpenNebula
347 348 349 |
# File 'lib/models/service.rb', line 347 def log_info() add_log(Logger::INFO, ) end |
#recover ⇒ nil, OpenNebula::Error
Recover a failed service.
254 255 256 257 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 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/models/service.rb', line 254 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 |
#replace_client(owner_client) ⇒ Object
Replaces this object’s client with a new one
185 186 187 |
# File 'lib/models/service.rb', line 185 def replace_client(owner_client) @client = owner_client end |
#report_ready? ⇒ true, false
Returns the running_status_vm option
171 172 173 |
# File 'lib/models/service.rb', line 171 def report_ready? @body['ready_status_gate'] end |
#set_state(state) ⇒ true, false
Sets a new state rubocop:disable Naming/AccessorMethodName
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/models/service.rb', line 193 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_action ⇒ Object
441 442 443 |
# File 'lib/models/service.rb', line 441 def shutdown_action @body['shutdown_action'] end |
#state ⇒ Integer
Returns the service state
112 113 114 |
# File 'lib/models/service.rb', line 112 def state @body['state'].to_i end |
#state_str ⇒ Object
Returns the string representation of the service state
124 125 126 |
# File 'lib/models/service.rb', line 124 def state_str STATE_STR[state] end |
#strategy ⇒ String
Returns 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
130 131 132 |
# File 'lib/models/service.rb', line 130 def transient_state? TRANSIENT_STATES.include? STATE_STR[state] end |
#uname ⇒ Object
175 176 177 |
# File 'lib/models/service.rb', line 175 def uname self['UNAME'] end |
#update(template_json = nil, append = false) ⇒ nil, OpenNebula::Error
Replaces the template contents
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'lib/models/service.rb', line 453 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
481 482 483 |
# File 'lib/models/service.rb', line 481 def update_raw(template_raw, append = false) super(template_raw, append) end |
#update_role(role_name, template_json) ⇒ nil, OpenNebula::Error
Updates a role
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 436 437 438 439 |
# File 'lib/models/service.rb', line 404 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 |