Run a container for each program
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
bc91048144
commit
59cf371568
@ -0,0 +1,4 @@
|
|||||||
|
---
|
||||||
|
BasedOnStyle: LLVM
|
||||||
|
IndentWidth: 4
|
||||||
|
FixNamespaceComments: false
|
@ -1,8 +1,12 @@
|
|||||||
cmake_minimum_required(VERSION 3.10)
|
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(planificador
|
||||||
add_executable(exe ${nameFile})
|
src/host.cpp
|
||||||
|
src/runner.cpp
|
||||||
|
src/main.cpp
|
||||||
|
)
|
||||||
|
target_compile_features(planificador PUBLIC cxx_std_17)
|
||||||
|
@ -0,0 +1,4 @@
|
|||||||
|
format:
|
||||||
|
find src/ \( -name '*.cpp' -o -name '*.hpp' \) -exec clang-format -i {} +
|
||||||
|
|
||||||
|
.PHONY: format
|
@ -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; }
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -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;
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
};
|
|
@ -1,35 +1,19 @@
|
|||||||
|
#include "program.hpp"
|
||||||
|
#include "runner.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include "programme.hpp"
|
|
||||||
#include "machine.hpp"
|
|
||||||
|
|
||||||
#include <vector>
|
int main() {
|
||||||
int main()
|
std::queue<sk::program> queue;
|
||||||
{
|
queue.push(sk::program{"echo $(( 1 + 2 ))",
|
||||||
//Programme prog1({"file1.txt","file2.txt","file3.txt"}, "image1");
|
"ghcr.io/moshell-lang/moshell:master"});
|
||||||
//std::queue<Programme> file;
|
sk::runner runner;
|
||||||
//file.push(prog1);
|
while (!queue.empty()) {
|
||||||
|
const sk::program ¤t = queue.front();
|
||||||
//while true{
|
sk::run_result res = runner.run_blocking(current);
|
||||||
//}
|
std::cout << "out: " << res.out << "\n";
|
||||||
|
std::cout << "err: " << res.err << "\n";
|
||||||
Machine m1("101.00.00.00",11);
|
queue.pop();
|
||||||
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<Machine> 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;
|
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace sk {
|
||||||
|
struct program {
|
||||||
|
std::string code;
|
||||||
|
std::string image;
|
||||||
|
};
|
||||||
|
}
|
@ -1,4 +0,0 @@
|
|||||||
#include "programme.hpp"
|
|
||||||
|
|
||||||
Programme::Programme(const std::vector<std::string>& files, const std::string& image): files{files}, imageContainer{image}
|
|
||||||
{}
|
|
@ -1,12 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class Programme{
|
|
||||||
std::vector<std::string> files;
|
|
||||||
std::string imageContainer;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Programme(const std::vector<std::string>& files, const std::string& image);
|
|
||||||
};
|
|
@ -0,0 +1,88 @@
|
|||||||
|
#include "runner.hpp"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cerrno>
|
||||||
|
#include <poll.h>
|
||||||
|
#include <spawn.h>
|
||||||
|
#include <system_error>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <wait.h>
|
||||||
|
|
||||||
|
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<char *const *>(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<char, 1024> buffer{};
|
||||||
|
std::string out;
|
||||||
|
std::string err;
|
||||||
|
std::array<pollfd, 2> 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};
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "program.hpp"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace sk {
|
||||||
|
struct run_result {
|
||||||
|
std::string out;
|
||||||
|
std::string err;
|
||||||
|
};
|
||||||
|
|
||||||
|
class runner {
|
||||||
|
public:
|
||||||
|
run_result run_blocking(const program &program);
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in new issue