Skip to content

Commit

Permalink
Create sub-namespaces for 'libsarus' utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gwangmu Lee committed Jul 15, 2024
1 parent f39e22e commit 803dc30
Show file tree
Hide file tree
Showing 80 changed files with 744 additions and 730 deletions.
4 changes: 2 additions & 2 deletions src/cli/CommandKill.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CommandKill : public Command {
"kill", containerName, "SIGHUP"};

// execute runc
auto status = libsarus::forkExecWait(args);
auto status = libsarus::process::forkExecWait(args);

if (status != 0) {
auto message = boost::format("%s exited with code %d") % args % status;
Expand Down Expand Up @@ -87,4 +87,4 @@ class CommandKill : public Command {
} // namespace cli
} // namespace sarus

#endif
#endif
4 changes: 2 additions & 2 deletions src/cli/CommandPs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CommandPs : public Command {
"list"};

// execute runc
auto status = libsarus::forkExecWait(args);
auto status = libsarus::process::forkExecWait(args);

if (status != 0) {
auto message = boost::format("%s exited with code %d") % args % status;
Expand All @@ -66,4 +66,4 @@ class CommandPs : public Command {
} // namespace cli
} // namespace sarus

#endif
#endif
4 changes: 2 additions & 2 deletions src/cli/CommandPull.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ class CommandPull : public Command {
std::string readPasswordFromStdin() {
auto password = std::string{};

libsarus::setStdinEcho(false); // disable echo
libsarus::process::setStdinEcho(false); // disable echo
std::getline(std::cin, password);
libsarus::setStdinEcho(true); // enable echo
libsarus::process::setStdinEcho(true); // enable echo

if(password.empty()) {
SARUS_THROW_ERROR("Failed to read password from stdin: empty value provided");
Expand Down
16 changes: 8 additions & 8 deletions src/cli/CommandRun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class CommandRun : public Command {

std::string key, value;
try {
std::tie(key, value) = libsarus::parseKeyValuePair(annotation);
std::tie(key, value) = libsarus::string::parseKeyValuePair(annotation);
}
catch(std::exception& e) {
auto message = boost::format("Error parsing annotation from CLI '%s': %s")
Expand Down Expand Up @@ -336,7 +336,7 @@ class CommandRun : public Command {
}
else {
try {
std::tie(name, value) = libsarus::parseEnvironmentVariable(variable);
std::tie(name, value) = libsarus::environment::parseVariable(variable);
}
catch(std::exception& e) {
auto message = boost::format("Error parsing environment variable requested from CLI '%s': %s")
Expand Down Expand Up @@ -366,7 +366,7 @@ class CommandRun : public Command {
parser = libsarus::MountParser{conf->getRootfsDirectory(), conf->userIdentity, conf->json["userMounts"]};
}
for (const auto& mountString : conf->commandRun.userMounts) {
auto map = libsarus::parseMap(mountString);
auto map = libsarus::string::parseMap(mountString);
conf->commandRun.mounts.push_back(parser.parseMountRequest(map));
}
}
Expand Down Expand Up @@ -480,10 +480,10 @@ class CommandRun : public Command {
cli::utility::printLog( "Checking that the user has SSH keys",
libsarus::LogLevel::INFO);

libsarus::setEnvironmentVariable("HOOK_BASE_DIR", conf->json["localRepositoryBaseDir"].GetString());
libsarus::environment::setVariable("HOOK_BASE_DIR", conf->json["localRepositoryBaseDir"].GetString());

auto passwdFile = boost::filesystem::path{ conf->json["prefixDir"].GetString() } / "etc/passwd";
libsarus::setEnvironmentVariable("PASSWD_FILE", passwdFile.string());
libsarus::environment::setVariable("PASSWD_FILE", passwdFile.string());

auto args = libsarus::CLIArguments{
std::string{ conf->json["prefixDir"].GetString() } + "/bin/ssh_hook",
Expand All @@ -510,7 +510,7 @@ class CommandRun : public Command {
}
};

return libsarus::forkExecWait(args, std::function<void()>{setUserIdentity}) == 0;
return libsarus::process::forkExecWait(args, std::function<void()>{setUserIdentity}) == 0;
}

void verifyThatImageIsAvailable() const {
Expand All @@ -520,7 +520,7 @@ class CommandRun : public Command {
// - we can access images on root_squashed filesystems
// - we do not create/update local repo files (e.g. repo metadata and lockfiles) with root ownership
auto rootIdentity = libsarus::UserIdentity{};
libsarus::switchIdentity(conf->userIdentity);
libsarus::process::switchIdentity(conf->userIdentity);

try {
auto imageStore = image_manager::ImageStore(conf);
Expand All @@ -542,7 +542,7 @@ class CommandRun : public Command {
SARUS_RETHROW_ERROR(e, "Failed to verify that image is available");
}

libsarus::switchIdentity(rootIdentity);
libsarus::process::switchIdentity(rootIdentity);

cli::utility::printLog(boost::format("Successfully verified that image %s is available") % conf->imageReference,
libsarus::LogLevel::INFO);
Expand Down
8 changes: 4 additions & 4 deletions src/cli/CommandSshKeygen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ class CommandSshKeygen : public Command {
}

void execute() override {
libsarus::setEnvironmentVariable("HOOK_BASE_DIR", conf->json["localRepositoryBaseDir"].GetString());
libsarus::environment::setVariable("HOOK_BASE_DIR", conf->json["localRepositoryBaseDir"].GetString());
auto passwdFile = boost::filesystem::path{ conf->json["prefixDir"].GetString() } / "etc/passwd";
libsarus::setEnvironmentVariable("PASSWD_FILE", passwdFile.string());
libsarus::environment::setVariable("PASSWD_FILE", passwdFile.string());
auto dropbearDir = boost::filesystem::path{ conf->json["prefixDir"].GetString() } / "dropbear";
libsarus::setEnvironmentVariable("DROPBEAR_DIR", dropbearDir.string());
libsarus::environment::setVariable("DROPBEAR_DIR", dropbearDir.string());

auto sshHook = boost::filesystem::path{conf->json["prefixDir"].GetString()} / "bin/ssh_hook";
auto args = libsarus::CLIArguments{sshHook.string(), "keygen"};
Expand All @@ -56,7 +56,7 @@ class CommandSshKeygen : public Command {
else if(libsarus::Logger::getInstance().getLevel() == libsarus::LogLevel::DEBUG) {
args.push_back("--debug");
}
libsarus::forkExecWait(args);
libsarus::process::forkExecWait(args);
}

bool requiresRootPrivileges() const override {
Expand Down
8 changes: 4 additions & 4 deletions src/cli/test/test_CLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ TEST(CLITestGroup, generated_config_for_CommandLoad) {
// temp dir
{
auto customTempDir = libsarus::PathRAII("/tmp/sarus-utest-temp-dir");
libsarus::createFoldersIfNecessary(customTempDir.getPath());
libsarus::filesystem::createFoldersIfNecessary(customTempDir.getPath());

auto conf = generateConfig(
{"load", "--temp-dir="+customTempDir.getPath().string(), "archive.tar", "library/image:tag"});
Expand Down Expand Up @@ -188,7 +188,7 @@ TEST(CLITestGroup, generated_config_for_CommandPull) {
// temp-dir option and custom server
{
auto customTempDir = libsarus::PathRAII("/tmp/sarus-utest-temp-dir");
libsarus::createFoldersIfNecessary(customTempDir.getPath());
libsarus::filesystem::createFoldersIfNecessary(customTempDir.getPath());

auto conf = generateConfig(
{"pull",
Expand Down Expand Up @@ -487,8 +487,8 @@ TEST (CLITestGroup, device_mounts_for_CommandRun) {
IGNORE_TEST(CLITestGroup, device_mounts_for_CommandRun) {
#endif
auto testDir = libsarus::PathRAII(
libsarus::makeUniquePathWithRandomSuffix(boost::filesystem::current_path() / "CLI-test-deviceMounts-CommandRun"));
libsarus::createFoldersIfNecessary(testDir.getPath());
libsarus::filesystem::makeUniquePathWithRandomSuffix(boost::filesystem::current_path() / "CLI-test-deviceMounts-CommandRun"));
libsarus::filesystem::createFoldersIfNecessary(testDir.getPath());
auto siteDevice = testDir.getPath() / "siteDevice";
auto siteDeviceMajorID = 511u;
auto siteDeviceMinorID = 511u;
Expand Down
12 changes: 6 additions & 6 deletions src/common/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Config::Config(const boost::filesystem::path& sarusInstallationPrefixDir)

Config::Config(const boost::filesystem::path& configFilename,
const boost::filesystem::path& configSchemaFilename)
: json{ libsarus::readAndValidateJSON(configFilename, configSchemaFilename) }
: json{ libsarus::json::readAndValidate(configFilename, configSchemaFilename) }
{}

void Config::Directories::initialize(bool useCentralizedRepository, const common::Config& config) {
Expand All @@ -36,20 +36,20 @@ void Config::Directories::initialize(bool useCentralizedRepository, const common
libsarus::LogLevel::DEBUG);
repository = config.getCentralizedRepositoryDirectory();
images = repository / "images";
libsarus::createFoldersIfNecessary(images,config.userIdentity.uid, config.userIdentity.gid);
libsarus::filesystem::createFoldersIfNecessary(images,config.userIdentity.uid, config.userIdentity.gid);
}
else {
libsarus::logMessage( boost::format("initializing CLI config's directories for local repository"),
libsarus::LogLevel::DEBUG);
repository = config.getLocalRepositoryDirectory();
images = repository / "images";
libsarus::createFoldersIfNecessary(images, config.userIdentity.uid, config.userIdentity.gid);
libsarus::filesystem::createFoldersIfNecessary(images, config.userIdentity.uid, config.userIdentity.gid);
}

cache = repository / "cache";
libsarus::createFoldersIfNecessary(cache, config.userIdentity.uid, config.userIdentity.gid);
libsarus::createFoldersIfNecessary(cache / "ociImages", config.userIdentity.uid, config.userIdentity.gid);
libsarus::createFoldersIfNecessary(cache / "blobs", config.userIdentity.uid, config.userIdentity.gid);
libsarus::filesystem::createFoldersIfNecessary(cache, config.userIdentity.uid, config.userIdentity.gid);
libsarus::filesystem::createFoldersIfNecessary(cache / "ociImages", config.userIdentity.uid, config.userIdentity.gid);
libsarus::filesystem::createFoldersIfNecessary(cache / "blobs", config.userIdentity.uid, config.userIdentity.gid);

bool tempDirWasSpecifiedThroughCLI = !tempFromCLI.empty();
if(tempDirWasSpecifiedThroughCLI) {
Expand Down
12 changes: 6 additions & 6 deletions src/common/ImageMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ ImageMetadata::ImageMetadata(const boost::filesystem::path& path, const libsarus
auto rootIdentity = libsarus::UserIdentity{};
try {
// switch to user identity to make sure we can access files on root_squashed filesystems
libsarus::setFilesystemUid(identity);
libsarus::process::setFilesystemUid(identity);

auto json = libsarus::readJSON(path);
auto json = libsarus::json::read(path);

libsarus::setFilesystemUid(rootIdentity);
libsarus::process::setFilesystemUid(rootIdentity);

parseJSON(json);
}
catch (const libsarus::Error& e) {
libsarus::setFilesystemUid(rootIdentity);
libsarus::process::setFilesystemUid(rootIdentity);
auto message = boost::format("Error creating image metadata from file %s") % path;
SARUS_RETHROW_ERROR(e, message.str());
}
Expand Down Expand Up @@ -109,7 +109,7 @@ void ImageMetadata::write(const boost::filesystem::path& path) const {
}
}

libsarus::writeJSON(json, path);
libsarus::json::write(json, path);

logMessage("Successfully written image metadata file", libsarus::LogLevel::INFO);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ void ImageMetadata::parseJSON(const rapidjson::Value& json) {
for (const auto& v : json["Env"].GetArray()) {
std::string variable = v.GetString();
std::string key, value;
std::tie(key, value) = libsarus::parseEnvironmentVariable(variable);
std::tie(key, value) = libsarus::environment::parseVariable(variable);
env[key] = value;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/common/test/test_GroupDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TEST_GROUP(GroupDBTestGroup) {

TEST(GroupDBTestGroup, testRead) {
// create file
auto path = libsarus::PathRAII{libsarus::makeUniquePathWithRandomSuffix("./test-etc-group-file")};
auto path = libsarus::PathRAII{libsarus::filesystem::makeUniquePathWithRandomSuffix("./test-etc-group-file")};
const auto& file = path.getPath();
std::ofstream of{file.c_str()};
of << "groupName0:x:0:" << std::endl
Expand Down Expand Up @@ -63,7 +63,7 @@ TEST(GroupDBTestGroup, testRead) {
}

TEST(GroupDBTestGroup, testWrite) {
auto path = libsarus::PathRAII{libsarus::makeUniquePathWithRandomSuffix("./test-etc-group-file")};
auto path = libsarus::PathRAII{libsarus::filesystem::makeUniquePathWithRandomSuffix("./test-etc-group-file")};
const auto& file = path.getPath();

// create entry
Expand Down
2 changes: 1 addition & 1 deletion src/common/test/test_ImageMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ TEST(ImageMetadataTestGroup, write_read_from_file) {
{"labelKey1", "labelValue1"}
};

auto file = libsarus::makeUniquePathWithRandomSuffix("/tmp/sarus-test-imagemetadata");
auto file = libsarus::filesystem::makeUniquePathWithRandomSuffix("/tmp/sarus-test-imagemetadata");
writtenMetadata.write(file);
auto readMetadata = common::ImageMetadata{file, libsarus::UserIdentity{}};

Expand Down
14 changes: 7 additions & 7 deletions src/hooks/amdgpu/AmdGpuHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ std::unordered_map<std::string, std::string> mapDevicesIdToRenderD(

fs::path byPath(path + "/by-path");
for (fs::directory_iterator itr(byPath); itr != end_itr; ++itr) {
if (!libsarus::isSymlink(itr->path()) ||
if (!libsarus::filesystem::isSymlink(itr->path()) ||
itr->path().string().find("card") == std::string::npos)
continue;

cardId = fs::path(fs::read_symlink(itr->path())).filename().string();

libsarus::replaceString(cardId, "card", "");
libsarus::string::replace(cardId, "card", "");

auto renderNName{itr->path().string()};
libsarus::replaceString(renderNName, "card", "render");
libsarus::string::replace(renderNName, "card", "render");
auto renderNPath{fs::path(renderNName)};

try {
Expand Down Expand Up @@ -112,7 +112,7 @@ void AmdGpuHook::activate() {
log("Activating AMD GPU support", libsarus::LogLevel::INFO);

try {
if (!libsarus::isDeviceFile(fs::path{"/dev/kfd"})) {
if (!libsarus::filesystem::isDeviceFile(fs::path{"/dev/kfd"})) {
return;
}
} catch (const libsarus::Error& e) {
Expand All @@ -127,7 +127,7 @@ void AmdGpuHook::activate() {
void AmdGpuHook::parseConfigJSONOfBundle() {
log("Parsing bundle's config.json", libsarus::LogLevel::INFO);

auto json = libsarus::readJSON(containerState.bundle() / "config.json");
auto json = libsarus::json::read(containerState.bundle() / "config.json");

libsarus::hook::applyLoggingConfigIfAvailable(json);

Expand Down Expand Up @@ -158,10 +158,10 @@ void AmdGpuHook::performBindMounts() const {
if (mountPoint.empty())
continue;

libsarus::validatedBindMount(mountPoint, mountPoint, userIdentity,
libsarus::mount::validatedBindMount(mountPoint, mountPoint, userIdentity,
rootfsDir);

if (libsarus::isDeviceFile(mountPoint)) {
if (libsarus::filesystem::isDeviceFile(mountPoint)) {
if (devicesCgroupPath.empty()) {
devicesCgroupPath =
libsarus::hook::findCgroupPath("devices", "/", containerState.pid());
Expand Down
12 changes: 6 additions & 6 deletions src/hooks/amdgpu/test/test_AmdGpuHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ void createDriSubdir(const fs::path& path, std::vector<int> ids) {
if (fs::exists(path)) {
fs::remove_all(path);
}
libsarus::createFoldersIfNecessary(path / "by-path");
libsarus::filesystem::createFoldersIfNecessary(path / "by-path");

int busId = 193;
for (auto id : ids) {
libsarus::createFileIfNecessary(path /
libsarus::filesystem::createFileIfNecessary(path /
(boost::format("card%1%") % id).str());
libsarus::createFileIfNecessary(
libsarus::filesystem::createFileIfNecessary(
path / (boost::format("renderD%1%") % (128 + id)).str());

fs::create_symlink(
Expand Down Expand Up @@ -70,7 +70,7 @@ void createOCIBundleConfigJSON(const boost::filesystem::path& bundleDir,
doc["process"]["env"].PushBack(
rj::Value{rocrVisibleDevices.c_str(), allocator}, allocator);
}
libsarus::writeJSON(doc, bundleDir / "config.json");
libsarus::json::write(doc, bundleDir / "config.json");
}

TEST_GROUP(AMDGPUHookTestGroup) {
Expand Down Expand Up @@ -123,7 +123,7 @@ std::vector<std::string> getExpectedDeviceFiles(
TEST(AMDGPUHookTestGroup,
find_all_render_devices_if_ROCR_VISIBLE_DEVICES_is_not_defined) {
auto mockDriPath = libsarus::PathRAII(
libsarus::makeUniquePathWithRandomSuffix(
libsarus::filesystem::makeUniquePathWithRandomSuffix(
boost::filesystem::current_path() / "mockDri"))
.getPath();
createDriSubdir(mockDriPath, {0, 1, 2, 3});
Expand All @@ -137,7 +137,7 @@ TEST(AMDGPUHookTestGroup,

TEST(AMDGPUHookTestGroup, find_all_render_devices_in_ROCR_VISIBLE_DEVICES) {
auto mockDriPath = libsarus::PathRAII(
libsarus::makeUniquePathWithRandomSuffix(
libsarus::filesystem::makeUniquePathWithRandomSuffix(
boost::filesystem::current_path() / "mockDri"))
.getPath();
createDriSubdir(mockDriPath, {0, 1, 2, 3});
Expand Down
Loading

0 comments on commit 803dc30

Please sign in to comment.