Skip to content

Commit

Permalink
feat: support base custom application installation location
Browse files Browse the repository at this point in the history
base可以通过app_prefix信息设置应用安装位置 如果base设置了app_prefix,
玲珑在构建时会将PREFIX设置成app_prefix指定的目录
在运行时也会将应用文件挂载成app_prefix指定的目录
  • Loading branch information
myml committed Oct 9, 2024
1 parent b203d63 commit 601e0bc
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 21 deletions.
4 changes: 4 additions & 0 deletions api/schema/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@
"description": {
"type": "string",
"description": "description of package info"
},
"app_prefix": {
"type": "string",
"description": "application install location. The field is intended solely for the use of the base, indicating the installation path of applications built using that base."
}
}
},
Expand Down
7 changes: 5 additions & 2 deletions api/schema/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,9 @@ $defs:
description:
type: string
description: description of package info
app_prefix:
type: string
description: application install location. The field is intended solely for the use of the base, indicating the installation path of applications built using that base.
PackageInfo:
title: PackageInfo
description: this is the each item output of ll-cli list --json
Expand Down Expand Up @@ -668,7 +671,7 @@ $defs:
type: string
description: version of linglong at the time of generating the file
config:
$ref: '#/$defs/RepoConfig'
$ref: "#/$defs/RepoConfig"
layers:
type: array
items:
Expand All @@ -688,7 +691,7 @@ $defs:
type: string
description: ostree commit hash
info:
$ref: '#/$defs/PackageInfoV2'
$ref: "#/$defs/PackageInfoV2"
type: object
properties:
# NOTE: "properties" is auto generated by referring all types is $defs
6 changes: 2 additions & 4 deletions apps/generators/05-initialize/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ int main()
});

mounts.push_back(
{ { "destination",
std::filesystem::path("/opt/apps") / annotations["org.deepin.linglong.appID"]
/ "files" },
{ "options", nlohmann::json::array({ "rbind", "rw" }) },
{ { "destination", annotations["org.deepin.linglong.appPrefix"] },
{ "options", nlohmann::json::array({ "rbind", "ro" }) },
{ "source",
std::filesystem::path(annotations["org.deepin.linglong.appDir"].get<std::string>())
/ "files" },
Expand Down
9 changes: 6 additions & 3 deletions apps/generators/25-host-env/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <filesystem>
#include <iostream>
#include <string>

extern char **environ;

Expand Down Expand Up @@ -80,9 +81,11 @@ int main()
}

auto annotations = content.at("annotations");
env.push_back("LINGLONG_APPID="
+ annotations.at("org.deepin.linglong.appID").get<std::string>());

auto appID = annotations.at("org.deepin.linglong.appID").get<std::string>();
// appPrefix
auto appPrefix = annotations.at("org.deepin.linglong.appPrefix").get<std::string>();
env.push_back("LINGLONG_APPID=" + appID);
env.push_back("LINGLONG_appPrefix=" + appPrefix);
std::cout << content.dump() << std::endl;
return 0;
}
5 changes: 5 additions & 0 deletions apps/uab/loader/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,11 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
auto appDir = std::filesystem::path{ layerFilesDir }.parent_path();

annotations["org.deepin.linglong.appDir"] = appDir.string();
if (opts.appPrefix) {
annotations["org.deepin.linglong.appPrefix"] = *opts.appPrefix;
} else {
annotations["org.deepin.linglong.appPrefix"] = "/opt/apps/" + opts.appID + "/files";
}
config.annotations = std::move(annotations);

// replace commands
Expand Down
4 changes: 4 additions & 0 deletions libs/api/src/linglong/api/types/v1/Generators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ j["version"] = x.version;
}

