79 lines
4 KiB
Ruby
79 lines
4 KiB
Ruby
class PVE::Cli
|
|
def opts_ha cl
|
|
cl.
|
|
opt( :group, "-gGROUP", "--group=GROUP", "Put host in GROUP", default: 'all').
|
|
opt( :comment, "-cCOMMENT", "--comment=COMMENT", "Set comment").
|
|
opt( :max_relocate, "-lCOUNT", "--max-relocate=COUNT", "How often host can be relocate before givingup?", default: 2).
|
|
opt( :max_restart, "-rCOUNT", "--max-restart=COUNT", "How often host can be restarted before givingup?", default: 2).
|
|
opt( :state, "-sSTATE", "--state=STATE", "Host should have STATE. If you start/stop be `pct/qm/e start/stop` STATE will be set before action.", default: "started")
|
|
end
|
|
|
|
def cli_ha
|
|
cli.sub :ha, "Inspect High-Availability", min: 2 do |hacli|
|
|
hacli.cmd( :create, "Create HA for CT/VM", &lambda {|name_or_id, group:, comment: nil, max_relocate:, max_restart:, state:|
|
|
connect
|
|
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
|
raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th
|
|
ha = th.ha
|
|
raise UsageError, "#{th.sid} is already High-Available" if ha.active?
|
|
ha.create group: group, comment: comment, max_relocate: max_relocate, max_restart: max_restart
|
|
}).tap {|cl| opts_ha cl }
|
|
|
|
hacli.cmd :remove, "Remove CT/VM from HA", min: 5, &lambda {|name_or_id|
|
|
connect
|
|
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
|
raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th
|
|
ha = th.ha
|
|
raise UsageError, "#{th.sid} is not High-Available" unless ha.active?
|
|
ha.delete
|
|
}
|
|
|
|
hacli.cmd( :active, "CT/VM should be high-available. Options are only for defaults, if not activated, yet.", &lambda {|name_or_id, group:, comment: nil, max_relocate:, max_restart:, state:|
|
|
connect
|
|
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
|
raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th
|
|
ha = th.ha
|
|
ha.create group: group, comment: comment, max_relocate: max_relocate, max_restart: max_restart if ha.active?
|
|
}).tap {|cl| opts_ha cl }
|
|
|
|
hacli.cmd :deactive, "CT/VM should NOT be high-available.", &lambda {|name_or_id|
|
|
connect
|
|
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
|
raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th
|
|
ha = th.ha
|
|
ha.delete unless ha.active?
|
|
}
|
|
|
|
hacli.cmd( :started, "CT/VM should be in state started. By stopping CT/VM via pct/e state will be changed in HA, too.", &lambda {|name_or_id, force: nil|
|
|
connect
|
|
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
|
raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th
|
|
ha = th.ha
|
|
ha = ha.create unless ha.active?
|
|
ha.disabled! if force and ha.error?
|
|
ha.started!
|
|
}).opt( :force, "-f", "--force", "If CT/VM is in error-state, first reset HA, than try to start.")
|
|
|
|
hacli.cmd :stopped, "CT/VM should be in state stopped. By starting CT/VM via pct/e state will be changed in HA, too.", min: 3, &lambda {|name_or_id|
|
|
connect
|
|
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
|
raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th
|
|
ha = th.ha
|
|
ha = ha.create unless ha.active?
|
|
ha.stopped!
|
|
}
|
|
|
|
hacli.cmd :reset, "If state of CT/VM is failed, Proxmox will not start/stop it anyway. You have to reset state (state=disabled), first", min: 3, aliases: [:rst], &lambda {|name_or_id|
|
|
connect
|
|
th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id)
|
|
raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th
|
|
ha = th.ha
|
|
raise UsageError, "#{th.sid} is not High-Available" if ha.active?
|
|
ha.state = :disabled
|
|
}
|
|
|
|
hacli.cmd 'help', '', aliases: ['-h', '--help'], &lambda {|*args| help hacli, *args }
|
|
end
|
|
end
|
|
end
|