Compare commits

..

1 Commits

Author SHA1 Message Date
d6691aea6b add services-artix 2023-12-28 11:13:17 +01:00
8 changed files with 494 additions and 310 deletions

View File

@@ -49,7 +49,6 @@ calamares_add_plugin(packagechooser
PackageChooserPage.cpp
PackageChooserViewStep.cpp
PackageModel.cpp
LoaderQueue.cpp
${_extra_src}
RESOURCES
packagechooser.qrc

View File

@@ -10,8 +10,6 @@
#include "Config.h"
#include "LoaderQueue.h"
#ifdef HAVE_APPDATA
#include "ItemAppData.h"
#endif
@@ -351,27 +349,6 @@ Config::setConfigurationMap( const QVariantMap& configurationMap )
}
}
// Lastly, load the groups data
const QString key = QStringLiteral( "groupsUrl" );
const auto& groupsUrlVariant = configurationMap.value( key );
m_queue = new LoaderQueue( this );
if ( Calamares::typeOf( groupsUrlVariant ) == Calamares::StringVariantType )
{
m_queue->append( SourceItem::makeSourceItem( groupsUrlVariant.toString(), configurationMap ) );
}
else if ( Calamares::typeOf( groupsUrlVariant ) == Calamares::ListVariantType )
{
for ( const auto& s : groupsUrlVariant.toStringList() )
{
m_queue->append( SourceItem::makeSourceItem( s, configurationMap ) );
}
}
setStatus( required() ? Status::FailedNoData : Status::Ok );
cDebug() << "Loading netinstall from" << m_queue->count() << "alternate sources.";
connect( m_queue, &LoaderQueue::done, this, &Config::loadingDone );
m_queue->load();
bool labels_ok = false;
auto labels = Calamares::getSubMap( configurationMap, "labels", labels_ok );
if ( labels_ok )

View File

@@ -19,8 +19,6 @@
#include <memory>
#include <optional>
class LoaderQueue;
enum class PackageChooserMode
{
Optional, // zero or one
@@ -124,8 +122,6 @@ private:
*/
std::optional< QString > m_packageChoice;
Calamares::Locale::TranslatedString* m_stepName; // As it appears in the sidebar
LoaderQueue* m_queue = nullptr;
};

View File

