diff --git a/test~coding~readingCppPrograms.fr/docker-compile-and-run.cpp b/test~coding~readingCppPrograms.fr/docker-compile-and-run.cpp index fe588f68187bd4d0b8ed4980cfdb7189060b774d..2e041912635c9505e405fc5d8cc6d8b86297ab5f 100644 --- a/test~coding~readingCppPrograms.fr/docker-compile-and-run.cpp +++ b/test~coding~readingCppPrograms.fr/docker-compile-and-run.cpp @@ -11,14 +11,17 @@ using namespace std; /** * TODO: + * - [X] make this into a program rather than a shell script for a minimum of safety * - [ ] set tight resources limits - * - [ ] make this into a program rather than a shell script for a minimum of safety + * - [ ] check proper handling of all potential errors + * - [ ] make this into a plain C program to not add a dependency on C++ **/ string compile_and_run = "compile-and-run.sh"; //string bin_dir = "/home/wims/public_html/bin/"; string bin_dir = ""; string docker = "/usr/bin/docker"; +bool verbose = false; void usage () { cerr << "docker-compile-and-run [program.cpp]" << endl; @@ -28,20 +31,13 @@ void usage () { cerr << "The exit status is that of the compiler." << endl; } -int system(string command) { - // cerr << "Running " << command << endl; - std::system(command.c_str()); -} - -void report(string comment, string cmd, vector<string> args) { - cerr << comment; - cerr << cmd; - for (auto arg: args) - cerr << " " << arg; - cerr << endl; -} - -int cpp_exec(string cmd, const vector<string> args) { +int my_exec(string cmd, const vector<string> args) { + if (verbose) { + cerr << "exec: " << cmd; + for (auto arg: args) + cerr << " " << arg; + cerr << endl; + } vector<const char*> argv; argv.push_back(cmd.c_str()); for ( auto &s: args ) @@ -51,9 +47,7 @@ int cpp_exec(string cmd, const vector<string> args) { return execv(cmd.c_str(), (char * const*)argv.data()); } -int exec(string cmd, const vector<string> args, const string in, const string out, const string err) { - report("exec: ", cmd, args); - +int my_system(string cmd, const vector<string> args, const string in, const string out, const string err) { // Taken from man waitpid int status; pid_t w; @@ -73,7 +67,7 @@ int exec(string cmd, const vector<string> args, const string in, const string ou dup2(fileno(f), STDOUT_FILENO); fclose(f); } - cpp_exec(cmd, args); + my_exec(cmd, args); perror("exec"); exit(EXIT_FAILURE); } @@ -85,13 +79,11 @@ int exec(string cmd, const vector<string> args, const string in, const string ou return EXIT_SUCCESS; } -int exec(string cmd, const vector<string> args) { - return exec(cmd, args, "", "", ""); +int my_system(string cmd, const vector<string> args) { + return my_system(cmd, args, "", "", ""); } -std::string pexec(const string cmd, vector<string> args) { - report("pexec: ", cmd, args); - +std::string my_popen(const string cmd, vector<string> args) { int pipefd[2]; pid_t cpid; @@ -109,7 +101,7 @@ std::string pexec(const string cmd, vector<string> args) { if (cpid == 0) { /* Child writes to pipe */ close(pipefd[0]); /* Close unused read end */ dup2(pipefd[1], STDOUT_FILENO); - cpp_exec(cmd, args); + my_exec(cmd, args); perror("exec"); exit(EXIT_FAILURE); } @@ -131,7 +123,7 @@ string docker_run(string container, string cmd, vector<string> args) { vector<string> docker_args = {"run", "-d", container, cmd}; for (auto arg: args) docker_args.push_back(arg); - string ID = pexec(docker, docker_args); + string ID = my_popen(docker, docker_args); // see http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring ID.erase(ID.find_last_not_of(" \n\r\t")+1); return ID; @@ -141,26 +133,24 @@ void docker_exec(string docker_id, vector<string> args) { vector<string> docker_args = {"exec", "-i", docker_id}; for (auto arg: args) docker_args.push_back(arg); - exec(docker, docker_args); + my_system(docker, docker_args); } void docker_cp(string docker_id, string source, string target) { // See http://stackoverflow.com/questions/22907231/copying-files-from-host-to-docker-container // TODO: replace with 'docker cp' of docker 1.8 when possible - exec(docker, - {"exec", "-i", docker_id, "/bin/bash", "-c", "cat > "+target,}, - source, "", ""); + my_system(docker, + {"exec", "-i", docker_id, "/bin/bash", "-c", "cat > "+target,}, + source, "", ""); } void docker_rm(string docker_id) { - exec(docker, {"rm", "-f", docker_id}, "", "/dev/null", ""); + my_system(docker, {"rm", "-f", docker_id}, "", "/dev/null", ""); } int main(int argc, char **argv) { - //cout << pexec("/usr/bin/id", {}) << "|" << endl; - + //cout << my_popen("/usr/bin/id", {}) << "|" << endl; //printf("egid: %d\n", getegid()); - //exec("/usr/bin/id", {}); //system("/usr/bin/id"); @@ -172,12 +162,12 @@ int main(int argc, char **argv) { string program=argv[1]; string docker_id = docker_run("crosbymichael/build-essential", "sleep", {"1000"}); - cout << "docker_id: " << docker_id << endl; + if (verbose) + cerr << "docker_id: " << docker_id << endl; docker_cp(docker_id, bin_dir+compile_and_run, compile_and_run); docker_cp(docker_id, program, program); docker_exec(docker_id, { "chmod", "700", compile_and_run }); docker_exec(docker_id, { "./"+compile_and_run, program}); docker_rm(docker_id); - cout.flush(); return 0; }