Compare commits

...

33 Commits

Author SHA1 Message Date
Comix 9f6fc4f376
refactor: make Signal to be type alias of std::string (#80)
std::string is not designed to be inherited form.
The destructor of std::string isn't virtual.

Signed-off-by: ComixHe <ComixHe1895@outlook.com>
2025-02-11 21:04:09 +08:00
black-desk dcef24b3e7
ci: use alls-green
Signed-off-by: black-desk <me@black-desk.cn>
2024-08-22 17:01:27 +08:00
black-desk fa10aa531f
docs: update README
Signed-off-by: black-desk <me@black-desk.cn>
2024-08-22 16:54:15 +08:00
black-desk 3089f45fef
docs: update README
Signed-off-by: black-desk <me@black-desk.cn>
2024-04-15 13:39:02 +08:00
black-desk 578d53fa4b
chore: release 0.3.2
Signed-off-by: black-desk <me@black-desk.cn>
2024-04-08 17:18:29 +08:00
black-desk 012609d414
chore: upgrade cmake scripts
Signed-off-by: black-desk <me@black-desk.cn>
2024-04-08 17:18:26 +08:00
black-desk 76ba54a107
build: upgrade PFL.cmake to v0.5.0-rc.1
To support build with cmake version 3.11.4

Signed-off-by: black-desk <me@black-desk.cn>
2024-04-07 11:53:23 +08:00
dependabot[bot] 7e3c5eac5a
build(deps): bump .format from `8625021` to `95bc22d` (#75)
Bumps [.format](https://github.com/black-desk/.format) from `8625021` to `95bc22d`.
- [Commits](86250216b6...95bc22d2c8)

---
updated-dependencies:
- dependency-name: ".format"
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-04-01 17:26:08 +08:00
black-desk 1d08ed8fe0
chore: bump version to 0.3.0
Signed-off-by: black-desk <me@black-desk.cn>
2024-03-31 02:38:30 +08:00
black-desk 706d38f353
build: upgrade PFL.cmake to v0.3.0
Signed-off-by: black-desk <me@black-desk.cn>
2024-03-31 02:28:27 +08:00
dependabot[bot] c5270705ee
build(deps): bump jwlawson/actions-setup-cmake from 1.14 to 2.0 (#71) 2024-03-26 06:58:52 +08:00
black-desk 7cf8dc743b
ci: update github action
Signed-off-by: black-desk <me@black-desk.cn>
2024-03-24 16:19:01 +08:00
black-desk 2f7c4661e6
build: down grade some dependencies
Signed-off-by: black-desk <me@black-desk.cn>
2024-03-24 16:18:58 +08:00
black-desk 46e47682e5
build: update cmake preset
Signed-off-by: black-desk <me@black-desk.cn>
2024-03-24 16:18:55 +08:00
black-desk 92025892c8
build: remove external and make spdlog optional
Signed-off-by: black-desk <me@black-desk.cn>
2024-03-24 16:18:52 +08:00
black-desk 9e803bd339
chore: update cmake files
Signed-off-by: black-desk <me@black-desk.cn>
2024-03-24 16:18:49 +08:00
ComixHe dfce95a6bd
chore: detect dependencies before adding it
Signed-off-by: ComixHe <self@comix.he.cn>
2024-03-22 16:14:59 +08:00
black-desk 9d2e9e2878
chore: release 0.2.3
Signed-off-by: black-desk <me@black-desk.cn>
2024-03-21 21:11:55 +08:00
black-desk 06ce6e6ebe
build: detect external libfmt
Signed-off-by: black-desk <me@black-desk.cn>
2024-03-21 20:58:01 +08:00
ComixHe 2f253417f1
chore: improve config file
Signed-off-by: ComixHe <self@comix.he.cn>
2024-03-21 19:28:21 +08:00
ComixHe 48a416c171
refactor: remove boost dependency
Signed-off-by: ComixHe <self@comix.he.cn>
2024-03-21 19:00:32 +08:00
ComixHe 4f19991d31 chore: add dependencies vendor
Signed-off-by: ComixHe <heyuming@deepin.org>
2024-03-21 17:11:20 +08:00
black-desk 2f8e8cf22d
ci: disable ccache
Seems disable ccache make ci faster.

Signed-off-by: black-desk <me@black-desk.cn>
2024-03-21 10:28:46 +08:00
black-desk 8b0ca6d76a
ci: add required permission for checks
3d7fde6859/README.md (required-permissions)

Signed-off-by: black-desk <me@black-desk.cn>
2024-03-21 10:27:14 +08:00
black-desk 0fab576125
feat: use without link to spdlog
Signed-off-by: black-desk <me@black-desk.cn>
2024-03-08 10:19:33 +08:00
black-desk ca0344ab0d
chore: set soversion to 0
Signed-off-by: black-desk <me@black-desk.cn>
2024-02-28 18:07:47 +08:00
black-desk 7b2200a872
chore: upgrade PFL.cmake
Signed-off-by: black-desk <me@black-desk.cn>
2024-02-28 18:07:46 +08:00
black-desk dab73a5258
chore: fix some cc warning
Signed-off-by: black-desk <me@black-desk.cn>
2024-02-28 17:22:01 +08:00
black-desk e9e952d947
feat: introduce ExecutableNotFoundError
Signed-off-by: black-desk <me@black-desk.cn>
2024-02-28 17:22:00 +08:00
black-desk c8e6e1d03f
chore: add missing cpp file
Signed-off-by: black-desk <me@black-desk.cn>
2024-02-28 17:21:56 +08:00
black-desk 6a4544b3eb
chore: update comments of extra types
Signed-off-by: black-desk <me@black-desk.cn>
2024-02-04 15:37:23 +08:00
black-desk 8f33bdfdeb
chore: release 0.2.2
Signed-off-by: black-desk <me@black-desk.cn>
2024-02-01 16:21:18 +08:00
black-desk 35d7c26757
fix: upgrade PFL.cmake
Upgrade PFL.cmake to fix ocppi-config.cmake file not found issue.

Signed-off-by: black-desk <me@black-desk.cn>
2024-02-01 16:09:55 +08:00
56 changed files with 1883 additions and 12474 deletions

@ -1 +1 @@
Subproject commit 86250216b60370fa08310d80fb8d4ef3d9b674f0
Subproject commit 95bc22d2c878c40eb8800781483ec8aafc2d9f86

View File

@ -8,6 +8,7 @@ jobs:
permissions:
checks: write
contents: read
issues: write
pull-requests: write
runs-on: ubuntu-latest
steps:
@ -17,48 +18,60 @@ jobs:
runs-on: "ubuntu-latest"
strategy:
matrix:
type: [Debug, Release, RelWithDebInfo]
container:
- debian:oldstable-slim
- debian:stable-slim
- debian:sid-slim
cxx: [g++, clang++]
container:
image: ${{ matrix.container }}
steps:
- name: Checkout project
uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: hendrikmuhs/ccache-action@v1.2
with:
key: ${{ matrix.cxx }}-${{ matrix.type }}
- name: Cache CPM.cmake Source
uses: actions/cache@v4
with:
path: .cache/cpm/source
key: ${{ runner.os }}-${{ hashFiles('**/CMakeLists.txt', '**/*.cmake') }}
key: ${{ hashFiles('**/CMakeLists.txt', '**/*.cmake') }}
- name: Setup cmake
uses: jwlawson/actions-setup-cmake@v2.0
- name: Install system dependencies
run: |
sudo apt update &&
sudo apt install libsystemd-dev
- name: Build project with cmake
apt update &&
apt install pkg-config make git g++ clang -y
- name: Build project with cmake by preset debug
run: |
mkdir -p .cache/cpm/source &&
export CXX="${{ matrix.cxx }}" &&
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" &&
export CPM_DOWNLOAD_ALL="ON" &&
export CPM_SOURCE_CACHE="$PWD/.cache/cpm/source" &&
export CFLAGS="$CFLAGS -Wall" &&
export CFLAGS="$CFLAGS -Wextra" &&
export CFLAGS="$CFLAGS -Wpedantic" &&
export CFLAGS="$CFLAGS -Wno-extra-semi -Wno-unused-command-line-argument -Wno-gnu-zero-variadic-macro-arguments" &&
export CFLAGS="$CFLAGS -fsanitize=address,undefined" &&
export CXXFLAGS="$CFLAGS" &&
cmake -B build -DCMAKE_BUILD_TYPE=${{ matrix.type }}&&
cmake --build build -j
- name: Run tests
if: ${{ false }} # This project has no tests for now.
cmake --workflow --preset debug
- name: Install extra dependencies
run: |
cmake --build build -t test -j
apt update &&
apt install libexpected-dev libspdlog-dev nlohmann-json3-dev -y
- name: Build project with cmake by preset ci
run: |
mkdir -p .cache/cpm/source &&
export CXX="${{ matrix.cxx }}" &&
export CPM_SOURCE_CACHE="$PWD/.cache/cpm/source" &&
cmake --workflow --preset ci
- name: Build project with cmake by preset ci-with-spdlog
run: |
mkdir -p .cache/cpm/source &&
export CXX="${{ matrix.cxx }}" &&
export CPM_SOURCE_CACHE="$PWD/.cache/cpm/source" &&
cmake --workflow --preset ci
pass:
name: Pass
needs: [checks, build-and-test]
if: always()
needs:
- checks
- build-and-test
runs-on: ubuntu-latest
steps:
- name: Placeholder
run: true
- name: Decide whether the needed jobs succeeded or failed
uses: re-actors/alls-green@release/v1
with:
jobs: ${{ toJSON(needs) }}

3
.gitignore vendored
View File

@ -1 +1,2 @@
build
**/build/
**/build-*/

View File

@ -1,86 +1,79 @@
cmake_minimum_required(VERSION 3.23)
cmake_minimum_required(VERSION 3.11.4)
project(
ocppi
LANGUAGES CXX
VERSION 0.2.1)
VERSION 0.3.2)
option(OCPPI_BUILD_EXAMPLES "Build examples of ocppi or not"
${PROJECT_IS_TOP_LEVEL})
option(OCPPI_WITH_SPDLOG "Build ocppi with spdlog or not." OFF)
option(OCPPI_ENABLE_TESTING "Enable test of ocppi or not"
${PROJECT_IS_TOP_LEVEL})
set(OCPPI_TL_EXPECTED_VERSION 1.0.0) # debian buster-backports
list(APPEND OCPPI_DEPENDENCIES PUBLIC
"tl-expected ${OCPPI_TL_EXPECTED_VERSION} REQUIRED")
list(APPEND OCPPI_LINK_LIBRARIES PUBLIC "tl::expected")
option(OCPPI_BUILD_SHARED_LIBRARY "Build a shared library or not"
${PROJECT_IS_TOP_LEVEL})
set(OCPPI_JSON_VERSION 3.5.0) # debian buster
list(APPEND OCPPI_DEPENDENCIES PUBLIC
"nlohmann_json ${OCPPI_JSON_VERSION} REQUIRED")
list(APPEND OCPPI_LINK_LIBRARIES PUBLIC "nlohmann_json::nlohmann_json")
option(OCPPI_INSTALL "Enable install target or not" ${PROJECT_IS_TOP_LEVEL})
if(OCPPI_WITH_SPDLOG)
set(OCPPI_SPDLOG_VERSION 1.3.1) # debian buster
list(APPEND OCPPI_COMPILE_OPTIONS PUBLIC -DOCPPI_WITH_SPDLOG)
list(APPEND OCPPI_LINK_LIBRARIES PUBLIC "spdlog::spdlog")
endif()
include(./cmake/CPM.cmake)
if(CMAKE_VERSION LESS 3.14)
message("CPM.cmake disabled")
else()
include(./cmake/CPM.cmake)
CPMFindPackage(
NAME tl-expected
VERSION "${OCPPI_TL_EXPECTED_VERSION}"
GITHUB_REPOSITORY TartanLlama/expected
GIT_TAG "v1.1.0"
GIT_SHALLOW ON
OPTIONS "EXPECTED_BUILD_TESTS OFF"
EXCLUDE_FROM_ALL ON)
CPMFindPackage(
NAME nlohmann_json
VERSION "${OCPPI_JSON_VERSION}"
URL "https://github.com/nlohmann/json/archive/refs/tags/v3.5.0.tar.gz"
EXCLUDE_FROM_ALL ON)
if(OCPPI_WITH_SPDLOG)
CPMFindPackage(
NAME spdlog
VERSION ${OCPPI_SPDLOG_VERSION}
GITHUB_REPOSITORY "gabime/spdlog"
GIT_TAG "v1.3.1"
GIT_SHALLOW ON
EXCLUDE_FROM_ALL ON
OPTIONS
"CMAKE_POSITION_INDEPENDENT_CODE ON" # NOTE:
# https://github.com/gabime/spdlog/issues/1190
)
endif()
endif()
CPMFindPackage(
NAME expected
VERSION 1.0.0
GITHUB_REPOSITORY TartanLlama/expected
GIT_TAG v1.1.0
GIT_SHALLOW ON
OPTIONS "EXPECTED_BUILD_TESTS OFF"
FIND_PACKAGE_ARGUMENTS "NAMES tl-expected"
EXCLUDE_FROM_ALL ON)
find_package(
tl-expected ${OCPPI_TL_EXPECTED_VERSION} REQUIRED)
if(NOT TARGET tl::expected)
# NOTE: v1.0.0 version of tl::expected doesn't have a namespaced alias so we
# have to add it here.
# NOTE: v1.0.0 version of tl::expected doesn't have a namespaced alias when
# using with add_subdirectory so we have to add it here.
add_library(tl::expected ALIAS expected)
endif()
CPMFindPackage(
NAME semver
VERSION 0.3.0
GITHUB_REPOSITORY Neargye/Semver
GIT_TAG v0.3.0
GIT_SHALLOW ON
GIT_PROGRESS ON
EXCLUDE_FROM_ALL ON)
find_package(
nlohmann_json ${OCPPI_JSON_VERSION} QUIET)
CPMFindPackage(
NAME nlohmann_json
VERSION 3.11.2
URL "https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz"
EXCLUDE_FROM_ALL ON
OPTIONS "JSON_Install ON")
CPMFindPackage(
NAME Boost COMPONENTS headers
VERSION 1.64.0
URL "https://github.com/boostorg/boost/releases/download/boost-1.84.0/boost-1.84.0.7z"
EXCLUDE_FROM_ALL ON)
if(NOT TARGET Boost::process)
add_library(Boost::process ALIAS Boost::headers)
if(NOT nlohmann_json_FOUND)
# NOTE: v3.5.0 version of nlohmann_json doesn't have a cmake config file in
# debian, so we have to fix this manually.
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake.fix)
find_package(nlohmann_json ${OCPPI_JSON_VERSION} REQUIRED)
endif()
CPMFindPackage(
NAME fmt
VERSION 9.1.0
GITHUB_REPOSITORY "fmtlib/fmt"
GIT_TAG "10.2.1"
GIT_SHALLOW ON
EXCLUDE_FROM_ALL ON
OPTIONS "CMAKE_POSITION_INDEPENDENT_CODE ON")
CPMFindPackage(
NAME spdlog
VERSION 1.10.0
GITHUB_REPOSITORY "gabime/spdlog"
GIT_TAG "v1.13.0"
GIT_SHALLOW ON
EXCLUDE_FROM_ALL ON
OPTIONS
"CMAKE_POSITION_INDEPENDENT_CODE ON" # NOTE:
# https://github.com/gabime/spdlog/issues/1190
"SPDLOG_FMT_EXTERNAL ON")
include(./cmake/GitSemver.cmake)
set(OCPPI_SEMVER ${PROJECT_VERSION})
@ -92,180 +85,163 @@ set(OCPPI_LIBRARY_TYPE "STATIC")
if(OCPPI_BUILD_SHARED_LIBRARY)
set(OCPPI_LIBRARY_TYPE "SHARED")
endif()
set(OCPPI_SOVERSION 1)
set(CMAKE_CXX_FLAGS_DEBUG "-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE")
include(./cmake/PFL.cmake)
pfl_init(ENABLE_TESTING ${OCPPI_ENABLE_TESTING} BUILD_EXAMPLES
${OCPPI_BUILD_EXAMPLES} INSTALL ${OCPPI_INSTALL})
pfl_init(AUTO)
pfl_add_library(
SOVERSION
${OCPPI_SOVERSION}
VERSION
${PROJECT_VERSION}
TYPE
${OCPPI_LIBRARY_TYPE}
INS
include/ocppi/configure.hpp.in
include/ocppi/runtime/configure.hpp.in
SOURCES
# find -regex '\.\/src\/.*\.\(h\|c\)pp' | sort
./src/ocppi/cli/CommandFailedError.cpp
./src/ocppi/cli/CommonCLI.cpp
./src/ocppi/cli/crun/Crun.cpp
./src/ocppi/cli/runc/Runc.cpp
./src/ocppi/cli/youki/Youki.cpp
./src/ocppi/configure.cpp
./src/ocppi/InvalidArgumentError.cpp
./src/ocppi/runtime/config/ConfigLoader.cpp
./src/ocppi/runtime/config/IncompatibleVersionError.cpp
./src/ocppi/runtime/config/InvalidConfigError.cpp
./src/ocppi/runtime/config/KeyMissingError.cpp
./src/ocppi/runtime/configure.cpp
./src/ocppi/runtime/config/WrongTypeError.cpp
./src/ocppi/runtime/ContainerID.cpp
./src/ocppi/runtime/CreateOption.cpp
./src/ocppi/runtime/DeleteOption.cpp
./src/ocppi/runtime/FeaturesOption.cpp
./src/ocppi/runtime/GlobalOption.cpp
./src/ocppi/runtime/KillOption.cpp
./src/ocppi/runtime/RunOption.cpp
./src/ocppi/runtime/Runtime.cpp
./src/ocppi/runtime/semver_range.cpp
./src/ocppi/runtime/semver_range.hpp
./src/ocppi/runtime/Signal.cpp
./src/ocppi/runtime/StartOption.cpp
./src/ocppi/runtime/StateOption.cpp
HEADERS
# find -regex '\.\/include\/.*\.hpp' | sort
./include/ocppi/cli/CLI.hpp
./include/ocppi/cli/CommandFailedError.hpp
./include/ocppi/cli/CommonCLI.hpp
./include/ocppi/cli/crun/Crun.hpp
./include/ocppi/cli/runc/Runc.hpp
./include/ocppi/cli/youki/Youki.hpp
./include/ocppi/InvalidArgumentError.hpp
./include/ocppi/runtime/config/ConfigLoader.hpp
./include/ocppi/runtime/config/IncompatibleVersionError.hpp
./include/ocppi/runtime/config/InvalidConfigError.hpp
./include/ocppi/runtime/config/KeyMissingError.hpp
./include/ocppi/runtime/config/types/Anet.hpp
./include/ocppi/runtime/config/types/BlockIODeviceThrottle.hpp
./include/ocppi/runtime/config/types/BlockIODeviceWeight.hpp
./include/ocppi/runtime/config/types/BlockIO.hpp
./include/ocppi/runtime/config/types/BoottimeClass.hpp
./include/ocppi/runtime/config/types/Capabilities.hpp
./include/ocppi/runtime/config/types/CappedCPU.hpp
./include/ocppi/runtime/config/types/CappedMemory.hpp
./include/ocppi/runtime/config/types/Class.hpp
./include/ocppi/runtime/config/types/Config.hpp
./include/ocppi/runtime/config/types/ConsoleSize.hpp
./include/ocppi/runtime/config/types/DeviceCgroup.hpp
./include/ocppi/runtime/config/types/FluffyCPU.hpp
./include/ocppi/runtime/config/types/FluffyMemory.hpp
./include/ocppi/runtime/config/types/Generators.hpp
./include/ocppi/runtime/config/types/helper.hpp
./include/ocppi/runtime/config/types/Hook.hpp
./include/ocppi/runtime/config/types/Hooks.hpp
./include/ocppi/runtime/config/types/HugepageLimit.hpp
./include/ocppi/runtime/config/types/Hyperv.hpp
./include/ocppi/runtime/config/types/Hypervisor.hpp
./include/ocppi/runtime/config/types/IdMapping.hpp
./include/ocppi/runtime/config/types/IdType.hpp
./include/ocppi/runtime/config/types/Image.hpp
./include/ocppi/runtime/config/types/IntelRdt.hpp
./include/ocppi/runtime/config/types/IoPriority.hpp
./include/ocppi/runtime/config/types/Kernel.hpp
./include/ocppi/runtime/config/types/LinuxDevice.hpp
./include/ocppi/runtime/config/types/Linux.hpp
./include/ocppi/runtime/config/types/LinuxResources.hpp
./include/ocppi/runtime/config/types/Mount.hpp
./include/ocppi/runtime/config/types/NamespaceReference.hpp
./include/ocppi/runtime/config/types/NamespaceType.hpp
./include/ocppi/runtime/config/types/NetworkInterfacePriority.hpp
./include/ocppi/runtime/config/types/PersonalityDomain.hpp
./include/ocppi/runtime/config/types/Personality.hpp
./include/ocppi/runtime/config/types/Pids.hpp
./include/ocppi/runtime/config/types/Process.hpp
./include/ocppi/runtime/config/types/PurpleCPU.hpp
./include/ocppi/runtime/config/types/PurpleMemory.hpp
./include/ocppi/runtime/config/types/Rdma.hpp
./include/ocppi/runtime/config/types/ResourcesNetwork.hpp
./include/ocppi/runtime/config/types/Rlimit.hpp
./include/ocppi/runtime/config/types/RootfsPropagation.hpp
./include/ocppi/runtime/config/types/Root.hpp
./include/ocppi/runtime/config/types/RootImageFormat.hpp
./include/ocppi/runtime/config/types/SchedulerFlag.hpp
./include/ocppi/runtime/config/types/Scheduler.hpp
./include/ocppi/runtime/config/types/SchedulerPolicy.hpp
./include/ocppi/runtime/config/types/SeccompAction.hpp
./include/ocppi/runtime/config/types/SeccompArch.hpp
./include/ocppi/runtime/config/types/SeccompFlag.hpp
./include/ocppi/runtime/config/types/Seccomp.hpp
./include/ocppi/runtime/config/types/SeccompOperators.hpp
./include/ocppi/runtime/config/types/Solaris.hpp
./include/ocppi/runtime/config/types/Storage.hpp
./include/ocppi/runtime/config/types/SyscallArg.hpp
./include/ocppi/runtime/config/types/Syscall.hpp
./include/ocppi/runtime/config/types/TimeOffsets.hpp
./include/ocppi/runtime/config/types/User.hpp
./include/ocppi/runtime/config/types/Vm.hpp
./include/ocppi/runtime/config/types/WindowsDevice.hpp
./include/ocppi/runtime/config/types/Windows.hpp
./include/ocppi/runtime/config/types/WindowsNetwork.hpp
./include/ocppi/runtime/config/types/WindowsResources.hpp
./include/ocppi/runtime/config/types/ZosDevice.hpp
./include/ocppi/runtime/config/types/Zos.hpp
./include/ocppi/runtime/config/WrongTypeError.hpp
./include/ocppi/runtime/ContainerID.hpp
./include/ocppi/runtime/CreateOption.hpp
./include/ocppi/runtime/DeleteOption.hpp
./include/ocppi/runtime/ExecOption.hpp
./include/ocppi/runtime/FeaturesOption.hpp
./include/ocppi/runtime/features/types/Apparmor.hpp
./include/ocppi/runtime/features/types/Cgroup.hpp
./include/ocppi/runtime/features/types/Features.hpp
./include/ocppi/runtime/features/types/Generators.hpp
./include/ocppi/runtime/features/types/helper.hpp
./include/ocppi/runtime/features/types/IntelRdt.hpp
./include/ocppi/runtime/features/types/Linux.hpp
./include/ocppi/runtime/features/types/NamespaceType.hpp
./include/ocppi/runtime/features/types/SeccompAction.hpp
./include/ocppi/runtime/features/types/SeccompArch.hpp
./include/ocppi/runtime/features/types/SeccompFlag.hpp
./include/ocppi/runtime/features/types/Seccomp.hpp
./include/ocppi/runtime/features/types/SeccompOperators.hpp
./include/ocppi/runtime/features/types/Selinux.hpp
./include/ocppi/runtime/GlobalOption.hpp
./include/ocppi/runtime/KillOption.hpp
./include/ocppi/runtime/ListOption.hpp
./include/ocppi/runtime/RunOption.hpp
./include/ocppi/runtime/Runtime.hpp
./include/ocppi/runtime/Signal.hpp
./include/ocppi/runtime/SpecRuntime.hpp
./include/ocppi/runtime/StartOption.hpp
./include/ocppi/runtime/StateOption.hpp
./include/ocppi/runtime/state/types/Generators.hpp
./include/ocppi/runtime/state/types/helper.hpp
./include/ocppi/runtime/state/types/State.hpp
./include/ocppi/runtime/state/types/Status.hpp
./include/ocppi/types/ContainerListItem.hpp
./include/ocppi/types/Generators.hpp
./include/ocppi/types/helper.hpp
OUTPUT_NAME
ocppi
EXAMPLES
parse-config
using-crun
with-logger
LINK_LIBRARIES
${OCPPI_LINK_LIBRARIES}
PUBLIC
nlohmann_json::nlohmann_json
tl::expected
PRIVATE
spdlog::spdlog
semver::semver
Boost::process
stdc++fs
COMPILE_FEATURES
PUBLIC
cxx_std_17)
cxx_std_17
COMPILE_OPTIONS
${OCPPI_COMPILE_OPTIONS}
DEPENDENCIES
${OCPPI_DEPENDENCIES}
SOURCES
# find -regex '\./src/.+\.[ch]\(pp\)?(\.in)?' -type f -printf '%P\n'| sort
include/ocppi/cli/CLI.hpp
include/ocppi/cli/CommandFailedError.hpp
include/ocppi/cli/CommonCLI.hpp
include/ocppi/cli/crun/Crun.hpp
include/ocppi/cli/ExecutableNotFoundError.hpp
include/ocppi/cli/runc/Runc.hpp
include/ocppi/cli/youki/Youki.hpp
include/ocppi/configure.hpp.in
include/ocppi/InvalidArgumentError.hpp
include/ocppi/runtime/config/types/Anet.hpp
include/ocppi/runtime/config/types/BlockIODeviceThrottle.hpp
include/ocppi/runtime/config/types/BlockIODeviceWeight.hpp
include/ocppi/runtime/config/types/BlockIO.hpp
include/ocppi/runtime/config/types/BoottimeClass.hpp
include/ocppi/runtime/config/types/Capabilities.hpp
include/ocppi/runtime/config/types/CappedCPU.hpp
include/ocppi/runtime/config/types/CappedMemory.hpp
include/ocppi/runtime/config/types/Class.hpp
include/ocppi/runtime/config/types/Config.hpp
include/ocppi/runtime/config/types/ConsoleSize.hpp
include/ocppi/runtime/config/types/DeviceCgroup.hpp
include/ocppi/runtime/config/types/FluffyCPU.hpp
include/ocppi/runtime/config/types/FluffyMemory.hpp
include/ocppi/runtime/config/types/Generators.hpp
include/ocppi/runtime/config/types/helper.hpp
include/ocppi/runtime/config/types/Hook.hpp
include/ocppi/runtime/config/types/Hooks.hpp
include/ocppi/runtime/config/types/HugepageLimit.hpp
include/ocppi/runtime/config/types/Hyperv.hpp
include/ocppi/runtime/config/types/Hypervisor.hpp
include/ocppi/runtime/config/types/IdMapping.hpp
include/ocppi/runtime/config/types/IdType.hpp
include/ocppi/runtime/config/types/Image.hpp
include/ocppi/runtime/config/types/IntelRdt.hpp
include/ocppi/runtime/config/types/IoPriority.hpp
include/ocppi/runtime/config/types/Kernel.hpp
include/ocppi/runtime/config/types/LinuxDevice.hpp
include/ocppi/runtime/config/types/Linux.hpp
include/ocppi/runtime/config/types/LinuxResources.hpp
include/ocppi/runtime/config/types/Mount.hpp
include/ocppi/runtime/config/types/NamespaceReference.hpp
include/ocppi/runtime/config/types/NamespaceType.hpp
include/ocppi/runtime/config/types/NetworkInterfacePriority.hpp
include/ocppi/runtime/config/types/PersonalityDomain.hpp
include/ocppi/runtime/config/types/Personality.hpp
include/ocppi/runtime/config/types/Pids.hpp
include/ocppi/runtime/config/types/Process.hpp
include/ocppi/runtime/config/types/PurpleCPU.hpp
include/ocppi/runtime/config/types/PurpleMemory.hpp
include/ocppi/runtime/config/types/Rdma.hpp
include/ocppi/runtime/config/types/ResourcesNetwork.hpp
include/ocppi/runtime/config/types/Rlimit.hpp
include/ocppi/runtime/config/types/RootfsPropagation.hpp
include/ocppi/runtime/config/types/Root.hpp
include/ocppi/runtime/config/types/RootImageFormat.hpp
include/ocppi/runtime/config/types/SchedulerFlag.hpp
include/ocppi/runtime/config/types/Scheduler.hpp
include/ocppi/runtime/config/types/SchedulerPolicy.hpp
include/ocppi/runtime/config/types/SeccompAction.hpp
include/ocppi/runtime/config/types/SeccompArch.hpp
include/ocppi/runtime/config/types/SeccompFlag.hpp
include/ocppi/runtime/config/types/Seccomp.hpp
include/ocppi/runtime/config/types/SeccompOperators.hpp
include/ocppi/runtime/config/types/Solaris.hpp
include/ocppi/runtime/config/types/Storage.hpp
include/ocppi/runtime/config/types/SyscallArg.hpp
include/ocppi/runtime/config/types/Syscall.hpp
include/ocppi/runtime/config/types/TimeOffsets.hpp
include/ocppi/runtime/config/types/User.hpp
include/ocppi/runtime/config/types/Vm.hpp
include/ocppi/runtime/config/types/WindowsDevice.hpp
include/ocppi/runtime/config/types/Windows.hpp
include/ocppi/runtime/config/types/WindowsNetwork.hpp
include/ocppi/runtime/config/types/WindowsResources.hpp
include/ocppi/runtime/config/types/ZosDevice.hpp
include/ocppi/runtime/config/types/Zos.hpp
include/ocppi/runtime/configure.hpp.in
include/ocppi/runtime/ContainerID.hpp
include/ocppi/runtime/CreateOption.hpp
include/ocppi/runtime/DeleteOption.hpp
include/ocppi/runtime/ExecOption.hpp
include/ocppi/runtime/FeaturesOption.hpp
include/ocppi/runtime/features/types/Apparmor.hpp
include/ocppi/runtime/features/types/Cgroup.hpp
include/ocppi/runtime/features/types/Features.hpp
include/ocppi/runtime/features/types/Generators.hpp
include/ocppi/runtime/features/types/helper.hpp
include/ocppi/runtime/features/types/IntelRdt.hpp
include/ocppi/runtime/features/types/Linux.hpp
include/ocppi/runtime/features/types/NamespaceType.hpp
include/ocppi/runtime/features/types/SeccompAction.hpp
include/ocppi/runtime/features/types/SeccompArch.hpp
include/ocppi/runtime/features/types/SeccompFlag.hpp
include/ocppi/runtime/features/types/Seccomp.hpp
include/ocppi/runtime/features/types/SeccompOperators.hpp
include/ocppi/runtime/features/types/Selinux.hpp
include/ocppi/runtime/GlobalOption.hpp
include/ocppi/runtime/KillOption.hpp
include/ocppi/runtime/ListOption.hpp
include/ocppi/runtime/RunOption.hpp
include/ocppi/runtime/Runtime.hpp
include/ocppi/runtime/Signal.hpp
include/ocppi/runtime/SpecRuntime.hpp
include/ocppi/runtime/StartOption.hpp
include/ocppi/runtime/StateOption.hpp
include/ocppi/runtime/state/types/Generators.hpp
include/ocppi/runtime/state/types/helper.hpp
include/ocppi/runtime/state/types/State.hpp
include/ocppi/runtime/state/types/Status.hpp
include/ocppi/types/ContainerListItem.hpp
include/ocppi/types/Generators.hpp
include/ocppi/types/helper.hpp
src/ocppi/cli/CLI.cpp
src/ocppi/cli/CommandFailedError.cpp
src/ocppi/cli/CommonCLI.cpp
src/ocppi/cli/crun/Crun.cpp
src/ocppi/cli/ExecutableNotFoundError.cpp
src/ocppi/cli/Process.cpp
src/ocppi/cli/Process.hpp
src/ocppi/cli/runc/Runc.cpp
src/ocppi/cli/youki/Youki.cpp
src/ocppi/configure.cpp
src/ocppi/InvalidArgumentError.cpp
src/ocppi/runtime/configure.cpp
src/ocppi/runtime/ContainerID.cpp
src/ocppi/runtime/CreateOption.cpp
src/ocppi/runtime/DeleteOption.cpp
src/ocppi/runtime/FeaturesOption.cpp
src/ocppi/runtime/GlobalOption.cpp
src/ocppi/runtime/KillOption.cpp
src/ocppi/runtime/RunOption.cpp
src/ocppi/runtime/Runtime.cpp
src/ocppi/runtime/Signal.cpp
src/ocppi/runtime/StartOption.cpp
src/ocppi/runtime/StateOption.cpp)

112
CMakePresets.json Normal file
View File

@ -0,0 +1,112 @@
{
"version": 6,
"cmakeMinimumRequired": {
"major": 3,
"minor": 25,
"patch": 0
},
"configurePresets": [
{
"name": "debug",
"displayName": "Debug configuration",
"description": "Configure ocppi for development and debugging.",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CPM_DOWNLOAD_ALL": "ON",
"OCPPI_WITH_SPDLOG": true,
"CMAKE_CXX_FLAGS": "-Wall -Wextra -Wpedantic -Og -g -fsanitize=address,undefined",
"CMAKE_EXPORT_COMPILE_COMMANDS": true,
"CMAKE_COLOR_DIAGNOSTICS": true
}
},
{
"name": "ci",
"displayName": "CI configuration",
"description": "Configure ocppi for CI.",
"binaryDir": "${sourceDir}/build-ci",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
},
"environment": {
"CPM_LOCAL_PACKAGES_ONLY": "ON"
}
},
{
"name": "ci-with-spdlog",
"inherits": "ci",
"displayName": "CI configuration with spdlog",
"description": "Configure ocppi with spdlog for CI.",
"binaryDir": "${sourceDir}/build-ci-with-spdlog",
"cacheVariables": {
"OCPPI_WITH_SPDLOG": true
}
}
],
"buildPresets": [
{
"name": "debug",
"displayName": "Debug build",
"description": "Build ocppi for development and debugging.",
"configurePreset": "debug"
},
{
"name": "ci",
"displayName": "CI build",
"description": "Build ocppi for CI.",
"configurePreset": "ci"
},
{
"name": "ci-with-spdlog",
"displayName": "CI build with spdlog",
"description": "Build ocppi for CI with spdlog.",
"configurePreset": "ci-with-spdlog"
}
],
"workflowPresets": [
{
"name": "debug",
"displayName": "Workflow for developers",
"description": "Configure, build then test for developing and debuging ocppi.",
"steps": [
{
"type": "configure",
"name": "debug"
},
{
"type": "build",
"name": "debug"
}
]
},
{
"name": "ci",
"displayName": "Workflow for CI",
"description": "Configure, build then test ocppi for CI.",
"steps": [
{
"type": "configure",
"name": "ci"
},
{
"type": "build",
"name": "ci"
}
]
},
{
"name": "ci-with-spdlog",
"displayName": "Workflow for CI with spdlog",
"description": "Configure, build then test ocppi for CI with spdlog.",
"steps": [
{
"type": "configure",
"name": "ci-with-spdlog"
},
{
"type": "build",
"name": "ci-with-spdlog"
}
]
}
]
}

