diff --git a/src/config.cpp b/src/config.cpp index 15f4b00..4b1909e 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1,32 +1,31 @@ #include "config.hpp" +std::vector sk::config::loadHostsFromToml(const fs::path &confFile) { + std::vector hosts; -std::vector sk::config::loadHostsFromToml(const fs::path& confFile) { - std::vector hosts; - - auto config = toml::parse_file( confFile.string() ); - toml::table *serverSection = config.get_as("serveurs"); - if (serverSection == nullptr) { - return hosts; - } + auto config = toml::parse_file(confFile.string()); + toml::table *serverSection = config.get_as("serveurs"); + if (serverSection == nullptr) { + return hosts; + } // Parcourir les serveurs - for (const auto& [serverNameKey, serverInfoValue] : *serverSection) { - std::string serverName{serverNameKey}; - toml::table *serverInfoPtr = serverInfoValue.as_table(); - if (serverInfoPtr == nullptr) { - - } - toml::table &serverInfo = *serverInfoPtr; - - if(serverInfo.get_as("ip") && serverInfo.get_as("nbContainerMax")){ - std::string serverIp = serverInfo.get_as("ip")->get(); - int serverMaxContainers = serverInfo.get_as("nbContainerMax")->get(); - - hosts.push_back(sk::host(serverIp,serverMaxContainers)); - } - } - - return hosts; + for (const auto &[serverNameKey, serverInfoValue] : *serverSection) { + std::string serverName{serverNameKey}; + toml::table *serverInfoPtr = serverInfoValue.as_table(); + if (serverInfoPtr == nullptr) { + } + toml::table &serverInfo = *serverInfoPtr; + + if (serverInfo.get_as("ip") && + serverInfo.get_as("nbContainerMax")) { + std::string serverIp = serverInfo.get_as("ip")->get(); + int serverMaxContainers = + serverInfo.get_as("nbContainerMax")->get(); + + hosts.push_back(sk::host(serverIp, serverMaxContainers)); + } + } + + return hosts; } - diff --git a/src/config.hpp b/src/config.hpp index 9962395..2ab83aa 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -1,16 +1,15 @@ #pragma once #include -#include #include +#include #include "host.hpp" namespace fs = std::filesystem; -namespace sk{ -class config{ - public: - static std::vector loadHostsFromToml(const fs::path& confFile); - +namespace sk { +class config { + public: + static std::vector loadHostsFromToml(const fs::path &confFile); }; } \ No newline at end of file diff --git a/src/host.hpp b/src/host.hpp index 866e496..c82c5d0 100644 --- a/src/host.hpp +++ b/src/host.hpp @@ -13,7 +13,7 @@ class host { host(const std::string &ip, unsigned int connectionsMax); void addConnection(); - const std::string& getIp() const; + const std::string &getIp() const; unsigned int getNbConnections() const; unsigned int getNbConnectionsMax() const; diff --git a/src/main.cpp b/src/main.cpp index f887bdc..eba443d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,28 +1,48 @@ +#include "config.hpp" #include "program.hpp" #include "runner.hpp" +#include #include #include +#include +#include +#include #include +#include #include "host.hpp" -#include "config.hpp" -int main() { - std::vector hosts = sk::config::loadHostsFromToml("../conf.toml"); +namespace fs = std::filesystem; - if(hosts.empty()){ - std::cerr<<"Pas de host"<(argv), nullptr) != 0) { + return sk::runner_backend::BubbleWrap; + } + int status = 0; + waitpid(pid, &status, 0); + std::cout << status << std::endl; + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { + std::cerr << "Using Docker" << std::endl; + return sk::runner_backend::Docker; + } + return sk::runner_backend::BubbleWrap; +} + +int main() { + std::vector hosts = sk::config::loadHostsFromToml("../conf.toml"); - for(const auto &host : hosts){ - std::cout< queue; queue.push(sk::program{"echo $(( 1 + 2 ))", "ghcr.io/moshell-lang/moshell:master"}); - sk::runner runner; + sk::runner runner(detect_backend()); while (!queue.empty()) { const sk::program ¤t = queue.front(); sk::run_result res = runner.run_blocking(current); diff --git a/src/runner.cpp b/src/runner.cpp index c8c4671..a3ad56d 100644 --- a/src/runner.cpp +++ b/src/runner.cpp @@ -9,6 +9,8 @@ #include namespace sk { +runner::runner(runner_backend backend) : backend{backend} {} + run_result runner::run_blocking(const program &program) { int in_pipe[2]; int out_pipe[2]; @@ -28,18 +30,29 @@ run_result runner::run_blocking(const program &program) { posix_spawn_file_actions_addclose(&actions, in_pipe[0]); posix_spawn_file_actions_addclose(&actions, out_pipe[1]); posix_spawn_file_actions_addclose(&actions, err_pipe[1]); - const char *const args[] = {"docker", - "run", - "--rm", - "-i", - "--pull=never", - "--cap-drop=ALL", - "--network=none", - "--memory=64m", - "--memory-swap=64m", - "--pids-limit=128", - program.image.c_str(), - nullptr}; + const char *const docker_args[] = {"docker", + "run", + "--rm", + "-i", + "--pull=never", + "--cap-drop=ALL", + "--network=none", + "--memory=64m", + "--memory-swap=64m", + "--pids-limit=128", + program.image.c_str(), + nullptr}; + const char *const bwrap_args[] = { + "bwrap", "--ro-bind", "/usr", "/usr", "--dir", + "/tmp", "--dir", "/var", "--proc", "/proc", + "--dev", "/dev", "--symlink", "usr/lib", "/lib", + "--symlink", "usr/lib64", "/lib64", "--symlink", "usr/bin", + "/bin", "--symlink", "usr/sbin", "/sbin", "--unshare-all", + "/bin/sh", nullptr}; + const char *const *args = docker_args; + if (backend == runner_backend::BubbleWrap) { + args = bwrap_args; + } pid_t pid; int exit_code; if (posix_spawnp(&pid, args[0], &actions, nullptr, diff --git a/src/runner.hpp b/src/runner.hpp index a7deba6..af5d732 100644 --- a/src/runner.hpp +++ b/src/runner.hpp @@ -4,13 +4,18 @@ #include namespace sk { -struct run_result { +struct [[nodiscard]] run_result { std::string out; std::string err; }; +enum class runner_backend { BubbleWrap, Docker }; + class runner { + runner_backend backend; + public: + explicit runner(runner_backend backend); run_result run_blocking(const program &program); }; }