canonical / cloud-init

Official upstream for the cloud-init: cloud instance initialization
https://cloud-init.io/
Other
2.87k stars 856 forks source link

Cloudinit throws an error when the hostname is an integer #5642

Open Sponge-Bas opened 3 weeks ago

Sponge-Bas commented 3 weeks ago

Bug report

When the hostname is an integer, cloud init fails in cloudinit/util.py line 1268:

    if "hostname" in cfg and cfg["hostname"].find(".") > 0:

cfg["hostname"] has the hostname parsed as an integer, but it should be parsed as a string.

Steps to reproduce the problem

1) Change your hostname to an integer, e.g. 12. 2) Reboot.

Environment details

cloud-init logs

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/cloudinit/cmd/main.py", line 786, in status_wrapper
    ret = functor(name, args)
  File "/usr/lib/python3/dist-packages/cloudinit/cmd/main.py", line 446, in main_init
    _maybe_set_hostname(init, stage="local", retry_stage="network")
  File "/usr/lib/python3/dist-packages/cloudinit/cmd/main.py", line 843, in _maybe_set_hostname
    (hostname, _fqdn, _) = util.get_hostname_fqdn(
  File "/usr/lib/python3/dist-packages/cloudinit/util.py", line 1240, in get_hostname_fqdn
    if "hostname" in cfg and cfg["hostname"].find(".") > 0:
AttributeError: 'int' object has no attribute 'find'
TheRealFalcon commented 3 weeks ago

Thanks for the bug report.

How is the hostname being passed to cloud-init? It it using the set hostname module, NoCloud configuration, or from a local config or cloud?

I'm asking because while cloud-init can certainly be more defensive, the input to cloud-init should really be a string.

Sponge-Bas commented 2 weeks ago

set_hostname is listed under cloud_init_modules in /etc/cloud/cloud.cfg, if that answers the question.

I have not done any manual config around cloud-init. This is just stock Ubuntu 22.04.

holmanb commented 2 weeks ago

set_hostname is listed under cloud_init_modules in /etc/cloud/cloud.cfg, if that answers the question.

I have not done any manual config around cloud-init. This is just stock Ubuntu 22.04.

@Sponge-Bas Above you said When the hostname is an integer. How did you set this? Please include full logs.

Sponge-Bas commented 2 weeks ago

I edited /etc/hostname and did a reboot. I won't include logs as I don't have time to verify they don't contain sensitive information. The provided reproducer should be sufficient.

holmanb commented 2 weeks ago

I could not reproduce using the provided instructions.

# hostname
1
# cat /etc/hostname 
12
# reboot -h now
# grep -i traceback /var/log/cloud-init.log 
# hostname
12

Please include more information in your report.