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(oci-gen): using outer json file to generate the patch content #933

Merged
merged 1 commit into from
Dec 20, 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
24 changes: 20 additions & 4 deletions libs/oci-cfg-generators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
#
# SPDX-License-Identifier: LGPL-3.0-or-later

macro(configure_patch_variable source)
get_filename_component(BASE_NAME ${source} NAME_WE)
string(TOUPPER ${BASE_NAME} PATCH_VAR)
string(REPLACE "-" "_" PATCH_VAR ${PATCH_VAR})
string(PREPEND PATCH_VAR "LINGLONG_OCI_CFG_GEN_")
file(READ ${source} ${PATCH_VAR})
endmacro()

configure_patch_variable(${PROJECT_SOURCE_DIR}/misc/lib/linglong/container/config.json)
configure_patch_variable(${PROJECT_SOURCE_DIR}/misc/lib/linglong/container/config.d/10-basics.json)
configure_patch_variable(${PROJECT_SOURCE_DIR}/misc/lib/linglong/container/config.d/25-host-rootfs.json)
configure_patch_variable(${PROJECT_SOURCE_DIR}/misc/lib/linglong/container/config.d/25-host-statics.json)

pfl_add_library(
MERGED_HEADER_PLACEMENT
DISABLE_INSTALL
Expand All @@ -14,22 +27,22 @@ pfl_add_library(
src/linglong/oci-cfg-generators/05_initialize.cpp
src/linglong/oci-cfg-generators/05_initialize.h
src/linglong/oci-cfg-generators/10_basics.cpp
src/linglong/oci-cfg-generators/10_basics.h
src/linglong/oci-cfg-generators/10_basics.h.in
src/linglong/oci-cfg-generators/20_devices.cpp
src/linglong/oci-cfg-generators/20_devices.h
src/linglong/oci-cfg-generators/25_host_env.cpp
src/linglong/oci-cfg-generators/25_host_env.h
src/linglong/oci-cfg-generators/25_host_rootfs.cpp
src/linglong/oci-cfg-generators/25_host_rootfs.h
src/linglong/oci-cfg-generators/25_host_rootfs.h.in
src/linglong/oci-cfg-generators/25_host_statics.cpp
src/linglong/oci-cfg-generators/25_host_statics.h
src/linglong/oci-cfg-generators/25_host_statics.h.in
src/linglong/oci-cfg-generators/30_user_home.cpp
src/linglong/oci-cfg-generators/30_user_home.h
src/linglong/oci-cfg-generators/40_host_ipc.cpp
src/linglong/oci-cfg-generators/40_host_ipc.h
src/linglong/oci-cfg-generators/90_legacy.cpp
src/linglong/oci-cfg-generators/90_legacy.h
src/linglong/oci-cfg-generators/builtins.h
src/linglong/oci-cfg-generators/builtins.h.in
src/linglong/oci-cfg-generators/generator.h
COMPILE_FEATURES
PUBLIC
Expand All @@ -39,3 +52,6 @@ pfl_add_library(
linglong::ocppi
linglong::api
)

get_real_target_name(GEN linglong::oci-cfg-generators)
target_include_directories(${GEN} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/src/linglong/oci-cfg-generators)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#pragma once

#include "generator.h"
#include "linglong/oci-cfg-generators/generator.h"

Check warning on line 7 in libs/oci-cfg-generators/src/linglong/oci-cfg-generators/00_id_mapping.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/oci-cfg-generators/generator.h" not found.

namespace linglong::generator {
struct IDMapping : public Generator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#pragma once

#include "generator.h"
#include "linglong/oci-cfg-generators/generator.h"

Check warning on line 7 in libs/oci-cfg-generators/src/linglong/oci-cfg-generators/05_initialize.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/oci-cfg-generators/generator.h" not found.

namespace linglong::generator {

Expand Down
161 changes: 8 additions & 153 deletions libs/oci-cfg-generators/src/linglong/oci-cfg-generators/10_basics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,159 +11,14 @@ namespace linglong::generator {

bool Basics::generate(ocppi::runtime::config::types::Config &config) const noexcept
{
auto rawPatch = R"({
"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"
]
}
}
]
})"_json;
nlohmann::json rawPatch;

try {
rawPatch = nlohmann::json::parse(basicsPatch);
} catch (const std::exception &e) {
std::cerr << "parse basicsPatch failed:" << e.what() << std::endl;
return false;
}

if (config.ociVersion != rawPatch["ociVersion"].get<std::string>()) {
std::cerr << "ociVersion mismatched" << std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#pragma once

#include "generator.h"
#include "linglong/oci-cfg-generators/generator.h"

namespace linglong::generator {

Expand All @@ -13,6 +13,9 @@ struct Basics : public Generator
[[nodiscard]] std::string_view name() const override { return "10-basics"; }

bool generate(ocppi::runtime::config::types::Config &config) const noexcept override;

private:
static constexpr std::string_view basicsPatch{ R"(@LINGLONG_OCI_CFG_GEN_10_BASICS@)" };
};

} // namespace linglong::generator
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#pragma once

#include "generator.h"
#include "linglong/oci-cfg-generators/generator.h"

Check warning on line 7 in libs/oci-cfg-generators/src/linglong/oci-cfg-generators/20_devices.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/oci-cfg-generators/generator.h" not found.

namespace linglong::generator {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#pragma once

#include "generator.h"
#include "linglong/oci-cfg-generators/generator.h"

Check warning on line 7 in libs/oci-cfg-generators/src/linglong/oci-cfg-generators/25_host_env.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/oci-cfg-generators/generator.h" not found.

namespace linglong::generator {
struct HostEnv : public Generator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,14 @@ namespace linglong::generator {

bool HostRootfs::generate(ocppi::runtime::config::types::Config &config) const noexcept
{
auto rawPatch = R"({
"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"]
}
}
]
})"_json;
nlohmann::json rawPatch;

try {
rawPatch = nlohmann::json::parse(hostRootfsPatch);
} catch (const std::exception &e) {
std::cerr << "parse basicsPatch failed:" << e.what() << std::endl;
return false;
}

if (config.ociVersion != rawPatch["ociVersion"].get<std::string>()) {
std::cerr << "ociVersion mismatched" << std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#pragma once

#include "generator.h"
#include "linglong/oci-cfg-generators/generator.h"

namespace linglong::generator {

Expand All @@ -13,6 +13,9 @@ struct HostRootfs : public Generator
[[nodiscard]] std::string_view name() const override { return "25-host-rootfs"; }

bool generate(ocppi::runtime::config::types::Config &config) const noexcept override;

private:
static constexpr std::string_view hostRootfsPatch{ R"(@LINGLONG_OCI_CFG_GEN_25_HOST_ROOTFS@)" };
};

} // namespace linglong::generator
Loading
Loading