3
# Copyright (C) 2012 Yahoo! Inc.
5
# Author: Joshua Harlow <harlowja@yahoo-inc.com>
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License version 3, as
9
# published by the Free Software Foundation.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
from cloudinit import log as logging
23
from cloudinit import util
25
REBOOT_FILE = "/var/run/reboot-required"
26
REBOOT_CMD = ["/sbin/reboot"]
29
def _multi_cfg_bool_get(cfg, *keys):
31
if util.get_cfg_option_bool(cfg, k, False):
36
def _fire_reboot(log, wait_attempts=6, initial_sleep=1, backoff=2):
39
wait_time = initial_sleep
40
for _i in range(0, wait_attempts):
43
elapsed = time.time() - start
44
log.debug("Rebooted, but still running after %s seconds", int(elapsed))
45
# If we got here, not good
46
elapsed = time.time() - start
47
raise RuntimeError(("Reboot did not happen"
48
" after %s seconds!") % (int(elapsed)))
51
def handle(_name, cfg, cloud, log, _args):
52
# Handle the old style + new config names
53
update = _multi_cfg_bool_get(cfg, 'apt_update', 'package_update')
54
upgrade = _multi_cfg_bool_get(cfg, 'package_upgrade', 'apt_upgrade')
55
reboot_if_required = _multi_cfg_bool_get(cfg, 'apt_reboot_if_required',
56
'package_reboot_if_required')
57
pkglist = util.get_cfg_option_list(cfg, 'packages', [])
60
if update or len(pkglist) or upgrade:
62
cloud.distro.update_package_sources()
63
except Exception as e:
64
util.logexc(log, "Package update failed")
69
cloud.distro.package_command("upgrade")
70
except Exception as e:
71
util.logexc(log, "Package upgrade failed")
76
cloud.distro.install_packages(pkglist)
77
except Exception as e:
78
util.logexc(log, "Failed to install packages: %s", pkglist)
81
# TODO(smoser): handle this less violently
82
# kernel and openssl (possibly some other packages)
83
# write a file /var/run/reboot-required after upgrading.
84
# if that file exists and configured, then just stop right now and reboot
85
reboot_fn_exists = os.path.isfile(REBOOT_FILE)
86
if (upgrade or pkglist) and reboot_if_required and reboot_fn_exists:
88
log.warn("Rebooting after upgrade or install per %s", REBOOT_FILE)
89
# Flush the above warning + anything else out...
90
logging.flushLoggers(log)
92
except Exception as e:
93
util.logexc(log, "Requested reboot did not happen!")
97
log.warn("%s failed with exceptions, re-raising the last one",