sh8s_deployer/src/main.cpp
2024-08-07 14:33:15 +02:00

138 lines
3.7 KiB
C++

// deployer main programm
// Copyright (C) 2024 Jean-Cloud
// GNU General Public License v3
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <filesystem>
#include "Services.h"
#include "Modules.h"
#include "BashManager.h"
using namespace std;
void help(char * argv0)
{
//temporary
cout << "usage: ./" << argv0 <<"action file \n with action=deploy or remove and file=path to a file or all. \n" << endl;
}
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
{
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;
}
cout << "service not on server" << endl;
return 1;
}
int createUser(string serviceUsername){
//TO DO
cout << "create user called" << endl;
return 0;
}
int createEnv(string serviceUsername){
//TO DO
cout << "create env called" << endl;
return 0;
}
int runBashScripts(string serviceUsername){
//TO DO
cout << "run bash scripts called" << endl;
return 0;
}
int deployAll(){
//this method deploys all the services that are on this server
cout << "deploying all" <<endl;
return 0;
}
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
if(int userCreated = createUser(serviceUsername);userCreated!=0){
return -1;
}
//environment variables creation
if(int envCreated = createEnv(serviceUsername);envCreated!=0){
return -1;
}
//bash scripts call
if(int bashScriptsRun = runBashScripts(serviceUsername);bashScriptsRun!=0){
return -1;
}
//call to the deploy functionality of all modules
//the modules themselves determine their course of action depending on the service
for(Module * mod_ptr : modules){
int modResult = (*mod_ptr).deploy(serviceUsername);
if (modResult!=0){
cerr << "Error in module " << (*mod_ptr) << " when deploying " << serviceUsername << endl;
}
}
return 0;
cerr << "Error creating user and environment of "<<serviceUsername << endl;
}
return 0;
}
int removeAll(){
cout << "removing all"<<endl;
return 0;
}
int removeService(string serviceUsername){
cout<< "removing service"<<endl;
return 0;
}
int main(int argc, char *argv[])
{
//check number of arguments
if (argc != 3){
cerr << "Invalid number of arguments. \n" << endl;
help(argv[0]);
return -1;
} else {
//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 {
deployService(service);
}
} else if (action=="remove"){
if(service=="all"){
removeAll();
} else {
removeService(service);
}
} else {
cerr << "Invalid argument. \n" << endl;
return -1;
}
}
}
return 0;
}