Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(gen): change the way of uising generators #931

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions debian/linglong-bin.install
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ etc/X11/Xsession.d/21linglong
etc/profile.d/linglong.sh
usr/bin/ll-cli
usr/bin/llpkg
usr/lib/linglong/*
usr/lib/systemd/system-environment-generators/61-linglong
usr/lib/systemd/system/org.deepin.linglong.PackageManager.service lib/systemd/system/
usr/lib/systemd/user/linglong-session-helper.service
Expand Down
111 changes: 15 additions & 96 deletions libs/linglong/src/linglong/runtime/container_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,38 +114,12 @@ void applyJSONPatch(ocppi::runtime::config::types::Config &cfg,
}
}

void applyJSONFilePatch(ocppi::runtime::config::types::Config &cfg,
const QFileInfo &info,
const generator::Generator &gen) noexcept
void applyJSONFilePatch(ocppi::runtime::config::types::Config &cfg, const QFileInfo &info) noexcept
{
LINGLONG_TRACE(QString("apply oci runtime config patch file %1").arg(info.absoluteFilePath()));

QFile file(info.absoluteFilePath());
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << LINGLONG_ERRV(file);
Q_ASSERT(false);
return;
}

auto content = file.readAll();
if (file.error() != QFile::NoError) {
qWarning() << LINGLONG_ERRV(file);
Q_ASSERT(false);
return;
}

if (content.startsWith("null")) {
qDebug() << "use builtin generator" << gen.name().data();

if (!gen.generate(cfg)) {
qWarning() << "generator" << gen.name().data() << "failed";
}

return;
}

auto patch =
utils::serialize::LoadJSON<api::types::v1::OciConfigurationPatch>(content.toStdString());
auto patch = utils::serialize::LoadJSONFile<api::types::v1::OciConfigurationPatch>(
info.absoluteFilePath());
if (!patch) {
qWarning() << LINGLONG_ERRV(patch);
Q_ASSERT(false);
Expand All @@ -155,56 +129,11 @@ void applyJSONFilePatch(ocppi::runtime::config::types::Config &cfg,
applyJSONPatch(cfg, *patch);
}

bool isCustomExecutablePatch(const QFileInfo &info) noexcept
{
QFile file{ info.absoluteFilePath() };
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return false;
}

constexpr auto comment = "# LINGLONG_BUILTIN_GENERATOR";
QString buf;
QTextStream stream{ &file };

buf = stream.readLine(); // check shebang, only support shell script for now
if (!buf.startsWith("#!")) {
return false;
}

while (!stream.atEnd()) {
buf = stream.readLine();
if (buf.isEmpty()) {
continue;
}

if (!buf.startsWith("#")) {
return true;
}

if (buf.startsWith(comment)) {
return false;
}
}

return false;
}

void applyExecutablePatch(ocppi::runtime::config::types::Config &cfg,
const QFileInfo &info,
const generator::Generator &gen) noexcept
const QFileInfo &info) noexcept
{
LINGLONG_TRACE(QString("process oci configuration generator %1").arg(info.absoluteFilePath()));

if (!isCustomExecutablePatch(info)) {
qDebug() << "use builtin generator" << gen.name().data();

if (!gen.generate(cfg)) {
qWarning() << "generator" << gen.name().data() << "failed";
}

return;
}

QProcess generatorProcess;
generatorProcess.setProgram(info.absoluteFilePath());
generatorProcess.start();
Expand Down Expand Up @@ -251,35 +180,25 @@ void applyPatches(ocppi::runtime::config::types::Config &cfg, const QFileInfoLis
continue;
}

auto gen = builtins.find(info.completeBaseName().toStdString());
if (gen == builtins.cend()) {
qInfo() << info.absoluteFilePath()
<< "is an unknown generator, but we don't support custom unknown generator "
"currently.";
if (info.completeSuffix() == "json") {
applyJSONFilePatch(cfg, info);
continue;
}

if (info.size() < 4) {
qDebug() << "generator" << info.absoluteFilePath() << "may be broken, use builtin";

if (!gen->second->generate(cfg)) {
qWarning() << "generator" << info.absoluteFilePath() << "failed";
}

if (info.isExecutable()) {
applyExecutablePatch(cfg, info);
continue;
}

if (info.completeSuffix() == "json") {
applyJSONFilePatch(cfg, info, *gen->second);
auto gen = builtins.find(info.completeBaseName().toStdString());
if (gen == builtins.cend()) {
qDebug() << "unsupported generator:" << info.absoluteFilePath();
continue;
}

if (info.isExecutable()) {
applyExecutablePatch(cfg, info, *gen->second);
continue;
if (!gen->second->generate(cfg)) {
qDebug() << "builtin generator failed:" << gen->first.data();
}

qDebug() << "unsupported generator type:" << info.absoluteFilePath();
}
}

Expand Down Expand Up @@ -309,8 +228,8 @@ auto getOCIConfig(const ContainerOptions &opts, const std::string &bundleDir) no
}
}

auto config = utils::serialize::LoadJSON<ocppi::runtime::config::types::Config>(
linglong::generator::initConfig);
auto config = utils::serialize::LoadJSONFile<ocppi::runtime::config::types::Config>(
containerConfigFilePath);
if (!config) {
Q_ASSERT(false);
return LINGLONG_ERR(config);
Expand Down
3 changes: 0 additions & 3 deletions misc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ install(
FILES ${LINGLONG_CONFIG_DIR}/10-basics.json
${LINGLONG_CONFIG_DIR}/25-host-rootfs.json
${LINGLONG_CONFIG_DIR}/25-host-statics.json
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/linglong/container/config.d)

install(PROGRAMS
${LINGLONG_CONFIG_DIR}/00-id-mapping
${LINGLONG_CONFIG_DIR}/05-initialize
${LINGLONG_CONFIG_DIR}/20-devices
Expand Down
6 changes: 4 additions & 2 deletions misc/lib/linglong/container/config.d/00-id-mapping
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#
# SPDX-License-Identifier: LGPL-3.0-or-later

# LINGLONG_BUILTIN_GENERATOR
echo "This is a placeholder file for using builtin oci configuration generator.
If you want to customize the behavior of current generator,
you could replace this file with a same name executable file." 1>&2

# if you want to use custom generator, please add your implementation here and remove above "LINGLONG_BUILTIN_GENERATOR" comment
exit 255
6 changes: 4 additions & 2 deletions misc/lib/linglong/container/config.d/05-initialize
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#
# SPDX-License-Identifier: LGPL-3.0-or-later

# LINGLONG_BUILTIN_GENERATOR
echo "This is a placeholder file for using builtin oci configuration generator.
If you want to customize the behavior of current generator,
you could replace this file with a same name executable file." 1>&2

# if you want to use custom generator, please add your implementation here and remove above "LINGLONG_BUILTIN_GENERATOR" comment
exit 255
121 changes: 120 additions & 1 deletion misc/lib/linglong/container/config.d/10-basics.json
Original file line number Diff line number Diff line change
@@ -1 +1,120 @@
null
{
"ociVersion": "1.0.1",
"patch": [
{
"op": "add",
"path": "/mounts/-",
"value": {
"destination": "/sys",
"type": "bind",
"source": "/sys",
"options": ["rbind", "nosuid", "noexec", "nodev"]
}
},
{
"op": "add",
"path": "/mounts/-",
"value": {
"destination": "/proc",
"type": "proc",
"source": "proc"
}
},
{
"op": "add",
"path": "/mounts/-",
"value": {
"destination": "/dev",
"type": "tmpfs",
"source": "tmpfs",
"options": ["nosuid", "strictatime", "mode=0755", "size=65536k"]
}
},
{
"op": "add",
"path": "/mounts/-",
"value": {
"destination": "/dev/pts",
"type": "devpts",
"source": "devpts",
"options": [
"nosuid",
"noexec",
"newinstance",
"ptmxmode=0666",
"mode=0620"
]
}
},
{
"op": "add",
"path": "/mounts/-",
"value": {
"destination": "/dev/shm",
"type": "tmpfs",
"source": "shm",
"options": ["nosuid", "noexec", "nodev", "mode=1777"]
}
},
{
"op": "add",
"path": "/mounts/-",
"value": {
"destination": "/dev/mqueue",
"type": "bind",
"source": "/dev/mqueue",
"options": ["rbind", "nosuid", "noexec", "nodev"]
}
},
{
"op": "add",
"path": "/mounts/-",
"value": {
"destination": "/sys/fs/cgroup",
"type": "cgroup",
"source": "cgroup",
"options": ["nosuid", "noexec", "nodev", "relatime", "ro"]
}
},
{
"op": "add",
"path": "/mounts/-",
"value": {
"destination": "/run",
"type": "tmpfs",
"source": "tmpfs",
"options": ["nosuid", "strictatime", "mode=0755", "size=65536k"]
}
},
{
"op": "add",
"path": "/mounts/-",
"value": {
"destination": "/run/udev",
"type": "bind",
"source": "/run/udev",
"options": ["rbind"]
}
},
{
"op": "add",
"path": "/mounts/-",
"value": {
"destination": "/tmp",
"type": "bind",
"source": "/tmp",
"options": ["rbind"]
}
},
{
"op": "add",
"path": "/mounts/-",
"value": {
"destination": "/etc/passwd",
"type": "bind",
"source": "/etc/passwd",
"options": ["ro", "rbind"]
}
}
]
}
6 changes: 4 additions & 2 deletions misc/lib/linglong/container/config.d/20-devices
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#
# SPDX-License-Identifier: LGPL-3.0-or-later

# LINGLONG_BUILTIN_GENERATOR
echo "This is a placeholder file for using builtin oci configuration generator.
If you want to customize the behavior of current generator,
you could replace this file with a same name executable file." 1>&2

# if you want to use custom generator, please add your implementation here and remove above "LINGLONG_BUILTIN_GENERATOR" comment
exit 255
6 changes: 4 additions & 2 deletions misc/lib/linglong/container/config.d/25-host-env
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#
# SPDX-License-Identifier: LGPL-3.0-or-later

# LINGLONG_BUILTIN_GENERATOR
echo "This is a placeholder file for using builtin oci configuration generator.
If you want to customize the behavior of current generator,
you could replace this file with a same name executable file." 1>&2

# if you want to use custom generator, please add your implementation here and remove above "LINGLONG_BUILTIN_GENERATOR" comment
exit 255
26 changes: 25 additions & 1 deletion misc/lib/linglong/container/config.d/25-host-rootfs.json
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
null
{
"ociVersion": "1.0.1",
"patch": [
{
"op": "add",
"path": "/mounts/-",
"value": {
"destination": "/run/host",
"type": "tmpfs",
"source": "tmpfs",
"options": ["nodev", "nosuid", "mode=700"]
}
},
{
"op": "add",
"path": "/mounts/-",
"value": {
"destination": "/run/host/rootfs",
"type": "bind",
"source": "/",
"options": ["rbind"]
}
}
]
}
Loading
Loading