inline void from_json(const json & j, PackageInfoV2& x) {
x.appPrefix = get_stack_optional<std::string>(j, "app_prefix");
x.arch = j.at("arch").get<std::vector<std::string>>();
x.base = j.at("base").get<std::string>();
x.channel = j.at("channel").get<std::string>();
Expand All @@ -491,6 +492,9 @@ x.version = j.at("version").get<std::string>();

inline void to_json(json & j, const PackageInfoV2 & x) {
j = json::object();
if (x.appPrefix) {
j["app_prefix"] = x.appPrefix;
}
j["arch"] = x.arch;
j["base"] = x.base;
j["channel"] = x.channel;
Expand Down
5 changes: 5 additions & 0 deletions libs/api/src/linglong/api/types/v1/PackageInfoV2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ using nlohmann::json;
*/
struct PackageInfoV2 {
/**
* application install location. The field is intended solely for the use of the base,
* indicating the installation path of applications built using that base.
*/
std::optional<std::string> appPrefix;
/**
* arch of package info
*/
std::vector<std::string> arch;
Expand Down
18 changes: 15 additions & 3 deletions libs/linglong/src/linglong/builder/linglong_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ utils::error::Result<void> splitDevelop(QString installFilepath,
iter.next();
auto filepath = iter.filePath();
qDebug() << filepath;
// $PROJECT_ROOT/.../files to /opt/apps/${appid}
// $PROJECT_ROOT/ to /runtime/
// $PROJECT_ROOT/.../files to installPrefix
filepath.replace(0, src.length(), prefix);
installRules.append(filepath);
}
Expand Down Expand Up @@ -472,6 +471,7 @@ utils::error::Result<void> Builder::build(const QStringList &args) noexcept
if (!baseLayerDir) {
return LINGLONG_ERR(baseLayerDir);
}
auto baseInfo = baseLayerDir->info();
printReplacedText(QString("%1%2%3%4")
.arg(base->id, -25) // NOLINT
.arg(base->version.toString(), -15) // NOLINT
Expand Down Expand Up @@ -547,6 +547,7 @@ set -e
.runtimeDir = {},
.baseDir = *baseLayerDir,
.appDir = {},
.appPrefix = baseInfo->appPrefix,
.patches = {},
.mounts = {},
.masks = {},
Expand All @@ -557,7 +558,12 @@ set -e
// 构建安装路径
QString installPrefix = "/runtime";
if (this->project.package.kind != "runtime") {
installPrefix = QString::fromStdString("/opt/apps/" + this->project.package.id + "/files");
if (baseInfo->appPrefix) {
installPrefix = QString::fromStdString(*baseInfo->appPrefix);
} else {
installPrefix =
QString::fromStdString("/opt/apps/" + this->project.package.id + "/files");
}
}
opts.mounts.push_back({
.destination = installPrefix.toStdString(),
Expand Down Expand Up @@ -887,6 +893,10 @@ utils::error::Result<void> Builder::exportUAB(const QString &destination, const
if (!baseDir) {
return LINGLONG_ERR(baseDir);
}
auto info = baseDir->info();
if (info->appPrefix) {
packager.setappPrefix(*info->appPrefix);
}
packager.appendLayer(*baseDir);

if (this->project.runtime) {
Expand Down Expand Up @@ -1091,7 +1101,9 @@ utils::error::Result<void> Builder::run(const QStringList &args)
if (!baseDir) {
return LINGLONG_ERR(baseDir);
}
auto baseInfo = baseDir->info();
options.baseDir = QDir(baseDir->absolutePath());
options.appPrefix = baseInfo->appPrefix;

if (this->project.runtime) {
auto ref = pullDependency(QString::fromStdString(*this->project.runtime),
Expand Down
2 changes: 2 additions & 0 deletions libs/linglong/src/linglong/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ int Cli::run(std::map<std::string, docopt::value> &args)
this->printer.printErr(LINGLONG_ERRV(baseLayerDir));
return -1;
}
auto baseInfo = baseLayerDir->info();

auto command = args["COMMAND"].asStringList();
if (command.empty()) {
Expand Down Expand Up @@ -340,6 +341,7 @@ int Cli::run(std::map<std::string, docopt::value> &args)
.runtimeDir = runtimeLayerDir,
.baseDir = *baseLayerDir,
.appDir = *appLayerDir,
.appPrefix = baseInfo->appPrefix,
.patches = {},
.mounts = std::move(applicationMounts),
.masks = {},
Expand Down
16 changes: 14 additions & 2 deletions libs/linglong/src/linglong/package/uab_packager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <QStandardPaths>

#include <fstream>
#include <string>
#include <unordered_set>
#include <utility>

Expand Down Expand Up @@ -161,6 +162,12 @@ utils::error::Result<void> UABPackager::setIcon(const QFileInfo &newIcon) noexce
return LINGLONG_OK;
}

// set app install dir, default is /opt/apps/$appid/files
void UABPackager::setappPrefix(const std::string &appPrefix) noexcept
{
m_appPrefix = appPrefix;
}

utils::error::Result<void> UABPackager::appendLayer(const LayerDir &layer) noexcept
{
LINGLONG_TRACE("append layer to uab")
Expand Down Expand Up @@ -540,8 +547,13 @@ utils::error::Result<void> UABPackager::prepareBundle(const QDir &bundleDir) noe
QTextStream stream{ &ldConf };
stream << "/runtime/lib" << Qt::endl;
stream << "/runtime/lib/" + arch->getTriplet() << Qt::endl;
stream << "/opt/apps/" + appID + "/files/lib" << Qt::endl;
stream << "/opt/apps/" + appID + "/files/lib/" + arch->getTriplet() << Qt::endl;
auto appPrefix = "/opt/apps/" + appID + "/files";
if (m_appPrefix) {
appPrefix = QString::fromStdString(*m_appPrefix);
}
stream << appPrefix + "/lib" << Qt::endl;
stream << appPrefix + "/lib/" + arch->getTriplet() << Qt::endl;

stream.flush();

// copy ll-box
Expand Down
3 changes: 3 additions & 0 deletions libs/linglong/src/linglong/package/uab_packager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <QUuid>

#include <filesystem>
#include <string>
#include <unordered_set>

namespace linglong::package {
Expand Down Expand Up @@ -67,6 +68,7 @@ class UABPackager
UABPackager(UABPackager &&) = delete;

utils::error::Result<void> setIcon(const QFileInfo &icon) noexcept;
void setappPrefix(const std::string &appPrefix) noexcept;
utils::error::Result<void> appendLayer(const LayerDir &layer) noexcept;
utils::error::Result<void> pack(const QString &uabFilename) noexcept;
utils::error::Result<void> exclude(const std::vector<std::string> &files) noexcept;
Expand All @@ -85,6 +87,7 @@ class UABPackager
std::unordered_set<std::string> excludeFiles;
std::unordered_set<std::string> includeFiles;
std::optional<QFileInfo> icon{ std::nullopt };
std::optional<std::string> m_appPrefix;
api::types::v1::UabMetaInfo meta;
QDir buildDir;
};
Expand Down
14 changes: 10 additions & 4 deletions libs/linglong/src/linglong/runtime/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,16 @@ Container::run(const ocppi::runtime::config::types::Process &process) noexcept

ofs << "/runtime/lib" << std::endl;
ofs << "/runtime/lib/" + arch->getTriplet().toStdString() << std::endl;
ofs << "/opt/apps/" + this->appID.toStdString() + "/files/lib" << std::endl;
ofs << "/opt/apps/" + this->appID.toStdString() + "/files/lib/"
+ arch->getTriplet().toStdString()
<< std::endl;

auto appPrefix = "/opt/apps/" + this->appID.toStdString() + "/files";
if (cfg.annotations) {
auto annotations = *cfg.annotations;
if (annotations.find("org.deepin.linglong.appPrefix") != annotations.end()) {
appPrefix = annotations["org.deepin.linglong.appPrefix"];
}
}
ofs << appPrefix + "/lib" << std::endl;
ofs << appPrefix + "/lib/" + arch->getTriplet().toStdString() << std::endl;
}
this->cfg.mounts->push_back(ocppi::runtime::config::types::Mount{
.destination = "/etc/ld.so.conf.d/zz_deepin-linglong-app.conf",
Expand Down
6 changes: 6 additions & 0 deletions libs/linglong/src/linglong/runtime/container_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ auto getOCIConfig(const ContainerOptions &opts) noexcept
if (opts.appDir) {
annotations["org.deepin.linglong.appDir"] = opts.appDir->absolutePath().toStdString();
}
if (opts.appPrefix) {
annotations["org.deepin.linglong.appPrefix"] = *opts.appPrefix;
} else {
annotations["org.deepin.linglong.appPrefix"] =
"/opt/apps/" + opts.appID.toStdString() + "/files";
}
config->annotations = std::move(annotations);

QDir configDotDDir = QFileInfo(containerConfigFilePath).dir().filePath("config.d");
Expand Down
7 changes: 4 additions & 3 deletions libs/linglong/src/linglong/runtime/container_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ struct ContainerOptions
QString appID;
QString containerID;

std::optional<QDir> runtimeDir; // mount to /runtime
QDir baseDir; // mount to /
std::optional<QDir> appDir; // mount to /opt/apps/${info.appid}/files
std::optional<QDir> runtimeDir; // mount to /runtime
QDir baseDir; // mount to /
std::optional<QDir> appDir; // mount to /opt/apps/${info.appid}/files
std::optional<std::string> appPrefix; // if appPrefix exists, mount appDir to appPrefix

std::vector<api::types::v1::OciConfigurationPatch> patches;
std::vector<ocppi::runtime::config::types::Mount> mounts; // extra mounts
Expand Down

0 comments on commit 601e0bc

Please sign in to comment.