Skip to content

Commit

Permalink
Merge pull request #4 from lbr38/dev
Browse files Browse the repository at this point in the history
corrections diverses
  • Loading branch information
lbr38 authored Jul 5, 2021
2 parents 32dea4e + bfc9535 commit 1ec9118
Show file tree
Hide file tree
Showing 29 changed files with 1,499 additions and 1,082 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ Conçu pour un usage en entreprise et pour faciliter le déploiement de mises à

<b>Principales fonctionnalités :</b>

- Créer des miroirs de repos, les mettre à jour, les dupliquer, les déployer sur les serveurs clients.
- Créer des miroirs de repos, les mettre à jour, les dupliquer, les rendre accessibles aux serveurs clients.
- Signer ses repos de paquets avec GPG.
- Système d'environnements (ex preprod, prod) permettant de rendre accessible les miroirs à des environnements de serveurs particuliers (nb d'environnement illimité).
- Planifications automatiques permettant d'exécuter les actions ci-dessus à n'importe quelle date/heure.
- Système d'environnements (ex: preprod, prod...) permettant de rendre accessible les miroirs à des environnements de serveurs particuliers
- Planifications automatiques permettant d'exécuter les actions ci-dessus à une date/heure souhaitée.

(voir tableau ci-dessous pour la liste complète)

