Compare commits
2 Commits
d81176ffa6
...
20a144eea1
Author | SHA1 | Date | |
---|---|---|---|
|
20a144eea1 | ||
|
45320747b5 |
BIN
bin/deployer
BIN
bin/deployer
Binary file not shown.
49
src/DockerModule.cpp
Normal file
49
src/DockerModule.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
// deployer DockerModule implementation
|
||||
// Copyright (C) 2024 Jean-Cloud
|
||||
// GNU General Public License v3
|
||||
|
||||
#include "DockerModule.h"
|
||||
|
||||
//constructor and destructor inline
|
||||
|
||||
//public methods
|
||||
int DockerModule::prepare()
|
||||
{
|
||||
return 0;
|
||||
//nothing to do ?
|
||||
}
|
||||
|
||||
int DockerModule::deploy (Service service)
|
||||
{
|
||||
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 ()
|
||||
{
|
||||
return 0;
|
||||
}
|
20
src/DockerModule.h
Normal file
20
src/DockerModule.h
Normal file
@ -0,0 +1,20 @@
|
||||
// deployer DockerModule header
|
||||
// Copyright (C) 2024 Jean-Cloud
|
||||
// GNU General Public License v3
|
||||
|
||||
#if !defined(DOCKERMODULE_H)
|
||||
#define DOCKERMODULE_H
|
||||
|
||||
#include "Module.h"
|
||||
|
||||
class DockerModule : public Module
|
||||
{
|
||||
public:
|
||||
DockerModule(){} //inline
|
||||
~DockerModule(){} //inline
|
||||
int prepare ();
|
||||
int deploy (Service service);
|
||||
int clean ();
|
||||
};
|
||||
|
||||
#endif
|
12
src/Makefile
12
src/Makefile
@ -2,15 +2,15 @@ ECHO = @echo
|
||||
GCC = g++
|
||||
RM = @rm -f
|
||||
CCFLAGS = -c -O3 -g -std=c++11 -pedantic -Wall
|
||||
OBJETS = $(SRC:.cpp=.o)
|
||||
SRC = $(wildcard *.cpp)
|
||||
EXE = deployer
|
||||
BIN = ../bin
|
||||
OBJETS_DIR = $(OBJETS:%=$(BIN)/%)
|
||||
EXE_DIR = $(EXE:%=$(BIN)/%)
|
||||
EXE = deployer
|
||||
OBJECTS = $(patsubst %.cpp,$(BIN)/%.o,$(SRC))
|
||||
LIBRARIES =
|
||||
|
||||
$(EXE_DIR) : $(OBJETS_DIR)
|
||||
$(EXE) : $(OBJECTS)
|
||||
$(ECHO) "objects $(OBJECTS) exe $(EXE) "
|
||||
|
||||
$(ECHO) "-Linking $(EXE)-"
|
||||
$(GCC) -o $@ $^ $(LIBRARIES)
|
||||
|
||||
@ -21,4 +21,4 @@ $(BIN)/%.o:%.cpp
|
||||
.PHONY: clean
|
||||
clean:
|
||||
$(ECHO) "-Cleaning-"
|
||||
$(RM) $(OBJETS) $(EXE)
|
||||
$(RM) $(OBJECTS) $(EXE)
|
@ -2,9 +2,35 @@
|
||||
// Copyright (C) 2024 Jean-Cloud
|
||||
// GNU General Public License v3
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include "Module.h"
|
||||
|
||||
Module::Module(char * serv):service(serv){}
|
||||
//constructor inline
|
||||
//destructor, could not be inline because pure virtual
|
||||
Module::~Module(){}
|
||||
|
||||
//protected methods (unprotected for tests purposes)
|
||||
int Module::isserviceOnServer()
|
||||
//this method tests if a certain service is on the current server
|
||||
//it uses the bash command dig, thanks to a pipe to a separate bash process
|
||||
{
|
||||
FILE *p;
|
||||
char result [100];
|
||||
|
||||
p = popen("ls -la","r"); //just to test that popen works
|
||||
if( p == NULL)
|
||||
{
|
||||
cout << "Unable to verify if the service is on the server, bash process opening error." << endl;
|
||||
return -1;
|
||||
}
|
||||
while(fgets(result,100,p)!=NULL){
|
||||
//if several lines, copy result somewhere to save it
|
||||
cout << result << endl;
|
||||
}
|
||||
pclose(p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
18
src/Module.h
18
src/Module.h
@ -6,16 +6,18 @@
|
||||
#define MODULE_H
|
||||
|
||||
#include <iostream>
|
||||
#include "Service.h"
|
||||
|
||||
class Module
|
||||
{
|
||||
protected:
|
||||
Module(char * serv);
|
||||
virtual ~Module();
|
||||
|
||||
private:
|
||||
bool activated; //true if the service requires this module
|
||||
char * service;
|
||||
|
||||
public:
|
||||
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 clean ()=0;
|
||||
//protected: commented for test purposes
|
||||
virtual int isserviceOnServer();
|
||||
};
|
||||
#endif
|
31
src/Service.cpp
Normal file
31
src/Service.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
//deployer Service implementation
|
||||
// Copyright (C) 2024 Jean-Cloud
|
||||
// GNU General Public License v3
|
||||
|
||||
#include "Service.h"
|
||||
|
||||
//constructor and destructor defined inline
|
||||
|
||||
//public methods
|
||||
int Service::getUserID () const
|
||||
{
|
||||
return userID;
|
||||
}
|
||||
string Service::getUsername() const
|
||||
{
|
||||
return username;
|
||||
}
|
||||
list<string> Service::getServers() const
|
||||
{
|
||||
return servers;
|
||||
}
|
||||
|
||||
//operator << override
|
||||
ostream & operator << (ostream & out, const Service & s)
|
||||
{
|
||||
out << "userID: " << s.userID << endl << "username: " << s.username << endl << "servers: ";
|
||||
for (string server : s.servers){
|
||||
out << server << " " << endl;
|
||||
}
|
||||
return out;
|
||||
}
|
28
src/Service.h
Normal file
28
src/Service.h
Normal file
@ -0,0 +1,28 @@
|
||||
//deployer Service header
|
||||
// Copyright (C) 2024 Jean-Cloud
|
||||
// GNU General Public License v3
|
||||
|
||||
#if !defined(SERVICE_H)
|
||||
#define SERVICE_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <string>
|
||||
#include <list>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Service {
|
||||
public:
|
||||
Service(int aUserID, string aUsername, list <string> aServers):userID(aUserID),username(aUsername),servers(aServers){}
|
||||
~Service(){}
|
||||
int getUserID () const;
|
||||
string getUsername() const;
|
||||
list<string> getServers() const;
|
||||
friend ostream & operator<<(ostream & out, const Service & s);
|
||||
private:
|
||||
int userID;
|
||||
string username;
|
||||
list <string> servers;
|
||||
};
|
||||
#endif
|
@ -9,18 +9,59 @@
|
||||
#include <string>
|
||||
#include "Services.h"
|
||||
|
||||
//constructor
|
||||
Services::Services(string ServicesCSV)
|
||||
{
|
||||
services=readServicesFromCSV(ServicesCSV);
|
||||
}
|
||||
//destructor
|
||||
Services::~Services(){}
|
||||
|
||||
//public methods
|
||||
vector<Service> Services::getServices()const
|
||||
{
|
||||
return services;
|
||||
}
|
||||
|
||||
vector <serviceData> Services::readServicesFromCSV (string CSV) const
|
||||
const Service* Services::findByUsername(string aUsername) const
|
||||
{
|
||||
for (const Service & service : services){
|
||||
if (service.getUsername().compare(aUsername)==0){
|
||||
return &service;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const Service * Services::findByID(int aUserID) const
|
||||
{
|
||||
for (const Service & service : services){
|
||||
if (service.getUserID()==aUserID){
|
||||
return &service;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
list<const Service*> Services::findByServer(string aServer) const
|
||||
{
|
||||
list<const Service*> result;
|
||||
for (const Service & service : services){
|
||||
for (string server : service.getServers()){
|
||||
if(server.compare(aServer)==0){
|
||||
result.push_back(&service);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//private methods
|
||||
vector <Service> Services::readServicesFromCSV (string CSV) const
|
||||
{
|
||||
//this method extracts the list of uid|username|servers from the services.csv file
|
||||
//and returns them in a vector <serviceData>, with serviceData a structure defined in the header
|
||||
vector <serviceData> result;
|
||||
//and returns them in a vector <Service>, with Service a structure defined in the header
|
||||
vector <Service> result;
|
||||
ifstream streamServices(CSV);
|
||||
if (!streamServices){
|
||||
cout << "Invalid services.csv file." << endl;
|
||||
@ -29,8 +70,8 @@ vector <serviceData> Services::readServicesFromCSV (string CSV) const
|
||||
string tmpUserID; //used before converting to int
|
||||
int userID;
|
||||
string username;
|
||||
string serveur;
|
||||
list <string> serveurs;
|
||||
string server;
|
||||
list <string> servers;
|
||||
while(getline(streamServices,line)){
|
||||
if (line.empty() || line[0] == '#') { //not taking comments and empty lines into account
|
||||
continue;
|
||||
@ -39,12 +80,13 @@ vector <serviceData> Services::readServicesFromCSV (string CSV) const
|
||||
getline(streamLine,tmpUserID,';'); //extracting the userID
|
||||
userID=stoi(tmpUserID);
|
||||
getline(streamLine,username,';'); //extracting the username
|
||||
while(getline(streamLine,serveur,';')){ //extracting the server(s)
|
||||
serveurs.push_back(serveur);
|
||||
while(getline(streamLine,server,';')){ //extracting the server(s)
|
||||
servers.push_back(server);
|
||||
}
|
||||
serviceData entry = {userID,username,serveurs};
|
||||
Service entry = {userID,username,servers};
|
||||
result.push_back(entry);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,24 +9,23 @@
|
||||
#include <cstring>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include "Service.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct serviceData {
|
||||
int userID;
|
||||
string username;
|
||||
list <string> serveurs;
|
||||
};
|
||||
|
||||
class Services
|
||||
//extracts the list of uid|username|service from the services.csv file
|
||||
{
|
||||
public:
|
||||
Services(string servicesCSV="../src/services.csv");
|
||||
vector<Service> getServices() const;
|
||||
const Service * findByUsername(string aUsername) const;
|
||||
const Service * findByID(int aUserID) const;
|
||||
list<const Service*> findByServer(string aServer) const;
|
||||
~Services();
|
||||
private:
|
||||
vector <serviceData> readServicesFromCSV (string CSV) const;
|
||||
vector <serviceData> services;
|
||||
|
||||
vector <Service> readServicesFromCSV (string CSV) const;
|
||||
vector <Service> services;
|
||||
};
|
||||
#endif
|
BIN
src/deployer
Executable file
BIN
src/deployer
Executable file
Binary file not shown.
@ -7,12 +7,13 @@
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include "Services.h"
|
||||
#include "Module.h"
|
||||
#include "DockerModule.h"
|
||||
using namespace std;
|
||||
|
||||
bool action_deploy=false;
|
||||
bool action_remove=false;
|
||||
bool all=false;
|
||||
//list<Module> modules;
|
||||
|
||||
void help()
|
||||
{
|
||||
@ -36,8 +37,9 @@ int main(int argc, char *argv[])
|
||||
cout << "Invalid argument. \n" << endl;
|
||||
exit(1);
|
||||
}
|
||||
Services myServices=Services("../src/services.csv");
|
||||
|
||||
|
||||
DockerModule dmodule;
|
||||
dmodule.isserviceOnServer();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user