debut SQLmanager

This commit is contained in:
eleonore12345 2024-09-13 23:41:45 +02:00
parent 180561618b
commit f6dbdc9fc3
8 changed files with 80 additions and 19 deletions

View File

@ -22,6 +22,7 @@ BashModule::BashModule()
//private methods
int BashModule::executeScript(string serviceUsername, string script)
{
cout << "debut execute script" <<endl;
//this method is called in deploy() and remove(), it executes the script if it exists
if(!filesystem::exists(script)){
cout << "No deploy.sh script for this service.";
@ -64,6 +65,7 @@ int BashModule::executeScriptAs(string serviceUsername, string script)
{
//this method is called in deploy(), it executes the script if it exists, as the user associated with the service
//check that the file exists
cout << "debut ex script as"<<endl;
if(!filesystem::exists(script)){
cout << "No deploy_user.sh script for this service.";
return 0;
@ -87,6 +89,7 @@ int BashModule::executeScriptAs(string serviceUsername, string script)
return -1;
}else{
//executing the script in a separate process
cout << "avant le fork "<<endl;
pid_t pid = fork();
if (pid == -1) {
cerr << "Error when forking." << endl;

View File

@ -13,7 +13,7 @@ $(EXE) : $(OBJECTS)
$(ECHO) "-Linking $(EXE)-"
$(GCC) -o $@ $^ $(LIBRARIES)
$(CP) $(EXE) ../testenv
$(CP) $(EXE) ../bin
$(BIN)/%.o:%.cpp
$(ECHO) "-Compilation $<- "
$(GCC) $(CCFLAGS) -o $@ $<

View File

@ -13,4 +13,4 @@ NginxModule nginxModule=NginxModule();
WireguardModule wireguardModule=WireguardModule();
EncryptionModule encryptionModule=EncryptionModule();
vector <Module *> modules={&bashModule,&nginxModule,&wireguardModule, &encryptionModule}; //&dockerModule
vector <Module *> modules={&bashModule,&wireguardModule, &encryptionModule}; //&dockerModule,&nginxModule

39
src/SQLmanager.cpp Normal file
View File

@ -0,0 +1,39 @@
//deployer SLmanager implementation
// Copyright (C) 2024 Jean-Cloud
// GNU General Public License v3
#include <iostream>
#include <cstring>
#include <vector>
#include <list>
#include "Service.h"
using namespace std;
//constructor and destructor inlin
//services
vector<Service> GetServices() const;
const Service * FindServiceByUsername(string aUsername) const;
const Service * FindServiceByID(int aUserID) const;
//list<const Service*> FindServicesByServer(string aServer) const;
int isServiceOnServer(string serviceUsername);
vector <Service> services;
//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" which is the notation for loopback in IpV6
// cout << "service " << serviceUsername << " on server" << endl;
// return 0;
// }
// cout << "service" << serviceUsername << " not on server" << endl;
// return 1;
// return 0;
// }

30
src/SQLmanager.h Normal file
View File

@ -0,0 +1,30 @@
//deployer SLmanager header
// Copyright (C) 2024 Jean-Cloud
// GNU General Public License v3
#if !defined(SQLMANAGER_H)
#define SQLMANAGER_H
#include <iostream>
#include <cstring>
#include <vector>
#include <list>
#include "Service.h"
using namespace std;
class SQLmanager {
public:
SQLmanager(){}
~SQLmanager(){}
//services
vector<Service> GetServices() const;
const Service * FindServiceByUsername(string aUsername) const;
const Service * FindServiceByID(int aUserID) const;
//list<const Service*> FindServicesByServer(string aServer) const;
int isServiceOnServer(string serviceUsername);
private:
vector <Service> services;
};
#endif

BIN
src/deployer Executable file

Binary file not shown.

BIN
src/general.db Normal file

Binary file not shown.

View File

@ -21,20 +21,7 @@ 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(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" which is the notation for loopback in IpV6
cout << "service " << serviceUsername << " on server" << endl;
return 0;
}
cout << "service" << serviceUsername << " not on server" << endl;
return 1;
return 0;
}
int createUser(string serviceUsername)
{
@ -44,6 +31,7 @@ int createUser(string serviceUsername)
Services services = Services();
const Service * service = services.FindByUsername(serviceUsername);
int uid = (*service).GetUserID()+uidStart;
cout << "uid vaut" << uid << endl;
//test if user already exists
string cmd = "id -u "+serviceUsername;
string res = BashManager::ExecuteAndReadResult (cmd);
@ -60,6 +48,7 @@ int createUser(string serviceUsername)
cerr << res2 << endl;
return -1;
}
cout << "user created " << endl;
return 0;
}
@ -255,14 +244,13 @@ int deployService(string serviceUsername){
for(Module * mod_ptr : modules){
int modResult = (*mod_ptr).Deploy(serviceUsername);
if (modResult!=0){
//cerr << "Error in " << (*mod_ptr) << " when deploying " << serviceUsername << endl;
cerr << "Error in module " << endl;
}
}
return 0;
cerr << "Error creating user and environment of "<<serviceUsername << endl;
}
return 0;
}
@ -274,6 +262,7 @@ int deployAll()
createEnv();
//initializing modules
for(Module * mod_ptr : modules){
cout << *mod_ptr << endl;
int modPrep = (*mod_ptr).Prepare();
int modClean = (*mod_ptr).Clean();
if ((modPrep && modClean) !=0){