structure du main avancee, modules.h change
This commit is contained in:
parent
b6a0db351c
commit
eb8f202889
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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 ();
|
||||
};
|
||||
|
||||
|
@ -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
|
@ -2,5 +2,5 @@
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
vector < string > modules = {"DockerModule"};
|
||||
DockerModule dockerModule=DockerModule();
|
||||
vector <Module> modules = {dockerModule};
|
92
src/main.cpp
92
src/main.cpp
@ -6,8 +6,11 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#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" <<endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int deployService(string service){
|
||||
cout << "deploying service" << endl;
|
||||
int deployService(string serviceUsername){
|
||||
//this method deploys indicated service if it is on this server
|
||||
//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(){
|
||||
@ -54,39 +86,43 @@ int removeAll(){
|
||||
return 0;
|
||||
}
|
||||
|
||||
int removeService(const char* service){
|
||||
int removeService(string serviceUsername){
|
||||
cout<< "removing service"<<endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
//check number of arguments
|
||||
if (argc != 3){
|
||||
cout << "Invalid number of arguments. \n" << endl;
|
||||
help(argv[0]);
|
||||
exit(1);
|
||||
return -1;
|
||||
} else {
|
||||
if (strcmp(argv[1],"deploy")==0){
|
||||
if(strcmp(argv[2],"all")==0){
|
||||
//check that data is mounted on the server
|
||||
if (!filesystem::exists("/data/mounted")) {
|
||||
cerr << "Error. The data is not mounted on the server" << endl;
|
||||
return -1;
|
||||
} else {
|
||||
//read instructions
|
||||
string action = argv[1];
|
||||
string service = argv[2];
|
||||
if (action=="deploy"){
|
||||
if(service=="all"){
|
||||
deployAll();
|
||||
} else {
|
||||
string service = argv[2];
|
||||
deployService(service);
|
||||
}
|
||||
} else if (strcmp(argv[1],"remove")==0){
|
||||
if(strcmp(argv[2],"all")==0){
|
||||
} else if (action=="remove"){
|
||||
if(service=="all"){
|
||||
removeAll();
|
||||
} else {
|
||||
removeService(argv[2]);
|
||||
removeService(service);
|
||||
}
|
||||
} else {
|
||||
cout << "Invalid argument. \n" << endl;
|
||||
cerr << "Invalid argument. \n" << endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//DockerModule dmodule;
|
||||
Services services = Services();
|
||||
isServiceOnServer(services.getServices()[0]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user