def handle(_name, cfg, cloud, log, _args): # Handle the old style + new config names update = _multi_cfg_bool_get(cfg, 'apt_update', 'package_update', 'repo_update') upgrade = _multi_cfg_bool_get(cfg, 'package_upgrade', 'apt_upgrade') reboot_if_required = _multi_cfg_bool_get(cfg, 'apt_reboot_if_required', 'package_reboot_if_required') pkglist = util.get_cfg_option_list(cfg, 'packages', []) # Amazon option that should also trigger an upgrade, but isn't bool upgrade_level = util.get_cfg_option_str(cfg, 'repo_upgrade', upgrade) if upgrade_level not in ('none', 'false', 'False'): upgrade = True else: # condense none, false, and False to none upgrade_level = 'none' upgrade_exclude = util.get_cfg_option_list(cfg, 'repo_upgrade_exclude', []) errors = [] if update or len(pkglist) or upgrade: try: cloud.distro.update_package_sources() except Exception as e: util.logexc(log, "Package update failed") errors.append(e) if upgrade: try: cloud.distro.upgrade_packages(upgrade_level, upgrade_exclude) except Exception as e: util.logexc(log, "Package upgrade failed") errors.append(e) if len(pkglist): try: cloud.distro.install_packages(pkglist) except Exception as e: util.logexc(log, "Failed to install packages: %s", pkglist) errors.append(e) # TODO(smoser): handle this less violently # kernel and openssl (possibly some other packages) # write a file /var/run/reboot-required after upgrading. # if that file exists and configured, then just stop right now and reboot reboot_fn_exists = os.path.isfile(REBOOT_FILE) if (upgrade or pkglist) and reboot_if_required and reboot_fn_exists: try: log.warn("Rebooting after upgrade or install per %s", REBOOT_FILE) # Flush the above warning + anything else out... logging.flushLoggers(log) _fire_reboot(log) except Exception as e: util.logexc(log, "Requested reboot did not happen!") errors.append(e) if len(errors): log.warn("%s failed with exceptions, re-raising the last one", len(errors)) raise errors[-1]
def handle(name, cfg, cloud, log, args): """Handler method activated by cloud-init.""" if not isinstance(cloud.distro, ubuntu.Distro): log.debug("%s: distro is '%s', not ubuntu. returning", name, cloud.distro.__class__) return cfg = util.mergemanydict([cfg, DEFAULT_CONFIG]) target = cfg['init_switch']['target'] reboot = cfg['init_switch']['reboot'] if len(args) != 0: target = args[0] if len(args) > 1: reboot = util.is_true(args[1]) if not target: log.debug("%s: target=%s. nothing to do", name, target) return if not util.which('dpkg'): log.warn("%s: 'dpkg' not available. Assuming not ubuntu", name) return supported = ('upstart', 'systemd') if target not in supported: log.warn("%s: target set to %s, expected one of: %s", name, target, str(supported)) if os.path.exists("/run/systemd/system"): current = "systemd" else: current = "upstart" if current == target: log.debug("%s: current = target = %s. nothing to do", name, target) return try: util.subp(['sh', '-s', target], data=SWITCH_INIT) except util.ProcessExecutionError as e: log.warn("%s: Failed to switch to init '%s'. %s", name, target, e) return if util.is_false(reboot): log.info("%s: switched '%s' to '%s'. reboot=false, not rebooting.", name, current, target) return try: log.warn("%s: switched '%s' to '%s'. rebooting.", name, current, target) logging.flushLoggers(log) _fire_reboot(log, wait_attempts=4, initial_sleep=4) except Exception as e: util.logexc(log, "Requested reboot did not happen!") raise
def handle(_name, cfg, cloud, log, _args): # Handle the old style + new config names update = _multi_cfg_bool_get(cfg, "apt_update", "package_update") upgrade = _multi_cfg_bool_get(cfg, "package_upgrade", "apt_upgrade") reboot_if_required = _multi_cfg_bool_get( cfg, "apt_reboot_if_required", "package_reboot_if_required" ) pkglist = util.get_cfg_option_list(cfg, "packages", []) errors = [] if update or len(pkglist) or upgrade: try: cloud.distro.update_package_sources() except Exception as e: util.logexc(log, "Package update failed") errors.append(e) if upgrade: try: cloud.distro.package_command("upgrade") except Exception as e: util.logexc(log, "Package upgrade failed") errors.append(e) if len(pkglist): try: cloud.distro.install_packages(pkglist) except Exception as e: util.logexc(log, "Failed to install packages: %s", pkglist) errors.append(e) # TODO(smoser): handle this less violently # kernel and openssl (possibly some other packages) # write a file /var/run/reboot-required after upgrading. # if that file exists and configured, then just stop right now and reboot reboot_fn_exists = os.path.isfile(REBOOT_FILE) if (upgrade or pkglist) and reboot_if_required and reboot_fn_exists: try: log.warning( "Rebooting after upgrade or install per %s", REBOOT_FILE ) # Flush the above warning + anything else out... logging.flushLoggers(log) _fire_reboot(log) except Exception as e: util.logexc(log, "Requested reboot did not happen!") errors.append(e) if len(errors): log.warning( "%s failed with exceptions, re-raising the last one", len(errors) ) raise errors[-1]
def handle(_name, cfg, cloud, log, _args): # Handle the old style + new config names update = _multi_cfg_bool_get(cfg, 'apt_update', 'package_update') upgrade = _multi_cfg_bool_get(cfg, 'package_upgrade', 'apt_upgrade') reboot_if_required = _multi_cfg_bool_get(cfg, 'apt_reboot_if_required', 'package_reboot_if_required') pkglist = util.get_cfg_option_list(cfg, 'packages', []) errors = [] if update or len(pkglist) or upgrade: try: cloud.distro.update_package_sources() except Exception as e: util.logexc(log, "Package update failed") errors.append(e) if upgrade: try: cloud.distro.package_command("upgrade") except Exception as e: util.logexc(log, "Package upgrade failed") errors.append(e) if len(pkglist): try: cloud.distro.install_packages(pkglist) except Exception as e: util.logexc(log, "Failed to install packages: %s", pkglist) errors.append(e) # TODO(smoser): handle this less violently # kernel and openssl (possibly some other packages) # write a file /var/run/reboot-required after upgrading. # if that file exists and configured, then just stop right now and reboot reboot_fn_exists = os.path.isfile(REBOOT_FILE) if (upgrade or pkglist) and reboot_if_required and reboot_fn_exists: try: log.warn("Rebooting after upgrade or install per %s", REBOOT_FILE) # Flush the above warning + anything else out... logging.flushLoggers(log) _fire_reboot(log) except Exception as e: util.logexc(log, "Requested reboot did not happen!") errors.append(e) if len(errors): log.warn("%s failed with exceptions, re-raising the last one", len(errors)) raise errors[-1]