@@ -1,205 +0,0 @@
/*
* SPDX-FileCopyrightText: 2016 Luca Giambonini <almack@chakraos.org>
* SPDX-FileCopyrightText: 2016 Lisa Vitolo <shainer@chakraos.org>
* SPDX-FileCopyrightText: 2017 Kyle Robbertze <krobbertze@gmail.com>
* SPDX-FileCopyrightText: 2017-2018 2020, Adriaan de Groot <groot@kde.org>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Calamares is Free Software: see the License-Identifier above.
*
*/
#include "LoaderQueue.h"
#include "Config.h"
#include "network/Manager.h"
#include "utils/Logger.h"
#include "utils/RAII.h"
#include "utils/Yaml.h"
#include <QNetworkReply>
#include <QTimer>
/** @brief Call fetchNext() on the queue if it can
*
* On destruction, a new call to fetchNext() is queued, so that
* the queue continues loading. Calling release() before the
* destructor skips the fetchNext(), ending the queue-loading.
*
* Calling done(b) is a conditional release: if @p b is @c true,
* queues a call to done() on the queue and releases it; otherwise,
* does nothing.
*/
class FetchNextUnless
{
public:
FetchNextUnless( LoaderQueue* q )
: m_q( q )
{
}
~FetchNextUnless()
{
if ( m_q )
{
QMetaObject::invokeMethod( m_q, "fetchNext", Qt::QueuedConnection );
}
}
void release() { m_q = nullptr; }
void done( bool b )
{
if ( b )
{
if ( m_q )
{
QMetaObject::invokeMethod( m_q, "done", Qt::QueuedConnection );
}
release();
}
}
private:
LoaderQueue* m_q = nullptr;
};
SourceItem
SourceItem::makeSourceItem( const QString& groupsUrl, const QVariantMap& configurationMap )
{
if ( groupsUrl == QStringLiteral( "local" ) )
{
return SourceItem { QUrl(), configurationMap.value( "groups" ).toList() };
}
else
{
return SourceItem { QUrl { groupsUrl }, QVariantList() };
}
}
LoaderQueue::LoaderQueue( Config* parent )
: QObject( parent )
, m_config( parent )
{
}
void
LoaderQueue::append( SourceItem&& i )
{
m_queue.append( std::move( i ) );
}
void
LoaderQueue::load()
{
QMetaObject::invokeMethod( this, "fetchNext", Qt::QueuedConnection );
}
void
LoaderQueue::fetchNext()
{
if ( m_queue.isEmpty() )
{
emit done();
return;
}
auto source = m_queue.takeFirst();
if ( source.isLocal() )
{
m_config->loadGroupList( source.data );
emit done();
}
else
{
fetch( source.url );
}
}
void
LoaderQueue::fetch( const QUrl& url )
{
FetchNextUnless next( this );
if ( !url.isValid() )
{
m_config->setStatus( Config::Status::FailedBadConfiguration );
cDebug() << "Invalid URL" << url;
return;
}
using namespace Calamares::Network;
cDebug() << "NetInstall loading groups from" << url;
QNetworkReply* reply = Manager().asynchronousGet(
url,
RequestOptions( RequestOptions::FakeUserAgent | RequestOptions::FollowRedirect, std::chrono::seconds( 30 ) ) );
if ( !reply )
{
cDebug() << Logger::SubEntry << "Request failed immediately.";
// If nobody sets a different status, this will remain
m_config->setStatus( Config::Status::FailedBadConfiguration );
}
else
{
// When the network request is done, **then** we might
// do the next item from the queue, so don't call fetchNext() now.
next.release();
m_reply = reply;
connect( reply, &QNetworkReply::finished, this, &LoaderQueue::dataArrived );
}
}
void
LoaderQueue::dataArrived()
{
FetchNextUnless next( this );
if ( !m_reply || !m_reply->isFinished() )
{
cWarning() << "NetInstall data called too early.";
m_config->setStatus( Config::Status::FailedInternalError );
return;
}
cDebug() << "NetInstall group data received" << m_reply->size() << "bytes from" << m_reply->url();
cqDeleter< QNetworkReply > d { m_reply };
// If m_required is *false* then we still say we're ready
// even if the reply is corrupt or missing.
if ( m_reply->error() != QNetworkReply::NoError )
{
cWarning() << "unable to fetch netinstall package lists.";
cDebug() << Logger::SubEntry << "Netinstall reply error: " << m_reply->error();
cDebug() << Logger::SubEntry << "Request for url: " << m_reply->url().toString()
<< " failed with: " << m_reply->errorString();
m_config->setStatus( Config::Status::FailedNetworkError );
return;
}
QByteArray yamlData = m_reply->readAll();
try
{
auto groups = ::YAML::Load( yamlData.constData() );
if ( groups.IsSequence() )
{
m_config->loadGroupList( Calamares::YAML::sequenceToVariant( groups ) );
next.done( m_config->statusCode() == Config::Status::Ok );
}
else if ( groups.IsMap() )
{
auto map = Calamares::YAML::mapToVariant( groups );
m_config->loadGroupList( map.value( "groups" ).toList() );
next.done( m_config->statusCode() == Config::Status::Ok );
}
else
{
cWarning() << "NetInstall groups data does not form a sequence.";
}
}
catch ( ::YAML::Exception& e )
{
Calamares::YAML::explainException( e, yamlData, "netinstall groups data" );
m_config->setStatus( Config::Status::FailedBadData );
}
}

View File

