Class: Pvectl::Wizards::CreateVm
- Inherits:
-
Object
- Object
- Pvectl::Wizards::CreateVm
- 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.
Constant Summary collapse
- OS_TYPES =
%w[l26 l24 win11 win10 win2k22 win2k19 win2k16 win8 win7 other].freeze
Instance Method Summary collapse
-
#collect_disks ⇒ Array<Hash>?
Collects disk configurations interactively.
-
#collect_nets ⇒ Array<Hash>?
Collects network configurations interactively.
-
#collect_params ⇒ Hash
Collects all parameters interactively.
-
#create_default_prompt ⇒ Object
Creates a default prompt, preferring TTY::Prompt with SimplePrompt fallback.
-
#initialize(options, global_options, prompt: nil) ⇒ CreateVm
constructor
Creates a new CreateVm wizard.
-
#run ⇒ Hash?
Runs the wizard and returns params or nil if cancelled.
Constructor Details
#initialize(options, global_options, prompt: nil) ⇒ CreateVm
Creates a new CreateVm wizard.
24 25 26 27 28 |
# File 'lib/pvectl/wizards/create_vm.rb', line 24 def initialize(, , prompt: nil) = = @prompt = prompt || create_default_prompt end |
Instance Method Details
#collect_disks ⇒ Array<Hash>?
Collects disk configurations interactively.
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_nets ⇒ Array<Hash>?
Collects network configurations interactively.
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_params ⇒ Hash
Collects all parameters interactively.
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: [: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_prompt ⇒ Object
Creates a default prompt, preferring TTY::Prompt with SimplePrompt fallback.
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 |
#run ⇒ Hash?
Runs the wizard and returns params or nil if cancelled.
33 34 35 |
# File 'lib/pvectl/wizards/create_vm.rb', line 33 def run collect_params end |