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

View file

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

View file

@ -24,31 +24,30 @@
using WriteMode = CalamaresUtils::System::WriteMode; using WriteMode = CalamaresUtils::System::WriteMode;
SetHostNameJob::SetHostNameJob( const QString& hostname, HostNameActions a ) SetHostNameJob::SetHostNameJob( const Config* c )
: Calamares::Job() : Calamares::Job()
, m_hostname( hostname ) , m_config( c )
, m_actions( a )
{ {
} }
QString QString
SetHostNameJob::prettyName() const SetHostNameJob::prettyName() const
{ {
return tr( "Set hostname %1" ).arg( m_hostname ); return tr( "Set hostname %1" ).arg( m_config->hostName() );
} }
QString QString
SetHostNameJob::prettyDescription() const 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 QString
SetHostNameJob::prettyStatusMessage() const SetHostNameJob::prettyStatusMessage() const
{ {
return tr( "Setting hostname %1." ).arg( m_hostname ); return tr( "Setting hostname %1." ).arg( m_config->hostName() );
} }
STATICTEST bool STATICTEST bool
@ -129,29 +128,32 @@ SetHostNameJob::exec()
return Calamares::JobResult::error( tr( "Internal Error" ) ); 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"; cError() << "Can't write to hostname file";
return Calamares::JobResult::error( tr( "Cannot write hostname to target system" ) ); 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"; cError() << "Can't write to hosts file";
return Calamares::JobResult::error( tr( "Cannot write hostname to target system" ) ); 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(); return Calamares::JobResult::ok();
} }

View file

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

View file

@ -17,7 +17,7 @@
// Implementation details // Implementation details
extern void setConfigurationDefaultGroups( const QVariantMap& map, QList< GroupDescription >& defaultGroups ); 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 ); extern bool addPasswordCheck( const QString& key, const QVariant& value, PasswordCheckList& passwordChecks );
/** @brief Test Config object methods and internals /** @brief Test Config object methods and internals
@ -243,12 +243,12 @@ UserTests::testHostActions()
{ {
m.insert( "location", string ); m.insert( "location", string );
} }
QCOMPARE( getHostNameActions( m ), // action is independent of writeHostsFile
HostNameActions( result ) | HostNameAction::WriteEtcHosts ); // write bits default to true QCOMPARE( getHostNameAction( m ), HostNameAction( result ) );
m.insert( "writeHostsFile", false ); m.insert( "writeHostsFile", false );
QCOMPARE( getHostNameActions( m ), HostNameActions( result ) ); QCOMPARE( getHostNameAction( m ), HostNameAction( result ) );
m.insert( "writeHostsFile", true ); m.insert( "writeHostsFile", true );
QCOMPARE( getHostNameActions( m ), HostNameActions( result ) | HostNameAction::WriteEtcHosts ); QCOMPARE( getHostNameAction( m ), HostNameAction( result ) );
} }
void void
@ -257,13 +257,16 @@ UserTests::testHostActions2()
Config c; Config c;
QVariantMap legacy; QVariantMap legacy;
// Test defaults
c.setConfigurationMap( legacy ); 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( "writeHostsFile", false );
legacy.insert( "setHostname", "Hostnamed" ); legacy.insert( "setHostname", "Hostnamed" );
c.setConfigurationMap( legacy ); 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 ); connect( config, &Config::fullNameChanged, this, &UsersPage::onFullNameTextEdited );
// If the hostname is going to be written out, then show the field // If the hostname is going to be written out, then show the field
if ( ( m_config->hostNameActions() & HostNameAction::EtcHostname ) if ( ( m_config->hostNameAction() == HostNameAction::EtcHostname )
|| ( m_config->hostNameActions() & HostNameAction::SystemdHostname ) ) || ( m_config->hostNameAction() == HostNameAction::SystemdHostname ) )
{ {
ui->textBoxHostname->setText( config->hostName() ); ui->textBoxHostname->setText( config->hostName() );
connect( ui->textBoxHostname, &QLineEdit::textEdited, config, &Config::setHostName ); connect( ui->textBoxHostname, &QLineEdit::textEdited, config, &Config::setHostName );