![alt text](https://github.com/lbr38/repomanager/blob/beta/repomanager.png?raw=true)

Expand All @@ -23,9 +22,9 @@ Le CPU et la RAM sont essentiellement sollicités pendant la création de miroir
L'espace disque est à adapter en fonction du nombre de miroirs créés / nombre de paquets qu'ils contiennent.


<h1>Beta version</h1>
<h1>Version beta</h1>

Compatible avec les systèmes Redhat/CentOS et Debian/Ubuntu :
Installation compatible sur les systèmes Redhat/CentOS et Debian/Ubuntu :
- Debian 10, Ubuntu bionic
- CentOS 7, 8, Fedora 33

Expand Down Expand Up @@ -78,6 +77,7 @@ apt update && apt install nginx php-fpm php-cli php7.4-sqlite3 sqlite3
</pre>

<b>SQLite</b>

S'assurer que l'extension sqlite pour php est activée (généralement dans /etc/php.d/) :

<pre>
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.0.2-beta
v2.0.3-beta
52 changes: 48 additions & 4 deletions www/class/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Log {
public $time;
public $name; // Nom complet du fichier de log (repomanager_... ou plan_...)
public $location; // Emplacement du fichier de log
public $title;
public $action;
public $pid;
public $steplog;

Expand All @@ -18,6 +18,8 @@ public function __construct(string $type) {
throw new Error('Erreur : le type de fichier de log ne peut pas être vide');
}

if (!empty($action)) { $this->action = $action; }

/**
* Génération d'un PID
*/
Expand Down Expand Up @@ -47,8 +49,11 @@ public function __construct(string $type) {
* Création du fichier PID
*/

touch("${PID_DIR}/{$this->pid}.pid");
file_put_contents("${PID_DIR}/{$this->pid}.pid", "PID=\"{$this->pid}\"LOG=\"$this->name\"");
//touch("${PID_DIR}/{$this->pid}.pid");
file_put_contents("${PID_DIR}/{$this->pid}.pid", "PID=\"{$this->pid}\"\nLOG=\"$this->name\"".PHP_EOL);
if (!empty($this->action)) {
file_put_contents("${PID_DIR}/{$this->pid}.pid", "ACTION=\"{$this->action}\"".PHP_EOL, FILE_APPEND);
}

/**
* Génération du fichier de log
Expand All @@ -69,16 +74,55 @@ public function __construct(string $type) {
exec("ln -sfn $this->location ${MAIN_LOGS_DIR}/lastlog.log");
}

/**
* Ajout d'un subpid au fichier de PID principal
*/
public function addsubpid(string $pid) {
global $PID_DIR;
file_put_contents("${PID_DIR}/{$this->pid}.pid", "SUBPID=\"${pid}\"".PHP_EOL, FILE_APPEND);
}

/**
* Ajout de l'action en cours de traitement au fichier de PID principal
*/
public function addaction(string $action) {
global $PID_DIR;
file_put_contents("${PID_DIR}/{$this->pid}.pid", "ACTION=\"${action}\"".PHP_EOL, FILE_APPEND);
}

/**
* Ajout de la cible en cours de traitement au fichier de PID principal
*/
public function addtarget(array $variables = []) {
global $PID_DIR;
global $OS_FAMILY;
extract($variables);

// Si la cible en cours de traitement est un groupe
if (!empty($group)) {
file_put_contents("${PID_DIR}/{$this->pid}.pid", "GROUP=\"${group}\"".PHP_EOL, FILE_APPEND);
}
// Si la cible en cours de traitement est un repo
if (!empty($name)) {
if ($OS_FAMILY == "Redhat") {
file_put_contents("${PID_DIR}/{$this->pid}.pid", "NAME=\"${name}\"".PHP_EOL, FILE_APPEND);
}
if ($OS_FAMILY == "Debian") {
file_put_contents("${PID_DIR}/{$this->pid}.pid", "NAME=\"${name}\"\nDIST=\"${dist}\"\nSECTION=\"${section}\"".PHP_EOL, FILE_APPEND);
}
}
}

public function write(string $content) {
file_put_contents($this->location, $content);
}

public function close() {
global $PID_DIR;

/**
* Suppression du fichier PID
*/

if (file_exists("${PID_DIR}/{$this->pid}.pid")) {
unlink("${PID_DIR}/{$this->pid}.pid");
}
Expand Down
63 changes: 37 additions & 26 deletions www/class/Planification.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,15 @@ public function exec() {
$this->checkAction_update_allowed();
$this->checkAction_update_gpgCheck();
$this->checkAction_update_gpgResign();
$this->log->addaction('update'); // ajout de l'action au fichier de PID principal
}

/**
* 6. Si l'action est '->' alors on vérifie que cette action est autorisée
*/
if (strpos($this->action, '->') !== false) {
$this->checkAction_env_allowed();
$this->log->addaction('->'); // ajout de l'action au fichier de PID principal
}

/**
Expand Down Expand Up @@ -229,40 +231,46 @@ public function exec() {

// TRAITEMENT //

/**
* 1. Cas où on traite 1 repo seulement
*/
if (!empty($this->repo->name) AND empty($this->group->name)) {
// Si $this->action = update alors on met à jour le repo
if ($this->action == "update") {
if ($this->repo->update() === false) {
$this->close(2, 'Une erreur est survenue pendant le traitement, voir les logs');
}
/**
* 1. Cas où on traite 1 repo seulement
*/
if (!empty($this->repo->name) AND empty($this->group->name)) {
// Ajout de la cible dans le fichier de PID principal de la planification
if ($OS_FAMILY == "Redhat") { $this->log->addtarget(array('name' => $this->repo->name)); }
if ($OS_FAMILY == "Debian") { $this->log->addtarget(array('name' => $this->repo->name, 'dist' => $this->repo->dist, 'section' => $this->repo->section)); }

// Si $this->action = update alors on met à jour le repo
if ($this->action == "update") {
if ($this->repo->update() === false) {
$this->close(2, 'Une erreur est survenue pendant le traitement, voir les logs');
}
}

// Si $this->action contient '->' alors il s'agit d'un changement d'env
if (strpos($this->action, '->') !== false) {
// Récupération de l'environnement source et de l'environnement cible
$this->repo->env = exec("echo '$this->action' | awk -F '->' '{print $1}'");
$this->repo->newEnv = exec("echo '$this->action' | awk -F '->' '{print $2}'");
if (empty($this->repo->env) OR empty($this->repo->newEnv)) {
$this->close(1, 'Erreur (EP04) : Environnement(s) non défini(s)'); // On sort avec 1 car on considère que c'est une erreur de type vérification
}
// Si $this->action contient '->' alors il s'agit d'un changement d'env
if (strpos($this->action, '->') !== false) {
// Récupération de l'environnement source et de l'environnement cible
$this->repo->env = exec("echo '$this->action' | awk -F '->' '{print $1}'");
$this->repo->newEnv = exec("echo '$this->action' | awk -F '->' '{print $2}'");
if (empty($this->repo->env) OR empty($this->repo->newEnv)) {
$this->close(1, 'Erreur (EP04) : Environnement(s) non défini(s)'); // On sort avec 1 car on considère que c'est une erreur de type vérification
}

// Traitement
if ($OS_FAMILY == "Redhat") { $this->log->title = 'NOUVEL ENVIRONNEMENT DE REPO'; }
if ($OS_FAMILY == "Debian") { $this->log->title = 'NOUVEL ENVIRONNEMENT DE SECTION'; }
if ($this->repo->changeEnv() === false) {
$this->close(2, 'Une erreur est survenue pendant le traitement, voir les logs');
}
// Traitement
if ($OS_FAMILY == "Redhat") { $this->log->title = 'NOUVEL ENVIRONNEMENT DE REPO'; }
if ($OS_FAMILY == "Debian") { $this->log->title = 'NOUVEL ENVIRONNEMENT DE SECTION'; }
if ($this->repo->changeEnv() === false) {
$this->close(2, 'Une erreur est survenue pendant le traitement, voir les logs');
}
}

}

/**
* 2. Cas où on traite un groupe de repos/sections
*/
if (!empty($this->group->name) AND !empty($this->groupList)) {
// Ajout de la cible dans le fichier de PID principal de la planification
$this->log->addtarget(array('group' => $this->group->name));

// Comme on boucle pour traiter plusieurs repos/sections, on ne peut pas tout quitter en cas d'erreur tant qu'on a pas bouclé sur tous les repos.
// Du coup on initialise une variable qu'on incrémentera en cas d'erreur.
// A la fin si cette variable > 0 alors on pourra quitter ce script en erreur ($this->close 1)
Expand Down Expand Up @@ -336,7 +344,7 @@ public function generateReminders() {
//$planFile = "plan-{$this->id}.conf";

/**
* 1. Récupération des informations de la planification toto
* 1. Récupération des informations de la planification
*/
$this->getInfo();

Expand Down Expand Up @@ -464,6 +472,9 @@ public function sendMail($title, $template) {
// Pour envoyer un mail HTML il faut inclure ces headers
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=utf8';
$headers[] = "From: noreply@${WWW_HOSTNAME}";
$headers[] = "X-Sender: noreply@${WWW_HOSTNAME}";
$headers[] = "Reply-To: noreply@${WWW_HOSTNAME}";
mail($EMAIL_DEST, $title, $template, implode("\r\n", $headers));
}

Expand Down Expand Up @@ -522,7 +533,7 @@ public function close($planError, $plan_msg_error) {
if (!empty($this->logList)) {
foreach ($this->logList as $log) {
$content = $content . file_get_contents($log);
// On supprime le sous-fichier de log puisque son contenu vient d'ere récupéré et qu'il sera intégré au fichier de log de la planification
// On supprime le sous-fichier de log puisque son contenu vient d'etre récupéré et qu'il sera intégré au fichier de log de la planification
unlink($log);
}
}
Expand Down
25 changes: 22 additions & 3 deletions www/class/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
include_once("${WWW_DIR}/class/inclusions/duplicate.php");
include_once("${WWW_DIR}/class/inclusions/deleteArchive.php");
include_once("${WWW_DIR}/class/inclusions/restore.php");
include_once("${WWW_DIR}/class/inclusions/cleanArchives.php");

class Repo {
public $db;
Expand Down Expand Up @@ -46,7 +47,7 @@ class Repo {
* Import des traits nécessaires pour les opérations sur les repos/sections
*/
use op_printDetails, op_getPackages, op_signPackages, op_createRepo, op_archive, op_finalize;
use changeEnv, duplicate, delete, deleteDist, deleteSection, deleteArchive, restore;
use changeEnv, duplicate, delete, deleteDist, deleteSection, deleteArchive, restore, cleanArchives;

public function __construct(array $variables = []) {
global $OS_FAMILY;
Expand Down Expand Up @@ -137,17 +138,26 @@ public function new() {
global $TEMP_DIR;
global $OS_FAMILY;
global $WWW_DIR;
global $PID_DIR;

/**
* Création d'un fichier de log principal + un fichier PID
*/
$this->log = new Log('repomanager');

/**
* Ajout du PID de ce processus dans le fichier PID ainsi que l'action effectuée et la cible (le repo)
*/
$this->log->addsubpid(getmypid());
$this->log->addaction('new');
if ($OS_FAMILY == "Redhat") { $this->log->addtarget(array('name' => $this->name)); }
if ($OS_FAMILY == "Debian") { $this->log->addtarget(array('name' => $this->name, 'dist' => $this->dist, 'section' => $this->section)); }

/**
* Lancement du script externe qui va construire le fichier de log principal à partir des petits fichiers de log de chaque étape
*/
$steps = 5;
exec("php ${WWW_DIR}/operations/check_running.php {$this->log->location} $TEMP_DIR/{$this->log->pid} $steps >/dev/null 2>/dev/null &");
exec("php ${WWW_DIR}/operations/check_running.php ${PID_DIR}/{$this->log->pid}.pid {$this->log->location} $TEMP_DIR/{$this->log->pid} $steps >/dev/null 2>/dev/null &");

try {
/**
Expand Down Expand Up @@ -199,17 +209,26 @@ public function update() {
global $TEMP_DIR;
global $OS_FAMILY;
global $WWW_DIR;
global $PID_DIR;

/**
* Création d'un fichier de log principal + un fichier PID
*/
$this->log = new Log('repomanager');

/**
* Ajout du PID de ce processus dans le fichier PID ainsi que l'action effectuée et la cible (le repo)
*/
$this->log->addsubpid(getmypid());
$this->log->addaction('update');
if ($OS_FAMILY == "Redhat") { $this->log->addtarget(array('name' => $this->name)); }
if ($OS_FAMILY == "Debian") { $this->log->addtarget(array('name' => $this->name, 'dist' => $this->dist, 'section' => $this->section)); }

/**
* Lancement du script externe qui va construire le fichier de log principal à partir des petits fichiers de log de chaque étape
*/
$steps = 6;
exec("php ${WWW_DIR}/operations/check_running.php {$this->log->location} ${TEMP_DIR}/{$this->log->pid} $steps >/dev/null 2>/dev/null &");
exec("php ${WWW_DIR}/operations/check_running.php ${PID_DIR}/{$this->log->pid}.pid {$this->log->location} ${TEMP_DIR}/{$this->log->pid} $steps >/dev/null 2>/dev/null &");

try {
/**
Expand Down
2 changes: 2 additions & 0 deletions www/class/inclusions/changeEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ public function changeEnv() {
}

echo '<p>Terminé <span class="greentext">✔</span></p>';

$this->cleanArchives();
}
}
?>
Loading

0 comments on commit 1ec9118

Please sign in to comment.