← Back to team overview

cloud-init-dev team mailing list archive

Re: [Merge] ~chad.smith/cloud-init:set-hostname-before-network into cloud-init:master

 

Sorry for the noise on the get_hostname part; I missed it getting sent through the datasource object.

Diff comments:

> diff --git a/cloudinit/cmd/main.py b/cloudinit/cmd/main.py
> index d2f1b77..120c3cc 100644
> --- a/cloudinit/cmd/main.py
> +++ b/cloudinit/cmd/main.py
> @@ -354,7 +355,17 @@ def main_init(name, args):
>      LOG.debug("[%s] %s will now be targeting instance id: %s. new=%s",
>                mode, name, iid, init.is_new_instance())
>  
> -    init.apply_network_config(bring_up=bool(mode != sources.DSMODE_LOCAL))
> +    if mode == sources.DSMODE_LOCAL:
> +        # Before network comes up, set any configured hostname to allow
> +        # dhcp clients to advertize this hostname to any DDNS services
> +        # lp:1746455.
> +        cloud = init.cloudify()
> +        (hostname, _fqdn) = util.get_hostname_fqdn(

You're right; I didn't see that it called through the datasource object.

> +            init.cfg, cloud, metadata_only=True)
> +        if hostname:  # Either metadata or user-data provided hostname content
> +            cc_set_hostname.handle(
> +                'pre-network-set-hostname', init.cfg, cloud, LOG, None)
> +    init.apply_network_config(bring_up=bring_up)
>  
>      if mode == sources.DSMODE_LOCAL:
>          if init.datasource.dsmode != mode:
> diff --git a/cloudinit/sources/__init__.py b/cloudinit/sources/__init__.py
> index a05ca2f..d1a213f 100644
> --- a/cloudinit/sources/__init__.py
> +++ b/cloudinit/sources/__init__.py
> @@ -276,21 +276,34 @@ class DataSource(object):
>              return "iid-datasource"
>          return str(self.metadata['instance-id'])
>  
> -    def get_hostname(self, fqdn=False, resolve_ip=False):
> +    def get_hostname(self, fqdn=False, resolve_ip=False, metadata_only=False):
> +        """Get hostname or fqdn from the datasource. Look it up if desired.
> +
> +        @param fqdn: Boolean, set True to return hostname with domain.
> +        @param resolve_ip: Boolean, set True to attempt to resolve an ipv4

Yes, I see that now; I missed it going through the datasource object.  I think we could wait on a refactor here; we likely need to consolidate the get_hostname/get_hostname_fqd and ensure ipv6 support throughout

> +            address provided in local-hostname metadata.
> +        @param metadata_only: Boolean, set True to avoid looking up hostname
> +            if metadata doesn't have local-hostname present.
> +
> +        @return: hostname or qualified hostname. Optionally return None when
> +            metadata_only is True and local-hostname data is not available.
> +        """
>          defdomain = "localdomain"
>          defhost = "localhost"
>          domain = defdomain
>  
>          if not self.metadata or 'local-hostname' not in self.metadata:
> +            if metadata_only:
> +                return None

No, I missed the code path through datasource object.

>              # this is somewhat questionable really.
>              # the cloud datasource was asked for a hostname
>              # and didn't have one. raising error might be more appropriate
>              # but instead, basically look up the existing hostname
>              toks = []
>              hostname = util.get_hostname()
> -            fqdn = util.get_fqdn_from_hosts(hostname)
> -            if fqdn and fqdn.find(".") > 0:
> -                toks = str(fqdn).split(".")
> +            hosts_fqdn = util.get_fqdn_from_hosts(hostname)
> +            if hosts_fqdn and hosts_fqdn.find(".") > 0:
> +                toks = str(hosts_fqdn).split(".")
>              elif hostname and hostname.find(".") > 0:
>                  toks = str(hostname).split(".")
>              elif hostname:


-- 
https://code.launchpad.net/~chad.smith/cloud-init/+git/cloud-init/+merge/339720
Your team cloud-init commiters is requested to review the proposed merge of ~chad.smith/cloud-init:set-hostname-before-network into cloud-init:master.


References