def icinga_cancel_downtime(host): icinga_host = lib.resolve_dns('icinga.wikimedia.org', 'CNAME') cmd = 'echo "[{now}] DEL_DOWNTIME_BY_HOST_NAME;{hostname}" > {commandfile}'.format( now=int(time.time()), hostname=host.split('.')[0], commandfile=get_commandfile()) return run_cumin(icinga_host, [cmd], timeout=300)
def main(): """Downtime a single host on Icinga.""" args = parse_args() user = lib.get_running_user() if args.debug: logger.setLevel(logging.DEBUG) if args.sleep: lib.print_line('Sleeping for {s} seconds'.format(s=args.sleep), host=args.host) time.sleep(args.sleep) lib.print_line('Running Puppet on the Icinga server', host=args.host) try: if args.debug: lib.run_puppet([lib.resolve_dns(lib.ICINGA_DOMAIN, 'CNAME')], no_raise=True) lib.icinga_downtime(args.host, user, args.phab_task_id, title='wmf-downtime-host') else: # This is needed due to a bug in tqdm and a limitation in Cumin with open(os.devnull, 'w', 1) as cumin_output: stderr = sys.stderr stdout = sys.stdout sys.stderr = cumin_output sys.stdout = cumin_output lib.run_puppet([lib.resolve_dns(lib.ICINGA_DOMAIN, 'CNAME')], no_raise=True) lib.icinga_downtime(args.host, user, args.phab_task_id, title='wmf-downtime-host') retcode = 0 except BaseException as e: message = 'Unable to run wmf-downtime-host' lib.print_line('{message}: {error}'.format(message=message, error=e), host=args.host) logger.exception(message) retcode = 2 finally: if not args.debug: sys.stderr = stderr sys.stdout = stdout return retcode
def run(args, user, log_path): """Run the WMF auto reimage according to command line arguments. Arguments: args -- parsed command line arguments user -- the user that launched the script, for auditing purposes log_path -- the path of the logfile """ previous = None # Previous state in conftool rename_from = None # In case of host rename, hold the previous hostname # Validate hosts have a signed Puppet certificate if not args.new: lib.validate_hosts([args.host], no_raise=args.no_verify) # Set Icinga downtime if not args.new and not args.no_downtime: lib.icinga_downtime(args.host, user, args.phab_task_id) # Depool via conftool if args.conftool and not args.new: previous = lib.conftool_depool(args.host, pooled=args.conftool_value) lib.print_line('Waiting 3 minutes to let the host drain', host=args.host) time.sleep(180) if args.no_pxe: lib.print_line('Skipping PXE reboot', host=args.host) if (not lib.validate_hosts([args.host], no_raise=True) and lib.puppet_check_cert_to_sign(args.host) == 1): # There is no signed or pending signing certificate for the host lib.puppet_generate_cert(args.host) else: lib.puppet_remove_host(args.host) # Cleanup Puppet # Reboot into PXE mode to start the reimage lib.set_pxe_boot(args.host, args.mgmt) status = lib.ipmitool_command(args.mgmt, ['chassis', 'power', 'status']) if status.startswith('Chassis Power is off'): lib.print_line('Current power status is off, powering on', host=args.host) ipmi_command = ['chassis', 'power', 'on'] else: lib.print_line('Power cycling', host=args.host) ipmi_command = ['chassis', 'power', 'cycle'] lib.print_line(lib.ipmitool_command(args.mgmt, ipmi_command).rstrip('\n'), host=args.host) # If the host is renamed, swap the hostnames now if args.rename is not None: rename_from = args.host args.host = args.rename args.mgmt = args.rename_mgmt # Wait that the host is booting into the installer using Cumin's direct backend lib.wait_reboot(args.host, start=datetime.utcnow(), installer_key=True, debian_installer=True) # Wait for the reboot into the new system time.sleep( 30 ) # Avoid race conditions, the host is in the d-i, need to wait anyway lib.wait_reboot(args.host, start=datetime.utcnow(), installer_key=True) # Generate the Puppet certificate and signing request if lib.detect_init_system(args.host) == 'systemd': lib.puppet_generate_cert(args.host) # Sign the new Puppet certificate if lib.puppet_wait_cert_and_sign(args.host): if args.mask: # Mask systemd services lib.mask_systemd_services(args.host, args.mask) lib.puppet_first_run(args.host) # Ensure the host is in Icinga lib.run_puppet([lib.resolve_dns(lib.ICINGA_DOMAIN, 'CNAME')], no_raise=True) lib.icinga_downtime(args.host, user, args.phab_task_id) lib.check_bios_bootparams(args.host, args.mgmt) # Issue a reboot and wait for it and also for Puppet to complete if not args.no_reboot: reboot_time = datetime.utcnow() # Ensure the host is in the known hosts lib.run_puppet([socket.getfqdn()], no_raise=True) lib.reboot_host(args.host) boot_time = datetime.utcnow() lib.wait_reboot(args.host, start=reboot_time) lib.wait_puppet_run(args.host, start=boot_time) # Run Apache fast test if args.apache: lib.run_apache_fast_test(args.host) # The unmask is *not* done automatically, the commands to unmask are printed and logged if args.mask: lib.print_unmask_message(args.host, args.mask) # The repool is *not* done automatically, the command to repool is printed and logged if args.conftool: lib.print_repool_message(previous, rename_from=rename_from, rename_to=args.rename) lib.print_line('Reimage completed', host=args.host)
def icinga_downtime(host, reason, seconds): icinga_host = lib.resolve_dns('icinga.wikimedia.org', 'CNAME') cmd = 'icinga-downtime -h {host} -d {seconds} -r "{reason}"'.format( host=host.split('.')[0], seconds=seconds, reason=reason) return run_cumin(icinga_host, [cmd], timeout=60)