Skip to content

Commit c99f0a4

Browse files
reddevillgdengbo11
authored andcommitted
feat: support run command in runtime or base environment
Signed-off-by: reddevillg <reddevillg@gmail.com>
1 parent b1fa722 commit c99f0a4

5 files changed

Lines changed: 61 additions & 29 deletions

File tree

libs/linglong/src/linglong/cli/cli.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -669,13 +669,14 @@ int Cli::run(const RunOptions &options)
669669
LogD("RunContext Config:\n{}", nlohmann::json(runContextCfg).dump());
670670

671671
auto containerID = runContext.getContainerId();
672-
LogD("run app: {} container id: {}", curAppRef->toString(), containerID);
672+
LogD("run {} with container id: {}", curAppRef->toString(), containerID);
673673

674-
const auto &appLayerItem = runContext.getCachedAppItem();
675-
if (!appLayerItem) {
674+
auto targetItem = runContext.getCachedTargetItem();
675+
if (!targetItem) {
676+
this->printer.printErr(LINGLONG_ERRV("failed to get cached target item", targetItem));
676677
return -1;
677678
}
678-
const auto &info = appLayerItem->info;
679+
const auto &info = targetItem->info;
679680

680681
auto commands = options.commands;
681682
if (options.commands.empty()) {
@@ -831,20 +832,20 @@ int Cli::runResolvedContext(runtime::RunContext &runContext,
831832
{
832833
LINGLONG_TRACE("run resolved context");
833834

834-
auto appLayerItem = runContext.getCachedAppItem();
835-
if (!appLayerItem) {
836-
this->printer.printErr(LINGLONG_ERRV("failed to get cached app item"));
835+
auto targetItem = runContext.getCachedTargetItem();
836+
if (!targetItem) {
837+
this->printer.printErr(LINGLONG_ERRV("failed to get cached target item", targetItem));
837838
return -1;
838839
}
839840

840841
auto commands = options.commands;
841842
if (options.commands.empty()) {
842-
commands = appLayerItem->info.command.value_or(std::vector<std::string>{ "bash" });
843+
commands = targetItem->info.command.value_or(std::vector<std::string>{ "bash" });
843844
}
844845
commands = filePathMapping(commands, options);
845846

846847
auto appCache =
847-
common::dir::getContainerCacheDir(appLayerItem->commit, runContext.getContainerId());
848+
common::dir::getContainerCacheDir(targetItem->commit, runContext.getContainerId());
848849

849850
runtime::RunContainerOptions runOptions;
850851
runOptions.enableSecurityContext(runtime::getDefaultSecurityContexts());
@@ -991,7 +992,9 @@ Cli::getCurrentContainers() const noexcept
991992

992993
myContainers.emplace_back(api::types::v1::CliContainer{
993994
.id = std::move(info->containerID),
994-
.package = std::move(info->app),
995+
.package = !info->app.empty()
996+
? info->app
997+
: (info->runtime && !info->runtime->empty() ? *info->runtime : info->base),
995998
.pid = container->pid,
996999
});
9971000
}
@@ -2089,12 +2092,12 @@ utils::error::Result<std::filesystem::path> Cli::ensureCache(runtime::RunContext
20892092
LINGLONG_TRACE("ensure cache via PM");
20902093

20912094
const auto &containerID = context.getContainerId();
2092-
auto appLayerItem = context.getCachedAppItem();
2093-
if (!appLayerItem) {
2094-
return LINGLONG_ERR("failed to get cached app item");
2095+
auto targetItem = context.getCachedTargetItem();
2096+
if (!targetItem) {
2097+
return LINGLONG_ERR("failed to get cached target item", targetItem);
20952098
}
20962099

2097-
auto appCache = common::dir::getContainerCacheDir(appLayerItem->commit, containerID);
2100+
auto appCache = common::dir::getContainerCacheDir(targetItem->commit, containerID);
20982101
auto runContextConfigFile = appCache / ".config";
20992102
std::error_code ec;
21002103
if (std::filesystem::exists(runContextConfigFile, ec)) {

libs/linglong/src/linglong/package_manager/package_manager.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,12 +1937,12 @@ utils::error::Result<void> PackageManager::initRunContext(const std::string &run
19371937
return LINGLONG_ERR("container id mismatch");
19381938
}
19391939

1940-
auto appLayerItem = ctx.getCachedAppItem();
1941-
if (!appLayerItem) {
1942-
return LINGLONG_ERR("failed to get cached app item");
1940+
auto targetItem = ctx.getCachedTargetItem();
1941+
if (!targetItem) {
1942+
return LINGLONG_ERR("failed to get cached target item", targetItem);
19431943
}
19441944

1945-
auto appCache = common::dir::getContainerCacheDir(appLayerItem->commit, containerID);
1945+
auto appCache = common::dir::getContainerCacheDir(targetItem->commit, containerID);
19461946
auto runContextConfigFile = appCache / ".config";
19471947
std::error_code ec;
19481948
if (std::filesystem::exists(runContextConfigFile, ec)) {

libs/linglong/src/linglong/runtime/container_builder.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,12 @@ auto ContainerBuilder::finalizeContainer(PreparedContainer &prepared) noexcept
353353
bool useOverlayMode = true;
354354
bool needGenLdConf = false;
355355
if (prepared.mode == ContainerMode::Init) {
356-
const auto &appLayer = prepared.runContext->getAppLayer();
357-
if (!appLayer) {
358-
return LINGLONG_ERR("app layer not found");
356+
auto targetLayer = prepared.runContext->getTargetLayer();
357+
if (!targetLayer) {
358+
return LINGLONG_ERR("target layer not found", targetLayer);
359359
}
360360

361-
triplet = appLayer->getReference().arch.getTriplet();
361+
triplet = targetLayer->get().getReference().arch.getTriplet();
362362
needGenLdConf = true;
363363
} else if (prepared.mode == ContainerMode::Build) {
364364
triplet = package::Architecture::currentCPUArchitecture().getTriplet();

libs/linglong/src/linglong/runtime/run_context.cpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,10 @@ utils::error::Result<void> RunContext::resolve(const api::types::v1::RunContextC
298298

299299
return std::move(layer).value();
300300
};
301-
auto findTargetLayer = [this, &_linglong_trace_message](const std::string &targetRefStr)
301+
auto findTargetLayer = [this](const std::string &targetRefStr)
302302
-> utils::error::Result<std::reference_wrapper<RuntimeLayer>> {
303+
LINGLONG_TRACE("find target layer");
304+
303305
auto fuzzyRef = package::FuzzyReference::parse(targetRefStr);
304306
if (!fuzzyRef) {
305307
return LINGLONG_ERR("failed to parse target layer reference", fuzzyRef);
@@ -1021,15 +1023,38 @@ utils::error::Result<std::filesystem::path> RunContext::getRuntimeLayerPath() co
10211023
return runtimeLayer->getLayerDir()->path();
10221024
}
10231025

1024-
utils::error::Result<api::types::v1::RepositoryCacheLayersItem> RunContext::getCachedAppItem()
1026+
utils::error::Result<std::reference_wrapper<RuntimeLayer>> RunContext::getTargetLayer()
10251027
{
1026-
LINGLONG_TRACE("get cached app item");
1028+
LINGLONG_TRACE("get target layer");
10271029

1028-
if (!appLayer) {
1029-
return LINGLONG_ERR("no app layer exist");
1030+
if (appLayer) {
1031+
return std::ref(*appLayer);
1032+
}
1033+
if (runtimeLayer) {
1034+
return std::ref(*runtimeLayer);
1035+
}
1036+
if (baseLayer) {
1037+
return std::ref(*baseLayer);
1038+
}
1039+
1040+
return LINGLONG_ERR("no layer resolved");
1041+
}
1042+
1043+
utils::error::Result<api::types::v1::RepositoryCacheLayersItem> RunContext::getCachedTargetItem()
1044+
{
1045+
LINGLONG_TRACE("get cached target item");
1046+
1047+
if (appLayer) {
1048+
return appLayer->getCachedItem();
1049+
}
1050+
if (runtimeLayer) {
1051+
return runtimeLayer->getCachedItem();
1052+
}
1053+
if (baseLayer) {
1054+
return baseLayer->getCachedItem();
10301055
}
10311056

1032-
return appLayer->getCachedItem();
1057+
return LINGLONG_ERR("no layer resolved");
10331058
}
10341059

10351060
} // namespace linglong::runtime

libs/linglong/src/linglong/runtime/run_context.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "ocppi/runtime/config/types/Generators.hpp"
1717

1818
#include <filesystem>
19+
#include <functional>
1920

2021
namespace linglong::generator {
2122
class ContainerCfgBuilder;
@@ -85,7 +86,10 @@ class RunContext
8586
[[nodiscard]] utils::error::Result<std::filesystem::path> getBaseLayerPath() const;
8687
[[nodiscard]] utils::error::Result<std::filesystem::path> getRuntimeLayerPath() const;
8788

88-
utils::error::Result<api::types::v1::RepositoryCacheLayersItem> getCachedAppItem();
89+
[[nodiscard]] utils::error::Result<std::reference_wrapper<RuntimeLayer>> getTargetLayer();
90+
91+
[[nodiscard]] utils::error::Result<api::types::v1::RepositoryCacheLayersItem>
92+
getCachedTargetItem();
8993

9094
[[nodiscard]] bool hasRuntime() const noexcept { return !!runtimeLayer; }
9195

0 commit comments

Comments
 (0)