From 59cf371568e5b192a53242b2ac0f01f06b7a138a Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Fri, 22 Sep 2023 10:51:56 +0200 Subject: [PATCH] Run a container for each program --- .clang-format | 4 +++ CMakeLists.txt | 12 ++++--- Makefile | 4 +++ src/host.cpp | 12 +++++++ src/host.hpp | 23 +++++++++++++ src/machine.cpp | 16 --------- src/machine.hpp | 20 ----------- src/main.cpp | 44 ++++++++---------------- src/program.hpp | 11 ++++++ src/programme.cpp | 4 --- src/programme.hpp | 12 ------- src/runner.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++ src/runner.hpp | 16 +++++++++ 13 files changed, 180 insertions(+), 86 deletions(-) create mode 100644 .clang-format create mode 100644 Makefile create mode 100644 src/host.cpp create mode 100644 src/host.hpp delete mode 100644 src/machine.cpp delete mode 100644 src/machine.hpp create mode 100644 src/program.hpp delete mode 100644 src/programme.cpp delete mode 100644 src/programme.hpp create mode 100644 src/runner.cpp create mode 100644 src/runner.hpp diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..37743a5 --- /dev/null +++ b/.clang-format @@ -0,0 +1,4 @@ +--- +BasedOnStyle: LLVM +IndentWidth: 4 +FixNamespaceComments: false diff --git a/CMakeLists.txt b/CMakeLists.txt index 0485513..5e1f698 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,12 @@ cmake_minimum_required(VERSION 3.10) -project(exe) +project(planificador) -set(CMAKE_CXX_STANDARD 17) +add_compile_options(-Wall -Wextra -pedantic) -file(GLOB nameFile "src/*.cpp") -add_executable(exe ${nameFile}) +add_executable(planificador + src/host.cpp + src/runner.cpp + src/main.cpp +) +target_compile_features(planificador PUBLIC cxx_std_17) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f064d57 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +format: + find src/ \( -name '*.cpp' -o -name '*.hpp' \) -exec clang-format -i {} + + +.PHONY: format diff --git a/src/host.cpp b/src/host.cpp new file mode 100644 index 0000000..011cd13 --- /dev/null +++ b/src/host.cpp @@ -0,0 +1,12 @@ +#include "host.hpp" + +namespace sk { +host::host(const std::string &ip, unsigned int connectionsMax) + : ip{ip}, connections{0}, connectionsMax{connectionsMax} {} + +void host::addConnection() { connections += 1; } + +unsigned int host::getNbConnections() const { return connections; } + +unsigned int host::getNbConnectionsMax() const { return connectionsMax; } +} diff --git a/src/host.hpp b/src/host.hpp new file mode 100644 index 0000000..949789f --- /dev/null +++ b/src/host.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include + +namespace sk { +class host { + std::string ip; + + unsigned int connections; + unsigned int connectionsMax; + + public: + host(const std::string &ip, unsigned int connectionsMax); + void addConnection(); + unsigned int getNbConnections() const; + unsigned int getNbConnectionsMax() const; + + bool operator<(const host &other) const { + return (connectionsMax - connections) < + other.getNbConnectionsMax() - other.getNbConnections(); + } +}; +} diff --git a/src/machine.cpp b/src/machine.cpp deleted file mode 100644 index 5dec998..0000000 --- a/src/machine.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "machine.hpp" - -Machine::Machine(const std::string& ip,unsigned int connectionsMax): ip{ip}, connections{0},connectionsMax{connectionsMax} -{} - -void Machine::addConnection(){ - connections+=1; -} - -unsigned int Machine::getNbConnections() const{ - return connections; -} - -unsigned int Machine::getNbConnectionsMax() const{ - return connectionsMax; -} \ No newline at end of file diff --git a/src/machine.hpp b/src/machine.hpp deleted file mode 100644 index 3ee31ba..0000000 --- a/src/machine.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include - -class Machine{ - std::string ip; - - unsigned int connections; - unsigned int connectionsMax; - - public: - Machine(const std::string& ip,unsigned int connectionsMax); - void addConnection(); - unsigned int getNbConnections() const; - unsigned int getNbConnectionsMax() const; - - bool operator<(const Machine& other) const { - return (connectionsMax-connections) < other.getNbConnectionsMax() - other.getNbConnections(); - } -}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 38d90fb..03d4672 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,35 +1,19 @@ +#include "program.hpp" +#include "runner.hpp" #include #include -#include "programme.hpp" -#include "machine.hpp" -#include -int main() -{ - //Programme prog1({"file1.txt","file2.txt","file3.txt"}, "image1"); - //std::queue file; - //file.push(prog1); - - //while true{ - //} - - Machine m1("101.00.00.00",11); - Machine m2("102.00.00.00",12); - Machine m3("103.00.00.00",13); - Machine m4("104.00.00.00",10); - Machine m5("105.00.00.00",9); - - std::priority_queue machines; - machines.push(m1); - machines.push(m2); - machines.push(m3); - machines.push(m4); - machines.push(m5); - - while (!machines.empty()) { - Machine top = machines.top(); - machines.pop(); - std::cout << "Connections: " << top.getNbConnectionsMax() << std::endl; +int main() { + std::queue queue; + queue.push(sk::program{"echo $(( 1 + 2 ))", + "ghcr.io/moshell-lang/moshell:master"}); + sk::runner runner; + while (!queue.empty()) { + const sk::program ¤t = queue.front(); + sk::run_result res = runner.run_blocking(current); + std::cout << "out: " << res.out << "\n"; + std::cout << "err: " << res.err << "\n"; + queue.pop(); } + return 0; } - \ No newline at end of file diff --git a/src/program.hpp b/src/program.hpp new file mode 100644 index 0000000..a74a616 --- /dev/null +++ b/src/program.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include +#include + +namespace sk { +struct program { + std::string code; + std::string image; +}; +} diff --git a/src/programme.cpp b/src/programme.cpp deleted file mode 100644 index f037651..0000000 --- a/src/programme.cpp +++ /dev/null @@ -1,4 +0,0 @@ -#include "programme.hpp" - -Programme::Programme(const std::vector& files, const std::string& image): files{files}, imageContainer{image} -{} \ No newline at end of file diff --git a/src/programme.hpp b/src/programme.hpp deleted file mode 100644 index 8caadd1..0000000 --- a/src/programme.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include -#include - -class Programme{ - std::vector files; - std::string imageContainer; - - public: - Programme(const std::vector& files, const std::string& image); -}; \ No newline at end of file diff --git a/src/runner.cpp b/src/runner.cpp new file mode 100644 index 0000000..c8c4671 --- /dev/null +++ b/src/runner.cpp @@ -0,0 +1,88 @@ +#include "runner.hpp" + +#include +#include +#include +#include +#include +#include +#include + +namespace sk { +run_result runner::run_blocking(const program &program) { + int in_pipe[2]; + int out_pipe[2]; + int err_pipe[2]; + if (pipe(in_pipe) == -1 || pipe(out_pipe) == -1 || pipe(err_pipe) == -1) { + throw std::system_error{errno, std::generic_category()}; + } + + posix_spawn_file_actions_t actions; + posix_spawn_file_actions_init(&actions); + posix_spawn_file_actions_addclose(&actions, in_pipe[1]); + posix_spawn_file_actions_addclose(&actions, out_pipe[0]); + posix_spawn_file_actions_addclose(&actions, err_pipe[0]); + posix_spawn_file_actions_adddup2(&actions, in_pipe[0], 0); + posix_spawn_file_actions_adddup2(&actions, out_pipe[1], 1); + posix_spawn_file_actions_adddup2(&actions, err_pipe[1], 2); + 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}; + pid_t pid; + int exit_code; + if (posix_spawnp(&pid, args[0], &actions, nullptr, + const_cast(args), nullptr) != 0) { + throw std::system_error{errno, std::generic_category()}; + } + + close(in_pipe[0]); + close(out_pipe[1]); + close(err_pipe[1]); + + write(in_pipe[1], program.code.data(), program.code.size()); + close(in_pipe[1]); + + std::array buffer{}; + std::string out; + std::string err; + std::array plist = {pollfd{out_pipe[0], POLLIN, 0}, + pollfd{err_pipe[0], POLLIN, 0}}; + while (poll(plist.data(), plist.size(), /*timeout*/ -1) > 0) { + if (plist[0].revents & POLLIN) { + ssize_t bytes_read = + read(out_pipe[0], buffer.data(), buffer.size()); + if (bytes_read == -1) { + throw std::system_error{errno, std::generic_category()}; + } + out.append(buffer.data(), bytes_read); + } else if (plist[1].revents & POLLIN) { + ssize_t bytes_read = + read(err_pipe[0], buffer.data(), buffer.size()); + if (bytes_read == -1) { + throw std::system_error{errno, std::generic_category()}; + } + err.append(buffer.data(), bytes_read); + } else { + break; + } + } + waitpid(pid, &exit_code, 0); + close(out_pipe[0]); + close(err_pipe[0]); + + posix_spawn_file_actions_destroy(&actions); + return run_result{out, err}; +} +} diff --git a/src/runner.hpp b/src/runner.hpp new file mode 100644 index 0000000..a7deba6 --- /dev/null +++ b/src/runner.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "program.hpp" +#include + +namespace sk { +struct run_result { + std::string out; + std::string err; +}; + +class runner { + public: + run_result run_blocking(const program &program); +}; +}