Class: Pvectl::Commands::CreateVm

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

Overview

Handler for the pvectl create vm command.

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

Examples:

Flag-based creation

pvectl create vm --name web --node pve1 --cores 4 --memory 4096

With disk and network

pvectl create vm 100 --name web --node pve1 \
  --disk storage=local-lvm,size=32G --net bridge=vmbr0

Dry-run mode

pvectl create vm --name web --node pve1 --dry-run

Class Method Summary collapse

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

Class Method Details

.register(cli) ⇒ void

This method returns an undefined value.

Registers the create command with the CLI.

Parameters:

  • the CLI application object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
96
97
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pvectl/commands/create_vm.rb', line 28

def self.register(cli)
  cli.desc "Create a resource"
  cli.long_desc "    Create a new virtual machine, container, snapshot, or backup.\n\n    For VMs and containers, you can either specify configuration via flags\n    or use --interactive for a guided wizard. Use --dry-run to preview\n    the configuration without actually creating the resource.\n\n    SUBCOMMANDS\n      create vm [ID]              Create a virtual machine\n      create container [ID]       Create an LXC container\n      create snapshot NAME        Create a snapshot (see: pvectl help create snapshot)\n      create backup ID...         Create a backup (vzdump)\n\n    EXAMPLES\n      Create a VM with specific config:\n        $ pvectl create vm 100 --cores 4 --memory 8192 --disk storage=local-lvm,size=50G\n\n      Create a VM using interactive wizard:\n        $ pvectl create vm --interactive\n\n      Preview VM creation without executing:\n        $ pvectl create vm 100 --cores 2 --memory 4096 --dry-run\n\n      Create a container:\n        $ pvectl create container 200 --hostname nginx --ostemplate local:vztmpl/debian-12.tar.zst\n\n      Create a backup:\n        $ pvectl create backup 100 --storage nfs-backup --mode snapshot\n\n    NOTES\n      If no ID is specified for VM/container, Proxmox auto-assigns the\n      next available ID.\n\n      VM creation supports cloud-init configuration via --cloud-init flag.\n\n      Backup modes: snapshot (default, no downtime), suspend (brief pause),\n      stop (full stop during backup).\n\n    SEE ALSO\n      pvectl help clone           Clone an existing VM or container\n      pvectl help delete          Delete resources\n      pvectl help get templates   List available templates\n  HELP\n  cli.arg_name \"RESOURCE_TYPE [ID...]\"\n  cli.command :create do |c|\n    c.desc \"Resource name\"\n    c.flag [:name], arg_name: \"NAME\"\n\n    c.desc \"Description/notes\"\n    c.flag [:description, :notes], arg_name: \"TEXT\"\n\n    # Backup-specific flags\n    c.desc \"Target storage for backup\"\n    c.flag [:storage], arg_name: \"STORAGE\"\n\n    c.desc \"Backup mode (snapshot, suspend, stop)\"\n    c.default_value \"snapshot\"\n    c.flag [:mode], arg_name: \"MODE\", must_match: %w[snapshot suspend stop]\n\n    c.desc \"Compression (zstd, gzip, lzo, 0 for none)\"\n    c.default_value \"zstd\"\n    c.flag [:compress], arg_name: \"TYPE\"\n\n    c.desc \"Protect backup from deletion\"\n    c.switch [:protected], negatable: false\n\n    # Common flags\n    c.desc \"Skip confirmation prompt\"\n    c.switch [:yes, :y], negatable: false\n\n    c.desc \"Timeout in seconds for sync operations\"\n    c.flag [:timeout], type: Integer, arg_name: \"SECONDS\"\n\n    c.desc \"Force async mode (return task ID immediately)\"\n    c.switch [:async], negatable: false\n\n    c.desc \"Stop on first error (default: continue and report all)\"\n    c.switch [:\"fail-fast\"], negatable: false\n\n    # Shared config flags (VM + container)\n    SharedFlags.common_config(c)\n    SharedFlags.vm_config(c)\n    SharedFlags.container_config(c)\n\n    c.desc \"Resource pool\"\n    c.flag [:pool], arg_name: \"POOL\"\n\n    c.desc \"Force interactive wizard mode\"\n    c.switch [:interactive], negatable: true\n\n    c.desc \"Show what would happen without creating\"\n    c.switch [:\"dry-run\"], negatable: false\n\n    # Container-specific create flags\n    c.desc \"Container hostname (container)\"\n    c.flag [:hostname], arg_name: \"NAME\"\n\n    c.desc \"OS template path (container): storage:vztmpl/name.tar.zst\"\n    c.flag [:ostemplate], arg_name: \"TEMPLATE\"\n\n    # Sub-commands\n    CreateSnapshot.register_subcommand(c)\n\n    c.action do |global_options, options, args|\n      resource_type = args.shift\n      resource_ids = args\n\n      exit_code = case resource_type\n      when \"vm\"\n        Commands::CreateVm.execute(resource_ids, options, global_options)\n      when \"container\", \"ct\"\n        Commands::CreateContainer.execute(resource_ids, options, global_options)\n      when \"backup\"\n        # Map :description to :notes for backup if notes not set\n        options[:notes] ||= options[:description]\n        Commands::CreateBackup.execute(resource_type, resource_ids, options, global_options)\n      else\n        $stderr.puts \"Error: Unknown resource type: \#{resource_type}\"\n        $stderr.puts \"Valid types: vm, container, backup (or use: create snapshot)\"\n        ExitCodes::USAGE_ERROR\n      end\n\n      exit exit_code if exit_code != 0\n    end\n  end\nend\n"

