Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions src/pvecontrol/actions/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import sys

import click
import proxmoxer.core

from pvecontrol.utils import print_task
from pvecontrol.cli import ResourceGroup, migration_related_command
from pvecontrol.models.vm import COLUMNS
from pvecontrol.cli import ResourceGroup, migration_related_command, task_related_command
from pvecontrol.models.vm import PVEVm, COLUMNS
from pvecontrol.models.cluster import PVECluster


Expand Down Expand Up @@ -77,12 +78,45 @@ def migrate(ctx, vmid, target, online, follow, wait, dry_run):
# Suivre la task cree
# pylint: disable=duplicate-code
proxmox.refresh()
_task = proxmox.find_task(upid)
print_task(proxmox, upid, follow, wait)
else:
print("Dry run, skipping migration")


@root.command()
@click.argument("vmid", type=int)
@click.option("-t", "--target", metavar="NODEID", required=True, help="ID of the target node")
@click.option(
"-a",
"--archive",
metavar="ARCHIVE",
required=True,
help="The archive to restore. Either the file system path to a .tar or .vma file or a proxmox storage backup volume identifier.",
)
@click.option(
"-s",
"--storage",
metavar="STORAGE",
help="Target storage ID where the VM's disks will be created (defaults to the storage from the backup configuration).",
)
@click.option("--force", is_flag=True, help="Overwrite existing VM")
@task_related_command
@click.pass_context
def restore(ctx, vmid, target, archive, storage, force, follow, wait):
"""Restore a VM from a backup archive"""

proxmox = PVECluster.create_from_config(ctx.obj["args"].cluster)

try:
upid = PVEVm.create(proxmox, vmid, target, archive=archive, storage=storage, force=force)
proxmox.refresh()
print_task(proxmox, upid, follow, wait)
except proxmoxer.core.ResourceException as e:
logging.error("Error creating VM: %s", e)
sys.exit(1)


# FIXME: merge with PVECluster.get_vm()
def _get_vm(proxmox, vmid):
for v in proxmox.vms:
logging.debug("_get_vm: %s", v)
Expand Down
1 change: 1 addition & 0 deletions src/pvecontrol/models/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def get_vm(self, vm_id):

result = None
node_name = None
# FIXME: wouldn't be easier AND faster to iterate over all nodes and vms directly?
for vm in self.resources_vms:
if vm["vmid"] == vm_id:
node_name = vm["node"]
Expand Down
6 changes: 6 additions & 0 deletions src/pvecontrol/models/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ def migrate(self, target, online=False):
upid = self._api.nodes(self.node).qemu(self.vmid).migrate.post(**options)
return upid

@staticmethod
def create(proxmox, vmid, target, **options):
if "force" in options:
options["force"] = 1 if options["force"] else 0
return proxmox.api.nodes(target).qemu().post(vmid=vmid, **options)

def get_backup_jobs(self, proxmox):
vm_backup_jobs = []
for backup_job in proxmox.backup_jobs:
Expand Down