Skip to content

Commit

Permalink
Adding proxy_host feature
Browse files Browse the repository at this point in the history
  • Loading branch information
remibergsma committed Oct 13, 2023
1 parent dac0d3a commit 18a7b9e
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions rolling_reboot.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@
help='Script to run on host after live migrations have completed')
@click.option('--post-reboot-script', metavar='<script>', help='Script to run after host has rebooted')
@click.option('--dry-run/--exec', is_flag=True, default=True, show_default=True, help='Enable/disable dry-run')
@click.option('--target-host', help='Target hypervisor the migrate VMS to', required=False)
@click.option('--proxy-host', help='Hypervisor the migrate VMS to, after which we migrate them back to origin', required=False)
@click_log.simple_verbosity_option(logging.getLogger(), default="INFO", show_default=True)
@click.argument('cluster')
@click.argument('target_host', required=False)
def main(profile, ignore_hosts, only_hosts, skip_os_version, reboot_action, pre_empty_script, post_empty_script,
post_reboot_script, dry_run, target_host, cluster):
post_reboot_script, dry_run, proxy_host, cluster, target_host):
"""Perform rolling reboot of hosts in CLUSTER"""

click_log.basic_config()
Expand All @@ -75,6 +76,19 @@ def main(profile, ignore_hosts, only_hosts, skip_os_version, reboot_action, pre_
hosts = cluster.get_all_hosts()
logging.debug(f"Found hosts: {hosts}")

if proxy_host:
proxy_host = co.get_host(name=proxy_host)
if proxy_host is None:
logging.info(f"Cannot find proxy host {proxy_host} in Cosmic!")
sys.exit(1)

logging.info(f"Using proxy host: migrate ALL VMs to {proxy_host['name']}, then back to origin")
if not ignore_hosts:
logging.info(f"Adding proxy host {proxy_host['name']} to ignore list")
ignore_hosts = proxy_host['name']
else:
ignore_hosts += f",{proxy_host['name']}"

if ignore_hosts:
ignore_hosts = ignore_hosts.replace(' ', '').split(',')
logging.info(f"Ignoring hosts: {str(ignore_hosts)}")
Expand All @@ -91,8 +105,6 @@ def main(profile, ignore_hosts, only_hosts, skip_os_version, reboot_action, pre_

hosts.sort(key=itemgetter('name'))

if target_host:
target_host = co.get_host(name=target_host)
for host in hosts:
logging.slack_value = host['name']
logging.zone_name = host['zonename']
Expand All @@ -119,10 +131,14 @@ def main(profile, ignore_hosts, only_hosts, skip_os_version, reboot_action, pre_
log_to_slack)

while True:
if proxy_host:
target_host = proxy_host
(_, _, failed) = host.empty(target=target_host)
if failed == 0:
break

logging.warning(f"Failed to empty host '{host['name']}', retrying...", log_to_slack)

if target_host:
logging.warning(
f"Failed to empty host '{host['name']}' with target '{target_host['name']}', resetting target host and retrying...",
Expand Down Expand Up @@ -155,6 +171,15 @@ def main(profile, ignore_hosts, only_hosts, skip_os_version, reboot_action, pre_

host.restart_vms_with_shutdown_policy()

if proxy_host:
logging.info(f"Host '{host['name']}' is now empty (VMs are on proxy host {proxy_host['name']}). "
f"Will now migrate VMs back to origin {host['name']}...", log_to_slack)
while True:
(_, _, failed) = proxy_host.empty(target=host)
if failed == 0:
break
logging.info(f"Host '{host['name']}' is done. It should now have the same VMs as before", log_to_slack)

target_host = host


Expand Down

0 comments on commit 18a7b9e

Please sign in to comment.