View File

@ -1,8 +1,9 @@
{
"description": "Container list item, compatible wity crun.",
"description": "Extra types used by ocppi API.",
"$schema": "http://json-schema.org/draft-04/schema#",
"$defs": {
"ContainerListItem": {
"description": "Container list item, compatible wity crun.",
"type": "object",
"properties": {
"id": {
@ -38,7 +39,7 @@
},
"type": "object",
"properties": {
"containerListItem": {
"ContainerListItem": {
"$ref": "#/$defs/ContainerListItem"
}
},

View File

@ -0,0 +1,13 @@
try_compile(
nlohmann_json_FOUND ${CMAKE_CURRENT_BINARY_DIR}/nlohmann_json_compile_test
${CMAKE_CURRENT_LIST_DIR}/nlohmann_json_compile_test.cpp)
if(NOT nlohmann_json_FOUND)
return()
endif()
if(NOT TARGET nlohmann_json::nlohmann_json)
add_library(nlohmann_json INTERFACE)
add_library(nlohmann_json::nlohmann_json ALIAS nlohmann_json)
install(TARGETS nlohmann_json EXPORT "${PROJECT_NAME}")
endif()

View File

@ -0,0 +1,18 @@
#include "nlohmann/json.hpp"
// NOTE: This cpp file is used by Findnlohmann_json.cmake to check
// whether the nlohmann_json library we founded at include directory
// is compatible with 3.5.0 or not.
#if NLOHMANN_JSON_VERSION_MAJOR != 3
#error require nlohmann_json compatible with 3.5.0
#endif
#if NLOHMANN_JSON_VERSION_MINOR < 5
#error require nlohmann_json compatible with 3.5.0
#endif
int main()
{
return 0;
}

View File

@ -42,7 +42,7 @@ if(NOT COMMAND cpm_message)
endfunction()
endif()
set(CURRENT_CPM_VERSION 0.38.6)
set(CURRENT_CPM_VERSION 0.38.8)
get_filename_component(CPM_CURRENT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" REALPATH)
if(CPM_DIRECTORY)
@ -99,6 +99,12 @@ macro(cpm_set_policies)
cmake_policy(SET CMP0135 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0135 NEW)
endif()
# treat relative git repository paths as being relative to the parent project's remote
if(POLICY CMP0150)
cmake_policy(SET CMP0150 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0150 NEW)
endif()
endmacro()
cpm_set_policies()
@ -294,12 +300,6 @@ function(CPMFindPackage)
return()
endif()
cpm_check_if_package_already_added(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}")
if(CPM_PACKAGE_ALREADY_ADDED)
cpm_export_variables(${CPM_ARGS_NAME})
return()
endif()
cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS})
if(NOT CPM_PACKAGE_FOUND)

View File

@ -16,11 +16,24 @@ function(gitsemver_message)
message(STATUS "GitSemver: " ${ARGN})
endfunction()
message(STATUS "GitSemver: --==Version: v0.1.2==--")
message(STATUS "GitSemver: --==Version: v0.1.4==--")
# GitSemver will write the result to varname if it successfully get version
# string from git repository.
function(GitSemver varname)
if(CMAKE_VERSION VERSION_LESS 3.21)
# https://www.scivision.dev/cmake-project-is-top-level/
get_property(
not_top
DIRECTORY
PROPERTY PARENT_DIRECTORY)
if(NOT not_top)
set(PROJECT_IS_TOP_LEVEL true)
else()
set(PROJECT_IS_TOP_LEVEL false)
endif()
endif()
if(NOT PROJECT_IS_TOP_LEVEL)
return()
endif()
@ -45,7 +58,7 @@ function(GitSemver varname)
COMMAND ${SED_EXECUTABLE} -e s/^v//
COMMAND ${SED_EXECUTABLE} -e s/+\$//
OUTPUT_VARIABLE ${varname} RESULTS_VARIABLE rets
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET COMMAND_ECHO STDOUT)
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
foreach(ret ${rets})
if(NOT ret EQUAL 0)

File diff suppressed because it is too large Load Diff

View File

@ -1,16 +1,9 @@
find_package(
PkgConfig REQUIRED)
pkg_check_modules(systemd REQUIRED IMPORTED_TARGET libsystemd)
pfl_add_executable(
INTERNAL
OUTPUT_NAME
parse-config
DISABLE_INSTALL
SOURCES
./src/main.cpp
COMPILE_FEATURES
PUBLIC
cxx_std_17
LINK_LIBRARIES
PUBLIC
ocppi::ocppi
PkgConfig::systemd
spdlog::spdlog)
ocppi::ocppi)

View File

@ -1,63 +1,23 @@
#include <unistd.h>
#include <algorithm>
#include <cstdio>
#include <exception>
#include <map>
#include <memory>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
#include <bits/types/struct_FILE.h>
#include "nlohmann/json.hpp"
#include "nlohmann/json_fwd.hpp"
#include "ocppi/runtime/config/types/Config.hpp"
#include "ocppi/runtime/config/types/Generators.hpp" // IWYU pragma: keep
#include "spdlog/common.h"
#include "spdlog/logger.h"
#include "spdlog/sinks/ansicolor_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/systemd_sink.h"
#include "spdlog/spdlog.h"
namespace spdlog
{
namespace sinks
{
class sink;
} // namespace sinks
} // namespace spdlog
void printException(const std::unique_ptr<spdlog::logger> &logger,
std::string_view msg, std::exception_ptr ptr) noexcept
void printException(std::string_view msg, std::exception_ptr ptr) noexcept
try {
std::rethrow_exception(ptr);
} catch (const std::exception &e) {
SPDLOG_LOGGER_ERROR(logger, "{}: {}", msg, e.what());
std::cerr << msg << ": " << e.what() << std::endl;
} catch (...) {
SPDLOG_LOGGER_ERROR(logger, "{}: unknown exception", msg);
std::cerr << msg << ": unknown exception" << std::endl;
}
int main()
{
std::unique_ptr<spdlog::logger> logger;
{
auto sinks = std::vector<std::shared_ptr<spdlog::sinks::sink>>(
{ std::make_shared<spdlog::sinks::systemd_sink_mt>(
"ocppi") });
if (isatty(stderr->_fileno)) {
sinks.push_back(std::make_shared<
spdlog::sinks::stderr_color_sink_mt>());
}
logger = std::make_unique<spdlog::logger>(
"ocppi", sinks.begin(), sinks.end());
logger->set_level(spdlog::level::trace);
}
try {
auto j = nlohmann::json::parse(R"(
{
@ -454,13 +414,12 @@ int main()
ocppi::runtime::config::types::Config cfg =
j.get<ocppi::runtime::config::types::Config>();
SPDLOG_LOGGER_INFO(logger, "OCI runtime config version: {}",
cfg.ociVersion);
SPDLOG_LOGGER_INFO(logger, "Parsed OCI runtime config: {}",
nlohmann::json(cfg).dump());
std::cerr << "OCI runtime config version: " << cfg.ociVersion
<< std::endl;
std::cerr << "Parsed OCI runtime config: "
<< nlohmann::json(cfg).dump() << std::endl;
} catch (...) {
printException(logger, "Parse OCI runtime config failed",
printException("Parse OCI runtime config",
std::current_exception());
return -1;
}

View File

@ -1,13 +1,14 @@
find_package(
PkgConfig REQUIRED)
pkg_check_modules(systemd REQUIRED IMPORTED_TARGET libsystemd)
list(APPEND LINK_LIBRARIES PUBLIC ocppi::ocppi)
if(OCPPI_WITH_SPDLOG)
list(APPEND LINK_LIBRARIES PRIVATE spdlog::spdlog)
endif()
pfl_add_executable(
INTERNAL
OUTPUT_NAME
using-crun
DISABLE_INSTALL
SOURCES
src/main.cpp
LINK_LIBRARIES
PUBLIC
ocppi::ocppi
PkgConfig::systemd
spdlog::spdlog)
${LINK_LIBRARIES})

View File

@ -1,130 +1,85 @@
#include <unistd.h>
#include <algorithm>
#include <cstdio>
#include <exception>
#include <filesystem>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include <bits/types/struct_FILE.h>
#include "nlohmann/json.hpp"
#include "nlohmann/json_fwd.hpp"
#include "ocppi/cli/CLI.hpp"
#include "ocppi/cli/crun/Crun.hpp"
#include "ocppi/runtime/Signal.hpp"
#include "ocppi/runtime/state/types/Generators.hpp" // IWYU pragma: keep
#include "ocppi/types/ContainerListItem.hpp"
#include "ocppi/types/Generators.hpp" // IWYU pragma: keep
#include "spdlog/common.h"
#include "spdlog/logger.h"
#include "spdlog/sinks/ansicolor_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/systemd_sink.h"
#include "spdlog/spdlog.h"
#include "tl/expected.hpp"
namespace spdlog
{
namespace sinks
{
class sink;
} // namespace sinks
} // namespace spdlog
void printException(const std::unique_ptr<spdlog::logger> &logger,
std::string_view msg, std::exception_ptr ptr) noexcept
void printException(std::string_view msg, std::exception_ptr ptr) noexcept
try {
std::rethrow_exception(ptr);
std::rethrow_exception(std::move(ptr));
} catch (const std::exception &e) {
SPDLOG_LOGGER_ERROR(logger, "{}: {}", msg, e.what());
std::cerr << msg << ": " << e.what() << std::endl;
} catch (...) {
SPDLOG_LOGGER_ERROR(logger, "{}: unknown exception", msg);
std::cerr << msg << ": unknown exception" << std::endl;
}
auto main() -> int
{
std::unique_ptr<spdlog::logger> logger;
{
auto sinks = std::vector<std::shared_ptr<spdlog::sinks::sink>>(
{ std::make_shared<spdlog::sinks::systemd_sink_mt>(
"ocppi") });
if (isatty(stderr->_fileno)) {
sinks.push_back(std::make_shared<
spdlog::sinks::stderr_color_sink_mt>());
}
logger = std::make_unique<spdlog::logger>(
"ocppi", sinks.begin(), sinks.end());
logger->set_level(spdlog::level::trace);
}
try {
std::unique_ptr<ocppi::cli::CLI> cli;
{
auto crun = ocppi::cli::crun::Crun::New("/usr/bin/crun",
logger);
if (!crun.has_value()) {
printException(logger, "New crun object failed",
crun.error());
auto crun =
ocppi::cli::crun::Crun::New("/usr/bin/crun");
if (!crun) {
printException("New crun object", crun.error());
return -1;
}
cli = std::move(crun.value());
}
auto bin = cli->bin();
SPDLOG_LOGGER_DEBUG(logger,
R"(Using OCI runtime CLI program "{}")",
bin.string());
auto list = cli->list();
if (!list.has_value()) {
printException(logger, "Run crun list failed",
list.error());
std::cerr << "Using OCI runtime CLI program " << bin.string()
<< std::endl;
auto containerList = cli->list();
if (!containerList) {
printException("Run crun list", containerList.error());
return -1;
}
if (list->empty()) {
SPDLOG_LOGGER_ERROR(logger, "No container exists.");
if (containerList->empty()) {
std::cerr << "No container exists." << std::endl;
return 0;
}
for (auto item : list.value()) {
nlohmann::json j = item;
SPDLOG_LOGGER_INFO(logger, "Existing container {}",
j.dump());
for (auto container : containerList.value()) {
nlohmann::json containerJSON = container;
std::cerr << "Existing container "
<< containerJSON.dump() << std::endl;
}
auto state = cli->state(list->front().id);
auto state = cli->state(containerList->front().id);
if (!state.has_value()) {
printException(logger, "Run crun state failed",
state.error());
if (!state) {
printException("Run crun state", state.error());
return -1;
}
nlohmann::json j = state.value();
std::cout << j.dump(1, '\t') << std::endl;
nlohmann::json stateJSON = state.value();
std::cout << stateJSON.dump(1, '\t') << std::endl;
auto killResult = cli->kill(list->front().id,
auto killResult = cli->kill(containerList->front().id,
ocppi::runtime::Signal("SIGTERM"));
if (!killResult.has_value()) {
printException(logger, "Run crun kill failed",
killResult.error());
if (!killResult) {
printException("Run crun kill", killResult.error());
return -1;
}
return 0;
} catch (...) {
printException(logger, "Failed to kill first crun container",
printException("Kill first crun container",
std::current_exception());
return -1;
}

View File

@ -0,0 +1,14 @@
if(NOT OCPPI_WITH_SPDLOG)
return()
endif()
pfl_add_executable(
OUTPUT_NAME
with-logger
DISABLE_INSTALL
SOURCES
src/main.cpp
LINK_LIBRARIES
PUBLIC
ocppi::ocppi
spdlog::spdlog)

View File

@ -0,0 +1,116 @@
#include <unistd.h>
#include <algorithm>
#include <cstdio>
#include <exception>
#include <filesystem>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include <bits/types/struct_FILE.h>
#include "nlohmann/json.hpp"
#include "ocppi/cli/CLI.hpp"
#include "ocppi/cli/crun/Crun.hpp"
#include "ocppi/runtime/Signal.hpp"
#include "ocppi/runtime/state/types/Generators.hpp" // IWYU pragma: keep
#include "ocppi/types/ContainerListItem.hpp"
#include "ocppi/types/Generators.hpp" // IWYU pragma: keep
#include "spdlog/common.h"
#include "spdlog/logger.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/syslog_sink.h"
#include "spdlog/spdlog.h"
#include "tl/expected.hpp"
namespace spdlog::sinks
{
class sink;
} // namespace spdlog
void printException(const std::shared_ptr<spdlog::logger> &logger,
std::string_view msg, std::exception_ptr ptr) noexcept
try {
std::rethrow_exception(std::move(ptr));
} catch (const std::exception &e) {
SPDLOG_LOGGER_ERROR(logger, "{}: {}", msg, e.what());
} catch (...) {
SPDLOG_LOGGER_ERROR(logger, "{}: unknown exception", msg);
}
auto main() -> int
{
std::shared_ptr<spdlog::logger> logger =
spdlog::stderr_color_mt("ocppi");
logger->set_level(spdlog::level::trace);
try {
std::unique_ptr<ocppi::cli::CLI> cli;
{
auto crun = ocppi::cli::crun::Crun::New("/usr/bin/crun",
logger);
if (!crun) {
printException(logger, "New crun object failed",
crun.error());
return -1;
}
cli = std::move(crun.value());
}
auto bin = cli->bin();
SPDLOG_LOGGER_DEBUG(logger,
R"(Using OCI runtime CLI program "{}")",
bin.string());
auto containerList = cli->list();
if (!containerList) {
printException(logger, "Run crun list failed",
containerList.error());
return -1;
}
if (containerList->empty()) {
SPDLOG_LOGGER_INFO(logger, "No container exists.");
return 0;
}
for (auto container : containerList.value()) {
nlohmann::json containerJSON = container;
SPDLOG_LOGGER_INFO(logger, "Existing container {}",
containerJSON.dump());
}
auto state = cli->state(containerList->front().id);
if (!state) {
printException(logger, "Run crun state failed",
state.error());
return -1;
}
nlohmann::json stateJSON = state.value();
std::cout << stateJSON.dump(1, '\t') << std::endl;
auto killResult = cli->kill(containerList->front().id,
ocppi::runtime::Signal("SIGTERM"));
if (!killResult) {
printException(logger, "Run crun kill failed",
killResult.error());
return -1;
}
return 0;
} catch (...) {
printException(logger, "Failed to kill first crun container",
std::current_exception());
return -1;
}
return 0;
}

View File

@ -1,9 +1,27 @@
#pragma once
#include <filesystem>
#include <string>
#include <vector>
#include "ocppi/runtime/FeaturesOption.hpp"
#include "ocppi/runtime/Runtime.hpp"
namespace ocppi
{
namespace runtime
{
struct CreateOption;
struct DeleteOption;
struct ExecOption;
struct FeaturesOption;
struct GlobalOption;
struct KillOption;
struct ListOption;
struct RunOption;
struct StartOption;
struct StateOption;
} // namespace runtime
} // namespace ocppi
namespace ocppi::cli
{

View File

@ -2,16 +2,21 @@
#include <exception>
#include <filesystem>
#include <memory>
#include <string>
#include <vector>
#include "ocppi/cli/CLI.hpp"
#include "ocppi/runtime/ContainerID.hpp"
#include "ocppi/runtime/FeaturesOption.hpp"
#include "ocppi/runtime/features/types/Features.hpp"
#include "tl/expected.hpp"
#ifdef OCPPI_WITH_SPDLOG
namespace spdlog
{
class logger;
}
#endif
namespace ocppi
{
namespace types
@ -26,37 +31,32 @@ struct DeleteOption;
struct ExecOption;
struct KillOption;
struct ListOption;
class Signal;
struct StartOption;
struct StateOption;
struct GlobalOption;
struct RunOption;
struct FeaturesOption;
namespace state
{
namespace types
namespace state::types
{
struct State;
} // namespace types
} // namespace state
} // namespace runtime
} // namespace ocppi
namespace spdlog
{
class logger;
} // namespace spdlog
namespace ocppi::cli
{
class CommonCLI : public virtual CLI {
protected:
#ifdef OCPPI_WITH_SPDLOG
CommonCLI(std::filesystem::path,
const std::unique_ptr<spdlog::logger> &);
const std::shared_ptr<spdlog::logger> &);
[[nodiscard]]
auto logger() const -> const std::unique_ptr<spdlog::logger> &;
auto logger() const -> const std::shared_ptr<spdlog::logger> &;
#else
CommonCLI(std::filesystem::path);
#endif
[[nodiscard]]
auto generateGlobalOptions(const runtime::GlobalOption &option)
@ -185,7 +185,9 @@ class CommonCLI : public virtual CLI {
private:
std::filesystem::path bin_;
const std::unique_ptr<spdlog::logger> &logger_;
#ifdef OCPPI_WITH_SPDLOG
std::shared_ptr<spdlog::logger> logger_;
#endif
};
}

View File

@ -0,0 +1,24 @@
#pragma once
#include <errno.h>
#include <filesystem>
#include <string>
#include <system_error>
namespace ocppi::cli
{
class ExecutableNotFoundError : public std::system_error {
using system_error::system_error;
public:
explicit ExecutableNotFoundError(const std::filesystem::path &exe)
: std::system_error(ENOENT, std::generic_category(),
"check executable at [" + exe.string() +
"]")
{
}
};
}

View File

@ -7,11 +7,6 @@
#include "ocppi/cli/CommonCLI.hpp"
#include "tl/expected.hpp"
namespace spdlog
{
class logger;
} // namespace spdlog
namespace ocppi::cli::crun
{
@ -19,8 +14,13 @@ class Crun final : public CommonCLI {
using CommonCLI::CommonCLI;
public:
static auto New(const std::filesystem::path &bin,
const std::unique_ptr<spdlog::logger> &logger) noexcept
static auto New(const std::filesystem::path &bin) noexcept
-> tl::expected<std::unique_ptr<Crun>, std::exception_ptr>;
#ifdef OCPPI_WITH_SPDLOG
static auto New(const std::filesystem::path &bin,
const std::shared_ptr<spdlog::logger> &logger) noexcept
-> tl::expected<std::unique_ptr<Crun>, std::exception_ptr>;
#endif
};
}

View File

@ -7,11 +7,6 @@
#include "ocppi/cli/CommonCLI.hpp"
#include "tl/expected.hpp"
namespace spdlog
{
class logger;
} // namespace spdlog
namespace ocppi::cli::runc
{
@ -19,9 +14,14 @@ class Runc : public CommonCLI {
using CommonCLI::CommonCLI;
public:
static auto New(const std::filesystem::path &bin,
const std::unique_ptr<spdlog::logger> &logger) noexcept
static auto New(const std::filesystem::path &bin) noexcept
-> tl::expected<std::unique_ptr<Runc>, std::exception_ptr>;
#ifdef OCPPI_WITH_SPDLOG
static auto New(const std::filesystem::path &bin,
const std::shared_ptr<spdlog::logger> &logger) noexcept
-> tl::expected<std::unique_ptr<Runc>, std::exception_ptr>;
#endif
};
}

View File

@ -7,11 +7,6 @@
#include "ocppi/cli/CommonCLI.hpp"
#include "tl/expected.hpp"
namespace spdlog
{
class logger;
} // namespace spdlog
namespace ocppi::cli::youki
{
@ -19,9 +14,13 @@ class Youki : public CommonCLI {
using CommonCLI::CommonCLI;
public:
static auto New(const std::filesystem::path &bin,
const std::unique_ptr<spdlog::logger> &logger) noexcept
static auto New(const std::filesystem::path &bin) noexcept
-> tl::expected<std::unique_ptr<Youki>, std::exception_ptr>;
};
#ifdef OCPPI_WITH_SPDLOG
static auto New(const std::filesystem::path &bin,
const std::shared_ptr<spdlog::logger> &logger) noexcept
-> tl::expected<std::unique_ptr<Youki>, std::exception_ptr>;
#endif
};
}

View File

@ -2,11 +2,7 @@
#include <string>
namespace ocppi::runtime
{
class Signal : public std::string {
using std::string::string;
};
namespace ocppi::runtime {
using Signal = std::string;
}

View File

@ -10,6 +10,7 @@
#include "ocppi/runtime/DeleteOption.hpp"
#include "ocppi/runtime/FeaturesOption.hpp"
#include "ocppi/runtime/KillOption.hpp"
#include "ocppi/runtime/Signal.hpp"
#include "ocppi/runtime/StartOption.hpp"
#include "ocppi/runtime/StateOption.hpp"
#include "ocppi/runtime/features/types/Features.hpp"
@ -24,8 +25,6 @@ namespace state::types
struct State;
}
class Signal;
class SpecRuntime {
public:
SpecRuntime() = default;

View File

@ -1,31 +0,0 @@
#pragma once
#include <exception>
#include <iosfwd>
#include "tl/expected.hpp"
namespace ocppi
{
namespace runtime
{
namespace config
{
namespace types
{
struct Config;
} // namespace types
} // namespace config
} // namespace runtime
} // namespace ocppi
namespace ocppi::runtime::config
{
struct ConfigLoader {
[[nodiscard]]
auto load(std::istream &in) const noexcept
-> tl::expected<types::Config, std::exception_ptr>;
};
}

View File

@ -1,25 +0,0 @@
#pragma once
#include <string>
#include "ocppi/InvalidArgumentError.hpp"
namespace ocppi::runtime::config
{
class IncompatibleVersionError : public common::InvalidArgumentError {
using InvalidArgumentError::InvalidArgumentError;
static constexpr const char *prefix = "incompatible version: ";
public:
explicit IncompatibleVersionError(const std::string &arg)
: InvalidArgumentError(prefix + arg)
{
}
explicit IncompatibleVersionError(const char *arg)
: InvalidArgumentError(prefix + std::string(arg))
{
}
};
}

View File

@ -1,24 +0,0 @@
#pragma once
#include <string>
#include "ocppi/InvalidArgumentError.hpp"
namespace ocppi::runtime::config
{
class InvalidConfigError : public common::InvalidArgumentError {
using InvalidArgumentError::InvalidArgumentError;
static constexpr const char *prefix = "invalid config: ";
public:
explicit InvalidConfigError(const std::string &arg)
: InvalidArgumentError(prefix + arg)
{
}
explicit InvalidConfigError(const char *arg)
: InvalidArgumentError(prefix + std::string(arg))
{
}
};
}

View File

@ -1,24 +0,0 @@
#pragma once
#include <string>
#include "ocppi/runtime/config/InvalidConfigError.hpp"
namespace ocppi::runtime::config
{
class KeyMissingError : public InvalidConfigError {
using InvalidConfigError::InvalidConfigError;
static constexpr const char *prefix = "missing key: ";
public:
explicit KeyMissingError(const std::string &arg)
: InvalidConfigError(prefix + arg)
{
}
explicit KeyMissingError(const char *arg)
: InvalidConfigError(prefix + std::string(arg))
{
}
};
}

View File

@ -1,24 +0,0 @@
#pragma once
#include <string>
#include "ocppi/runtime/config/InvalidConfigError.hpp"
namespace ocppi::runtime::config
{
class WrongTypeError : public InvalidConfigError {
using InvalidConfigError::InvalidConfigError;
static constexpr const char *prefix = "wrong type: ";
public:
explicit WrongTypeError(const std::string &arg)
: InvalidConfigError(prefix + arg)
{
}
explicit WrongTypeError(const char *arg)
: InvalidConfigError(prefix + std::string(arg))
{
}
};
}

View File

@ -1,6 +1,6 @@
#pragma once
namespace ocppi
namespace ocppi::runtime
{
constexpr auto oci_runtime_spec_min = "@OCPPI_OCI_RUNTIME_SPEC_MIN@";

View File

@ -19,8 +19,15 @@
namespace ocppi {
namespace types {
/**
* Container list item, compatible wity crun.
*/
using nlohmann::json;
/**
* Container list item, compatible wity crun.
*/
struct ContainerListItem {
std::string bundle;
std::string created;

View File

@ -4,9 +4,18 @@ list(APPEND CMAKE_MODULE_PATH "@PACKAGE_cmakeModulesDir@")
include(CMakeFindDependencyMacro)
find_dependency(Boost 1.74.0 COMPONENTS headers)
find_dependency(spdlog 1.10.0)
find_dependency(tl-expected 1.0.0)
find_dependency(nlohmann_json 3.11.2)
set(OCPPI_USE_VENDOR_EXPECTED @OCPPI_USE_VENDOR_EXPECTED@)
if(NOT OCPPI_USE_VENDOR_EXPECTED)
find_dependency(tl-expected @TL_EXPECTED_MINIMAL_VERSION@)
endif()
set(OCPPI_USE_VENDOR_JSON @OCPPI_USE_VENDOR_JSON@)
if(NOT OCPPI_USE_VENDOR_JSON)
find_dependency(nlohmann_json @JSON_MINIMAL_VERSION@)
endif()
if(TARGET ocppi::ocppi)
return()
endif()
include(${CMAKE_CURRENT_LIST_DIR}/ocppi.cmake)

1
src/ocppi/cli/CLI.cpp Normal file
View File

@ -0,0 +1 @@
#include "ocppi/cli/CLI.hpp"

View File

@ -1,27 +1,22 @@
#include "ocppi/cli/CommonCLI.hpp"
#include <algorithm>
#include <cerrno>
#include <istream>
#include <iterator>
#include <map>
#include <optional>
#include <string>
#include <system_error>
#include <utility>
#include <vector>
#include "boost/process/args.hpp"
#include "boost/process/io.hpp"
#include "boost/process/pipe.hpp"
#include "boost/process/system.hpp"
#include "nlohmann/json.hpp"
#include "nlohmann/json_fwd.hpp"
#include "ocppi/cli/CommandFailedError.hpp"
#include "ocppi/cli/ExecutableNotFoundError.hpp"
#include "ocppi/cli/Process.hpp"
#include "ocppi/runtime/ContainerID.hpp"
#include "ocppi/runtime/CreateOption.hpp"
#include "ocppi/runtime/DeleteOption.hpp"
#include "ocppi/runtime/ExecOption.hpp"
#include "ocppi/runtime/FeaturesOption.hpp"
#include "ocppi/runtime/GlobalOption.hpp"
#include "ocppi/runtime/KillOption.hpp"
#include "ocppi/runtime/ListOption.hpp"
@ -34,13 +29,30 @@
#include "ocppi/runtime/state/types/State.hpp"
#include "ocppi/types/ContainerListItem.hpp"
#include "ocppi/types/Generators.hpp" // IWYU pragma: keep
#include "spdlog/fmt/ranges.h" // IWYU pragma: keep
#ifdef OCPPI_WITH_SPDLOG
#include <memory>
#include "spdlog/sinks/null_sink.h"
#include "spdlog/spdlog.h"
#if !defined(SPDLOG_USE_STD_FORMAT)
#if !defined(SPDLOG_FMT_EXTERNAL)
#ifdef SPDLOG_HEADER_ONLY
#ifndef FMT_HEADER_ONLY
#define FMT_HEADER_ONLY
#endif
#endif
#include "spdlog/fmt/bundled/ranges.h"
#else
#include "fmt/ranges.h"
#endif
#endif
namespace spdlog
{
class logger;
} // namespace spdlog
#endif
namespace ocppi::cli
{
@ -50,7 +62,9 @@ namespace
template <typename Result>
auto doCommand(const std::string &bin,
[[maybe_unused]] const std::unique_ptr<spdlog::logger> &logger,
#ifdef OCPPI_WITH_SPDLOG
[[maybe_unused]] const std::shared_ptr<spdlog::logger> &logger,
#endif
std::vector<std::string> &&globalOption,
const std::string &command, std::vector<std::string> &&options,
std::vector<std::string> &&arguments) -> Result
@ -62,53 +76,67 @@ auto doCommand(const std::string &bin,
args.insert(args.end(), std::make_move_iterator(arguments.begin()),
std::make_move_iterator(arguments.end()));
#ifdef OCPPI_WITH_SPDLOG
SPDLOG_LOGGER_DEBUG(logger, R"(Executing "{}" with arguments: {})", bin,
args);
#endif
if constexpr (std::is_void_v<Result>) {
auto ret = boost::process::system(
bin, boost::process::args(std::move(args)));
auto ret = runProcess(bin, args);
if (ret != 0) {
throw CommandFailedError(ret, bin);
}
return;
} else {
boost::process::ipstream out_ips;
auto ret = boost::process::system(
bin, boost::process::args(std::move(args)),
boost::process::std_out > out_ips);
std::string output;
auto ret = runProcess(bin, args, output);
if (ret != 0) {
throw CommandFailedError(ret, bin);
}
auto json_result = nlohmann::json::parse(out_ips);
auto json_result = nlohmann::json::parse(output);
return json_result.get<Result>();
}
}
}
#ifdef OCPPI_WITH_SPDLOG
CommonCLI::CommonCLI(std::filesystem::path bin,
const std::unique_ptr<spdlog::logger> &logger)
const std::shared_ptr<spdlog::logger> &logger)
: bin_(std::move(bin))
, logger_(std::move(logger))
, logger_(logger != nullptr ?
logger :
spdlog::create<spdlog::sinks::null_sink_st>(""))
{
if (std::filesystem::exists(bin_)) {
return;
}
throw std::system_error(ENOENT, std::generic_category());
throw ExecutableNotFoundError(bin_);
}
auto CommonCLI::logger() const -> const std::shared_ptr<spdlog::logger> &
{
assert(this->logger_ != nullptr);
return this->logger_;
}
#else
CommonCLI::CommonCLI(std::filesystem::path bin)
: bin_(std::move(bin))
{
if (std::filesystem::exists(bin_)) {
return;
}
throw ExecutableNotFoundError(bin_);
}
#endif
auto CommonCLI::bin() const noexcept -> const std::filesystem::path &
{
return this->bin_;
}
auto CommonCLI::logger() const -> const std::unique_ptr<spdlog::logger> &
{
return this->logger_;
}
auto CommonCLI::state(const runtime::ContainerID &id) const noexcept
-> tl::expected<runtime::state::types::State, std::exception_ptr>
{
@ -120,7 +148,10 @@ auto CommonCLI::state(const runtime::ContainerID &id,
-> tl::expected<runtime::state::types::State, std::exception_ptr>
try {
return doCommand<runtime::state::types::State>(
this->bin(), this->logger(),
this->bin(),
#ifdef OCPPI_WITH_SPDLOG
this->logger(),
#endif
this->generateGlobalOptions(option), "state",
this->generateSubcommandOptions(option), { id });
} catch (...) {
@ -143,7 +174,10 @@ try {
opt.extra.emplace_back("-b");
opt.extra.emplace_back(pathToBundle);
doCommand<void>(this->bin(), this->logger(),
doCommand<void>(this->bin(),
#ifdef OCPPI_WITH_SPDLOG
this->logger(),
#endif
this->generateGlobalOptions(opt), "create",
this->generateSubcommandOptions(opt), { id });
return {};
@ -161,7 +195,10 @@ auto CommonCLI::start(const runtime::ContainerID &id,
const runtime::StartOption &option) noexcept
-> tl::expected<void, std::exception_ptr>
try {
doCommand<void>(this->bin(), this->logger(),
doCommand<void>(this->bin(),
#ifdef OCPPI_WITH_SPDLOG
this->logger(),
#endif
this->generateGlobalOptions(option), "start",
this->generateSubcommandOptions(option), { id });
return {};
@ -181,7 +218,10 @@ auto CommonCLI::kill(const runtime::ContainerID &id,
const runtime::KillOption &option) noexcept
-> tl::expected<void, std::exception_ptr>
try {
doCommand<void>(this->bin(), this->logger(),
doCommand<void>(this->bin(),
#ifdef OCPPI_WITH_SPDLOG
this->logger(),
#endif
this->generateGlobalOptions(option), "kill",
this->generateSubcommandOptions(option),
{ id, signal });
@ -201,7 +241,10 @@ auto CommonCLI::delete_(const runtime::ContainerID &id,
const runtime::DeleteOption &option) noexcept
-> tl::expected<void, std::exception_ptr>
try {
doCommand<void>(this->bin(), this->logger(),
doCommand<void>(this->bin(),
#ifdef OCPPI_WITH_SPDLOG
this->logger(),
#endif
this->generateGlobalOptions(option), "delete",
this->generateSubcommandOptions(option), { id });
return {};
@ -228,7 +271,10 @@ try {
arguments.push_back(executable);
arguments.insert(arguments.end(), command.begin(), command.end());
doCommand<void>(this->bin(), this->logger(),
doCommand<void>(this->bin(),
#ifdef OCPPI_WITH_SPDLOG
this->logger(),
#endif
this->generateGlobalOptions(option), "exec",
this->generateSubcommandOptions(option),
std::move(arguments));
@ -252,7 +298,10 @@ try {
new_option.format = runtime::ListOption::OutputFormat::Json;
return doCommand<std::vector<types::ContainerListItem>>(
this->bin(), this->logger(),
this->bin(),
#ifdef OCPPI_WITH_SPDLOG
this->logger(),
#endif
this->generateGlobalOptions(option), "list",
this->generateSubcommandOptions(option), {});
} catch (...) {
@ -275,7 +324,10 @@ try {
opt.extra.emplace_back("-b");
opt.extra.emplace_back(pathToBundle);
doCommand<void>(this->bin(), this->logger(),
doCommand<void>(this->bin(),
#ifdef OCPPI_WITH_SPDLOG
this->logger(),
#endif
this->generateGlobalOptions(opt), "run",
this->generateSubcommandOptions(opt), { id });
return {};
@ -293,7 +345,10 @@ auto CommonCLI::features(const runtime::FeaturesOption &option) const noexcept
-> tl::expected<runtime::features::types::Features, std::exception_ptr>
try {
return doCommand<runtime::features::types::Features>(
this->bin(), this->logger(),
this->bin(),
#ifdef OCPPI_WITH_SPDLOG
this->logger(),
#endif
this->generateGlobalOptions(option), "features",
this->generateSubcommandOptions(option), {});
} catch (...) {

View File

@ -0,0 +1 @@
#include "ocppi/cli/ExecutableNotFoundError.hpp"

106
src/ocppi/cli/Process.cpp Normal file
View File

@ -0,0 +1,106 @@
#include "ocppi/cli/Process.hpp"
#include <errno.h>
#include <sys/wait.h>
#include <unistd.h>
#include <array>
#include <cstddef>
#include <memory>
#include <string>
#include <system_error>
#include <vector>
int runProcess(const std::string &binaryPath,
const std::vector<std::string> &args, std::string &output)
{
constexpr auto bufferSize = 1024;
auto cArgs = std::make_unique<const char *[]>(args.size() + 2);
cArgs[0] = binaryPath.data();
for (std::size_t i = 1; i <= args.size(); ++i) {
cArgs[i] = args[i - 1].data();
}
cArgs[args.size() + 1] = nullptr;
int pipes[2];
if (::pipe(pipes) == -1) {
throw std::system_error(errno, std::generic_category(), "pipe");
}
pid_t childId{ -1 };
childId = fork();
if (childId == -1) {
throw std::system_error(errno, std::generic_category(), "fork");
}
int ret{ 0 };
if (childId == 0) {
::close(pipes[0]);
::dup2(pipes[1], 1);
ret = execvp(binaryPath.data(), (char **)(cArgs.get()));
throw std::system_error(errno, std::generic_category(),
"execvp");
}
::close(pipes[1]);
std::array<char, bufferSize> buffer{};
ssize_t readCount{ 0 };
while (true) {
readCount = read(pipes[0], buffer.data(), bufferSize);
if (readCount == -1) {
if (errno == EINTR) {
continue;
}
throw std::system_error(errno, std::generic_category(),
"read");
}
if (readCount == 0) {
break;
}
output.append(buffer.data(), readCount);
}
if (::wait(&ret) == -1) {
throw std::system_error(errno, std::generic_category(), "wait");
}
::close(pipes[0]);
return ret;
}
int runProcess(const std::string &binaryPath,
const std::vector<std::string> &args)
{
auto cArgs = std::make_unique<const char *[]>(args.size() + 2);
cArgs[0] = binaryPath.data();
for (std::size_t i = 1; i <= args.size(); ++i) {
cArgs[i] = args[i - 1].data();
}
cArgs[args.size() + 1] = nullptr;
pid_t childId{ -1 };
childId = fork();
if (childId == -1) {
throw std::system_error(errno, std::generic_category(), "fork");
}
int ret{ 0 };
if (childId == 0) {
ret = execvp(binaryPath.data(), (char **)(cArgs.get()));
throw std::system_error(errno, std::generic_category(),
"execvp");
}
if (::wait(&ret) == -1) {
throw std::system_error(errno, std::generic_category(), "wait");
}
return ret;
}

10
src/ocppi/cli/Process.hpp Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include <string>
#include <vector>
int runProcess(const std::string &binaryPath,
const std::vector<std::string> &args, std::string &output);
int runProcess(const std::string &binaryPath,
const std::vector<std::string> &args);

View File

@ -1,17 +1,23 @@
#include "ocppi/cli/crun/Crun.hpp"
#include "spdlog/spdlog.h" // IWYU pragma: keep
namespace spdlog
{
class logger;
} // namespace spdlog
#ifdef OCPPI_WITH_SPDLOG
#include "spdlog/spdlog.h"
#endif
namespace ocppi::cli::crun
{
#ifdef OCPPI_WITH_SPDLOG
auto Crun::New(const std::filesystem::path &bin) noexcept
-> tl::expected<std::unique_ptr<Crun>, std::exception_ptr>
try {
return std::unique_ptr<Crun>(new Crun(bin, spdlog::default_logger()));
} catch (...) {
return tl::unexpected(std::current_exception());
}
auto Crun::New(const std::filesystem::path &bin,
const std::unique_ptr<spdlog::logger> &logger) noexcept
const std::shared_ptr<spdlog::logger> &logger) noexcept
-> tl::expected<std::unique_ptr<Crun>, std::exception_ptr>
try {
return std::unique_ptr<Crun>(new Crun(bin, logger));
@ -19,4 +25,15 @@ try {
return tl::unexpected(std::current_exception());
}
#else
auto Crun::New(const std::filesystem::path &bin) noexcept
-> tl::expected<std::unique_ptr<Crun>, std::exception_ptr>
try {
return std::unique_ptr<Crun>(new Crun(bin));
} catch (...) {
return tl::unexpected(std::current_exception());
}
#endif
}

View File

@ -1 +1,3 @@
#include "ocppi/cli/runc/Runc.hpp" // IWYU pragma: keep
// TODO: not implemented yet

View File

@ -1 +1,3 @@
#include "ocppi/cli/youki/Youki.hpp" // IWYU pragma: keep
// TODO: not implemented yet

View File

@ -1,61 +0,0 @@
#include "ocppi/runtime/config/ConfigLoader.hpp"
#include <map>
#include <string>
#include <nlohmann/detail/iterators/iter_impl.hpp>
#include "nlohmann/json.hpp"
#include "nlohmann/json_fwd.hpp"
#include "ocppi/runtime/config/IncompatibleVersionError.hpp"
#include "ocppi/runtime/config/KeyMissingError.hpp"
#include "ocppi/runtime/config/WrongTypeError.hpp"
#include "ocppi/runtime/config/types/Config.hpp"
#include "ocppi/runtime/config/types/Generators.hpp" // IWYU pragma: keep
#include "ocppi/runtime/semver_range.hpp"
#include "semver.hpp"
namespace ocppi::runtime::config
{
namespace
{
[[nodiscard]]
auto doLoad(std::istream &input, const ConfigLoader &) -> types::Config
{
auto j = nlohmann::json::parse(input);
constexpr auto ociVersionKey = "ociVersion";
auto itVersion = j.find(ociVersionKey);
if (itVersion == j.end()) {
throw KeyMissingError(ociVersionKey);
}
if (!itVersion->is_string()) {
throw WrongTypeError(ociVersionKey);
}
auto version = itVersion->get<std::string>();
const auto &[min, max] = support_spec_semver_range();
auto semver = semver::from_string(version);
if (semver < min || max < semver) {
throw IncompatibleVersionError(version);
}
return j.get<types::Config>();
}
}
[[nodiscard]]
auto ConfigLoader::load(std::istream &input) const noexcept
-> tl::expected<types::Config, std::exception_ptr>
try {
return doLoad(input, *this);
} catch (...) {
return tl::unexpected(std::current_exception());
}
}

View File

@ -1 +0,0 @@
#include "ocppi/runtime/config/IncompatibleVersionError.hpp" // IWYU: keep

View File

@ -1 +0,0 @@
#include "ocppi/runtime/config/InvalidConfigError.hpp" // IWYU: keep

View File

@ -1 +0,0 @@
#include "ocppi/runtime/config/KeyMissingError.hpp" // IWYU: keep

View File

@ -1 +0,0 @@
#include "ocppi/runtime/config/WrongTypeError.hpp" // IWYU: keep

View File

@ -1,18 +0,0 @@
#include "ocppi/runtime/semver_range.hpp"
#include "ocppi/runtime/configure.hpp" // for oci_runtime_spec_max, oci_runtime_spe...
#include "semver.hpp" // for from_string, version (ptr only)
namespace ocppi::runtime
{
auto support_spec_semver_range()
-> const std::pair<semver::version, semver::version> &
{
static const auto &min_ = semver::from_string(oci_runtime_spec_min);
static const auto &max_ = semver::from_string(oci_runtime_spec_max);
static const auto &range_ = std::make_pair(min_, max_);
return range_;
}
}

View File

@ -1,16 +0,0 @@
#pragma once
#include <utility> // for pair
namespace semver
{
struct version;
} // namespace semver
namespace ocppi::runtime
{
auto support_spec_semver_range()
-> const std::pair<semver::version, semver::version> &;
}

View File

@ -1,13 +1,14 @@
# codegen
[codegen.sh](./codegen.sh) use [quicktype] to
[codegen.sh](../codegen.sh) use [quicktype] to
generate json (de)serializing code in libs/runtime/include
from [runtime-spec].
from [runtime-spec] and some extra types we need.
[quicktype]: https://github.com/glideapps/quicktype
[runtime-spec]: https://github.com/opencontainers/runtime-spec
The generated code needs to apply a little [patch](./fix.patch) to compile.
The generated code needs to apply a little [patch](./fix.patch),
as [quicktype] is not working very well.
You can recreate that patch follow these instructions:

View File

@ -1,6 +1,6 @@
diff -ruN include.orig/ocppi/runtime/config/types/Config.hpp include/ocppi/runtime/config/types/Config.hpp
--- include.orig/ocppi/runtime/config/types/Config.hpp 2024-01-23 10:46:55.816634773 +0800
+++ include/ocppi/runtime/config/types/Config.hpp 2024-01-23 10:46:58.973939424 +0800
--- include.orig/ocppi/runtime/config/types/Config.hpp 2024-02-04 15:32:58.725688802 +0800
+++ include/ocppi/runtime/config/types/Config.hpp 2024-02-04 15:33:05.217400316 +0800
@@ -41,11 +41,11 @@
* Open Container Initiative Runtime Specification Container Configuration Schema
*/
@ -16,8 +16,8 @@ diff -ruN include.orig/ocppi/runtime/config/types/Config.hpp include/ocppi/runti
std::string ociVersion;
std::optional<Process> process;
diff -ruN include.orig/ocppi/runtime/config/types/Generators.hpp include/ocppi/runtime/config/types/Generators.hpp
--- include.orig/ocppi/runtime/config/types/Generators.hpp 2024-01-23 10:46:55.816634773 +0800
+++ include/ocppi/runtime/config/types/Generators.hpp 2024-01-23 10:46:58.977941062 +0800
--- include.orig/ocppi/runtime/config/types/Generators.hpp 2024-02-04 15:32:58.725688802 +0800
+++ include/ocppi/runtime/config/types/Generators.hpp 2024-02-04 15:33:05.605383182 +0800
@@ -683,7 +683,7 @@
x.network = get_stack_optional<ResourcesNetwork>(j, "network");
x.pids = get_stack_optional<Pids>(j, "pids");
@ -62,8 +62,8 @@ diff -ruN include.orig/ocppi/runtime/config/types/Generators.hpp include/ocppi/r
if (x.mounts) {
j["mounts"] = x.mounts;
diff -ruN include.orig/ocppi/runtime/config/types/Linux.hpp include/ocppi/runtime/config/types/Linux.hpp
--- include.orig/ocppi/runtime/config/types/Linux.hpp 2024-01-23 10:46:55.816634773 +0800
+++ include/ocppi/runtime/config/types/Linux.hpp 2024-01-23 10:46:58.977941062 +0800
--- include.orig/ocppi/runtime/config/types/Linux.hpp 2024-02-04 15:32:58.725688802 +0800
+++ include/ocppi/runtime/config/types/Linux.hpp 2024-02-04 15:33:06.129360052 +0800
@@ -62,7 +62,7 @@
std::optional<LinuxResources> resources;
std::optional<RootfsPropagation> rootfsPropagation;
@ -74,8 +74,8 @@ diff -ruN include.orig/ocppi/runtime/config/types/Linux.hpp include/ocppi/runtim
std::optional<std::vector<IdMapping>> uidMappings;
};
diff -ruN include.orig/ocppi/runtime/config/types/LinuxResources.hpp include/ocppi/runtime/config/types/LinuxResources.hpp
--- include.orig/ocppi/runtime/config/types/LinuxResources.hpp 2024-01-23 10:46:55.816634773 +0800
+++ include/ocppi/runtime/config/types/LinuxResources.hpp 2024-01-23 10:46:58.981942700 +0800
--- include.orig/ocppi/runtime/config/types/LinuxResources.hpp 2024-02-04 15:32:58.725688802 +0800
+++ include/ocppi/runtime/config/types/LinuxResources.hpp 2024-02-04 15:33:06.473344887 +0800
@@ -41,7 +41,7 @@
std::optional<ResourcesNetwork> network;
std::optional<Pids> pids;
@ -86,8 +86,8 @@ diff -ruN include.orig/ocppi/runtime/config/types/LinuxResources.hpp include/ocp
}
}
diff -ruN include.orig/ocppi/runtime/features/types/Features.hpp include/ocppi/runtime/features/types/Features.hpp
--- include.orig/ocppi/runtime/features/types/Features.hpp 2024-01-23 10:46:55.816634773 +0800
+++ include/ocppi/runtime/features/types/Features.hpp 2024-01-23 10:46:58.981942700 +0800
--- include.orig/ocppi/runtime/features/types/Features.hpp 2024-02-04 15:32:58.725688802 +0800
+++ include/ocppi/runtime/features/types/Features.hpp 2024-02-04 15:33:07.629293977 +0800
@@ -35,7 +35,7 @@
struct Features {
std::optional<std::map<std::string, nlohmann::json>> annotations;
@ -98,8 +98,8 @@ diff -ruN include.orig/ocppi/runtime/features/types/Features.hpp include/ocppi/r
std::string ociVersionMax;
std::string ociVersionMin;
diff -ruN include.orig/ocppi/runtime/features/types/Generators.hpp include/ocppi/runtime/features/types/Generators.hpp
--- include.orig/ocppi/runtime/features/types/Generators.hpp 2024-01-23 10:46:55.816634773 +0800
+++ include/ocppi/runtime/features/types/Generators.hpp 2024-01-23 10:46:58.985944337 +0800
--- include.orig/ocppi/runtime/features/types/Generators.hpp 2024-02-04 15:32:58.725688802 +0800
+++ include/ocppi/runtime/features/types/Generators.hpp 2024-02-04 15:33:07.633293801 +0800
@@ -199,7 +199,7 @@
inline void from_json(const json & j, Features& x) {
x.annotations = get_stack_optional<std::map<std::string, nlohmann::json>>(j, "annotations");
@ -121,8 +121,8 @@ diff -ruN include.orig/ocppi/runtime/features/types/Generators.hpp include/ocppi
if (x.mountOptions) {
j["mountOptions"] = x.mountOptions;
diff -ruN include.orig/ocppi/runtime/state/types/Generators.hpp include/ocppi/runtime/state/types/Generators.hpp
--- include.orig/ocppi/runtime/state/types/Generators.hpp 2024-01-23 10:46:55.820636453 +0800
+++ include/ocppi/runtime/state/types/Generators.hpp 2024-01-23 10:47:24.843470267 +0800
--- include.orig/ocppi/runtime/state/types/Generators.hpp 2024-02-04 15:32:58.725688802 +0800
+++ include/ocppi/runtime/state/types/Generators.hpp 2024-02-04 15:33:08.329263199 +0800
@@ -31,7 +31,7 @@
void to_json(json & j, const Status & x);
@ -133,8 +133,8 @@ diff -ruN include.orig/ocppi/runtime/state/types/Generators.hpp include/ocppi/ru
x.id = j.at("id").get<std::string>();
x.ociVersion = j.at("ociVersion").get<std::string>();
diff -ruN include.orig/ocppi/runtime/state/types/State.hpp include/ocppi/runtime/state/types/State.hpp
--- include.orig/ocppi/runtime/state/types/State.hpp 2024-01-23 10:46:55.820636453 +0800
+++ include/ocppi/runtime/state/types/State.hpp 2024-01-23 10:46:58.985944337 +0800
--- include.orig/ocppi/runtime/state/types/State.hpp 2024-02-04 15:32:58.725688802 +0800
+++ include/ocppi/runtime/state/types/State.hpp 2024-02-04 15:33:08.385260739 +0800
@@ -41,7 +41,7 @@
* Open Container Runtime State Schema
*/
@ -145,8 +145,8 @@ diff -ruN include.orig/ocppi/runtime/state/types/State.hpp include/ocppi/runtime
/**
* the container's ID
diff -ruN include.orig/ocppi/types/Generators.hpp include/ocppi/types/Generators.hpp
--- include.orig/ocppi/types/Generators.hpp 2024-01-23 10:46:55.820636453 +0800
+++ include/ocppi/types/Generators.hpp 2024-01-23 10:46:58.989945975 +0800
--- include.orig/ocppi/types/Generators.hpp 2024-02-04 15:32:58.725688802 +0800
+++ include/ocppi/types/Generators.hpp 2024-02-04 15:33:34.088156609 +0800
@@ -17,7 +17,6 @@
#include <nlohmann/json.hpp>
#include "ocppi/types/helper.hpp"
@ -170,20 +170,20 @@ diff -ruN include.orig/ocppi/types/Generators.hpp include/ocppi/types/Generators
}
-inline void from_json(const json & j, Types& x) {
-x.containerListItem = get_stack_optional<ContainerListItem>(j, "containerListItem");
-x.containerListItem = get_stack_optional<ContainerListItem>(j, "ContainerListItem");
-}
-
-inline void to_json(json & j, const Types & x) {
-j = json::object();
-if (x.containerListItem) {
-j["containerListItem"] = x.containerListItem;
-j["ContainerListItem"] = x.containerListItem;
-}
-}
}
}
diff -ruN include.orig/ocppi/types/Types.hpp include/ocppi/types/Types.hpp
--- include.orig/ocppi/types/Types.hpp 2024-01-23 10:46:55.820636453 +0800
--- include.orig/ocppi/types/Types.hpp 2024-02-04 15:32:58.725688802 +0800
+++ include/ocppi/types/Types.hpp 1970-01-01 08:00:00.000000000 +0800
@@ -1,39 +0,0 @@
-// Thish file is generated by /tools/codegen
@ -210,13 +210,13 @@ diff -ruN include.orig/ocppi/types/Types.hpp include/ocppi/types/Types.hpp
-namespace ocppi {
-namespace types {
-/**
-* Container list item, compatible wity crun.
-* Extra types used by ocppi API.
-*/
-
-using nlohmann::json;
-
-/**
-* Container list item, compatible wity crun.
-* Extra types used by ocppi API.
-*/
-struct Types {
-std::optional<ContainerListItem> containerListItem;

View File

@ -30,9 +30,7 @@ mv build-iwyu/{new-,}compile_commands.json
# shellcheck disable=SC2046
"$IWYU_TOOL" -p build-iwyu \
$(find . -path './libs*' \( -name '*.c' -o -name '*.cpp' \) -printf "%p ") \
-- \
-Xiwyu --mapping_file="$(pwd)/tools/iwyu/mapping.imp" |
$(find . -path './libs*' \( -name '*.c' -o -name '*.cpp' \) -printf "%p ") |
tee build/iwyu.out
IWYU_FIX_INCLUDES=${IWYU_FIX_INCLUDES:=$({

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +0,0 @@
[
{ ref: "boost-1.75-all.imp" },
]

View File

@ -1,11 +0,0 @@
#!/bin/bash
set -e
set -o pipefail
GIT=${GIT:="git"}
repoRoot="$("$GIT" rev-parse --show-toplevel)"
cd "$repoRoot"
wget -O cmake/CPM.cmake https://github.com/cpm-cmake/CPM.cmake/releases/latest/download/CPM.cmake

View File

@ -9,3 +9,5 @@ repoRoot="$("$GIT" rev-parse --show-toplevel)"
cd "$repoRoot"
wget -O cmake/GitSemver.cmake https://github.com/black-desk/GitSemver.cmake/releases/latest/download/GitSemver.cmake
wget -O cmake/PFL.cmake https://github.com/black-desk/PFL.cmake/releases/latest/download/PFL.cmake
wget -O cmake/CPM.cmake https://github.com/cpm-cmake/CPM.cmake/releases/latest/download/CPM.cmake

View File

@ -1,11 +0,0 @@
#!/bin/bash
set -e
set -o pipefail
GIT=${GIT:="git"}
repoRoot="$("$GIT" rev-parse --show-toplevel)"
cd "$repoRoot"
wget -O cmake/PFL.cmake https://github.com/black-desk/PFL.cmake/releases/latest/download/PFL.cmake