@@ -1,77 +0,0 @@
/*
* SPDX-FileCopyrightText: 2016 Luca Giambonini <almack@chakraos.org>
* SPDX-FileCopyrightText: 2016 Lisa Vitolo <shainer@chakraos.org>
* SPDX-FileCopyrightText: 2017 Kyle Robbertze <krobbertze@gmail.com>
* SPDX-FileCopyrightText: 2017-2018 2020, Adriaan de Groot <groot@kde.org>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Calamares is Free Software: see the License-Identifier above.
*
*/
#ifndef NETINSTALL_LOADERQUEUE_H
#define NETINSTALL_LOADERQUEUE_H
#include <QQueue>
#include <QUrl>
#include <QVariantList>
class Config;
class QNetworkReply;
/** @brief Data about an entry in *groupsUrl*
*
* This can be a specific URL, or "local" which uses data stored
* in the configuration file itself.
*/
struct SourceItem
{
QUrl url;
QVariantList data;
bool isUrl() const { return url.isValid(); }
bool isLocal() const { return !data.isEmpty(); }
bool isValid() const { return isUrl() || isLocal(); }
/** @brief Create a SourceItem
*
* If the @p groupsUrl is @c "local" then the *groups* key in
* the @p configurationMap is used as the source; otherwise the
* string is used as an actual URL.
*/
static SourceItem makeSourceItem( const QString& groupsUrl, const QVariantMap& configurationMap );
};
/** @brief Queue of source items to load
*
* Queue things up by calling append() and then kick things off
* by calling load(). This will try to load the items, in order;
* the first one that succeeds will end the loading process.
*
* Signal done() is emitted when done (also when all of the items fail).
*/
class LoaderQueue : public QObject
{
Q_OBJECT
public:
LoaderQueue( Config* parent );
void append( SourceItem&& i );
int count() const { return m_queue.count(); }
public Q_SLOTS:
void load();
void fetchNext();
void fetch( const QUrl& url );
void dataArrived();
Q_SIGNALS:
void done();
private:
QQueue< SourceItem > m_queue;
Config* m_config = nullptr;
QNetworkReply* m_reply = nullptr;
};
#endif

View File

