creation BashManager et avancement DockerModule

This commit is contained in:
eleonore12345 2024-08-06 16:36:33 +02:00
parent 615f72fc92
commit 88ae1628d1
5 changed files with 84 additions and 29 deletions

34
src/BashManager.cpp Normal file
View File

@ -0,0 +1,34 @@
// deployer BashManager implementation
// Copyright (C) 2024 Jean-Cloud
// GNU General Public License v3
#include <iostream>
#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;
}

19
src/BashManager.h Normal file
View File

@ -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 <string>
#include <list>
using namespace std;
class BashManager
{
public:
static string executeAndReadResult(string command);
static int execute(string command);
};
#endif

View File

@ -3,47 +3,43 @@
// GNU General Public License v3 // GNU General Public License v3
#include "DockerModule.h" #include "DockerModule.h"
#include "BashManager.h"
//constructor and destructor inline //constructor and destructor inline
//public methods //public methods
int DockerModule::prepare() int DockerModule::prepare()
{ {
return 0; return BashManager::execute("systemctl start docker docker.socket");
//nothing to do ?
} }
int DockerModule::deploy (Service service) 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; 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 () 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; return 0;
} }

6
src/Modules.h Normal file
View File

@ -0,0 +1,6 @@
#include <vector>
#include <string>
using namespace std;
vector < string > modules = {"DockerModule"};

View File

@ -26,7 +26,7 @@ int isServiceOnServer(Service service)
p = popen(cmd.c_str(),"r"); p = popen(cmd.c_str(),"r");
if( p == NULL) 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; return -1;
} }
fgets(result,100,p); fgets(result,100,p);