Class: Pvectl::Commands::CreateContainer

Inherits:
Object
  • Object
show all
Includes:
CreateResourceCommand, SharedConfigParsers
Defined in:
lib/pvectl/commands/create_container.rb,
sig/pvectl/commands/create_container.rbs

Overview

Handler for the pvectl create container command.

Includes CreateResourceCommand for shared workflow and overrides template methods with container-specific behavior.

Examples:

Flag-based creation

pvectl create container --hostname web-ct --ostemplate local:vztmpl/debian-12.tar.zst \
  --rootfs storage=local-lvm,size=8G --cores 2 --memory 2048

Instance Method Summary collapse

Methods included from SharedConfigParsers

#build_ct_config_params, #build_vm_config_params, #parse_ct_mountpoints, #parse_ct_nets, #parse_vm_cloud_init, #parse_vm_disks, #parse_vm_nets

Methods included from CreateResourceCommand

#display_summary, #display_summary_and_confirm, #execute, included, #initialize, #interactive_mode?, #load_config, #perform_create_with_params, #perform_interactive, #resolve_default_node, #service_options, #usage_error

Instance Method Details

#build_create_service(connection, task_repo) ⇒ Services::CreateContainer



43
44
45
46
47
48
49
50
# File 'lib/pvectl/commands/create_container.rb', line 43

def build_create_service(connection, task_repo)
  ct_repo = Pvectl::Repositories::Container.new(connection)
  Pvectl::Services::CreateContainer.new(
    container_repository: ct_repo,
    task_repository: task_repo,
    options: service_options
  )
end

#build_params_from_flagsHash

Extracts CLI options into a service params hash.

Delegates container config parsing to SharedConfigParsers#build_ct_config_params, then adds create-specific parameters.

Raises:

  • (ArgumentError)

    if parser validation fails



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pvectl/commands/create_container.rb', line 82

def build_params_from_flags
  params = build_ct_config_params
  params[:hostname] = @options[:hostname]
  params[:node] = @options[:node] || resolve_default_node
  params[:ostemplate] = @options[:ostemplate]
  params[:description] = @options[:description]
  params[:pool] = @options[:pool]

  ctid = @args.first
  params[:ctid] = ctid.to_i if ctid

  params.compact
end

#build_wizardObject



36
37
38
# File 'lib/pvectl/commands/create_container.rb', line 36

def build_wizard
  Pvectl::Wizards::CreateContainer.new(@options, @global_options)
end

#display_resource_summary(params) ⇒ void



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/pvectl/commands/create_container.rb', line 98

def display_resource_summary(params)
  $stdout.puts "  Hostname:  #{params[:hostname]}"
  $stdout.puts "  Node:      #{params[:node] || '(from context)'}"
  $stdout.puts "  Template:  #{truncate_template(params[:ostemplate])}"
  $stdout.puts "  CPU:       #{params[:cores] || 1} cores" if params[:cores]
  $stdout.puts "  Memory:    #{params[:memory] || 512} MB"
  $stdout.puts "  Swap:      #{params[:swap] || 512} MB"

  if params[:rootfs]
    $stdout.puts "  Root FS:   #{params[:rootfs][:storage]}, #{params[:rootfs][:size]}"
  end

  if params[:mountpoints]
    params[:mountpoints].each_with_index do |mp, i|
      $stdout.puts "  Mount#{i}:    #{mp[:storage]}, #{mp[:size]} -> #{mp[:mp]}"
    end
  end

  if params[:nets]
    params[:nets].each_with_index do |net, i|
      ip_info = net[:ip] ? ", #{net[:ip]}" : ""
      $stdout.puts "  Net#{i}:      #{net[:bridge]}, #{net[:name] || 'eth0'}#{ip_info}"
    end
  end

  $stdout.puts "  Unpriv:    #{params[:privileged] ? 'No' : 'Yes'}"
  $stdout.puts "  Features:  #{params[:features]}" if params[:features]
  $stdout.puts "  Tags:      #{params[:tags]}" if params[:tags]
  $stdout.puts "  Pool:      #{params[:pool]}" if params[:pool]
end

#output_result(result) ⇒ void



54
55
56
57
58
59
60
61
62
# File 'lib/pvectl/commands/create_container.rb', line 54

def output_result(result)
  presenter = Pvectl::Presenters::ContainerOperationResult.new
  format = @global_options[:output] || "table"
  color_flag = @global_options[:color]

  formatter = Pvectl::Formatters::Registry.for(format)
  output = formatter.format([result], presenter, color: color_flag)
  puts output
end

#perform_createInteger

Validates flags and performs flag-based creation.



67
68
69
70
71
72
73
# File 'lib/pvectl/commands/create_container.rb', line 67

def perform_create
  return usage_error("--hostname is required") unless @options[:hostname]
  return usage_error("--ostemplate is required") unless @options[:ostemplate]
  return usage_error("--rootfs is required") unless @options[:rootfs]

  super
end

#required_params_missing?Boolean



31
32
33
# File 'lib/pvectl/commands/create_container.rb', line 31

def required_params_missing?
  !@options[:hostname]
end

#resource_id_labelString



26
27
28
# File 'lib/pvectl/commands/create_container.rb', line 26

def resource_id_label
  "CTID"
end

#resource_labelString



21
22
23
# File 'lib/pvectl/commands/create_container.rb', line 21

def resource_label
  "container"
end

#truncate_template(template) ⇒ String

Truncates long template paths for display.



133
134
135
136
137
138
# File 'lib/pvectl/commands/create_container.rb', line 133

def truncate_template(template)
  return template unless template

  parts = template.split("/")
  parts.last || template
end