Class: Pvectl::Wizards::CreateVm

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/wizards/create_vm.rb,
sig/pvectl/wizards/create_vm.rbs

Overview

Interactive wizard for creating a VM step by step.

Uses TTY::Prompt (when available) or a fallback prompt to guide the user through VM configuration. Returns a normalized params hash compatible with Services::CreateVm, or nil if the user cancels.

Examples:

Running the wizard

wizard = CreateVm.new(options, global_options)
params = wizard.run
#=> { name: "web", node: "pve1", cores: 4, ... } or nil

Constant Summary collapse

OS_TYPES =

Returns:

  • (Array[String])
%w[l26 l24 win11 win10 win2k22 win2k19 win2k16 win8 win7 other].freeze

Instance Method Summary collapse

Constructor Details

#initialize(options, global_options, prompt: nil) ⇒ CreateVm

Creates a new CreateVm wizard.

Parameters:

  • options (Hash)

    command options (may contain pre-filled values)

  • global_options (Hash)

    global CLI options

  • prompt (Object) (defaults to: nil)

    prompt instance (injectable for testing)

  • prompt: (Object) (defaults to: nil)


24
25
26
27
28
# File 'lib/pvectl/wizards/create_vm.rb', line 24

def initialize(options, global_options, prompt: nil)
  @options = options
  @global_options = global_options
  @prompt = prompt || create_default_prompt
end

Instance Method Details

#collect_disksArray<Hash>?

Collects disk configurations interactively.

Returns:

  • (Array<Hash>, nil)

    disk configs or nil if none added



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pvectl/wizards/create_vm.rb', line 70

def collect_disks
  disks = []
  loop do
    input = @prompt.ask("Disk config (storage=X,size=Y) or empty to skip:")
    break if input.nil? || input.empty?

    disks << Parsers::DiskConfig.parse(input)
    break unless @prompt.yes?("Add another disk?")
  end
  disks.empty? ? nil : disks
end

#collect_netsArray<Hash>?

Collects network configurations interactively.

Returns:

  • (Array<Hash>, nil)

    net configs or nil if none added



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pvectl/wizards/create_vm.rb', line 85

def collect_nets
  nets = []
  loop do
    input = @prompt.ask("Network config (bridge=X) or empty to skip:")
    break if input.nil? || input.empty?

    nets << Parsers::NetConfig.parse(input)
    break unless @prompt.yes?("Add another network?")
  end
  nets.empty? ? nil : nets
end

#collect_paramsHash

Collects all parameters interactively.

Returns:

  • (Hash)

    collected parameters



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pvectl/wizards/create_vm.rb', line 52

def collect_params
  params = {}
  params[:name] = @prompt.ask("VM name:", required: true)
  params[:node] = @prompt.ask("Node:", default: @options[:node])
  params[:cores] = @prompt.ask("CPU cores:", default: 1, convert: :int)
  params[:sockets] = @prompt.ask("CPU sockets:", default: 1, convert: :int)
  params[:memory] = @prompt.ask("Memory (MB):", default: 2048, convert: :int)
  params[:disks] = collect_disks
  params[:nets] = collect_nets
  params[:ostype] = @prompt.select("OS type:", OS_TYPES)
  params[:agent] = true if @prompt.yes?("Enable QEMU guest agent?")
  params[:start] = true if @prompt.yes?("Start VM after creation?")
  params.compact
end

#create_default_promptObject

Creates a default prompt, preferring TTY::Prompt with SimplePrompt fallback.

Returns:

  • (Object)

    prompt object



42
43
44
45
46
47
# File 'lib/pvectl/wizards/create_vm.rb', line 42

def create_default_prompt
  require "tty-prompt"
  TTY::Prompt.new
rescue LoadError
  Config::SimplePrompt.new
end

#runHash?

Runs the wizard and returns params or nil if cancelled.

Returns:

  • (Hash, nil)

    creation params or nil if user cancels



33
34
35
# File 'lib/pvectl/wizards/create_vm.rb', line 33

def run
  collect_params
end