Skip to content

Commit

Permalink
Merge pull request #99 from MissionCriticalCloud/allow-local-vm-live-…
Browse files Browse the repository at this point in the history
…migrates

Allow local vm live migrates
  • Loading branch information
remibergsma authored Dec 1, 2023
2 parents 487f737 + f7b7daa commit 815407f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
37 changes: 28 additions & 9 deletions live_migrate_virtual_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,24 @@
@click.option('--skip-backingfile-merge', is_flag=True, help='Do not attempt merge backing file')
@click.option('--skip-within-cluster', is_flag=True, default=False, show_default=True,
help='Enable/disable migration within cluster')
@click.option('--only-within-cluster', is_flag=True, default=False, show_default=True,
help='Only do migration within cluster')
@click.option('--dry-run/--exec', is_flag=True, default=True, show_default=True, help='Enable/disable dry-run')
@click_log.simple_verbosity_option(logging.getLogger(), default="INFO", show_default=True)
@click.argument('vm-name')
@click.argument('cluster')
@click.argument('cluster', required=False)
def main(profile, zwps_to_cwps, migrate_offline_with_rsync, rsync_target_host, add_affinity_group, destination_dc, is_project_vm,
avoid_storage_pool, skip_backingfile_merge, skip_within_cluster, dry_run, vm_name, cluster):
"""Live migrate VM to CLUSTER"""
avoid_storage_pool, skip_backingfile_merge, skip_within_cluster, only_within_cluster, dry_run, vm_name, cluster):
"""Live migrate VM"""
"""Unless --migrate-offline-with-rsync is passed, then we migrate offline"""
# TODO break this down into funtions no more than 30 lines # noqa
# TODO change the default behaviour to "migrate within the current cluster" and add a specific option to "migrate to another cluster" e.g. --to-cluster # noqa

# 2022-01-01, after an upgrade of an unknow component of KVM/CentOS, the migration to another cluster caused a network hiccup,
# the migration within a cluster is added to mitigate a network hiccup during a migration to another cluster

# Live migrate requires running VM. Unless migrate_offline_with_rsync==True, then we stop the VM as this is offline


click_log.basic_config()

Expand All @@ -65,14 +75,20 @@ def main(profile, zwps_to_cwps, migrate_offline_with_rsync, rsync_target_host, a

cs = CosmicSQL(server=profile, dry_run=dry_run)

# Work around migration issue: first in the same pod to limit possible hiccup
vm = co.get_vm(name=vm_name, is_project_vm=is_project_vm)

if not vm:
logging.error(f"Cannot migrate, VM '{vm_name}' not found!")
sys.exit(1)

# Live migrate requires running VM. Unless migrate_offline_with_rsync==True, then we stop the VM as this is offline
if skip_within_cluster and only_within_cluster:
logging.error(f"Cannot use 'skip_within_cluster' together with 'only_within_cluster'!")
sys.exit(1)

if not only_within_cluster and not cluster:
logging.error(f"We need a cluster name if you're not only migrating within the cluster!")
sys.exit(1)

if not migrate_offline_with_rsync:
if not vm['state'] == 'Running':
logging.error(f"Cannot migrate, VM has has state: '{vm['state']}'")
Expand All @@ -86,10 +102,11 @@ def main(profile, zwps_to_cwps, migrate_offline_with_rsync, rsync_target_host, a
logging.info(f"VM Migration failed at {datetime.now().strftime('%d-%m-%Y %H:%M:%S')}\n")
sys.exit(1)

if not live_migrate(co, cs, cluster, vm_name, destination_dc, add_affinity_group, is_project_vm, zwps_to_cwps,
log_to_slack, dry_run):
logging.info(f"VM Migration failed at {datetime.now().strftime('%d-%m-%Y %H:%M:%S')}\n")
sys.exit(1)
if not only_within_cluster:
if not live_migrate(co, cs, cluster, vm_name, destination_dc, add_affinity_group, is_project_vm, zwps_to_cwps,
log_to_slack, dry_run):
logging.info(f"VM Migration failed at {datetime.now().strftime('%d-%m-%Y %H:%M:%S')}\n")
sys.exit(1)
logging.info(f"VM Migration completed at {datetime.now().strftime('%d-%m-%Y %H:%M:%S')}\n")

if migrate_offline_with_rsync:
Expand Down Expand Up @@ -316,10 +333,12 @@ def live_migrate(co, cs, cluster, vm_name, destination_dc, add_affinity_group, i

target_cluster = co.get_cluster(name=cluster)
if not target_cluster:
logging.error(f"Cannot migrate, cluster '{cluster}' not found!")
return False

vm = co.get_vm(name=vm_name, is_project_vm=is_project_vm)
if not vm:
logging.error(f"Cannot migrate, VM '{vm_name}' not found!")
return False

if not vm['state'] == 'Running':
Expand Down
16 changes: 15 additions & 1 deletion tests/test_live_migrate_virtual_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def test_main(self):
self.cs_instance.update_zwps_to_cwps.assert_not_called()
self.cs_instance.update_service_offering_of_vm.assert_not_called()
self.target_cluster.find_migration_host.assert_called_with(self.vm)
self.source_host.get_disks.assert_called_with(self.vm)
self.source_host.get_disks.assert_called_with(self.vm['instancename'])
self.cs_instance.get_volume_size.assert_called_with('path1')
self.cs_instance.update_volume_size.assert_not_called()
self.vm.migrate_within_cluster.assert_called()
Expand Down Expand Up @@ -259,6 +259,20 @@ def test_failures(self):
self.vm.migrate_within_cluster.assert_not_called()
self.vm.migrate.assert_called()

self._setup_mocks()
self.vm.migrate.return_value = False
self.assertEqual(1, self.runner.invoke(live_migrate_virtual_machine.main,
['--exec', '--skip-within-cluster', '--only-within-cluster', '-p', 'profile', 'vm', 'target_cluster']).exit_code)
self.vm.migrate_within_cluster.assert_not_called()
self.vm.migrate.assert_not_called()

self._setup_mocks()
self.vm.migrate.return_value = False
self.assertEqual(0, self.runner.invoke(live_migrate_virtual_machine.main,
['--exec', '--only-within-cluster', '-p', 'profile', 'vm', 'target_cluster']).exit_code)
self.vm.migrate_within_cluster.assert_called()
self.vm.migrate.assert_not_called()

self._setup_mocks()
self.vm['hostname'] = self.source_host['name']
self.assertEqual(1, self.runner.invoke(live_migrate_virtual_machine.main,
Expand Down

0 comments on commit 815407f

Please sign in to comment.