Instance Method Details

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

Returns VM creation service.

Parameters:

  • API connection

  • task repository

Returns:

  • VM creation service



182
183
184
185
186
187
188
189
# File 'lib/pvectl/commands/create_vm.rb', line 182

def build_create_service(connection, task_repo)
  vm_repo = Pvectl::Repositories::Vm.new(connection)
  Pvectl::Services::CreateVm.new(
    vm_repository: vm_repo,
    task_repository: task_repo,
    options: service_options
  )
end

#build_params_from_flagsHash

Extracts CLI options into a service params hash.

Delegates VM config parsing to SharedConfigParsers#build_vm_config_params, then adds create-specific parameters.

Returns:

  • service-compatible parameters

Raises:

  • if parser validation fails



221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/pvectl/commands/create_vm.rb', line 221

def build_params_from_flags
  params = build_vm_config_params
  params[:name] = @options[:name]
  params[:node] = @options[:node] || resolve_default_node
  params[:description] = @options[:description]
  params[:pool] = @options[:pool]

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

  params.compact
end

#build_wizardObject

Returns VM creation wizard.

Returns:

  • VM creation wizard



175
176
177
# File 'lib/pvectl/commands/create_vm.rb', line 175

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

#display_resource_summary(params) ⇒ void

This method returns an undefined value.

Parameters:

  • VM creation parameters



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/pvectl/commands/create_vm.rb', line 236

def display_resource_summary(params)
  $stdout.puts "  Name:      #{params[:name]}"
  $stdout.puts "  Node:      #{params[:node] || '(from context)'}"

  if params[:cores] || params[:sockets]
    $stdout.puts "  CPU:       #{params[:cores] || 1} cores, #{params[:sockets] || 1} socket(s)"
  end

  $stdout.puts "  Memory:    #{params[:memory] || 2048} MB"

  if params[:disks]
    params[:disks].each_with_index do |disk, i|
      $stdout.puts "  Disk#{i}:     #{disk[:storage]}, #{disk[:size]}"
    end
  end

  if params[:nets]
    params[:nets].each_with_index do |net, i|
      $stdout.puts "  Net#{i}:      #{net[:bridge]}, #{net[:model] || 'virtio'}"
    end
  end

  $stdout.puts "  OS Type:   #{params[:ostype]}" if params[:ostype]
  $stdout.puts "  BIOS:      #{params[:bios]}" if params[:bios]
  $stdout.puts "  CD-ROM:    #{params[:cdrom]}" if params[:cdrom]
  $stdout.puts "  Agent:     enabled" if params[:agent]
  $stdout.puts "  Tags:      #{params[:tags]}" if params[:tags]
  $stdout.puts "  Pool:      #{params[:pool]}" if params[:pool]
end

#output_result(result) ⇒ void

This method returns an undefined value.

Parameters:

  • operation result



193
194
195
196
197
198
199
200
201
# File 'lib/pvectl/commands/create_vm.rb', line 193

def output_result(result)
  presenter = Pvectl::Presenters::VmOperationResult.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.

Overrides shared #perform_create to add VM-specific validation.

Returns:

  • exit code



208
209
210
211
212
# File 'lib/pvectl/commands/create_vm.rb', line 208

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

  super
end

#required_params_missing?Boolean

Returns true if --name is missing.

Returns:

  • true if --name is missing



170
171
172
# File 'lib/pvectl/commands/create_vm.rb', line 170

def required_params_missing?
  !@options[:name]
end

#resource_id_labelString

Returns human label for VM IDs.

Returns:

  • human label for VM IDs



165
166
167
# File 'lib/pvectl/commands/create_vm.rb', line 165

def resource_id_label
  "VMID"
end

#resource_labelString

Returns human label for VM resources.

Returns:

  • human label for VM resources



160
161
162
# File 'lib/pvectl/commands/create_vm.rb', line 160

def resource_label
  "VM"
end