Skip to content

Commit f0256a0

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

5 files changed

Lines changed: 59 additions & 28 deletions

File tree

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,11 @@ int Cli::run(const RunOptions &options)
671671
auto containerID = runContext.getContainerId();
672672
LogD("run app: {} container id: {}", curAppRef->toString(), containerID);
673673

674-
const auto &appLayerItem = runContext.getCachedAppItem();
675-
if (!appLayerItem) {
674+
const auto &targetItem = runContext.getCachedTargetItem();
675+
if (!targetItem) {
676676
return -1;
677677
}
678-
const auto &info = appLayerItem->info;
678+
const auto &info = targetItem->info;
679679

680680
auto commands = options.commands;
681681
if (options.commands.empty()) {
@@ -831,20 +831,20 @@ int Cli::runResolvedContext(runtime::RunContext &runContext,
831831
{
832832
LINGLONG_TRACE("run resolved context");
833833

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

840840
auto commands = options.commands;
841841
if (options.commands.empty()) {
842-
commands = appLayerItem->info.command.value_or(std::vector<std::string>{ "bash" });
842+
commands = targetItem->info.command.value_or(std::vector<std::string>{ "bash" });
843843
}
844844
commands = filePathMapping(commands, options);
845845

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

849849
runtime::RunContainerOptions runOptions;
850850
runOptions.enableSecurityContext(runtime::getDefaultSecurityContexts());
@@ -991,7 +991,9 @@ Cli::getCurrentContainers() const noexcept
991991

992992
myContainers.emplace_back(api::types::v1::CliContainer{
993993
.id = std::move(info->containerID),
994-
.package = std::move(info->app),
994+
.package = !info->app.empty()
995+
? info->app
996+
: (info->runtime && !info->runtime->empty() ? *info->runtime : info->base),
995997
.pid = container->pid,
996998
});
997999
}
@@ -2089,12 +2091,12 @@ utils::error::Result<std::filesystem::path> Cli::ensureCache(runtime::RunContext
20892091
LINGLONG_TRACE("ensure cache via PM");
20902092

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

2097-
auto appCache = common::dir::getContainerCacheDir(appLayerItem->commit, containerID);
2099+
auto appCache = common::dir::getContainerCacheDir(targetItem->commit, containerID);
20982100
auto runContextConfigFile = appCache / ".config";
20992101
std::error_code ec;
21002102
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");
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+
const auto &targetLayer = prepared.runContext->getTargetLayer();
357+
if (!targetLayer) {
358+
return LINGLONG_ERR("target layer not found");
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)