Class: Pvectl::Services::CreateVm

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

Overview

Orchestrates VM creation operations.

Handles auto-VMID allocation, parameter building (mapping disk/net/cloud-init configs to Proxmox API format), sync/async modes, and optional auto-start.

Examples:

Basic VM creation

service = CreateVm.new(vm_repository: vm_repo, task_repository: task_repo)
result = service.execute(name: "web-server", node: "pve1",
                         cores: 4, memory: 4096,
                         disks: [{ storage: "local-lvm", size: "32G" }])

Async creation with auto-start

service = CreateVm.new(vm_repository: vm_repo, task_repository: task_repo,
                       options: { async: true, start: true })
result = service.execute(vmid: 200, name: "db-server", node: "pve1",
                         cores: 8, memory: 16384)

Constant Summary collapse

DEFAULT_TIMEOUT =

Returns Default timeout for create operations (seconds).

Returns:

  • (Integer)

    Default timeout for create operations (seconds)

300
START_TIMEOUT =

Returns Default timeout for start operations (seconds).

Returns:

  • (Integer)

    Default timeout for start operations (seconds)

60

Instance Method Summary collapse

Constructor Details

#initialize(vm_repository:, task_repository:, options: {}) ⇒ CreateVm

Creates a new CreateVm service.

Parameters:

  • vm_repository (Repositories::Vm)

    VM repository

  • task_repository (Repositories::Task)

    Task repository

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

    Options (timeout, async, start)



34
35
36
37
38
# File 'lib/pvectl/services/create_vm.rb', line 34

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

Instance Method Details

#execute(vmid: nil, name:, node:, cores: nil, sockets: nil, cpu_type: nil, numa: nil, memory: nil, balloon: nil, disks: nil, scsihw: nil, cdrom: nil, nets: nil, bios: nil, boot_order: nil, machine: nil, efidisk: nil, cloud_init: nil, agent: nil, ostype: nil, description: nil, tags: nil, pool: nil) ⇒ Models::VmOperationResult

Executes VM creation operation.

Parameters:

  • vmid (Integer, nil) (defaults to: nil)

    VM identifier (auto-allocated if nil)

  • name (String)

    VM name

  • node (String)

    Target node

  • cores (Integer, nil) (defaults to: nil)

    Number of CPU cores

  • sockets (Integer, nil) (defaults to: nil)

    Number of CPU sockets

  • cpu_type (String, nil) (defaults to: nil)

    CPU type (e.g. “host”, “kvm64”)

  • numa (Boolean, nil) (defaults to: nil)

    Enable NUMA

  • memory (Integer, nil) (defaults to: nil)

    Memory in MB

  • balloon (Integer, nil) (defaults to: nil)

    Balloon memory in MB

  • disks (Array<Hash>, nil) (defaults to: nil)

    Disk configurations

  • scsihw (String, nil) (defaults to: nil)

    SCSI controller type

  • cdrom (String, nil) (defaults to: nil)

    CD-ROM ISO path

  • nets (Array<Hash>, nil) (defaults to: nil)

    Network configurations

  • bios (String, nil) (defaults to: nil)

    BIOS type (seabios or ovmf)

  • boot_order (String, nil) (defaults to: nil)

    Boot order (e.g. “scsi0;net0”)

  • machine (String, nil) (defaults to: nil)

    Machine type (e.g. “q35”)

  • efidisk (String, nil) (defaults to: nil)

    EFI disk specification

  • cloud_init (Hash, nil) (defaults to: nil)

    Cloud-init parameters

  • agent (Boolean, nil) (defaults to: nil)

    Enable QEMU guest agent

  • ostype (String, nil) (defaults to: nil)

    OS type (e.g. “l26”, “win10”)

  • description (String, nil) (defaults to: nil)

    VM description

  • tags (String, nil) (defaults to: nil)

    Tags (semicolon-separated)

  • pool (String, nil) (defaults to: nil)

    Resource pool

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pvectl/services/create_vm.rb', line 66

def execute(vmid: nil, name:, node:, cores: nil, sockets: nil, cpu_type: nil,
            numa: nil, memory: nil, balloon: nil, disks: nil, scsihw: nil,
            cdrom: nil, nets: nil, bios: nil, boot_order: nil, machine: nil,
            efidisk: nil, cloud_init: nil, agent: nil, ostype: nil,
            description: nil, tags: nil, pool: nil)
  vmid ||= @vm_repository.next_available_vmid

  params = build_params(
    name: name, cores: cores, sockets: sockets, cpu_type: cpu_type,
    numa: numa, memory: memory, balloon: balloon, disks: disks,
    scsihw: scsihw, cdrom: cdrom, nets: nets, bios: bios,
    boot_order: boot_order, machine: machine, efidisk: efidisk,
    cloud_init: cloud_init, agent: agent, ostype: ostype,
    description: description, tags: tags, pool: pool
  )

  upid = @vm_repository.create(node, vmid, params)
  resource_info = { vmid: vmid, name: name, node: node }

  if @options[:async]
    build_result(resource_info, task_upid: upid, success: :pending)
  else
    task = @task_repository.wait(upid, timeout: timeout)
    start_vm(vmid, node) if task.successful? && @options[:start]
    build_result(resource_info, task: task, success: task.successful?)
  end
rescue StandardError => e
  build_result({ vmid: vmid, name: name, node: node },
               success: false, error: e.message)
end