From eb8f2028890017903a3bae8eb61516ef3eac635e Mon Sep 17 00:00:00 2001 From: eleonore12345 Date: Wed, 7 Aug 2024 10:44:53 +0200 Subject: [PATCH] structure du main avancee, modules.h change --- src/DockerModule.cpp | 11 ++--- src/DockerModule.h | 4 +- src/Module.h | 4 +- src/Modules.h | 4 +- src/main.cpp | 108 ++++++++++++++++++++++++++++--------------- 5 files changed, 83 insertions(+), 48 deletions(-) diff --git a/src/DockerModule.cpp b/src/DockerModule.cpp index 95be0b7..8abf2f0 100644 --- a/src/DockerModule.cpp +++ b/src/DockerModule.cpp @@ -13,19 +13,18 @@ int DockerModule::prepare() return BashManager::execute("systemctl start docker docker.socket"); } -int DockerModule::deploy (Service service) +int DockerModule::deploy (string serviceUsername) { + //go to the right directory //pulling images int pulling =BashManager::execute("docker-compose pull")==0; if(pulling==0){ //starting service int starting=BashManager::execute("docker-compose up -d --remove-orphans"); - }else{ - cerr << "Error in DockerModule deploying "<< service << endl; + cerr << "Error in DockerModule deploying "<< serviceUsername << endl; return -1; } - return 0; } @@ -41,11 +40,11 @@ int removeContainersWithServiceName(string serviceName) return BashManager::execute(cmd); } -int DockerModule::remove (Service service) +int DockerModule::remove (string serviceUsername) { //remove unwanted containers removeContainersCreatedByDockerCompose(); - removeContainersWithServiceName(service.getUsername()); + removeContainersWithServiceName(serviceUsername); return 0; } diff --git a/src/DockerModule.h b/src/DockerModule.h index b8d3492..ebc9a79 100644 --- a/src/DockerModule.h +++ b/src/DockerModule.h @@ -13,8 +13,8 @@ class DockerModule : public Module DockerModule(){} //inline ~DockerModule(){} //inline int prepare (); - int deploy (Service service); - int remove(Service service); + int deploy (string serviceUsername); + int remove(string serviceUsername); int clean (); }; diff --git a/src/Module.h b/src/Module.h index 553c73f..e0def5c 100644 --- a/src/Module.h +++ b/src/Module.h @@ -15,8 +15,8 @@ class Module //=0 means pure virtual method, making Module a pure virtual class virtual ~Module()=0; //make protected to ensure it is not used externally? virtual int prepare ()=0; - virtual int deploy (Service service)=0; - virtual int remove(Service service)=0; + virtual int deploy (string serviceUsername)=0; + virtual int remove(string serviceUsername)=0; virtual int clean ()=0; }; #endif \ No newline at end of file diff --git a/src/Modules.h b/src/Modules.h index ba4f9e4..2cba951 100644 --- a/src/Modules.h +++ b/src/Modules.h @@ -2,5 +2,5 @@ #include using namespace std; - -vector < string > modules = {"DockerModule"}; \ No newline at end of file +DockerModule dockerModule=DockerModule(); +vector modules = {dockerModule}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ab161b0..8c9475e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,8 +6,11 @@ #include #include #include +#include #include "Services.h" +#include "Modules.h" #include "DockerModule.h" +#include "BashManager.h" using namespace std; void help(char * argv0) @@ -16,22 +19,13 @@ void help(char * argv0) cout << "usage: ./" << argv0 <<"action file \n with action=deploy or remove and file=path to a file or all. \n" << endl; } -int isServiceOnServer(Service service) +int isServiceOnServer(string serviceUsername) //this method tests if a certain service is on the current server //it looks into the /etc/hosts file thanks to a pipe to a separate bash process { - FILE *p; - char result [100]={0}; - string cmd ="getent hosts " +service.getUsername(); - p = popen(cmd.c_str(),"r"); - if( p == NULL) - { - cerr << "Unable to verify if the service is on the server, bash process opening error." << endl; - return -1; - } - fgets(result,100,p); - pclose(p); - if(strstr(result,"::1")!=nullptr){ + string cmd ="getent hosts " +serviceUsername; + string result = BashManager::executeAndReadResult(cmd); + if(result.find("::1")!=string::npos){ //if result contains "::1" cout << "service on server" << endl; return 0; } @@ -39,14 +33,52 @@ int isServiceOnServer(Service service) return 1; } +int createUser(string serviceUsername){ + //TO DO + return 0; +} + +int createEnv(string serviceUsername){ + //TO DO + return 0; +} + +int runBashScripts(string serviceUsername){ + //TO DO + return 0; +} + int deployAll(){ + //this methods deploys all the services that are on this server cout << "deploying all" <