@@ -0,0 +1,419 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# === This file is part of Calamares - <https://calamares.io> ===
#
# SPDX-FileCopyrightText: 2016 Artoo <artoo@manjaro.org>
# SPDX-FileCopyrightText: 2017 Philip Müller <philm@manjaro.org>
# SPDX-FileCopyrightText: 2018 Artoo <artoo@artixlinux.org>
# SPDX-FileCopyrightText: 2018-2019 Adriaan de Groot <groot@kde.org>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Calamares is Free Software: see the License-Identifier above.
#
import libcalamares
from libcalamares.utils import target_env_call, warning
from os.path import exists, join
import gettext
_ = gettext.translation("calamares-python",
localedir=libcalamares.utils.gettext_path(),
languages=libcalamares.utils.gettext_languages(),
fallback=True).gettext
def pretty_name():
return _("Configure artix services")
# TODO: cleran up this merged mess, implement meta class
class OpenrcController:
"""
This is the openrc service controller.
All of its state comes from global storage and the job
configuration at initialization time.
"""
def __init__(self):
self.root = libcalamares.globalstorage.value('rootMountPoint')
# Translate the entries in the config to the actions passed to rc-config
self.services = dict()
self.services["add"] = libcalamares.job.configuration.get('services', [])
self.services["del"] = libcalamares.job.configuration.get('disable', [])
openrc = libcalamares.job.configuration.get('openrc', None)
if openrc is None:
openrc = dict()
if type(openrc) is not dict:
libcalamares.utils.warning("Job configuration *openrc* will be ignored.")
openrc = dict()
self.openrc_initdDir = openrc.get('initdDir')
self.openrc_runlevelsDir = openrc.get('runlevelsDir')
def make_failure_description(self, state, name, runlevel):
"""
Returns a generic "could not <foo>" failure message, specialized
for the action @p state and the specific service @p name in @p runlevel.
"""
if state == "add":
description = _("Cannot add service {name!s} to run-level {level!s}.")
elif state == "del":
description = _("Cannot remove service {name!s} from run-level {level!s}.")
else:
description = _("Unknown service-action <code>{arg!s}</code> for service {name!s} in run-level {level!s}.")
return description.format(arg=state, name=name, level=runlevel)
def update(self, state):
"""
Call rc-update for each service listed
in services for the given @p state. rc-update
is called with @p state as the command as well.
"""
for svc in self.services.get(state, []):
if isinstance(svc, str):
name = svc
runlevel = "default"
mandatory = False
else:
name = svc["name"]
runlevel = svc.get("runlevel", "default")
mandatory = svc.get("mandatory", False)
service_path = self.root + self.openrc_initdDir + "/" + name
runlevel_path = self.root + self.openrc_runlevelsDir + "/" + runlevel
if exists(service_path):
if exists(runlevel_path):
ec = target_env_call(["rc-update", state, name, runlevel])
if ec != 0:
warning("Cannot {} service {} to {}".format(state, name, runlevel))
warning("rc-update returned error code {!s}".format(ec))
if mandatory:
title = _("Cannot modify service")
diagnostic = _("<code>rc-update {arg!s}</code> call in chroot returned error code {num!s}.").format(arg=state, num=ec)
return (title,
self.make_failure_description(state, name, runlevel) + " " + diagnostic
)
else:
warning("Target runlevel {} does not exist for {}.".format(runlevel, name))
if mandatory:
title = _("Target runlevel does not exist")
diagnostic = _("The path for runlevel {level!s} is <code>{path!s}</code>, which does not exist.").format(level=runlevel, path=runlevel_path)
return (title,
self.make_failure_description(state, name, runlevel) + " " + diagnostic
)
else:
warning("Target service {} does not exist in {}.".format(name, self.openrc_initdDir))
if mandatory:
title = _("Target service does not exist")
diagnostic = _("The path for service {name!s} is <code>{path!s}</code>, which does not exist.").format(name=name, path=service_path)
return (title,
self.make_failure_description(state, name, runlevel) + " " + diagnostic
)
def run(self):
"""Run the controller
"""
for state in ("add", "del"):
r = self.update(state)
if r is not None:
return r
class S6Controller:
"""
This is the s6 service controller.
All of its state comes from global storage and the job
configuration at initialization time.
"""
def __init__(self):
self.root = libcalamares.globalstorage.value('rootMountPoint')
s6 = libcalamares.job.configuration.get('s6', None)
if s6 is None:
s6 = dict()
if type(s6) is not dict:
libcalamares.utils.warning("Job configuration *s6* will be ignored.")
s6 = dict()
self.s6_dbDir = s6.get('dbDir')
self.s6_defaultBundle = s6.get('defaultBundle')
self.services = dict()
self.services["add"] = libcalamares.job.configuration.get('services', [])
self.services["delete"] = libcalamares.job.configuration.get('disable', [])
def makeBundle(self):
"""
Call s6-rc-bundle with each service listed
in services as arg.
"""
deleteBundles = self.services.get("delete", [])
if deleteBundles:
ec = target_env_call(["s6-rc-bundle", "-c", self.s6_dbDir, "delete", *deleteBundles])
if ec != 0:
warning("Cannot delete {}".format(*deleteBundles))
warning("s6-rc-bundle returned error code {!s}".format(ec))
for svc in self.services.get('add', []):
ec = target_env_call(["s6-rc-db", "-c", self.s6_dbDir, "type", svc])
if ec != 0:
warning("Service {} does not exist.".format(svc))
warning("s6-rc-db returned error code {!s}".format(ec))
else:
ec = target_env_call(["s6-service", "add", self.s6_defaultBundle, svc])
if ec != 0:
warning("Cannot add service {} to {} bundle".format(svc, self.s6_defaultBundle))
warning("s6-service returned error code {!s}".format(ec))
ec = target_env_call(["s6-db-reload", "-r"])
if ec != 0:
warning("Cannot reload service db.")
warning("s6-db-reload returned error code {!s}".format(ec))
def run(self):
"""Run the controller
"""
r = self.makeBundle()
if r is not None:
return r
class DinitController:
"""
This is the dinit service controller.
All of its state comes from global storage and the job
configuration at initialization time.
"""
def __init__(self):
self.root = libcalamares.globalstorage.value('rootMountPoint')
# Translate the entries in the config to the actions passed to sv-helper
self.services = dict()
self.services["enable"] = libcalamares.job.configuration.get('services', [])
self.services["disable"] = libcalamares.job.configuration.get('disable', [])
dinit = libcalamares.job.configuration.get('dinit', None)
if dinit is None:
dinit = dict()
if type(dinit) is not dict:
libcalamares.utils.warning("Job configuration *dinit* will be ignored.")
dinit = dict()
self.dinit_initdDir = dinit.get('initdDir')
self.dinit_runsvDir = dinit.get('runsvDir')
def make_failure_description(self, state, name):
"""
Returns a generic "could not <foo>" failure message, specialized
for the action @p state and the specific service @p name.
"""
if state == "enable":
description = _("Cannot enable service {name!s}.")
elif state == "disable":
description = _("Cannot disable service {name!s}.")
else:
description = _("Unknown service-action <code>{arg!s}</code> for service {name!s}.")
return description.format(arg=state, name=name)
def update(self, state):
"""
Process each service listed
in services for the given @p state.
"""
for svc in self.services.get(state, []):
if isinstance(svc, str):
name = svc
mandatory = False
else:
name = svc["name"]
mandatory = svc.get("mandatory", False)
service_path = self.root + self.dinit_initdDir + "/" + name
runlevel_path = self.root + self.dinit_runsvDir
src = self.dinit_initdDir + "/" + name
dest = self.dinit_runsvDir + "/"
if state == 'enable':
cmd = ["ln", "-sv", src, dest]
elif state == 'disable':
cmd = ["rm", "-rv", dest]
if exists(service_path):
if exists(runlevel_path):
ec = target_env_call(cmd)
if ec != 0:
warning("Cannot {} service {}".format(state, name))
warning("{} returned error code {!s}".format(cmd, ec))
if mandatory:
title = _("Cannot modify service")
diagnostic = _("<code>cmd {arg!s}</code> call in chroot returned error code {num!s}.").format(arg=state, num=ec)
return (title,
self.make_failure_description(state, name) + " " + diagnostic
)
else:
warning("Target service {} does not exist in {}.".format(name, self.dinit_initdDir))
if mandatory:
title = _("Target service does not exist")
diagnostic = _("The path for service {name!s} is <code>{path!s}</code>, which does not exist.").format(name=name, path=service_path)
return (title,
self.make_failure_description(state, name) + " " + diagnostic
)
def run(self):
"""Run the controller
"""
for state in ("enable", "disable"):
r = self.update(state)
if r is not None:
return r
class RunitController:
"""
This is the runit service controller.
All of its state comes from global storage and the job
configuration at initialization time.
"""
def __init__(self):
self.root = libcalamares.globalstorage.value('rootMountPoint')
# Translate the entries in the config to the actions passed to sv-helper
self.services = dict()
self.services["enable"] = libcalamares.job.configuration.get('services', [])
self.services["disable"] = libcalamares.job.configuration.get('disable', [])
runit = libcalamares.job.configuration.get('runit', None)
if runit is None:
runit = dict()
if type(runit) is not dict:
libcalamares.utils.warning("Job configuration *runit* will be ignored.")
runit = dict()
self.runit_svDir = libcalamares.job.configuration['svDir']
self.runit_runsvDir = libcalamares.job.configuration['runsvDir']
def make_failure_description(self, state, name, runlevel):
"""
Returns a generic "could not <foo>" failure message, specialized
for the action @p state and the specific service @p name in @p runlevel.
"""
if state == "enable":
description = _("Cannot enable service {name!s} to run-level {level!s}.")
elif state == "disable":
description = _("Cannot disable service {name!s} from run-level {level!s}.")
else:
description = _("Unknown service-action <code>{arg!s}</code> for service {name!s} in run-level {level!s}.")
return description.format(arg=state, name=name, level=runlevel)
def update(self, state):
"""
Call sv-helper for each service listed
in services for the given @p state.
"""
for svc in self.services.get(state, []):
if isinstance(svc, str):
name = svc
runlevel = "default"
mandatory = False
else:
name = svc["name"]
runlevel = svc.get("runlevel", "default")
mandatory = svc.get("mandatory", False)
service_path = self.root + self.runit_svDir + "/" + name
runlevel_path = self.root + self.runit_runsvDir + "/" + runlevel
src = self.runit_svDir + "/" + name
dest = self.runit_runsvDir + "/" + runlevel + "/"
if state == 'enable':
cmd = ["ln", "-sv", src, dest]
elif state == 'disable':
cmd = ["rm", "-rv", dest]
if exists(service_path):
if exists(runlevel_path):
ec = target_env_call(cmd)
if ec != 0:
warning("Cannot {} service {} to {}".format(state, name, runlevel))
warning("{} returned error code {!s}".format(cmd, ec))
if mandatory:
title = _("Cannot modify service")
diagnostic = _("<code>cmd {arg!s}</code> call in chroot returned error code {num!s}.").format(arg=state, num=ec)
return (title,
self.make_failure_description(state, name, runlevel) + " " + diagnostic
)
else:
warning("Target runlevel {} does not exist for {}.".format(runlevel, name))
if mandatory:
title = _("Target runlevel does not exist")
diagnostic = _("The path for runlevel {level!s} is <code>{path!s}</code>, which does not exist.").format(level=runlevel, path=runlevel_path)
return (title,
self.make_failure_description(state, name, runlevel) + " " + diagnostic
)
else:
warning("Target service {} does not exist in {}.".format(name, self.svDir))
if mandatory:
title = _("Target service does not exist")
diagnostic = _("The path for service {name!s} is <code>{path!s}</code>, which does not exist.").format(name=name, path=service_path)
return (title,
self.make_failure_description(state, name, runlevel) + " " + diagnostic
)
def run(self):
"""Run the controller
"""
for state in ("enable", "disable"):
r = self.update(state)
if r is not None:
return r
def run():
"""
Setup services
"""
if libcalamares.globalstorage.contains("initProvider"):
init_provider = libcalamares.globalstorage.value("initProvider")
match init_provider:
case 'openrc':
return OpenrcController().run()
case's6':
return S6Controller().run()
case 'dinit':
return DinitController().run()
case 'runit':
return RunitController().run()
case _:
return None

