コード例 #1
0
def handle(name, cfg, cloud, log, _args):
    manage_hosts = util.get_cfg_option_str(cfg, "manage_etc_hosts", False)
    if util.translate_bool(manage_hosts, addons=['template']):
        (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
        if not hostname:
            log.warn(("Option 'manage_etc_hosts' was set,"
                     " but no hostname was found"))
            return

        # Render from a template file
        tpl_fn_name = cloud.get_template_filename("hosts.%s" %
                                                  (cloud.distro.osfamily))
        if not tpl_fn_name:
            raise RuntimeError(("No hosts template could be"
                                " found for distro %s") %
                               (cloud.distro.osfamily))

        templater.render_to_file(tpl_fn_name, '/etc/hosts',
                                 {'hostname': hostname, 'fqdn': fqdn})

    elif manage_hosts == "localhost":
        (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
        if not hostname:
            log.warn(("Option 'manage_etc_hosts' was set,"
                     " but no hostname was found"))
            return

        log.debug("Managing localhost in /etc/hosts")
        cloud.distro.update_etc_hosts(hostname, fqdn)
    else:
        log.debug(("Configuration option 'manage_etc_hosts' is not set,"
                   " not managing /etc/hosts in module %s"), name)
コード例 #2
0
def search_for_mirror_dns(configured, mirrortype, cfg, cloud):
    """
    Try to resolve a list of predefines DNS names to pick mirrors
    """
    mirror = None

    if configured:
        mydom = ""
        doms = []

        if mirrortype == "primary":
            mirrordns = "mirror"
        elif mirrortype == "security":
            mirrordns = "security-mirror"
        else:
            raise ValueError("unknown mirror type")

        # if we have a fqdn, then search its domain portion first
        (_, fqdn) = util.get_hostname_fqdn(cfg, cloud)
        mydom = ".".join(fqdn.split(".")[1:])
        if mydom:
            doms.append(".%s" % mydom)

        doms.extend((".localdomain", "",))

        mirror_list = []
        distro = cloud.distro.name
        mirrorfmt = "http://%s-%s%s/%s" % (distro, mirrordns, "%s", distro)
        for post in doms:
            mirror_list.append(mirrorfmt % (post))

        mirror = search_for_mirror(mirror_list)

    return mirror
コード例 #3
0
def handle(_name, cfg, cloud, log, _args):
    (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)

    manage_hosts = util.get_cfg_option_str(cfg, "manage_etc_hosts", False)
    if manage_hosts in ("True", "true", True, "template"):
        # render from template file
        try:
            if not hostname:
                log.info("manage_etc_hosts was set, but no hostname found")
                return

            util.render_to_file('hosts', '/etc/hosts',
                                {'hostname': hostname, 'fqdn': fqdn})
        except Exception:
            log.warn("failed to update /etc/hosts")
            raise
    elif manage_hosts == "localhost":
        log.debug("managing 127.0.1.1 in /etc/hosts")
        update_etc_hosts(hostname, fqdn, log)
        return
    else:
        if manage_hosts not in ("False", False):
            log.warn("Unknown value for manage_etc_hosts.  Assuming False")
        else:
            log.debug("not managing /etc/hosts")
コード例 #4
0
def handle(name, cfg, cloud, log, _args):
    if util.get_cfg_option_bool(cfg, "preserve_hostname", False):
        log.debug(("Configuration option 'preserve_hostname' is set,"
                   " not setting the hostname in module %s"), name)
        return
    (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
    # Check for previous successful invocation of set-hostname

    # set-hostname artifact file accounts for both hostname and fqdn
    # deltas. As such, it's format is different than cc_update_hostname's
    # previous-hostname file which only contains the base hostname.
    # TODO consolidate previous-hostname and set-hostname artifact files and
    # distro._read_hostname implementation so we only validate  one artifact.
    prev_fn = os.path.join(cloud.get_cpath('data'), "set-hostname")
    prev_hostname = {}
    if os.path.exists(prev_fn):
        prev_hostname = util.load_json(util.load_file(prev_fn))
    hostname_changed = (hostname != prev_hostname.get('hostname') or
                        fqdn != prev_hostname.get('fqdn'))
    if not hostname_changed:
        log.debug('No hostname changes. Skipping set-hostname')
        return
    log.debug("Setting the hostname to %s (%s)", fqdn, hostname)
    try:
        cloud.distro.set_hostname(hostname, fqdn)
    except Exception as e:
        msg = "Failed to set the hostname to %s (%s)" % (fqdn, hostname)
        util.logexc(log, msg)
        raise SetHostnameError("%s: %s" % (msg, e))
    write_json(prev_fn, {'hostname': hostname, 'fqdn': fqdn})
コード例 #5
0
ファイル: test_util.py プロジェクト: cloud-init/cloud-init
 def test_get_hostname_fqdn_from_passes_metadata_only_to_cloud(self):
     """Calls to cloud.get_hostname pass the metadata_only parameter."""
     mycloud = FakeCloud('cloudhost', 'cloudhost.mycloud.com')
     _hn, _fqdn = util.get_hostname_fqdn(
         cfg={}, cloud=mycloud, metadata_only=True)
     self.assertEqual(
         [{'fqdn': True, 'metadata_only': True},
          {'metadata_only': True}], mycloud.calls)
コード例 #6
0
ファイル: test_util.py プロジェクト: cloud-init/cloud-init
 def test_get_hostname_fqdn_from_without_fqdn_or_hostname(self):
     """When cfg has neither hostname nor fqdn cloud.get_hostname."""
     mycloud = FakeCloud('cloudhost', 'cloudhost.mycloud.com')
     hostname, fqdn = util.get_hostname_fqdn(cfg={}, cloud=mycloud)
     self.assertEqual('cloudhost', hostname)
     self.assertEqual('cloudhost.mycloud.com', fqdn)
     self.assertEqual(
         [{'fqdn': True, 'metadata_only': False},
          {'metadata_only': False}], mycloud.calls)
コード例 #7
0
ファイル: test_util.py プロジェクト: cloud-init/cloud-init
 def test_get_hostname_fqdn_from_cfg_hostname_without_domain(self):
     """When cfg has a hostname without a '.' query cloud.get_hostname."""
     mycloud = FakeCloud('cloudhost', 'cloudhost.mycloud.com')
     hostname, fqdn = util.get_hostname_fqdn(
         cfg={'hostname': 'myhost'}, cloud=mycloud)
     self.assertEqual('myhost', hostname)
     self.assertEqual('cloudhost.mycloud.com', fqdn)
     self.assertEqual(
         [{'fqdn': True, 'metadata_only': False}], mycloud.calls)
コード例 #8
0
def handle(_name, cfg, cloud, log, _args):
    if util.get_cfg_option_bool(cfg, "preserve_hostname", False):
        log.debug("preserve_hostname is set. not updating hostname")
        return

    (hostname, _fqdn) = util.get_hostname_fqdn(cfg, cloud)
    try:
        prev = "%s/%s" % (cloud.get_cpath('data'), "previous-hostname")
        update_hostname(hostname, prev, log)
    except Exception:
        log.warn("failed to set hostname\n")
        raise
コード例 #9
0
def handle(_name, cfg, cloud, log, _args):
    if util.get_cfg_option_bool(cfg, "preserve_hostname", False):
        log.debug("preserve_hostname is set. not setting hostname")
        return(True)

    (hostname, _fqdn) = util.get_hostname_fqdn(cfg, cloud)
    try:
        set_hostname(hostname, log)
    except Exception:
        util.logexc(log)
        log.warn("failed to set hostname to %s\n", hostname)

    return(True)
コード例 #10
0
def handle(name, cfg, cloud, log, _args):
    if util.get_cfg_option_bool(cfg, "preserve_hostname", False):
        log.debug(("Configuration option 'preserve_hostname' is set,"
                    " not setting the hostname in module %s"), name)
        return

    (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
    try:
        log.debug("Setting the hostname to %s (%s)", fqdn, hostname)
        cloud.distro.set_hostname(hostname, fqdn)
    except Exception:
        util.logexc(log, "Failed to set the hostname to %s (%s)", fqdn,
                    hostname)
        raise
コード例 #11
0
def handle(name, cfg, cloud, log, _args):
    if util.get_cfg_option_bool(cfg, "preserve_hostname", False):
        log.debug(("Configuration option 'preserve_hostname' is set,"
                   " not updating the hostname in module %s"), name)
        return

    (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
    try:
        prev_fn = os.path.join(cloud.get_cpath('data'), "previous-hostname")
        log.debug("Updating hostname to %s (%s)", fqdn, hostname)
        cloud.distro.update_hostname(hostname, fqdn, prev_fn)
    except Exception:
        util.logexc(log, "Failed to update the hostname to %s (%s)", fqdn,
                    hostname)
        raise
コード例 #12
0
ファイル: main.py プロジェクト: cloud-init/cloud-init
def _maybe_set_hostname(init, stage, retry_stage):
    """Call set-hostname if metadata, vendordata or userdata provides it.

    @param stage: String representing current stage in which we are running.
    @param retry_stage: String represented logs upon error setting hostname.
    """
    cloud = init.cloudify()
    (hostname, _fqdn) = util.get_hostname_fqdn(
        init.cfg, cloud, metadata_only=True)
    if hostname:  # meta-data or user-data hostname content
        try:
            cc_set_hostname.handle('set-hostname', init.cfg, cloud, LOG, None)
        except cc_set_hostname.SetHostnameError as e:
            LOG.debug(
                'Failed setting hostname in %s stage. Will'
                ' retry in %s stage. Error: %s.', stage, retry_stage, str(e))
コード例 #13
0
def _maybe_set_hostname(init, stage, retry_stage):
    """Call set-hostname if metadata, vendordata or userdata provides it.

    @param stage: String representing current stage in which we are running.
    @param retry_stage: String represented logs upon error setting hostname.
    """
    cloud = init.cloudify()
    (hostname, _fqdn) = util.get_hostname_fqdn(init.cfg,
                                               cloud,
                                               metadata_only=True)
    if hostname:  # meta-data or user-data hostname content
        try:
            cc_set_hostname.handle('set-hostname', init.cfg, cloud, LOG, None)
        except cc_set_hostname.SetHostnameError as e:
            LOG.debug(
                'Failed setting hostname in %s stage. Will'
                ' retry in %s stage. Error: %s.', stage, retry_stage, str(e))
コード例 #14
0
def handle(name, cfg, cloud, log, _args):
    if util.get_cfg_option_bool(cfg, "preserve_hostname", False):
        log.debug(
            "Configuration option 'preserve_hostname' is set,"
            " not setting the hostname in module %s",
            name,
        )
        return

    # Set prefer_fqdn_over_hostname value in distro
    hostname_fqdn = util.get_cfg_option_bool(
        cfg, "prefer_fqdn_over_hostname", None
    )
    if hostname_fqdn is not None:
        cloud.distro.set_option("prefer_fqdn_over_hostname", hostname_fqdn)

    (hostname, fqdn, is_default) = util.get_hostname_fqdn(cfg, cloud)
    # Check for previous successful invocation of set-hostname

    # set-hostname artifact file accounts for both hostname and fqdn
    # deltas. As such, it's format is different than cc_update_hostname's
    # previous-hostname file which only contains the base hostname.
    # TODO consolidate previous-hostname and set-hostname artifact files and
    # distro._read_hostname implementation so we only validate  one artifact.
    prev_fn = os.path.join(cloud.get_cpath("data"), "set-hostname")
    prev_hostname = {}
    if os.path.exists(prev_fn):
        prev_hostname = util.load_json(util.load_file(prev_fn))
    hostname_changed = hostname != prev_hostname.get(
        "hostname"
    ) or fqdn != prev_hostname.get("fqdn")
    if not hostname_changed:
        log.debug("No hostname changes. Skipping set-hostname")
        return
    if is_default and hostname == "localhost":
        # https://github.com/systemd/systemd/commit/d39079fcaa05e23540d2b1f0270fa31c22a7e9f1
        log.debug("Hostname is localhost. Let other services handle this.")
        return
    log.debug("Setting the hostname to %s (%s)", fqdn, hostname)
    try:
        cloud.distro.set_hostname(hostname, fqdn)
    except Exception as e:
        msg = "Failed to set the hostname to %s (%s)" % (fqdn, hostname)
        util.logexc(log, msg)
        raise SetHostnameError("%s: %s" % (msg, e)) from e
    write_json(prev_fn, {"hostname": hostname, "fqdn": fqdn})
コード例 #15
0
def find_apt_mirror(cloud, cfg):
    """ find an apt_mirror given the cloud and cfg provided """

    # TODO: distro and defaults should be configurable
    distro = "ubuntu"
    defaults = {
        'ubuntu': "http://archive.ubuntu.com/ubuntu",
        'debian': "http://archive.debian.org/debian",
    }
    mirror = None

    cfg_mirror = cfg.get("apt_mirror", None)
    if cfg_mirror:
        mirror = cfg["apt_mirror"]
    elif "apt_mirror_search" in cfg:
        mirror = util.search_for_mirror(cfg['apt_mirror_search'])
    else:
        if cloud:
            mirror = cloud.get_mirror()

        mydom = ""

        doms = []

        if not mirror and cloud:
            # if we have a fqdn, then search its domain portion first
            (_hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
            mydom = ".".join(fqdn.split(".")[1:])
            if mydom:
                doms.append(".%s" % mydom)

        if not mirror:
            doms.extend((".localdomain", "",))

            mirror_list = []
            mirrorfmt = "http://%s-mirror%s/%s" % (distro, "%s", distro)
            for post in doms:
                mirror_list.append(mirrorfmt % post)

            mirror = util.search_for_mirror(mirror_list)

    if not mirror:
        mirror = defaults[distro]

    return mirror
コード例 #16
0
def find_apt_mirror_info(cloud, cfg):
    """find an apt_mirror given the cloud and cfg provided."""

    mirror = None

    # this is less preferred way of specifying mirror preferred would be to
    # use the distro's search or package_mirror.
    mirror = cfg.get("apt_mirror", None)

    search = cfg.get("apt_mirror_search", None)
    if not mirror and search:
        mirror = util.search_for_mirror(search)

    if (not mirror and
            util.get_cfg_option_bool(cfg, "apt_mirror_search_dns", False)):
        mydom = ""
        doms = []

        # if we have a fqdn, then search its domain portion first
        (_hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
        mydom = ".".join(fqdn.split(".")[1:])
        if mydom:
            doms.append(".%s" % mydom)

        doms.extend((".localdomain", "",))

        mirror_list = []
        distro = cloud.distro.name
        mirrorfmt = "http://%s-mirror%s/%s" % (distro, "%s", distro)
        for post in doms:
            mirror_list.append(mirrorfmt % (post))

        mirror = util.search_for_mirror(mirror_list)

    mirror_info = cloud.datasource.get_package_mirror_info()

    # this is a bit strange.
    # if mirror is set, then one of the legacy options above set it
    # but they do not cover security. so we need to get that from
    # get_package_mirror_info
    if mirror:
        mirror_info.update({'primary': mirror})

    return mirror_info
コード例 #17
0
def handle(name, _cfg, _cloud, log, _args):
    default_interface = 'eth0'
    system_info = util.system_info()
    if 'aix' in system_info['platform'].lower():
        default_interface = 'en0'

    interface = util.get_cfg_option_str(_cfg,
                                        'set_hostname_from_interface',
                                        default=default_interface)
    log.debug('Setting hostname based on interface %s' % interface)
    set_hostname = False
    fqdn = None
    # Look up the IP address on the interface
    # and then reverse lookup the hostname in DNS
    info = netinfo.netdev_info()
    if interface in info:
        set_short = util.get_cfg_option_bool(_cfg, "set_dns_shortname", False)
        if 'addr' in info[interface] and info[interface]['addr']:
            # Handle IPv4 address
            set_hostname = _set_hostname(_cfg, _cloud, log,
                                         info[interface]['addr'], set_short)
        elif 'addr6' in info[interface] and info[interface]['addr6']:
            # Handle IPv6 addresses
            for ipaddr in info[interface]['addr6']:
                ipaddr = ipaddr.split('/')[0]
                set_hostname = _set_hostname(_cfg, _cloud, log, ipaddr,
                                             set_short)
                if set_hostname:
                    break
    else:
        log.warning('Interface %s was not found on the system. '
                    'Interfaces found on system: %s' %
                    (interface, info.keys()))

    # Reverse lookup failed, fall back to cc_set_hostname way.
    if not set_hostname:
        (short_hostname, fqdn) = util.get_hostname_fqdn(_cfg, _cloud)
        try:
            log.info('Fall back to setting hostname on VM as %s' % fqdn)
            _cloud.distro.set_hostname(short_hostname, fqdn=fqdn)
        except Exception:
            util.logexc(log, "Failed to set the hostname to %s", fqdn)
            raise
コード例 #18
0
def handle(name, _cfg, _cloud, log, _args):
    default_interface = 'eth0'
    system_info = util.system_info()
    if 'aix' in system_info['platform'].lower():
        default_interface = 'en0'

    interface = util.get_cfg_option_str(_cfg,
                                        'set_hostname_from_interface',
                                        default=default_interface)
    log.debug('Setting hostname based on interface %s' % interface)
    set_hostname = False
    fqdn = None
    # Look up the IP address on the interface
    # and then reverse lookup the hostname in DNS
    info = netinfo.netdev_info()
    if interface in info:
        set_short = util.get_cfg_option_bool(_cfg, "set_dns_shortname", False)
        if 'addr' in info[interface] and info[interface]['addr']:
            # Handle IPv4 address
            set_hostname =_set_hostname(_cfg, _cloud, log,
                                        info[interface]['addr'], set_short)
        elif 'addr6' in info[interface] and info[interface]['addr6']:
            # Handle IPv6 addresses
            for ipaddr in info[interface]['addr6']:
                ipaddr = ipaddr.split('/')[0]
                set_hostname = _set_hostname(_cfg, _cloud, log, ipaddr,
                                             set_short)
                if set_hostname:
                    break
    else:
        log.warning('Interface %s was not found on the system. '
                    'Interfaces found on system: %s' % (interface,
                                                        info.keys()))

    # Reverse lookup failed, fall back to cc_set_hostname way.
    if not set_hostname:
        (short_hostname, fqdn) = util.get_hostname_fqdn(_cfg, _cloud)
        try:
            log.info('Fall back to setting hostname on VM as %s' % fqdn)
            _cloud.distro.set_hostname(short_hostname, fqdn=fqdn)
        except Exception:
            util.logexc(log, "Failed to set the hostname to %s", fqdn)
            raise
コード例 #19
0
def handle(name, cfg, cloud, log, _args):
    if util.get_cfg_option_bool(cfg, "preserve_hostname", False):
        log.debug(("Configuration option 'preserve_hostname' is set,"
                   " not updating the hostname in module %s"), name)
        return

    # Set prefer_fqdn_over_hostname value in distro
    hostname_fqdn = util.get_cfg_option_bool(cfg, "prefer_fqdn_over_hostname",
                                             None)
    if hostname_fqdn is not None:
        cloud.distro.set_option('prefer_fqdn_over_hostname', hostname_fqdn)

    (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
    try:
        prev_fn = os.path.join(cloud.get_cpath('data'), "previous-hostname")
        log.debug("Updating hostname to %s (%s)", fqdn, hostname)
        cloud.distro.update_hostname(hostname, fqdn, prev_fn)
    except Exception:
        util.logexc(log, "Failed to update the hostname to %s (%s)", fqdn,
                    hostname)
        raise
コード例 #20
0
def handle(name, cfg, cloud, log, _args):
    if util.get_cfg_option_bool(cfg, "preserve_hostname", False):
        log.debug(("Configuration option 'preserve_hostname' is set,"
                   " not setting the hostname in module %s"), name)
        return

    # Set prefer_fqdn_over_hostname value in distro
    hostname_fqdn = util.get_cfg_option_bool(cfg,
                                             "prefer_fqdn_over_hostname",
                                             None)
    if hostname_fqdn is not None:
        cloud.distro.set_option('prefer_fqdn_over_hostname', hostname_fqdn)

    (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
    # Check for previous successful invocation of set-hostname

    # set-hostname artifact file accounts for both hostname and fqdn
    # deltas. As such, it's format is different than cc_update_hostname's
    # previous-hostname file which only contains the base hostname.
    # TODO consolidate previous-hostname and set-hostname artifact files and
    # distro._read_hostname implementation so we only validate  one artifact.
    prev_fn = os.path.join(cloud.get_cpath('data'), "set-hostname")
    prev_hostname = {}
    if os.path.exists(prev_fn):
        prev_hostname = util.load_json(util.load_file(prev_fn))
    hostname_changed = (hostname != prev_hostname.get('hostname') or
                        fqdn != prev_hostname.get('fqdn'))
    if not hostname_changed:
        log.debug('No hostname changes. Skipping set-hostname')
        return
    log.debug("Setting the hostname to %s (%s)", fqdn, hostname)
    try:
        cloud.distro.set_hostname(hostname, fqdn)
    except Exception as e:
        msg = "Failed to set the hostname to %s (%s)" % (fqdn, hostname)
        util.logexc(log, msg)
        raise SetHostnameError("%s: %s" % (msg, e)) from e
    write_json(prev_fn, {'hostname': hostname, 'fqdn': fqdn})
コード例 #21
0
def handle(name, cfg, cloud, log, _args):
    if util.get_cfg_option_bool(cfg, "preserve_hostname", False):
        log.debug(("Configuration option 'preserve_hostname' is set,"
                   " not setting the hostname in module %s"), name)
        return
    (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
    # Check for previous successful invocation of set-hostname

    # set-hostname artifact file accounts for both hostname and fqdn
    # deltas. As such, it's format is different than cc_update_hostname's
    # previous-hostname file which only contains the base hostname.
    # TODO consolidate previous-hostname and set-hostname artifact files and
    # distro._read_hostname implementation so we only validate  one artifact.
    prev_fn = os.path.join(cloud.get_cpath('data'), "set-hostname")
    prev_hostname = {}
    if os.path.exists(prev_fn):
        prev_hostname = util.load_json(util.load_file(prev_fn))
    hostname_changed = (hostname != prev_hostname.get('hostname')
                        or fqdn != prev_hostname.get('fqdn'))
    if not hostname_changed:
        log.debug('No hostname changes. Skipping set-hostname')
        return

    # BEGIN MULTIPLE DOMAIN NAMES PATCH
    # cloud-init doesn't expect DHCP to have more than one domain names.
    # Assume the first domain name to be the primary one.
    fqdn = "{}.{}".format(hostname,
                          fqdn.split(' ')[0]) if ' ' in fqdn else fqdn
    # END MULTIPLE DOMAIN NAMES PATCH

    log.debug("Setting the hostname to %s (%s)", fqdn, hostname)
    try:
        cloud.distro.set_hostname(hostname, fqdn)
    except Exception as e:
        msg = "Failed to set the hostname to %s (%s)" % (fqdn, hostname)
        util.logexc(log, msg)
        raise SetHostnameError("%s: %s" % (msg, e))
    write_json(prev_fn, {'hostname': hostname, 'fqdn': fqdn})
コード例 #22
0
def search_for_mirror_dns(configured, mirrortype, cfg, cloud):
    """
    Try to resolve a list of predefines DNS names to pick mirrors
    """
    mirror = None

    if configured:
        mydom = ""
        doms = []

        if mirrortype == "primary":
            mirrordns = "mirror"
        elif mirrortype == "security":
            mirrordns = "security-mirror"
        else:
            raise ValueError("unknown mirror type")

        # if we have a fqdn, then search its domain portion first
        (_, fqdn) = util.get_hostname_fqdn(cfg, cloud)
        mydom = ".".join(fqdn.split(".")[1:])
        if mydom:
            doms.append(".%s" % mydom)

        doms.extend((
            ".localdomain",
            "",
        ))

        mirror_list = []
        distro = cloud.distro.name
        mirrorfmt = "http://%s-%s%s/%s" % (distro, mirrordns, "%s", distro)
        for post in doms:
            mirror_list.append(mirrorfmt % (post))

        mirror = search_for_mirror(mirror_list)

    return mirror
コード例 #23
0
 def test_get_hostname_fqdn_from_cfg_hostname_with_domain(self):
     """When cfg has only hostname key which represents a fqdn, use that."""
     hostname, fqdn = util.get_hostname_fqdn(
         cfg={'hostname': 'myhost.domain.com'}, cloud=None)
     self.assertEqual('myhost', hostname)
     self.assertEqual('myhost.domain.com', fqdn)
コード例 #24
0
 def test_get_hostname_fqdn_from_only_cfg_fqdn(self):
     """When cfg only has the fqdn key, derive hostname and fqdn from it."""
     hostname, fqdn = util.get_hostname_fqdn(
         cfg={'fqdn': 'myhost.domain.com'}, cloud=None)
     self.assertEqual('myhost', hostname)
     self.assertEqual('myhost.domain.com', fqdn)
コード例 #25
0
ファイル: test_util.py プロジェクト: loilter/cloud-init
 def test_get_hostname_fqdn_from_cfg_fqdn_and_hostname(self):
     """When cfg has both fqdn and hostname keys, return them."""
     hostname, fqdn = util.get_hostname_fqdn(
         cfg={'fqdn': 'myhost.domain.com', 'hostname': 'other'}, cloud=None)
     self.assertEqual('other', hostname)
     self.assertEqual('myhost.domain.com', fqdn)
コード例 #26
0
def handle(name, cfg, cloud, log, _args):
    logger.debug("Cloud-init config: {}".format(cfg))
    # fetch all required data from Cloud-init
    # Datasource name
    dsname = cloud.datasource.dsname
    logger.debug("Datasource: {}".format(dsname))
    # Metadata (datasource specific)
    metadata_ds = cloud.datasource.metadata
    logger.debug("Meta-Data ds: {}".format(metadata_ds))
    # Metadata in stable v1 format (the same structure for all datasources)
    instance_data_json = load_json(load_file("{}/{}".format(cloud.datasource.paths.run_dir, INSTANCE_JSON_FILE)))
    metadata_v1 = instance_data_json.get('v1')
    logger.debug("Meta-Data v1: {}".format(metadata_v1))
    # User-Data
    userdata = cloud.datasource.userdata
    logger.debug("User-Data: {}".format(userdata))
    # Vendor-Data
    vendordata = cloud.datasource.vendordata
    logger.debug("Vendor-Data: {}".format(vendordata))
    # Network-config
    init_stage = Init()
    (netcfg, netcfg_src) = init_stage._find_networking_config()
    logger.debug("Network-config: {}".format(netcfg))
    logger.debug("Network-config source: {}".format(netcfg_src))
    # Hostname with FQDN (if exist)
    (hostname, fqdn) = get_hostname_fqdn(cfg, cloud, metadata_only=True)
    logger.debug("Hostname: {}, FQDN: {}".format(hostname, fqdn))
    # Get users list
    (users, _) = ug_util.normalize_users_groups(cfg, cloud.distro)
    logger.debug("Users: {}".format(users))
    (default_user, default_user_config) = ug_util.extract_default(users)
    logger.debug("Default user: {}".format(default_user))
    # Get OVF properties
    if 'OVF' in dsname:
        ovf_environment = ovf_get_properties(cloud.datasource.environment)
        logger.debug("OVF environment: {}".format(ovf_environment))

    # VyOS configuration file selection
    cfg_file_name = '/opt/vyatta/etc/config/config.boot'
    bak_file_name = '/opt/vyatta/etc/config.boot.default'

    # open configuration file
    if not path.exists(cfg_file_name):
        file_name = bak_file_name
    else:
        file_name = cfg_file_name

    logger.debug("Using configuration file: {}".format(file_name))
    with open(file_name, 'r') as f:
        config_file = f.read()
    config = ConfigTree(config_file)

    # Initialization of variables
    DEFAULT_VYOS_USER = '******'
    DEFAULT_VYOS_PASSWORD = '******'
    logins_configured = False
    network_configured = False

    # configure system logins
    # Prepare SSH public keys for default user, to be sure that global keys applied to the default account (if it exist)
    ssh_keys = metadata_v1['public_ssh_keys']
    # append SSH keys from cloud-config
    ssh_keys.extend(cfg.get('ssh_authorized_keys', []))
    # Configure authentication for default user account
    if default_user:
        # key-based
        for ssh_key in ssh_keys:
            if set_ssh_login(config, default_user, ssh_key):
                logins_configured = True
        # password-based
        password = cfg.get('password')
        if password:
            if set_pass_login(config, default_user, password):
                logins_configured = True

    # Configure all users accounts
    for user, user_cfg in users.items():
        # Configure password-based authentication
        password = user_cfg.get('passwd')
        if password and password != '':
            if set_pass_login(config, user, password):
                logins_configured = True

        # Configure key-based authentication
        for ssh_key in user_cfg.get('ssh_authorized_keys', []):
            if set_ssh_login(config, user, ssh_key):
                logins_configured = True

    # Create a fallback user if there was no others
    if not logins_configured:
        logger.debug("Adding fallback user: {}".format(DEFAULT_VYOS_USER))
        set_pass_login(config, DEFAULT_VYOS_USER, DEFAULT_VYOS_PASSWORD)

    # apply settings from OVF template
    if 'OVF' in dsname:
        set_config_ovf(config, ovf_environment)
        # Empty hostname option may be interpreted as 'null' string by some hypervisors
        # we need to replace it to the empty value to process it later properly
        if hostname and hostname == 'null':
            hostname = None
        network_configured = True

    # process networking configuration data
    if netcfg and network_configured is False:
        # check which one version of config we have
        # version 1
        if netcfg['version'] == 1:
            for interface_config in netcfg['config']:
                set_config_interfaces_v1(config, interface_config)
            network_configured = True

        # version 2
        if netcfg['version'] == 2:
            if 'ethernets' in netcfg:
                for interface_name, interface_config in netcfg['ethernets'].items():
                    set_config_interfaces_v2(config, interface_name, interface_config)
                network_configured = True

    # enable DHCPv4 on eth0 if network still not configured
    if network_configured is False:
        set_config_dhcp(config)

    # enable SSH service
    set_config_ssh(config)
    # configure hostname and domain
    if hostname:
        set_config_hostname(config, hostname, fqdn)
    else:
        set_config_hostname(config, 'vyos', None)

    # save a new configuration file
    try:
        with open(cfg_file_name, 'w') as f:
            f.write(config.to_string())
            logger.debug("Configuration file saved: {}".format(cfg_file_name))
    except Exception as e:
        logger.error("Failed to write configs into file {}: {}".format(cfg_file_name, e))
コード例 #27
0
def handle(name, cfg, cloud, log, _args):
    init = stages.Init()
    dc = init.fetch()
    cfg_file_name = '/opt/vyatta/etc/config/config.boot'
    bak_file_name = '/opt/vyatta/etc/config.boot.default'
    metadata = cloud.datasource.metadata
    (netcfg, _) = init._find_networking_config()
    (users, _) = ug_util.normalize_users_groups(cfg, cloud.distro)
    (hostname, _) = util.get_hostname_fqdn(cfg, cloud)
    key_x = 1
    key_y = 0

    # look at data that can be used for configuration
    #print(dir(dc))

    if not os.path.exists(cfg_file_name):
        file_name = bak_file_name
    else:
        file_name = cfg_file_name

    with open(file_name, 'r') as f:
        config_file = f.read()
    config = ConfigTree(config_file)

    if 'Azure' in dc.dsname:
        encrypted_pass = True
        for key, val in users.items():
            user = key

            if 'passwd' in val:
                password = val.get('passwd')
                set_pass_login(config, user, password, encrypted_pass)

            vyos_keys = metadata['public-keys']

            for ssh_key in vyos_keys:
                set_ssh_login(config, log, user, ssh_key, key_x)
                key_x = key_x + 1
    else:
        encrypted_pass = False
        for user in users:
            password = util.get_cfg_option_str(cfg, 'passwd', None)

            if not password:
                password = util.get_cfg_option_str(cfg, 'password', None)

            if password and password != '':
                hash = re.match("(^\$.\$)", password)
                hash_count = password.count('$')
                if hash and hash_count >= 3:
                    base64 = password.split('$')[3]
                    base_64_len = len(base64)
                    if ((hash.group(1) == '$1$' and base_64_len == 22)
                            or (hash.group(1) == '$5$' and base_64_len == 43)
                            or (hash.group(1) == '$6$' and base_64_len == 86)):
                        encrypted_pass = True
                set_pass_login(config, user, password, encrypted_pass)

            vyos_keys = cloud.get_public_ssh_keys() or []
            if 'ssh_authorized_keys' in cfg:
                cfgkeys = cfg['ssh_authorized_keys']
                vyos_keys.extend(cfgkeys)

            for ssh_key in vyos_keys:
                set_ssh_login(config, log, user, ssh_key, key_x)
                key_x = key_x + 1

    if 'OVF' in dc.dsname:
        set_config_ovf(config, hostname, metadata)
        key_y = 1
    elif netcfg:
        for interface in netcfg['config']:
            if interface['type'] == 'physical':
                key_y = 1
                set_config_interfaces(config, interface)

            if interface['type'] == 'nameserver':
                set_config_nameserver(config, log, interface)

        set_config_ssh(config)
        set_config_hostname(config, hostname)
    else:
        set_config_dhcp(config)
        set_config_ssh(config)
        set_config_hostname(config, hostname)

    if key_y == 0:
        set_config_dhcp(config)

    try:
        with open(cfg_file_name, 'w') as f:
            f.write(config.to_string())
    except Exception as e:
        util.logexc(log, "Failed to write configs into file %s error %s",
                    file_name, e)
コード例 #28
0
ファイル: test_util.py プロジェクト: cloud-init/cloud-init
 def test_get_hostname_fqdn_from_only_cfg_fqdn(self):
     """When cfg only has the fqdn key, derive hostname and fqdn from it."""
     hostname, fqdn = util.get_hostname_fqdn(
         cfg={'fqdn': 'myhost.domain.com'}, cloud=None)
     self.assertEqual('myhost', hostname)
     self.assertEqual('myhost.domain.com', fqdn)
コード例 #29
0
ファイル: test_util.py プロジェクト: cloud-init/cloud-init
 def test_get_hostname_fqdn_from_cfg_fqdn_and_hostname(self):
     """When cfg has both fqdn and hostname keys, return them."""
     hostname, fqdn = util.get_hostname_fqdn(
         cfg={'fqdn': 'myhost.domain.com', 'hostname': 'other'}, cloud=None)
     self.assertEqual('other', hostname)
     self.assertEqual('myhost.domain.com', fqdn)
コード例 #30
0
ファイル: test_util.py プロジェクト: cloud-init/cloud-init
 def test_get_hostname_fqdn_from_cfg_hostname_with_domain(self):
     """When cfg has only hostname key which represents a fqdn, use that."""
     hostname, fqdn = util.get_hostname_fqdn(
         cfg={'hostname': 'myhost.domain.com'}, cloud=None)
     self.assertEqual('myhost', hostname)
     self.assertEqual('myhost.domain.com', fqdn)
コード例 #31
0
def handle_iscsi(cfg, cloud, log, definition, dev_entry_iscsi):
    # Handle iSCSI LUN
    device = definition.get("device")
    try:
        (iscsi_host,
         iscsi_proto,
         iscsi_port,
         iscsi_lun,
         iscsi_target) = device.split(":", 5)[1:]
    except Exception as e:
        util.logexc(log,
                    "handle_iscsi: "
                    "expected \"device\" attribute in the format: "
                    "\"iscsi:<iSCSI host>:<protocol>:<port>:<LUN>:"
                    "<iSCSI target name>\": %s" % e)
        return
    if dev_entry_iscsi == 0:
        (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
        if "initiator_name" in definition:
            initiator_name = definition.get("initiator_name")
        else:
            initiator_name = "iqn.2005-02.com.open-iscsi:%s" % hostname
        util.write_file(ISCSI_INITIATOR_PATH, "InitiatorName=%s" % initiator_name)
        multipath_tmpl_fn = cloud.get_template_filename("multipath.conf")
        if multipath_tmpl_fn:
            templater.render_to_file(multipath_tmpl_fn, "/etc/multipath.conf", {})
        else:
            log.warn("handle_iscsi: template multipath.conf not found")
        if cloud.distro.osfamily == "redhat":
            iscsi_services = ["iscsi", "iscsid"]
            multipath_services = ["multipathd"]
        elif cloud.distro.osfamily == 'debian':
            iscsi_services = ["open-iscsi", "iscsid"]
            multipath_services = ["multipathd"]
        else:
            util.logexc(log,
                        "handle_iscsi: "
                        "unsupported osfamily \"%s\"" % cloud.distro.osfamily)
            return
        for service in iscsi_services:
            _service_wrapper(cloud, log, service, "enable")
            _service_wrapper(cloud, log, service, "restart")
        for service in multipath_services:
            _service_wrapper(cloud, log, service, "enable")
            _service_wrapper(cloud, log, service, "restart")
    blockdev = _iscsi_lun_discover(log,
                                   iscsi_host,
                                   iscsi_port,
                                   iscsi_lun,
                                   iscsi_target)
    if blockdev:
        lvm_group = definition.get("lvm_group")
        lvm_volume = definition.get("lvm_volume")
        fs_type = definition.get("fs_type")
        fs_label = definition.get("fs_label")
        fs_opts = definition.get("fs_opts")
        mount_point = definition.get("mount_point")
        mount_opts = definition.get("mount_opts")
        if not mount_opts:
            mount_opts = 'defaults,_netdev'
        else:
            if mount_opts.find("_netdev") == -1:
                mount_opts = "%s,_netdev" % (mount_opts)
        fs_freq = definition.get("fs_freq")
        if not fs_freq:
            fs_freq = "1"
        fs_passno = definition.get("fs_passno")
        if not fs_passno:
            fs_passno = "2"
        if lvm_group and lvm_volume:
            for vg_name in _list_vg_names():
                if vg_name == lvm_group:
                    util.logexc(log,
                                "handle_iscsi: "
                                "logical volume group '%s' exists already"
                                % lvm_group)
                    return
            for lv_name in _list_lv_names():
                if lv_name == lvm_volume:
                    util.logexc(log,
                                "handle_iscsi: "
                                "logical volume '%s' exists already"
                                % lvm_volume)
                    return
            blockdev = _create_lv(log, blockdev, lvm_group, lvm_volume)
        if blockdev:
            if mount_point and fs_type:
                _create_fs(log, blockdev, fs_type, fs_label, fs_opts)
                _add_fstab_entry(log,
                                 blockdev,
                                 mount_point,
                                 fs_type,
                                 fs_label,
                                 mount_opts,
                                 fs_freq,
                                 fs_passno)
                _mount_fs(log, mount_point)
            else:
                util.logexc(log,
                            "handle_iscsi: "
                            "expexted \"mount_point\" "
                            "and \"fs_type\" parameters")