Support processing nodelists from file repository and assume nodelists are zip fiels

This commit is contained in:
Deon George
2022-11-04 22:26:08 +11:00
parent 2790381a30
commit b45e2c6dd8
3 changed files with 48 additions and 34 deletions

View File

@@ -12,7 +12,7 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use App\Models\{Address,Domain,Nodelist,Setup,System,Zone};
use App\Models\{Address,Domain,File,Nodelist,Setup,System,Zone};
use App\Traits\Import as ImportTrait;
class NodelistImport implements ShouldQueue
@@ -23,18 +23,18 @@ class NodelistImport implements ShouldQueue
protected const LOGKEY = 'JNI';
private const importkey = 'nodelist';
private string $file;
private File $file;
private bool $delete_file;
private bool $delete_recs;
/**
* Import Nodelist constructor.
*
* @param string $file
* @param File $file
* @param bool $delete_recs
* @param bool $delete_file
*/
public function __construct(string $file,bool $delete_recs=FALSE,bool $delete_file=TRUE)
public function __construct(File $file,bool $delete_recs=FALSE,bool $delete_file=TRUE)
{
$this->file = $file;
$this->delete_file = $delete_file;
@@ -52,19 +52,22 @@ class NodelistImport implements ShouldQueue
$us = Setup::findOrFail(config('app.id'));
// Get the file from the host
$file = $this->getFileFromHost('',self::importkey,$this->file);
$file = $this->getFileFromHost(self::importkey,$this->file);
Log::debug(sprintf('%s:Loading file [%s] (%s).',static::LOGKEY,$file,getcwd()));
$lines = $this->getFileLines($file);
Log::debug(sprintf('%s:Processing [%d] lines.',static::LOGKEY,$lines));
$fh = fopen($file,'r');
$fh = NULL;
$z = $this->openFile($file,$fh);
// Line 1 tells us the nodelist and the CRC
$line = stream_get_line($fh, 0, "\r\n");
$line = stream_get_line($fh,0,"\r\n");
$matches = [];
if ((! preg_match('/^;A\ /',$line)) || (! preg_match('/^;A\ (.*)\ Nodelist for ([MTWFS][a-z]+,\ [JFMASOND][a-z]+\ [0-9]{1,2},\ [0-9]{4})\ --\ Day\ number\ ([0-9]+)\ :\ ([0-9a-f]+)$/',$line,$matches)))
abort(500,'Nodelist invalid');
if ((! preg_match('/^;A\ /',$line)) || (! preg_match('/^;A\ (.*)\ Nodelist for ([MTWFS][a-z]+,\ [JFMASOND][a-z]+\ [0-9]{1,2},\ [0-9]{4})\ --\ Day\ number\ ([0-9]+)\ :\ ([0-9a-f]+)$/',$line,$matches))) {
Log::error(sprintf('Nodelist file [%d] is not valid?',$this->file->id),['m'=>$matches,'l'=>$line]);
throw new \Exception('Invalid nodelist for file '.$this->file->id);
}
$file_crc = $matches[4];
$do = Domain::where('name',strtolower($matches[1]))->single();
@@ -98,7 +101,7 @@ class NodelistImport implements ShouldQueue
$tocrc = '';
while (! feof($fh)) {
$line = stream_get_line($fh, 0, "\r\n");
$line = stream_get_line($fh,0,"\r\n");
$tocrc .= $line."\r\n";
// Lines beginning with a semicolon(;) are comments