View File

@@ -0,0 +1,7 @@
# SPDX-FileCopyrightText: no
# SPDX-License-Identifier: CC0-1.0
---
type: "job"
name: "services-artix"
interface: "python"
script: "main.py"

View File

@@ -0,0 +1,68 @@
# SPDX-FileCopyrightText: no
# SPDX-License-Identifier: CC0-1.0
#
# openrc services module to modify service runlevels via rc-update in the chroot
#
# Services can be added (to any runlevel, or multiple runlevels) or deleted.
# Handle del with care and only use it if absolutely necessary.
#
# if a service is listed in the conf but is not present/detected on the target system,
# or a runlevel does not exist, it will be ignored and skipped; a warning is logged.
#
---
openrc:
# initdDir: holds the openrc service directory location
initdDir: /etc/init.d
# runlevelsDir: holds the runlevels directory location
runlevelsDir: /etc/runlevels
dinit:
# initdDir: holds the runit service directory location
initdDir: /etc/dinit.d
# runsvDir: holds the runlevels directory location
runsvDir: /etc/dinit.d/boot.d
runit:
# svDir: holds the runit service directory location
svDir: /etc/runit/sv
# runsvDir: holds the runlevels directory location
runsvDir: /etc/runit/runsvdir
s6:
# database path
dbDir: /etc/s6/rc/compiled
# default bundle name
defaultBundle: default
# services: a list of entries to **enable**
# disable: a list of entries to **disable**
#
# Each entry has three fields:
# - name: the service name
# - (optional) runlevel: can hold any runlevel present on the target
# system; if no runlevel is provided, "default" is assumed.
# - (optional) mandatory: if set to true, a failure to modify
# the service will result in installation failure, rather than just
# a warning. The default is false.
#
# an entry may also be a single string, which is interpreted
# as the name field (runlevel "default" is assumed then, and not-mandatory).
#
# # Example services and disable settings:
# # - add foo1 to default, but it must succeed
# # - add foo2 to nonetwork
# # - remove foo3 from default
# # - remove foo4 from default
# services:
# - name: foo1
# mandatory: true
# - name: foo2
# runlevel: nonetwork
# disable:
# - name: foo3
# runlevel: default
# - foo4
services: []
disable: []