예제 #1
0
def handle(_name, cfg, _cloud, log, args):
    if len(args) != 0:
        timezone = args[0]
    else:
        timezone = util.get_cfg_option_str(cfg, "timezone", False)

    if not timezone:
        return

    tz_file = os.path.join("/usr/share/zoneinfo", timezone)
    if not os.path.isfile(tz_file):
        log.debug("Invalid timezone %s" % tz_file)
        raise Exception("Invalid timezone %s" % tz_file)

    try:
        with open("/etc/timezone", "wb") as fp:
            fp.write("%s\n" % timezone)
    except:
        log.exception("Failed to write to /etc/timezone")

    if os.path.exists("/etc/sysconfig/clock"):
        try:
            with open("/etc/sysconfig/clock", "w") as fp:
                fp.write('ZONE="%s"\n' % timezone)
        except:
            log.exception("Failed to write to /etc/sysconfig/clock")

    try:
        shutil.copy(tz_file, "/etc/localtime")
    except:
        log.exception("Failed to copy %s to /etc/localtime" % tz_file)
예제 #2
0
def handle(_name, cfg, _cloud, log, args):
    if len(args) != 0:
        msg_in = args[0]
    else:
        msg_in = util.get_cfg_option_str(cfg, "final_message")

    if not msg_in:
        msg_in = "Finished at $TIMESTAMP. Up $UPTIME seconds"

    uptime = "na"
    try:
        with open("/proc/uptime", 'r') as fh:
            uptime_tmp = fh.read().split(" ")[0]
            uptime_tmp = uptime_tmp.strip()
            if uptime_tmp:
                uptime = uptime_tmp
    except IOError as e:
        log.warn("Unable to open /proc/uptime")

    try:
        ts = time.strftime("%a, %d %b %Y %H:%M:%S %z", time.gmtime())
    except:
        ts = "na"

    try:
        subs = {'UPTIME': uptime, 'TIMESTAMP': ts}
        log.info(util.render_string(msg_in, subs))
    except Exception as e:
        log.warn("Failed to render string: %s" % e)

    with open(boot_finished, "wb") as fp:
        fp.write(uptime + ":" + ts + "\n")
예제 #3
0
def handle(_name, cfg, cloud, log, args):

    locale = None
    if len(args) != 0:
        locale = args[0]
    else:
        locale = util.get_cfg_option_str(cfg, "locale", cloud.get_locale())

    locale_cfgfile = util.get_cfg_option_str(cfg, "locale_configfile",
                                             "/etc/default/locale")

    if not locale:
        return

    log.debug("Setting locale to %s" % locale)
    try:
        apply_locale(locale, locale_cfgfile)
    except Exception as e:
        log.debug(traceback.format_exc(e))
        raise Exception("Failed to apply locale %s" % locale)
예제 #4
0
def handle(_name, cfg, cloud, log, _args):
    (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
    manage_hosts = str(util.get_cfg_option_str(cfg, "manage_etc_hosts", 'False'))
    manage_hosts = manage_hosts.lower().strip()
    if manage_hosts in ["true", "template"]:
        # render from template file
        try:
            if not hostname:
                log.warn("manage_etc_hosts was set, but no hostname found")
                return
            tmpl_fn = 'hosts-%s' % (util.determine_platform())
            util.render_to_file(tmpl_fn, '/etc/hosts',
                                {'hostname': hostname, 'fqdn': fqdn})
        except Exception:
            log.warn("Failed to update /etc/hosts")
            raise
    else:
        if manage_hosts not in ["false"]:
            log.warn("Unknown value for manage_etc_hosts.  Assuming False.")
        else:
            log.debug("Not managing /etc/hosts")