From 88ae1628d1a61522de70128722278635fcc5ceea Mon Sep 17 00:00:00 2001 From: eleonore12345 Date: Tue, 6 Aug 2024 16:36:33 +0200 Subject: [PATCH] creation BashManager et avancement DockerModule --- src/BashManager.cpp | 34 +++++++++++++++++++++++++++++ src/BashManager.h | 19 ++++++++++++++++ src/DockerModule.cpp | 52 ++++++++++++++++++++------------------------ src/Modules.h | 6 +++++ src/main.cpp | 2 +- 5 files changed, 84 insertions(+), 29 deletions(-) create mode 100644 src/BashManager.cpp create mode 100644 src/BashManager.h create mode 100644 src/Modules.h diff --git a/src/BashManager.cpp b/src/BashManager.cpp new file mode 100644 index 0000000..313721a --- /dev/null +++ b/src/BashManager.cpp @@ -0,0 +1,34 @@ +// deployer BashManager implementation +// Copyright (C) 2024 Jean-Cloud +// GNU General Public License v3 + +#include +#include "BashManager.h" + +int BashManager::execute(string command) +{ + FILE * p = popen(command.c_str(),"r"); + if( p == NULL) + { + cerr << "Error executing " << command << endl; + return -1; + } + pclose(p); + return 0; +} + +string BashManager::executeAndReadResult(string command) +{ + string result=""; + char lineBuffer [4095]={0}; //4095 is the maximum length of a shell line + FILE * p = popen(command.c_str(),"r"); + if( p == nullptr) + { + cerr << "Error executing "<< command << endl; + } + while(fgets(lineBuffer,sizeof(lineBuffer),p)){ + result += lineBuffer; + } + pclose(p); + return result; +} \ No newline at end of file diff --git a/src/BashManager.h b/src/BashManager.h new file mode 100644 index 0000000..e7d1fa3 --- /dev/null +++ b/src/BashManager.h @@ -0,0 +1,19 @@ +// deployer BashManager header +// Copyright (C) 2024 Jean-Cloud +// GNU General Public License v3 + +#if !defined(BASHMANAGER_H) +#define BASHMANAGER_H + +#include +#include + +using namespace std; +class BashManager +{ + public: + static string executeAndReadResult(string command); + static int execute(string command); +}; + +#endif \ No newline at end of file diff --git a/src/DockerModule.cpp b/src/DockerModule.cpp index 38d61b9..d26e2e8 100644 --- a/src/DockerModule.cpp +++ b/src/DockerModule.cpp @@ -3,47 +3,43 @@ // GNU General Public License v3 #include "DockerModule.h" +#include "BashManager.h" //constructor and destructor inline //public methods int DockerModule::prepare() { - return 0; - //nothing to do ? + return BashManager::execute("systemctl start docker docker.socket"); } int DockerModule::deploy (Service service) { + //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; + return -1; + } + return 0; - /* bash script for inspo - isServiceOnServer(); -docker_service="$(echo "$service" | tr '.' '_')" - -## deploy - - - if $deploy ; then - section "Logging to registry" - # XXX Login to docker registry - - section "Pulling images" - docker-compose pull - if [ "$?" -ne 0 ] ; then - echo "PULL FAILED" - fi - - section "Starting service" - run docker-compose up -d --remove-orphans - [ "$?" -ne 0 ] && echo "Erreur docker compose" && returncode=1 - else - section "Removing containers" - run docker-compose down --rmi all --remove-orphans - fi -fi -*/ } int DockerModule::clean () { + BashManager::execute("docker-compose down --rmi all --remove-orphans"); + /* + if ! "$deploy" ; then + section "Remove stray containers" + while read container ; do + [ -z "$container" ] && continue || true + echo "Removing $container" + run docker rm "$container" + done <<< "$(docker ps | grep "$docker_service" | cut -d ' ' -f 1)" +fi +*/ return 0; } \ No newline at end of file diff --git a/src/Modules.h b/src/Modules.h new file mode 100644 index 0000000..673c2e4 --- /dev/null +++ b/src/Modules.h @@ -0,0 +1,6 @@ +#include +#include + +using namespace std; + +vector < string > modules = {"DockerModule"}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index cf5802f..ab161b0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,7 +26,7 @@ int isServiceOnServer(Service service) p = popen(cmd.c_str(),"r"); if( p == NULL) { - cout << "Unable to verify if the service is on the server, bash process opening error." << endl; + cerr << "Unable to verify if the service is on the server, bash process opening error." << endl; return -1; } fgets(result,100,p);