[users] Move the hostname-setting config

- The configuration for writing the hostname (to /etc/hostname,
  to /etc/hosts and possibly to systemd-hostname) is read-only,
  because it comes from the config file and won't change after.
main
Adriaan de Groot 4 years ago
parent 32e3933355
commit 2efce1ac7a

@ -36,6 +36,22 @@ static const QRegExp HOSTNAME_RX( "^[a-zA-Z0-9][-a-zA-Z0-9_]*$" );
static constexpr const int HOSTNAME_MIN_LENGTH = 2;
static constexpr const int HOSTNAME_MAX_LENGTH = 63;
const NamedEnumTable< HostNameAction >&
hostNameActionNames()
{
// *INDENT-OFF*
// clang-format off
static const NamedEnumTable< HostNameAction > names {
{ QStringLiteral( "none" ), HostNameAction::None },
{ QStringLiteral( "etcfile" ), HostNameAction::EtcHostname },
{ QStringLiteral( "hostnamed" ), HostNameAction::SystemdHostname }
};
// clang-format on
// *INDENT-ON*
return names;
}
Config::Config( QObject* parent )
: QObject( parent )
{
@ -378,6 +394,25 @@ setConfigurationDefaultGroups( const QVariantMap& map, QStringList& defaultGroup
}
}
STATICTEST HostNameActions
getHostNameActions( const QVariantMap& configurationMap )
{
HostNameAction setHostName = HostNameAction::EtcHostname;
QString hostnameActionString = CalamaresUtils::getString( configurationMap, "setHostname" );
if ( !hostnameActionString.isEmpty() )
{
bool ok = false;
setHostName = hostNameActionNames().find( hostnameActionString, ok );
if ( !ok )
{
setHostName = HostNameAction::EtcHostname; // Rather than none
}
}
HostNameAction writeHosts = CalamaresUtils::getBool( configurationMap, "writeHostsFile", true ) ? HostNameAction::WriteEtcHosts : HostNameAction::None;
return setHostName | writeHosts;
}
void
Config::setConfigurationMap( const QVariantMap& configurationMap )
@ -393,6 +428,8 @@ Config::setConfigurationMap( const QVariantMap& configurationMap )
setAutologinGroup( CalamaresUtils::getString( configurationMap, "autologinGroup" ) );
setSudoersGroup( CalamaresUtils::getString( configurationMap, "sudoersGroup" ) );
m_hostNameActions = getHostNameActions( configurationMap );
setConfigurationDefaultGroups( configurationMap, m_defaultGroups );
m_doAutoLogin = CalamaresUtils::getBool( configurationMap, "doAutologin", false );

@ -21,9 +21,23 @@
#ifndef USERS_CONFIG_H
#define USERS_CONFIG_H
#include "utils/NamedEnum.h"
#include <QObject>
#include <QVariantMap>
enum 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)
};
Q_DECLARE_FLAGS( HostNameActions, HostNameAction )
Q_DECLARE_OPERATORS_FOR_FLAGS( HostNameActions )
const NamedEnumTable< HostNameAction >& hostNameActionNames();
class Config : public QObject
{
Q_OBJECT
@ -41,6 +55,7 @@ class Config : public QObject
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( bool writeRootPassword READ writeRootPassword CONSTANT )
Q_PROPERTY( bool reuseUserPasswordForRoot READ reuseUserPasswordForRoot WRITE setReuseUserPasswordForRoot NOTIFY
@ -79,6 +94,8 @@ public:
QString hostName() const { return m_hostName; }
/// Status message about hostname -- empty for "ok"
QString hostNameStatus() const;
/// How to write the hostname
HostNameActions hostNameActions() const { return m_hostNameActions; }
/// Should the user be automatically logged-in?
bool doAutoLogin() const { return m_doAutoLogin; }
@ -159,6 +176,8 @@ private:
bool m_customLoginName = false;
bool m_customHostName = false;
HostNameActions m_hostNameActions;
};
#endif

@ -27,13 +27,13 @@
#include <QDir>
#include <QFile>
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusReply>
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusReply>
using WriteMode = CalamaresUtils::System::WriteMode;
SetHostNameJob::SetHostNameJob( const QString& hostname, Actions a )
SetHostNameJob::SetHostNameJob( const QString& hostname, HostNameActions a )
: Calamares::Job()
, m_hostname( hostname )
, m_actions( a )
@ -138,7 +138,7 @@ SetHostNameJob::exec()
return Calamares::JobResult::error( tr( "Internal Error" ) );
}
if ( m_actions & Action::EtcHostname )
if ( m_actions & HostNameAction::EtcHostname )
{
if ( !setFileHostname( m_hostname ) )
{
@ -147,7 +147,7 @@ SetHostNameJob::exec()
}
}
if ( m_actions & Action::WriteEtcHosts )
if ( m_actions & HostNameAction::WriteEtcHosts )
{
if ( !writeFileEtcHosts( m_hostname ) )
{
@ -156,7 +156,7 @@ SetHostNameJob::exec()
}
}
if ( m_actions & Action::SystemdHostname )
if ( m_actions & HostNameAction::SystemdHostname )
{
// Does its own logging
setSystemdHostname( m_hostname );

@ -21,23 +21,15 @@
#ifndef SETHOSTNAMEJOB_CPP_H
#define SETHOSTNAMEJOB_CPP_H
#include "Config.h"
#include "Job.h"
class SetHostNameJob : public Calamares::Job
{
Q_OBJECT
public:
enum Action
{
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)
};
Q_DECLARE_FLAGS( Actions, Action )
SetHostNameJob( const QString& hostname, Actions a );
SetHostNameJob( const QString& hostname, HostNameActions a );
QString prettyName() const override;
QString prettyDescription() const override;
QString prettyStatusMessage() const override;
@ -45,9 +37,7 @@ public:
private:
const QString m_hostname;
const Actions m_actions;
const HostNameActions m_actions;
};
Q_DECLARE_OPERATORS_FOR_FLAGS( SetHostNameJob::Actions )
#endif // SETHOSTNAMEJOB_CPP_H

