Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check for create_hostname_file key before writing /etc/hostname #4330

Merged
merged 9 commits into from
Sep 12, 2023

Conversation

catmsred
Copy link
Collaborator

@catmsred catmsred commented Aug 10, 2023

Proposed Commit Message

Some users would like to be able to bring up their systems without automatically creating /etc/hostname if it does not exist already. On distros with systemd, the hostname/hostnamectl command can be used to set the hostname transiently (which does not create /etc/hostname).  To support this without changing previous behavior (that cloud-init will set the hostname statically and create /etc/hostname if it does not already exist), this change proposes a new key create_hostname_file which if true or not present, sets the hostname statically and creates the hostname file if it does not yet exist; if false, sets the hostname transiently and does not create the hostname file.

This change does not affect BSD or OpenBSD (or any distros that inherit their _write_hostname), as their hostname management does not use an /etc/hostname file.

Test Steps

This change adds unit tests for all non-BSD distributions to ensure that if the create_hostname_file key is set to False, their hostname files are not created. The existing test_cc_set_hostname tests look for /etc/hostname to confirm the hostname has been set correctly so they provide validation that without the create_hostname_file key, there are no regressions in behaviour.

References

Jira card: https://warthogs.atlassian.net/browse/SC-1588
SF case: https://canonical.lightning.force.com/lightning/r/Case/5008e00000EHIhYAAX/view

Checklist:

  • My code follows the process laid out in the documentation
  • I have updated or added any unit tests accordingly
  • I have updated or added any documentation accordingly

@catmsred catmsred changed the title feat: check for preserve_etchostname_status key before writing /etc/hostname [WIP] feat: check for preserve_etchostname_status key before writing /etc/hostname Aug 10, 2023
@igalic
Copy link
Collaborator

igalic commented Aug 11, 2023

BSDs store the hostname in rc.conf(5) like most of its config, but like most other Unices, they have a hostname(1) to (re)set it on the fly.

I can help bring it to feature parity

@catmsred
Copy link
Collaborator Author

@igalic Thank you for the offer! Looking through the BSD distro code, it looked to me like BSD and FreeBSD did update rc.conf as you say, but OpenBSD wrote /etc/myname. As in every other case, this write happens after the call the hostname. In most of the other systems that use /etc/hostname, the call to hostname will write to /etc/hostname if the file exists but will not create it if is does not exist. Prior to the proposed changed, cloud-init ALWAYS created /etc/hostname on a call to Distro.set_hostname() for those systems. So this change would allow a user to not automatically create /etc/hostname if it did not already exist (similar to hostname functionality). Is there an equivalent difference in BSD flavours? E.g., Do you know if /etc/myname or the hostname key in rc.conf is always created when hostname is run?

@igalic
Copy link
Collaborator

igalic commented Aug 11, 2023

hostname(1) on all BSD hasn't changed much since 1993: it sets the sysctl(3) kern.hostname, and does nothing else.

The /etc/rc.d/hostname service on FreeBSD reads the hostname from /etc/rc.conf and uses hostname(1) to set it. On OpenBSD it's /etc/rc which reads /etc/myname doing the same. NetBSD does a mix of the two.

@blackboxsw
Copy link
Collaborator

Bikeshed schema key suggestion: having our preexisting preserve_hostname key makes this a bit tougher to name properly so other suggestions welcome.

        "write_etc_hostname": {
          "type": "string",
          "default": true,
          "description": "When true, statically set hostname across reboots by writing the appropriate hostname file under /etc with the preferred value from user-data and meta-data. Systemd prefers /etc/hostname, FreeBSD ``/etc/rc.conf`` or /etc/myname. Default: ``true``"
        },

Also, given BSDs will be using different etc files for persisting hostname maybe it's not ideal to call out etc_hostname specifcally? 2nd alternative write_hostname_file

@catmsred
Copy link
Collaborator Author

Re: keyname write_hostname_file

The original intention of this key would be to still write the file if it already existed. If it's named write_hostname_file that kind of implies to me that we never write to the hostname file even if it exists. Do we want that behavior (never write to hostname file) or would it make more sense to have the key named create_hostname_file?

@catmsred
Copy link
Collaborator Author

@igalic (and anyone else with an opinion) what would you consider the correct BSD behavior if the create_hostname_file key is false? On BSD/FreeBSD, the code already makes the assumption that /etc/rc exists so I assume there's no time we would expect the set_hostname or update_hostname configs to be creating it so it would make sense to me that this key is not relevant in these systems.

