Class: Pvectl::Services::VmLifecycle
- Inherits:
-
Object
- Object
- Pvectl::Services::VmLifecycle
- Defined in:
- lib/pvectl/services/vm_lifecycle.rb,
sig/pvectl/services/vm_lifecycle.rbs
Overview
Orchestrates VM lifecycle operations.
Handles execution of start/stop/shutdown/restart/reset/suspend/resume operations with sync/async modes, error handling, and result collection.
Constant Summary collapse
- SYNC_OPERATIONS =
i[start stop reset resume].freeze
- ASYNC_OPERATIONS =
i[shutdown restart suspend].freeze
- ALL_OPERATIONS =
(SYNC_OPERATIONS + ASYNC_OPERATIONS).freeze
- DEFAULT_TIMEOUT =
60
Instance Method Summary collapse
-
#call_api(operation, vm) ⇒ String
Calls the appropriate API method.
-
#execute(operation, vms) ⇒ Array<Models::OperationResult>
Executes a lifecycle operation on a list of VMs.
-
#execute_single(operation, vm) ⇒ Models::OperationResult
Executes operation on a single VM.
-
#initialize(vm_repository, task_repository, options = {}) ⇒ VmLifecycle
constructor
Creates a new VmLifecycle service.
-
#sync_mode?(operation) ⇒ Boolean
Determines if operation should run in sync mode.
-
#timeout ⇒ Integer
Returns configured timeout.
-
#validate_operation!(operation) ⇒ void
Validates operation is supported.
Constructor Details
#initialize(vm_repository, task_repository, options = {}) ⇒ VmLifecycle
Creates a new VmLifecycle service.
27 28 29 30 31 |
# File 'lib/pvectl/services/vm_lifecycle.rb', line 27 def initialize(vm_repository, task_repository, = {}) @vm_repository = vm_repository @task_repository = task_repository = end |
Instance Method Details
#call_api(operation, vm) ⇒ String
Calls the appropriate API method.
91 92 93 |
# File 'lib/pvectl/services/vm_lifecycle.rb', line 91 def call_api(operation, vm) @vm_repository.send(operation, vm.vmid, vm.node) end |
#execute(operation, vms) ⇒ Array<Models::OperationResult>
Executes a lifecycle operation on a list of VMs.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/pvectl/services/vm_lifecycle.rb', line 38 def execute(operation, vms) validate_operation!(operation) results = [] vms.each do |vm| result = execute_single(operation, vm) results << result break if [:fail_fast] && result.failed? end results end |
#execute_single(operation, vm) ⇒ Models::OperationResult
Executes operation on a single VM.
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 |
# File 'lib/pvectl/services/vm_lifecycle.rb', line 58 def execute_single(operation, vm) task_upid = call_api(operation, vm) if sync_mode?(operation) task = @task_repository.wait(task_upid, timeout: timeout) Models::VmOperationResult.new( vm: vm, operation: operation, task: task, success: task.successful? ) else Models::VmOperationResult.new( vm: vm, operation: operation, task_upid: task_upid, success: :pending ) end rescue StandardError => e Models::VmOperationResult.new( vm: vm, operation: operation, success: false, error: e. ) end |
#sync_mode?(operation) ⇒ Boolean
Determines if operation should run in sync mode.
99 100 101 102 103 104 |
# File 'lib/pvectl/services/vm_lifecycle.rb', line 99 def sync_mode?(operation) return false if [:async] return true if [:wait] SYNC_OPERATIONS.include?(operation) end |
#timeout ⇒ Integer
Returns configured timeout.
109 110 111 |
# File 'lib/pvectl/services/vm_lifecycle.rb', line 109 def timeout [:timeout] || DEFAULT_TIMEOUT end |
#validate_operation!(operation) ⇒ void
This method returns an undefined value.
Validates operation is supported.
117 118 119 120 121 |
# File 'lib/pvectl/services/vm_lifecycle.rb', line 117 def validate_operation!(operation) return if ALL_OPERATIONS.include?(operation) raise ArgumentError, "Unknown operation: #{operation}. Valid: #{ALL_OPERATIONS.join(', ')}" end |