Class: Pvectl::Commands::MigrateVm

Inherits:
Object
  • Object
show all
Includes:
MigrateCommand
Defined in:
lib/pvectl/commands/migrate_vm.rb

Overview

Handler for the ‘pvectl migrate vm` command.

Migrates one or more virtual machines to another cluster node. Always requires –target and confirmation (–yes to skip).

Examples:

Migrate a single VM

pvectl migrate vm 100 --target pve2 --yes

Online migration

pvectl migrate vm 100 --target pve2 --online --yes

Constant Summary collapse

RESOURCE_TYPE =
:vm
SUPPORTED_RESOURCES =
%w[vm].freeze

Constants included from MigrateCommand

Pvectl::Commands::MigrateCommand::NODE_NAME_FORMAT

Class Method Summary collapse

Methods included from MigrateCommand

#execute, included, #initialize

Class Method Details

.register(cli) ⇒ void

This method returns an undefined value.

Registers the migrate command with the CLI.

Parameters:

  • cli (GLI::App)

    the CLI application object



23
24
25
26
27
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
# File 'lib/pvectl/commands/migrate_vm.rb', line 23

def self.register(cli)
  cli.desc "Migrate a resource to another node"
  cli.long_desc <<~HELP
    Migrate virtual machines or containers between cluster nodes.
    Supports live migration (--online) for zero-downtime moves.

    EXAMPLES
      Offline migration:
        $ pvectl migrate vm 100 --target pve2

      Live migration (zero downtime):
        $ pvectl migrate vm 100 --target pve2 --online

      Migrate a container with restart:
        $ pvectl migrate ct 200 --target pve2 --restart

      Migrate to different storage:
        $ pvectl migrate vm 100 --target pve2 --target-storage local-lvm

      Batch migrate all VMs from a node:
        $ pvectl migrate vm --all --node pve1 --target pve2 --yes

      Migrate VMs matching a selector:
        $ pvectl migrate vm --all -l tags=web --target pve2 --yes

    NOTES
      --online (live migration) requires shared storage or migration
      network configured between nodes.

      Container migration with --restart briefly stops the container,
      migrates, then starts it on the target node.

      --target is required for all migrations.

      Default timeout is 600 seconds. Use --timeout to adjust for
      large VMs with lots of memory/disk.

    SEE ALSO
      pvectl help clone           Clone instead of move
      pvectl help get nodes       List available target nodes
  HELP
  cli.arg_name "RESOURCE_TYPE [ID...]"
  cli.command :migrate do |c|
    c.desc "Target node (required)"
    c.flag [:target, :t], arg_name: "NODE"

    c.desc "Online/live migration"
    c.switch [:online], negatable: false

    c.desc "Restart migration (container only)"
    c.switch [:restart], negatable: false

    c.desc "Target storage mapping"
    c.flag [:"target-storage"], arg_name: "STORAGE"

    c.desc "Select all resources of this type"
    c.switch [:all, :A], negatable: false

    c.desc "Filter by source node"
    c.flag [:node, :n], arg_name: "NODE"

    c.desc "Filter by selector (e.g., status=running,tags=prod)"
    c.flag [:l, :selector], arg_name: "SELECTOR", multiple: true

    c.desc "Skip confirmation prompt"
    c.switch [:yes, :y], negatable: false

    c.desc "Stop on first error"
    c.switch [:"fail-fast"], negatable: false

    c.desc "Wait for migration to complete (sync mode)"
    c.switch [:wait], negatable: false

    c.desc "Timeout in seconds for sync operations (default: 600)"
    c.flag [:timeout], type: Integer, arg_name: "SECONDS"

    c.action do |global_options, options, args|
      resource_type = args.shift

      exit_code = case resource_type
      when "vm"
        Commands::MigrateVm.execute(args, options, global_options)
      when "container", "ct"
        Commands::MigrateContainer.execute(args, options, global_options)
      else
        $stderr.puts "Error: Unknown resource type: #{resource_type}"
        $stderr.puts "Valid types: vm, container, ct"
        ExitCodes::USAGE_ERROR
      end

      exit exit_code if exit_code != 0
    end
  end
end