structure du main avancee, modules.h change

This commit is contained in:
eleonore12345 2024-08-07 10:44:53 +02:00
parent b6a0db351c
commit eb8f202889
5 changed files with 83 additions and 48 deletions

View File

@ -13,19 +13,18 @@ int DockerModule::prepare()
return BashManager::execute("systemctl start docker docker.socket"); 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 //pulling images
int pulling =BashManager::execute("docker-compose pull")==0; int pulling =BashManager::execute("docker-compose pull")==0;
if(pulling==0){ if(pulling==0){
//starting service //starting service
int starting=BashManager::execute("docker-compose up -d --remove-orphans"); int starting=BashManager::execute("docker-compose up -d --remove-orphans");
}else{ }else{
cerr << "Error in DockerModule deploying "<< service << endl; cerr << "Error in DockerModule deploying "<< serviceUsername << endl;
return -1; return -1;
} }
return 0; return 0;
} }
@ -41,11 +40,11 @@ int removeContainersWithServiceName(string serviceName)
return BashManager::execute(cmd); return BashManager::execute(cmd);
} }
int DockerModule::remove (Service service) int DockerModule::remove (string serviceUsername)
{ {
//remove unwanted containers //remove unwanted containers
removeContainersCreatedByDockerCompose(); removeContainersCreatedByDockerCompose();
removeContainersWithServiceName(service.getUsername()); removeContainersWithServiceName(serviceUsername);
return 0; return 0;
} }

View File

@ -13,8 +13,8 @@ class DockerModule : public Module
DockerModule(){} //inline DockerModule(){} //inline
~DockerModule(){} //inline ~DockerModule(){} //inline
int prepare (); int prepare ();
int deploy (Service service); int deploy (string serviceUsername);
int remove(Service service); int remove(string serviceUsername);
int clean (); int clean ();
}; };

View File

@ -15,8 +15,8 @@ class Module
//=0 means pure virtual method, making Module a pure virtual class //=0 means pure virtual method, making Module a pure virtual class
virtual ~Module()=0; //make protected to ensure it is not used externally? virtual ~Module()=0; //make protected to ensure it is not used externally?
virtual int prepare ()=0; virtual int prepare ()=0;
virtual int deploy (Service service)=0; virtual int deploy (string serviceUsername)=0;
virtual int remove(Service service)=0; virtual int remove(string serviceUsername)=0;
virtual int clean ()=0; virtual int clean ()=0;
}; };
#endif #endif

View File

@ -2,5 +2,5 @@
#include <string> #include <string>
using namespace std; using namespace std;
DockerModule dockerModule=DockerModule();
vector < string > modules = {"DockerModule"}; vector <Module> modules = {dockerModule};

View File

@ -6,8 +6,11 @@
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#include <cstring> #include <cstring>
#include <filesystem>
#include "Services.h" #include "Services.h"
#include "Modules.h"
#include "DockerModule.h" #include "DockerModule.h"
#include "BashManager.h"
using namespace std; using namespace std;
void help(char * argv0) 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; 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 //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 //it looks into the /etc/hosts file thanks to a pipe to a separate bash process
{ {
FILE *p; string cmd ="getent hosts " +serviceUsername;
char result [100]={0}; string result = BashManager::executeAndReadResult(cmd);
string cmd ="getent hosts " +service.getUsername(); if(result.find("::1")!=string::npos){ //if result contains "::1"
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){
cout << "service on server" << endl; cout << "service on server" << endl;
return 0; return 0;
} }
@ -39,14 +33,52 @@ int isServiceOnServer(Service service)
return 1; 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(){ int deployAll(){
//this methods deploys all the services that are on this server
cout << "deploying all" <<endl; cout << "deploying all" <<endl;
return 0; return 0;
} }
int deployService(string service){ int deployService(string serviceUsername){
cout << "deploying service" << endl; //this method deploys indicated service if it is on this server
return 0; //TO DO: faire des boucles if cohérentes
if (isServiceOnServer(serviceUsername)==0){
//bash user creation
int userCreated = createUser(serviceUsername);
//environment variables creation
int envCreated = createEnv(serviceUsername);
//bash scripts call
int bashScriptsRun = runBashScripts(serviceUsername);
//call to the deploy functionality of all modules
//the modules themselves determine their course of action depending on the service
if (userCreated && envCreated){
for(Module * mod : modules){
int modResult = (*mod).deploy(serviceUsername);
if (modResult!=0){
cerr << "Error in module "<< mod << " when deploying " << serviceUsername << endl;
}
}
return 0;
}else{
cerr << "Error creating user and environment of "<<serviceUsername << endl;
}
}
} }
int removeAll(){ int removeAll(){
@ -54,39 +86,43 @@ int removeAll(){
return 0; return 0;
} }
int removeService(const char* service){ int removeService(string serviceUsername){
cout<< "removing service"<<endl; cout<< "removing service"<<endl;
return 0; return 0;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
//check number of arguments
if (argc != 3){ if (argc != 3){
cout << "Invalid number of arguments. \n" << endl; cout << "Invalid number of arguments. \n" << endl;
help(argv[0]); help(argv[0]);
exit(1); return -1;
} else { } else {
if (strcmp(argv[1],"deploy")==0){ //check that data is mounted on the server
if(strcmp(argv[2],"all")==0){ if (!filesystem::exists("/data/mounted")) {
deployAll(); cerr << "Error. The data is not mounted on the server" << endl;
}else{ return -1;
string service = argv[2];
deployService(service);
}
} else if (strcmp(argv[1],"remove")==0){
if(strcmp(argv[2],"all")==0){
removeAll();
}else{
removeService(argv[2]);
}
} else { } else {
cout << "Invalid argument. \n" << endl; //read instructions
string action = argv[1];
string service = argv[2];
if (action=="deploy"){
if(service=="all"){
deployAll();
} else {
deployService(service);
}
} else if (action=="remove"){
if(service=="all"){
removeAll();
} else {
removeService(service);
}
} else {
cerr << "Invalid argument. \n" << endl;
exit(1); exit(1);
} }
//DockerModule dmodule;
Services services = Services();
isServiceOnServer(services.getServices()[0]);
} }
return 0; return 0;
} }