diff --git a/src/BashManager.cpp b/src/BashManager.cpp index 313721a..c02f6af 100644 --- a/src/BashManager.cpp +++ b/src/BashManager.cpp @@ -31,4 +31,4 @@ string BashManager::executeAndReadResult(string command) } pclose(p); return result; -} \ No newline at end of file +} diff --git a/src/BashModule.cpp b/src/BashModule.cpp new file mode 100644 index 0000000..955a3cd --- /dev/null +++ b/src/BashModule.cpp @@ -0,0 +1,70 @@ +// deployer BashModule implementation +// Copyright (C) 2024 Jean-Cloud +// GNU General Public License v3 + +#include <filesystem> +#include <unistd.h> +#include <sys/types.h> +#include <sys/wait.h> +#include "BashModule.h" +#include "BashManager.h" + +using namespace std; + +//destructor inline +BashModule::BashModule() +{ + name="Bash"; +} + +//private methods +int BashModule::executeScript(string serviceUsername) +{ + //this method is called in deploy(), it executes the script deploy.sh if it exists + string deployscript="./services/"+serviceUsername+"/deploy.sh"; + if(filesystem::exists(deployscript)){ + pid_t pid = fork(); + if (pid == -1) { + cerr << "Error when forking." << endl; + return -1; + } else if (pid > 0) { + int status; + waitpid(-1,&status,0); + if(status==-1){ + cerr << "Error when executing " << deployscript << endl; + } + cout << "status vaut " << status << endl; + return status; + } else { + if(execl("/bin/bash", "/bin/bash", "--noediting", "--noprofile", "--norc", deployscript.c_str(), (char *)0)==-1) + { + cerr << "Error in the execl call of " << deployscript << endl; + } + } + } + return 0; +} + +//public methods +int BashModule::prepare() +{ + return 0; +} + +int BashModule::deploy (string serviceUsername) +{ + cout << "deploy in bash module called" << endl; + executeScript(serviceUsername); + return 0; +} + + +int BashModule::remove (string serviceUsername) +{ + return 0; +} + +int BashModule::clean() +{ + return 0; +} diff --git a/src/BashModule.h b/src/BashModule.h new file mode 100644 index 0000000..9280f13 --- /dev/null +++ b/src/BashModule.h @@ -0,0 +1,23 @@ +// deployer BashModule header +// Copyright (C) 2024 Jean-Cloud +// GNU General Public License v3 + +#if !defined(BASHMODULE_H) +#define BASHMODULE_H + +#include "Module.h" + +class BashModule : public Module +{ + public: + BashModule(); + ~BashModule(){} //inline + int prepare (); + int deploy (string serviceUsername); + int remove(string serviceUsername); + int clean (); + private: + int executeScript(string serviceUsername); +}; + +#endif \ No newline at end of file diff --git a/src/Modules.h b/src/Modules.h index dad43b0..67c7176 100644 --- a/src/Modules.h +++ b/src/Modules.h @@ -1,8 +1,9 @@ #include <vector> #include <string> -#include "Module.h" #include "DockerModule.h" +#include "BashModule.h" using namespace std; DockerModule dockerModule=DockerModule(); -vector <Module *> modules={&dockerModule}; \ No newline at end of file +BashModule bashModule=BashModule(); +vector <Module *> modules={&bashModule}; //&dockerModule \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index dda465d..8d1d981 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,12 +45,6 @@ int createEnv(string serviceUsername){ return 0; } -int runBashScripts(string serviceUsername){ - //TO DO - cout << "run bash scripts called" << endl; - return 0; -} - int deployAll(){ //this method deploys all the services that are on this server cout << "deploying all" <<endl; @@ -69,10 +63,6 @@ int deployService(string serviceUsername){ if(int envCreated = createEnv(serviceUsername);envCreated!=0){ return -1; } - //bash scripts call - if(int bashScriptsRun = runBashScripts(serviceUsername);bashScriptsRun!=0){ - return -1; - } //call to the deploy functionality of all modules //the modules themselves determine their course of action depending on the service for(Module * mod_ptr : modules){ diff --git a/testenv/Dockerfile b/testenv/Dockerfile index 2cf6f30..c432479 100644 --- a/testenv/Dockerfile +++ b/testenv/Dockerfile @@ -1,21 +1,4 @@ FROM ubuntu:22.04 WORKDIR /usr/src/deployer_test -RUN mkdir -p /data/mounted && \ -apt-get -y update && apt-get install -y \ -ca-certificates \ -curl \ -gnupg \ -lsb-release &&\ -apt-get -y update &&\ -apt-get -y install ca-certificates curl && \ -install -m 0755 -d /etc/apt/keyrings && \ -curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc && \ -chmod a+r /etc/apt/keyrings/docker.asc && \ -echo \ -"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ -$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ -tee /etc/apt/sources.list.d/docker.list > /dev/null && \ -apt-get -y update && \ -apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin CMD ["sh"] diff --git a/testenv/docker-compose.yaml b/testenv/docker-compose.yaml index 6a563cb..d1cd5d6 100644 --- a/testenv/docker-compose.yaml +++ b/testenv/docker-compose.yaml @@ -6,5 +6,7 @@ services: volumes: - .:/usr/src/deployer_test - ./test_hosts:/etc/hosts + - /var/run/docker.sock:/var/run/docker.sock stdin_open: true tty: true + privileged: true diff --git a/testenv/services/test.sh8s.sh/deploy.sh b/testenv/services/test.sh8s.sh/deploy.sh new file mode 100644 index 0000000..4f48e5e --- /dev/null +++ b/testenv/services/test.sh8s.sh/deploy.sh @@ -0,0 +1 @@ +touch done