@ -34,28 +34,9 @@
CALAMARES_PLUGIN_FACTORY_DEFINITION( UsersViewStepFactory, registerPlugin< UsersViewStep >(); )
static const NamedEnumTable< SetHostNameJob::Action >&
hostnameActions()
{
using Action = SetHostNameJob::Action;
// *INDENT-OFF*
// clang-format off
static const NamedEnumTable< Action > names {
{ QStringLiteral( "none" ), Action::None },
{ QStringLiteral( "etcfile" ), Action::EtcHostname },
{ QStringLiteral( "hostnamed" ), Action::SystemdHostname }
};
// clang-format on
// *INDENT-ON*
return names;
}
UsersViewStep::UsersViewStep( QObject* parent )
: Calamares::ViewStep( parent )
, m_widget( nullptr )
, m_actions( SetHostNameJob::Action::None )
, m_config( new Config( this ) )
{
emit nextStatusChanged( true );
@ -158,7 +139,8 @@ UsersViewStep::onLeave()
j = new SetPasswordJob( "root", m_widget->getRootPassword() );
m_jobs.append( Calamares::job_ptr( j ) );
j = new SetHostNameJob( m_config->hostName(), m_actions );
// TODO: Config object should create jobs
j = new SetHostNameJob( m_config->hostName(), m_config->hostNameActions() );
m_jobs.append( Calamares::job_ptr( j ) );
m_widget->fillGlobalStorage();
@ -184,21 +166,4 @@ UsersViewStep::setConfigurationMap( const QVariantMap& configurationMap )
m_widget->addPasswordCheck( i.key(), i.value() );
}
}
using Action = SetHostNameJob::Action;
QString hostnameActionString = CalamaresUtils::getString( configurationMap, "setHostname" );
if ( hostnameActionString.isEmpty() )
{
hostnameActionString = QStringLiteral( "EtcFile" );
}
bool ok = false;
auto hostnameAction = hostnameActions().find( hostnameActionString, ok );
if ( !ok )
{
hostnameAction = Action::EtcHostname;
}
Action hostsfileAction = getBool( configurationMap, "writeHostsFile", true ) ? Action::WriteEtcHosts : Action::None;
m_actions = hostsfileAction | hostnameAction;
}

@ -20,8 +20,6 @@
#ifndef USERSPAGEPLUGIN_H
#define USERSPAGEPLUGIN_H
#include "SetHostNameJob.h"
#include "DllMacro.h"
#include "utils/PluginFactory.h"
#include "viewpages/ViewStep.h"
@ -61,8 +59,6 @@ private:
UsersPage* m_widget;
QList< Calamares::job_ptr > m_jobs;
SetHostNameJob::Actions m_actions;
Config* m_config;
};

Loading…
Cancel
Save