262 lines
8.1 KiB
PHP
262 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\DatastorePcc;
|
|
use App\Models\DomainName;
|
|
use App\Models\GitRepo;
|
|
use App\Models\LDCNetwork;
|
|
use App\Models\Project;
|
|
use App\Models\Server;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class ExportDatas extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'datas:export';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Export data in Excel format';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function handle()
|
|
{
|
|
|
|
$this->exportProjects("EDL_WEB.csv");
|
|
$this->exportServers("EDL_Servers.csv");
|
|
$this->exportDatastorePcc("EDL_Datastore_pcc.csv");
|
|
$this->exportDomainNames("EDL_Domain_Names.csv");
|
|
$this->exportLdcNetworks("EDL_LDC_Networks.csv");
|
|
$this->exportGitRepos("EDL_GIT_Repos.csv");
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Export git repos as filename.csv
|
|
*
|
|
* @param string $filename
|
|
* @return void
|
|
*/
|
|
private function exportGitRepos(string $filename)
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\GitRepo> */
|
|
$repos = GitRepo::orderBy('name', 'ASC')->get();
|
|
Storage::disk('export')->append($filename, "Nom");
|
|
foreach ($repos as $repo) {
|
|
$infos = [
|
|
trim($repo->name),
|
|
];
|
|
|
|
Storage::disk('export')->append($filename, implode(";", $infos));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Export ldc networks as filename.csv
|
|
*
|
|
* @param string $filename
|
|
* @return void
|
|
*/
|
|
private function exportLdcNetworks(string $filename)
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\LDCNetwork> */
|
|
$networks = LDCNetwork::all();
|
|
Storage::disk('export')->append($filename, "CIDR;First IP;Last IP");
|
|
foreach ($networks as $network) {
|
|
$infos = [
|
|
trim($network->cidr),
|
|
trim($network->first_ip),
|
|
trim($network->last_ip),
|
|
];
|
|
|
|
Storage::disk('export')->append($filename, implode(";", $infos));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Export domain names as filename.csv
|
|
*
|
|
* @param string $filename
|
|
* @return void
|
|
*/
|
|
private function exportDomainNames(string $filename)
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\DomainName> */
|
|
$domains = DomainName::all();
|
|
Storage::disk('export')->append(
|
|
$filename,
|
|
"ASCII;UTF-8;DATE D'EXPIRATION;RENOUVELLEMENT;MARQUE REF.;DNS REDIRIGES;TYPE CERTIFICATS;COMMENTAIRE"
|
|
);
|
|
foreach ($domains as $domain) {
|
|
$infos = [
|
|
trim($domain->ascii_name),
|
|
trim($domain->utf8_name),
|
|
trim($domain->expiration_date),
|
|
$domain->renew === 1 ? 'OUI' : 'NON',
|
|
trim($domain->related_brand),
|
|
trim($domain->redirect_dns),
|
|
trim($domain->certificats),
|
|
trim($domain->comment),
|
|
];
|
|
|
|
Storage::disk('export')->append($filename, implode(";", $infos));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Export data store pcc as filename.csv
|
|
*
|
|
* @param string $filename
|
|
* @return void
|
|
*/
|
|
private function exportDatastorePcc(string $filename)
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\DatastorePcc> */
|
|
$datastores = DatastorePcc::all();
|
|
Storage::disk('export')->append($filename, "NOM;COMMENTAIRE");
|
|
foreach ($datastores as $datastore) {
|
|
$infos = [
|
|
trim($datastore->title),
|
|
trim($datastore->comment)
|
|
];
|
|
|
|
Storage::disk('export')->append($filename, implode(";", $infos));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Export servers as filename.csv
|
|
*
|
|
* @param string $filename
|
|
* @return void
|
|
*/
|
|
private function exportServers(string $filename)
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Server> */
|
|
$servers = Server::orderBy('env', 'DESC')->get();
|
|
Storage::disk('export')->append(
|
|
$filename,
|
|
"ENV;NOM;TITRE;FUNCTION;SSH;IP INFRAWEB;VIRTUAL IP;DATASTORE PCC;COMMENTAIRE"
|
|
);
|
|
foreach ($servers as $server) {
|
|
$infos = [
|
|
trim($server->env),
|
|
trim($server->name),
|
|
trim($server->title),
|
|
trim($server->function),
|
|
trim($server->ssh),
|
|
trim($server->infraweb_ip),
|
|
trim($server->virtual_ip),
|
|
!is_null($server->datastore_pcc_id) ? trim(DatastorePcc::find($server->datastore_pcc_id)->title) : '',
|
|
trim($server->comment)
|
|
];
|
|
|
|
Storage::disk('export')->append($filename, implode(";", $infos));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Export projects as filename.csv
|
|
*
|
|
* @param string $filename
|
|
* @return void
|
|
*/
|
|
private function exportProjects(string $filename)
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Project> */
|
|
$projects = Project::orderBy('name', 'ASC')->get();
|
|
$fields = [
|
|
'DEV',
|
|
'name',
|
|
'dev_haproxy_id',
|
|
'dev_url',
|
|
'https',
|
|
'php',
|
|
'dev_web_server_id',
|
|
'dev_unix_user',
|
|
'dev_unix_pwd',
|
|
'dev_document_root',
|
|
'dev_db_server_id',
|
|
'dev_db_schema',
|
|
'dev_db_user',
|
|
'dev_db_pwd',
|
|
'dev_htaccess_user',
|
|
'dev_htaccess_pwd',
|
|
'dev_comment',
|
|
'dev_eai',
|
|
'dev_whitelist_fw',
|
|
];
|
|
|
|
Storage::disk('export')->append(
|
|
$filename,
|
|
// phpcs:ignore
|
|
"ENV;NOM;ID HAPROXY;URL;HTTPS;PHP;SERVEUR WEB;SSH;USER UNIX;PWD UNIX;DOCUMENT ROOT;SERVEUR BDD;SSH;USER DB;PWD DB;HTACCESS USER;HTACCESS PWD;COMMENTAIRE;FLUX EAI;WHITELIST FW"
|
|
);
|
|
$this->writeProjectExport($projects, $fields, $filename);
|
|
|
|
$prod = implode(";", $fields);
|
|
$prod = str_replace("dev", "prod", $prod);
|
|
$prod = str_replace("DEV", "PROD", $prod);
|
|
$prod = str_replace("prod_htaccess_user", "BLANK", $prod);
|
|
$prod = str_replace("prod_htaccess_pwd", "BLANK", $prod);
|
|
$fields = explode(";", $prod);
|
|
|
|
$this->writeProjectExport($projects, $fields, $filename);
|
|
}
|
|
|
|
/**
|
|
* Append Project to filename.csv
|
|
*
|
|
* @param Collection $projects
|
|
* @param string[] $fields
|
|
* @param string $filename
|
|
* @return void
|
|
*/
|
|
private function writeProjectExport(Collection $projects, array $fields, string $filename)
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Project> $projects */
|
|
foreach ($projects as $project) {
|
|
$infos = [];
|
|
foreach ($fields as $field) {
|
|
switch ($field) {
|
|
case 'DEV':
|
|
case 'PROD':
|
|
$infos[] = $field;
|
|
break;
|
|
case 'https':
|
|
$infos[] = $project->{$field} === 1 ? 'OUI' : 'NON';
|
|
break;
|
|
case 'dev_web_server_id':
|
|
case 'dev_db_server_id':
|
|
case 'prod_web_server_id':
|
|
case 'prod_db_server_id':
|
|
$server = Server::find($project->{$field});
|
|
$infos[] = trim($server->name);
|
|
$infos[] = trim($server->ssh);
|
|
break;
|
|
case 'BLANK':
|
|
$infos[] = "";
|
|
break;
|
|
default:
|
|
$infos[] = trim($project->{$field});
|
|
}//end switch
|
|
}//end foreach
|
|
Storage::disk('export')->append($filename, implode(";", $infos));
|
|
}//end foreach
|
|
}
|
|
}
|