[users] Untangle setting-hostname from writing-/etc/hosts

Exactly one kind of setting-hostname is done, and that's
entirely independent of writing /etc/hosts. Don't make it
a set of flags, use an enum and a bool.
This commit is contained in:
Adriaan de Groot 2022-04-11 11:10:40 +02:00
parent 6a6aa8867b
commit 854c711ac6
6 changed files with 47 additions and 45 deletions

View File

@ -734,8 +734,8 @@ setConfigurationDefaultGroups( const QVariantMap& map, QList< GroupDescription >
}
}
STATICTEST HostNameActions
getHostNameActions( const QVariantMap& configurationMap )
STATICTEST HostNameAction
getHostNameAction( const QVariantMap& configurationMap )
{
HostNameAction setHostName = HostNameAction::EtcHostname;
QString hostnameActionString = CalamaresUtils::getString( configurationMap, "location" );
@ -749,10 +749,7 @@ getHostNameActions( const QVariantMap& configurationMap )
}
}
HostNameAction writeHosts = CalamaresUtils::getBool( configurationMap, "writeHostsFile", true )
? HostNameAction::WriteEtcHosts
: HostNameAction::None;
return setHostName | writeHosts;
return setHostName;
}
/** @brief Process entries in the passwordRequirements config entry
@ -871,7 +868,8 @@ Config::setConfigurationMap( const QVariantMap& configurationMap )
// TODO:3.3: Remove calls to copyLegacy
copyLegacy( configurationMap, "setHostname", hostnameSettings, "location" );
copyLegacy( configurationMap, "writeHostsFile", hostnameSettings, "writeHostsFile" );
m_hostNameActions = getHostNameActions( hostnameSettings );
m_hostNameAction = getHostNameAction( hostnameSettings );
m_writeEtcHosts = CalamaresUtils::getBool( hostnameSettings, "writeHostsFile", true );
}
setConfigurationDefaultGroups( configurationMap, m_defaultGroups );
@ -951,7 +949,7 @@ Config::createJobs() const
j = new SetPasswordJob( "root", rootPassword() );
jobs.append( Calamares::job_ptr( j ) );
j = new SetHostNameJob( hostName(), hostNameActions() );
j = new SetHostNameJob( this );
jobs.append( Calamares::job_ptr( j ) );
return jobs;

View File

@ -20,15 +20,12 @@
#include <QObject>
#include <QVariantMap>
enum HostNameAction
enum class HostNameAction
{
None = 0x0,
EtcHostname = 0x1, // Write to /etc/hostname directly
SystemdHostname = 0x2, // Set via hostnamed(1)
WriteEtcHosts = 0x4 // Write /etc/hosts (127.0.1.1 is this host)
None,
EtcHostname, // Write to /etc/hostname directly
SystemdHostname, // Set via hostnamed(1)
};
Q_DECLARE_FLAGS( HostNameActions, HostNameAction )
Q_DECLARE_OPERATORS_FOR_FLAGS( HostNameActions )
const NamedEnumTable< HostNameAction >& hostNameActionNames();
@ -103,7 +100,7 @@ class PLUGINDLLEXPORT Config : public Calamares::ModuleSystem::Config
Q_PROPERTY( QString hostName READ hostName WRITE setHostName NOTIFY hostNameChanged )
Q_PROPERTY( QString hostNameStatus READ hostNameStatus NOTIFY hostNameStatusChanged )
Q_PROPERTY( HostNameActions hostNameActions READ hostNameActions CONSTANT )
Q_PROPERTY( HostNameAction hostNameAction READ hostNameAction CONSTANT )
Q_PROPERTY( QString userPassword READ userPassword WRITE setUserPassword NOTIFY userPasswordChanged )
Q_PROPERTY( QString userPasswordSecondary READ userPasswordSecondary WRITE setUserPasswordSecondary NOTIFY
@ -208,7 +205,9 @@ public:
/// Status message about hostname -- empty for "ok"
QString hostNameStatus() const;
/// How to write the hostname
HostNameActions hostNameActions() const { return m_hostNameActions; }
HostNameAction hostNameAction() const { return m_hostNameAction; }
/// Write /etc/hosts ?
bool writeEtcHosts() const { return m_writeEtcHosts; }
/// Should the user be automatically logged-in?
bool doAutoLogin() const { return m_doAutoLogin; }
@ -337,7 +336,8 @@ private:
bool m_isReady = false; ///< Used to reduce readyChanged signals
HostNameActions m_hostNameActions;
HostNameAction m_hostNameAction = HostNameAction::EtcHostname;
bool m_writeEtcHosts = false;
PasswordCheckList m_passwordChecks;
};

View File

@ -24,31 +24,30 @@
using WriteMode = CalamaresUtils::System::WriteMode;
SetHostNameJob::SetHostNameJob( const QString& hostname, HostNameActions a )
SetHostNameJob::SetHostNameJob( const Config* c )
: Calamares::Job()
, m_hostname( hostname )
, m_actions( a )
, m_config( c )
{
}
QString
SetHostNameJob::prettyName() const
{
return tr( "Set hostname %1" ).arg( m_hostname );
return tr( "Set hostname %1" ).arg( m_config->hostName() );
}
QString
SetHostNameJob::prettyDescription() const
{
return tr( "Set hostname <strong>%1</strong>." ).arg( m_hostname );
return tr( "Set hostname <strong>%1</strong>." ).arg( m_config->hostName() );
}
QString
SetHostNameJob::prettyStatusMessage() const
{
return tr( "Setting hostname %1." ).arg( m_hostname );
return tr( "Setting hostname %1." ).arg( m_config->hostName() );
}
STATICTEST bool
@ -129,29 +128,32 @@ SetHostNameJob::exec()
return Calamares::JobResult::error( tr( "Internal Error" ) );
}
if ( m_actions & HostNameAction::EtcHostname )
switch ( m_config->hostNameAction() )
{
if ( !setFileHostname( m_hostname ) )
case HostNameAction::None:
break;
case HostNameAction::EtcHostname:
if ( !setFileHostname( m_config->hostName() ) )
{
cError() << "Can't write to hostname file";
return Calamares::JobResult::error( tr( "Cannot write hostname to target system" ) );
}
break;
case HostNameAction::SystemdHostname:
// Does its own logging
setSystemdHostname( m_config->hostName() );
break;
}
if ( m_actions & HostNameAction::WriteEtcHosts )
if ( m_config->writeEtcHosts() )
{
if ( !writeFileEtcHosts( m_hostname ) )
if ( !writeFileEtcHosts( m_config->hostName() ) )
{
cError() << "Can't write to hosts file";
return Calamares::JobResult::error( tr( "Cannot write hostname to target system" ) );
}
}
if ( m_actions & HostNameAction::SystemdHostname )
{
// Does its own logging
setSystemdHostname( m_hostname );
}
return Calamares::JobResult::ok();
}

View File

@ -20,15 +20,14 @@ class SetHostNameJob : public Calamares::Job
{
Q_OBJECT
public:
SetHostNameJob( const QString& hostname, HostNameActions a );
SetHostNameJob( const Config* c );
QString prettyName() const override;
QString prettyDescription() const override;
QString prettyStatusMessage() const override;
Calamares::JobResult exec() override;
private:
const QString m_hostname;
const HostNameActions m_actions;
const Config* m_config;
};
#endif // SETHOSTNAMEJOB_CPP_H

View File

@ -17,7 +17,7 @@
// Implementation details
extern void setConfigurationDefaultGroups( const QVariantMap& map, QList< GroupDescription >& defaultGroups );
extern HostNameActions getHostNameActions( const QVariantMap& configurationMap );
extern HostNameAction getHostNameAction( const QVariantMap& configurationMap );
extern bool addPasswordCheck( const QString& key, const QVariant& value, PasswordCheckList& passwordChecks );
/** @brief Test Config object methods and internals
@ -243,12 +243,12 @@ UserTests::testHostActions()
{
m.insert( "location", string );
}
QCOMPARE( getHostNameActions( m ),
HostNameActions( result ) | HostNameAction::WriteEtcHosts ); // write bits default to true
// action is independent of writeHostsFile
QCOMPARE( getHostNameAction( m ), HostNameAction( result ) );
m.insert( "writeHostsFile", false );
QCOMPARE( getHostNameActions( m ), HostNameActions( result ) );
QCOMPARE( getHostNameAction( m ), HostNameAction( result ) );
m.insert( "writeHostsFile", true );
QCOMPARE( getHostNameActions( m ), HostNameActions( result ) | HostNameAction::WriteEtcHosts );
QCOMPARE( getHostNameAction( m ), HostNameAction( result ) );
}
void
@ -257,13 +257,16 @@ UserTests::testHostActions2()
Config c;
QVariantMap legacy;
// Test defaults
c.setConfigurationMap( legacy );
QCOMPARE( c.hostNameActions(), HostNameAction::EtcHostname | HostNameAction::WriteEtcHosts );
QCOMPARE( c.hostNameAction(), HostNameAction::EtcHostname );
QCOMPARE( c.writeEtcHosts(), true );
legacy.insert( "writeHostsFile", false );
legacy.insert( "setHostname", "Hostnamed" );
c.setConfigurationMap( legacy );
QCOMPARE( c.hostNameActions(), HostNameAction::SystemdHostname );
QCOMPARE( c.hostNameAction(), HostNameAction::SystemdHostname );
QCOMPARE( c.writeEtcHosts(), false );
}

View File

@ -106,8 +106,8 @@ UsersPage::UsersPage( Config* config, QWidget* parent )
connect( config, &Config::fullNameChanged, this, &UsersPage::onFullNameTextEdited );
// If the hostname is going to be written out, then show the field
if ( ( m_config->hostNameActions() & HostNameAction::EtcHostname )
|| ( m_config->hostNameActions() & HostNameAction::SystemdHostname ) )
if ( ( m_config->hostNameAction() == HostNameAction::EtcHostname )
|| ( m_config->hostNameAction() == HostNameAction::SystemdHostname ) )
{
ui->textBoxHostname->setText( config->hostName() );
connect( ui->textBoxHostname, &QLineEdit::textEdited, config, &Config::setHostName );