Add VM automirror support

Allows shipping snapshots automatically to remote clusters on a cron,
identically to how autobackup handles local snapshot exports.

VMs are selected based on configured tags, and individual destination
clusters can be specified based on a colon-separated suffix to the
tag(s).

Automirror snapshots use the prefix "am" (analogous to "ab" for
autobackups) to differentiate them from normal "mr" mirrors.
This commit is contained in:
2024-11-15 01:37:27 -05:00
parent cebc660fb0
commit 078d48a50b
8 changed files with 1010 additions and 120 deletions

View File

@ -2577,6 +2577,89 @@ def cli_vm_autobackup(email_report, force_full_flag, wait_flag, cron_flag):
finish(retcode, retmsg)
###############################################################################
# > pvc vm automirror
###############################################################################
@click.command(
name="automirror", short_help="Perform automatic virtual machine mirrors."
)
@connection_req
@click.option(
"--email-report",
"email_report",
default=None,
help="Email a mirror summary report to the specified address(es), comma-separated.",
)
@click.option(
"--email-errors-only",
"email_errors_only_flag",
is_flag=True,
default=False,
show_default=True,
help="Only send a mirror summary report when at least one error occurrs.",
)
@click.option(
"--wait/--no-wait",
"wait_flag",
is_flag=True,
default=True,
show_default=True,
help="Wait or don't wait for task to complete, showing progress if waiting.",
)
@click.option(
"--cron",
"cron_flag",
is_flag=True,
default=False,
show_default=True,
help="Run in cron mode (returns immediately with no output once job is submitted).",
)
def cli_vm_automirror(email_report, email_errors_only_flag, wait_flag, cron_flag):
"""
Perform automated mirrors of VMs, with integrated cleanup and full/incremental scheduling.
This command enables automatic mirrors of PVC VMs at the block level, leveraging the various "pvc vm snapshot"
functions with an internal rentention and cleanup system. VMs and the destination cluster(s) are selected based
on configured VM tags and a set of static configs in the cluster's `pvc.conf` configuration.
This command should be run from cron or a timer at a regular interval (e.g. daily, hourly, etc.) which defines
how often mirrors are taken. Mirror retention is based only on the number of recorded mirrors on the remote side,
not on the time interval between them. Mirrors taken manually outside of the "automirror" command are not counted
towards the format or retention of automirrors.
WARNING: Running this command manually will interfere with the schedule! Do not run manually except for testing.
The actual details of the automirror, including retention policies, are defined in the main PVC configuration file
`/etc/pvc/pvc.conf`. See the sample configuration for more details.
An optional report on all current mirrors can be emailed to one or more email addresses using the
"--email-report" flag. This report will include information on all current known mirrors.
"""
if cron_flag:
wait_flag = False
if email_report is not None:
email_recipients = email_report.split(",")
else:
email_recipients = None
retcode, retmsg = pvc.lib.vm.vm_automirror(
CLI_CONFIG,
email_recipients=email_recipients,
email_errors_only_flag=email_errors_only_flag,
wait_flag=wait_flag,
)
if retcode and wait_flag:
retmsg = wait_for_celery_task(CLI_CONFIG, retmsg)
if cron_flag:
finish(retcode, None)
else:
finish(retcode, retmsg)
###############################################################################
# > pvc vm tag
###############################################################################
@ -6918,6 +7001,7 @@ cli_vm_backup.add_command(cli_vm_backup_restore)
cli_vm_backup.add_command(cli_vm_backup_remove)
cli_vm.add_command(cli_vm_backup)
cli_vm.add_command(cli_vm_autobackup)
cli_vm.add_command(cli_vm_automirror)
cli_vm_tag.add_command(cli_vm_tag_get)
cli_vm_tag.add_command(cli_vm_tag_add)
cli_vm_tag.add_command(cli_vm_tag_remove)

View File

@ -714,6 +714,26 @@ def vm_autobackup(config, email_recipients=None, force_full_flag=False, wait_fla
return get_wait_retdata(response, wait_flag)
def vm_automirror(
config, email_recipients=None, email_errors_only_flag=False, wait_flag=True
):
"""
Perform a cluster VM automirror
API endpoint: POST /vm//automirror
API arguments: email_recipients=email_recipients, email_errors_only=email_errors_only_flag
API schema: {"message":"{data}"}
"""
params = {
"email_recipients": email_recipients,
"email_errors_only": email_errors_only_flag,
}
response = call_api(config, "post", "/vm/automirror", params=params)
return get_wait_retdata(response, wait_flag)
def vm_vcpus_set(config, vm, vcpus, topology, restart):
"""
Set the vCPU count of the VM with topology