I'm still looking into the OpenBSD /etc/myname file and whether cloud-init currently creates it if it doesn't exist and if so whether we should in the case create_hostname_file is False.

@blackboxsw
Copy link
Collaborator

Re: keyname write_hostname_file

The original intention of this key would be to still write the file if it already existed. If it's named write_hostname_file that kind of implies to me that we never write to the hostname file even if it exists. Do we want that behavior (never write to hostname file) or would it make more sense to have the key named create_hostname_file?

@catmsred create_hostname_file works well. +1

…ostname

Some users would like to be able to bring up their systems without
automatically creating /etc/hostname if it does not exist already.
On distros with systemd, the hostname/hostnamectl command can be
used to set the hostname transiently (which does not create
/etc/hostname).  To support this without changing previous behavior
(that cloud-init will set the hostname statically and create
/etc/hostname if it does not already exist), this change proposes a
new key preserve_etchostname_state which is true, sets the hostname
transiently and if not present or False, sets it statically.

This change does not effect BSD or OpenBSD (or any distros that
inherit their _write_hostname), as their hostname management does
not use an /etc/hostname file.
Changed the key name for creating the hostname file if it does not exist
to create_hostname_file since that is more intuitive the the previously
proposed preserve_hostname_state.  This commit still excludes BSD
instances.
The new create_hostname_file is necessary for the distro to be aware of.
This change adds create_hostname_file to the cfg distro uses when it
runs cc_set_hostname and cc_update_hostname.
This config adds unit tests for all the non-BSD updated cc_set_hostname
calls for the create_hostname_file being set to False (i.e. testing that
the file is not created)
This commit normalizes the opensuse create_hostname_file variable to
match the other distros.
@catmsred catmsred changed the title [WIP] feat: check for preserve_etchostname_status key before writing /etc/hostname feat: check for preserve_etchostname_status key before writing /etc/hostname Sep 5, 2023
@catmsred catmsred changed the title feat: check for preserve_etchostname_status key before writing /etc/hostname feat: check for create_hostname_file key before writing /etc/hostname Sep 6, 2023
@catmsred
Copy link
Collaborator Author

catmsred commented Sep 6, 2023

@s-makin
Recommended documentation update:
On https://cloudinit.readthedocs.io/en/latest/reference/modules.html#set-hostname and https://cloudinit.readthedocs.io/en/latest/reference/modules.html#update-hostname, add the following bullet to Config Schema:

  • create_hostname_file: (boolean) If false, the hostname file (e.g. /etc/hostname) will not be created if it does not exist. If true, the hostname will always be created. Default: true.

@aciba90 aciba90 self-assigned this Sep 8, 2023
Copy link
Contributor

@aciba90 aciba90 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @catmsred, for improving cloud-init. I did a superficial review.

New cloud-config keys need to be added to the json-schema, in order to be validated and documented.

So adding something like #4330 (comment) including #4330 (comment) will do it.

If you want to validate the rendered documentation, execute:

tox -e doc && (cd doc/rtd_html/ && python -m http.server)
open http://0.0.0.0:8000

Add new create_hostname_file key to the schema for cc_set_hostname and
cc_update_hostname so it is correctly documented.
Copy link
Contributor

@aciba90 aciba90 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, looks great, thanks. I have left inline requests to fix/improve some things.

In addition to that, could you please reference, in the PR main comment, the Jira and SF tickets associated to this PR for future back tracing?

cloudinit/distros/opensuse.py Outdated Show resolved Hide resolved
cloudinit/distros/opensuse.py Outdated Show resolved Hide resolved
cloudinit/config/schemas/schema-cloud-config-v1.json Outdated Show resolved Hide resolved
cloudinit/config/schemas/schema-cloud-config-v1.json Outdated Show resolved Hide resolved
cloudinit/config/schemas/schema-cloud-config-v1.json Outdated Show resolved Hide resolved
When key name was updated from preserve_etc_hostname_status to
create_hostname_file, the opensuse logic was not correctly updated.
This commit brings opensuse into alignment with the correct
create_hostname_file key logic that is implemented in other distros.
create_hostname_file should be true by default.  This change correctly
sets that value as well as expanding the description to clarify the
behavior on systemd distros.
Copy link
Contributor

@aciba90 aciba90 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks, @catmsred.

@aciba90 aciba90 merged commit be7f64d into canonical:main Sep 12, 2023
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants