Class: Pvectl::Services::VmLifecycle

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/services/vm_lifecycle.rb

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.

Examples:

Basic usage

service = VmLifecycle.new(vm_repo, task_repo)
results = service.execute(:start, [vm1, vm2])
results.each { |r| puts "#{r.vm.vmid}: #{r.status_text}" }

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

Constructor Details

#initialize(vm_repository, task_repository, options = {}) ⇒ VmLifecycle

Creates a new VmLifecycle service.

Parameters:

  • vm_repository (Repositories::Vm)

    VM repository

  • task_repository (Repositories::Task)

    Task repository

  • options (Hash) (defaults to: {})

    Options (timeout, async, wait, fail_fast)



27
28
29
30
31
# File 'lib/pvectl/services/vm_lifecycle.rb', line 27

def initialize(vm_repository, task_repository, options = {})
  @vm_repository = vm_repository
  @task_repository = task_repository
  @options = options
end

Instance Method Details

#execute(operation, vms) ⇒ Array<Models::OperationResult>

Executes a lifecycle operation on a list of VMs.

Parameters:

  • operation (Symbol)

    Operation to execute

  • vms (Array<Models::Vm>)

    VMs to operate on

Returns:



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 @options[:fail_fast] && result.failed?
  end
  results
end