Class: Pvectl::Wizards::CreateContainer
- Inherits:
-
Object
- Object
- Pvectl::Wizards::CreateContainer
- Defined in:
- lib/pvectl/wizards/create_container.rb,
sig/pvectl/wizards/create_container.rbs
Overview
Interactive wizard for creating an LXC container step by step.
Uses TTY::Prompt (when available) or a fallback prompt to guide the user through container configuration. Returns a normalized params hash compatible with Services::CreateContainer, or nil if the user cancels.
Instance Method Summary collapse
-
#collect_mountpoints ⇒ Array<Hash>?
Collects mountpoint configurations interactively.
-
#collect_nets ⇒ Array<Hash>?
Collects network configurations interactively.
-
#collect_params ⇒ Hash
Collects all parameters interactively.
-
#collect_rootfs ⇒ Hash
Collects root filesystem configuration.
-
#create_default_prompt ⇒ Object
Creates a default prompt, preferring TTY::Prompt with SimplePrompt fallback.
-
#initialize(options, global_options, prompt: nil) ⇒ CreateContainer
constructor
Creates a new CreateContainer wizard.
-
#run ⇒ Hash?
Runs the wizard and returns params or nil if cancelled.
Constructor Details
#initialize(options, global_options, prompt: nil) ⇒ CreateContainer
Creates a new CreateContainer wizard.
22 23 24 25 26 |
# File 'lib/pvectl/wizards/create_container.rb', line 22 def initialize(, , prompt: nil) = = @prompt = prompt || create_default_prompt end |
Instance Method Details
#collect_mountpoints ⇒ Array<Hash>?
Collects mountpoint configurations interactively.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/pvectl/wizards/create_container.rb', line 77 def collect_mountpoints mps = [] loop do input = @prompt.ask("Mountpoint (mp=/path,storage=X,size=Y) or empty to skip:") break if input.nil? || input.empty? mps << Parsers::LxcMountConfig.parse(input) break unless @prompt.yes?("Add another mountpoint?") end mps.empty? ? nil : mps end |
#collect_nets ⇒ Array<Hash>?
Collects network configurations interactively.
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/pvectl/wizards/create_container.rb', line 92 def collect_nets nets = [] loop do input = @prompt.ask("Network config (bridge=X[,ip=Y]) or empty to skip:") break if input.nil? || input.empty? nets << Parsers::LxcNetConfig.parse(input) break unless @prompt.yes?("Add another network?") end nets.empty? ? nil : nets end |
#collect_params ⇒ Hash
Collects all parameters interactively.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/pvectl/wizards/create_container.rb', line 50 def collect_params params = {} params[:hostname] = @prompt.ask("Container hostname:", required: true) params[:ostemplate] = @prompt.ask("OS template path:", required: true) params[:node] = @prompt.ask("Node:", default: [:node]) params[:cores] = @prompt.ask("CPU cores:", default: 1, convert: :int) params[:memory] = @prompt.ask("Memory (MB):", default: 512, convert: :int) params[:swap] = @prompt.ask("Swap (MB):", default: 512, convert: :int) params[:rootfs] = collect_rootfs params[:mountpoints] = collect_mountpoints params[:nets] = collect_nets params[:privileged] = true unless @prompt.yes?("Unprivileged container?", default: true) params[:start] = true if @prompt.yes?("Start container after creation?") params.compact end |
#collect_rootfs ⇒ Hash
Collects root filesystem configuration.
69 70 71 72 |
# File 'lib/pvectl/wizards/create_container.rb', line 69 def collect_rootfs input = @prompt.ask("Root FS (storage=X,size=Y):", required: true) Parsers::LxcMountConfig.parse(input) end |
#create_default_prompt ⇒ Object
Creates a default prompt, preferring TTY::Prompt with SimplePrompt fallback.
40 41 42 43 44 45 |
# File 'lib/pvectl/wizards/create_container.rb', line 40 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.
31 32 33 |
# File 'lib/pvectl/wizards/create_container.rb', line 31 def run collect_params end |