[users] Put hostname settings in a *hostname* key

Move settings into a structured setting for *hostname*, with suitable
sub-keys. Legacy settings remain supported, produce a warning.
This commit is contained in:
Adriaan de Groot 2022-04-08 10:45:50 +02:00
parent 55dcd831bc
commit 04f38ea661
3 changed files with 42 additions and 9 deletions

View File

@ -738,7 +738,7 @@ STATICTEST HostNameActions
getHostNameActions( const QVariantMap& configurationMap )
{
HostNameAction setHostName = HostNameAction::EtcHostname;
QString hostnameActionString = CalamaresUtils::getString( configurationMap, "setHostname" );
QString hostnameActionString = CalamaresUtils::getString( configurationMap, "location" );
if ( !hostnameActionString.isEmpty() )
{
bool ok = false;
@ -826,6 +826,17 @@ either( T ( *f )( const QVariantMap&, const QString&, U ),
}
}
static void
copyLegacy( const QVariantMap& source, const QString& sourceKey, QVariantMap& target, const QString& targetKey )
{
if ( source.contains( sourceKey ) )
{
const QVariant legacyValue = source.value( sourceKey );
cWarning() << "Legacy *users* key" << sourceKey << "overrides hostname-settings.";
target.insert( targetKey, legacyValue );
}
}
void
Config::setConfigurationMap( const QVariantMap& configurationMap )
{
@ -844,7 +855,15 @@ Config::setConfigurationMap( const QVariantMap& configurationMap )
? SudoStyle::UserAndGroup
: SudoStyle::UserOnly;
m_hostNameActions = getHostNameActions( configurationMap );
// Handle *hostname* key and subkeys and legacy settings
{
bool ok = false; // Ignored
QVariantMap hostnameSettings = CalamaresUtils::getSubMap( configurationMap, "hostname", ok );
copyLegacy( configurationMap, "setHostname", hostnameSettings, "location" );
copyLegacy( configurationMap, "writeHostsFile", hostnameSettings, "writeHostsFile" );
m_hostNameActions = getHostNameActions( hostnameSettings );
}
setConfigurationDefaultGroups( configurationMap, m_defaultGroups );

View File

@ -240,7 +240,7 @@ UserTests::testHostActions()
QVariantMap m;
if ( set )
{
m.insert( "setHostname", string );
m.insert( "location", string );
}
QCOMPARE( getHostNameActions( m ),
HostNameActions( result ) | HostNameAction::WriteEtcHosts ); // write bits default to true

View File

@ -149,19 +149,33 @@ allowWeakPasswordsDefault: false
# that the shell actually exists or is executable.
userShell: /bin/bash
# Hostname setting
# Hostname settings
#
# The user can enter a hostname; this is configured into the system
# in some way; pick one of:
# in some way. There are settings for how a hostname is guessed (as
# a default / suggestion) and where (or how) the hostname is set in
# the target system.
#
# Key *hostname* has the following sub-keys (just one):
#
# - *location* How the hostname is set in the target system:
# - *None*, to not set the hostname at all
# - *EtcFile*, to write to `/etc/hostname` directly
# - *Hostnamed*, to use systemd hostnamed(1) over DBus
# The default is *EtcFile*.
# The default is *EtcFile*. Setting this to *None* will
# hide the hostname field.
# - *writeHostsFile* Should /etc/hosts be written with a hostname for
# this machine (also adds localhost and some ipv6 standard entries).
# Defaults to *true*.
hostname:
location: EtcFile
writeHostsFile: true
# This is a legacy setting for hostname.location; if it is set
# at all, and there is no setting for hostname.location, it is used.
setHostname: EtcFile
# Should /etc/hosts be written with a hostname for this machine
# (also adds localhost and some ipv6 standard entries).
# Defaults to *true*.
# This is a legacy setting for hostname.writeHostsFile
writeHostsFile: true
presets: