Rework nodelist import and ignoring addresses that we manage

This commit is contained in:
Deon George 2023-10-04 23:42:58 +11:00
parent b854cf9fe0
commit 32c0088339
2 changed files with 83 additions and 39 deletions

View File

@ -36,13 +36,20 @@ class NodelistImport extends Command
*/ */
public function handle() public function handle()
{ {
try {
return Job::dispatchSync( return Job::dispatchSync(
is_numeric($x=$this->argument('file')) ? File::findOrFail($x) : $x, is_numeric($x=$this->argument('file'))
? File::findOrFail($x)
: sprintf('%s/%s',config('fido.dir'),$this->argument('file')),
$this->argument('domain'), $this->argument('domain'),
$this->option('delete'), $this->option('delete'),
$this->option('unlink'), $this->option('unlink'),
$this->option('test'), $this->option('test'),
$this->option('ignorecrc'), $this->option('ignorecrc'),
); );
} catch (\Exception $e) {
$this->error($e->getMessage());
}
} }
} }

View File

@ -13,7 +13,7 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use App\Models\{Address, Domain, File, Mailer, Nodelist, Setup, System, Zone}; use App\Models\{Address,Domain,File,Mailer,Nodelist,Setup,System,SystemZone,User,Zone};
use App\Traits\Import as ImportTrait; use App\Traits\Import as ImportTrait;
class NodelistImport implements ShouldQueue class NodelistImport implements ShouldQueue
@ -75,6 +75,26 @@ class NodelistImport implements ShouldQueue
public function handle() public function handle()
{ {
$us = Setup::findOrFail(config('app.id')); $us = Setup::findOrFail(config('app.id'));
$our_systems = SystemZone::select('system_id')
->get()
->pluck('system')
->flatten()
->pluck('addresses')
->flatten()
->filter(function($item) { return $item->active; })
->pluck('id');
$our_users = User::with(['systems.addresses'])
->get()
->pluck('systems')
->flatten()
->pluck('addresses')
->flatten()
->filter(function($item) { return $item->active; })
->pluck('id')
->diff($our_systems);
$our_addresses = $us->system->addresses->pluck('id');
// Get the file from the host // Get the file from the host
$file = $this->getFileFromHost(self::importkey,$this->file); $file = $this->getFileFromHost(self::importkey,$this->file);
@ -119,6 +139,11 @@ class NodelistImport implements ShouldQueue
if ($this->delete_recs) if ($this->delete_recs)
$no->addresses()->detach(); $no->addresses()->detach();
elseif ($no->addresses->count()) {
Log::error($x=sprintf('%s:! Nodelist [%s] for [%s] has existing records [%d]',self::LOGKEY,$date,$do->name,$no->addresses->count()));
throw new \Exception($x);
}
$p = $c = 0; $p = $c = 0;
@ -244,8 +269,31 @@ class NodelistImport implements ShouldQueue
$ao->role = $role; $ao->role = $role;
$ao->hub_id = $hub_id; $ao->hub_id = $hub_id;
if ($ao->exists) if ($ao->exists) {
Log::debug(sprintf('%s:- Processing existing address [%s]',self::LOGKEY,$ao->ftn)); Log::info(sprintf('%s:- Processing existing address [%s] (%d)',self::LOGKEY,$ao->ftn,$ao->id));
// If the address is linked to a user's system, or our system, we'll not process it any further
$skip = FALSE;
if ($our_addresses->contains($ao->id)) {
Log::info(sprintf('%s:! Ignoring updating an address belonging to me',self::LOGKEY));
$skip = TRUE;
}
if ($our_systems->contains($ao->id)) {
Log::info(sprintf('%s:! Ignoring a system managed by this site',self::LOGKEY));
$skip = TRUE;
}
if ($our_users->contains($ao->id)) {
Log::info(sprintf('%s:! Ignoring a system managed by a user',self::LOGKEY));
$skip = TRUE;
}
if ($skip) {
$no->addresses()->attach($ao,['role'=>$ao->role]);
continue;
}
}
$sysop = trim(str_replace('_',' ',$fields[4])); $sysop = trim(str_replace('_',' ',$fields[4]));
$system = trim(str_replace('_',' ',$fields[2])); $system = trim(str_replace('_',' ',$fields[2]));
@ -376,8 +424,6 @@ class NodelistImport implements ShouldQueue
Log::info(sprintf('%s:= Matched [%s] to existing system [%s] with address [%s]',self::LOGKEY,$ao->ftn,$ao->system->name,$ao->system->address)); Log::info(sprintf('%s:= Matched [%s] to existing system [%s] with address [%s]',self::LOGKEY,$ao->ftn,$ao->system->name,$ao->system->address));
$so = $ao->system; $so = $ao->system;
// Dont change the system details if a user exists here, or its us
if ((! $so->users->count()) && ($so->id != $us->system_id)) {
// If the sysop name is different // If the sysop name is different
if ($so->sysop !== $sysop) { if ($so->sysop !== $sysop) {
Log::alert(sprintf('%s:! Sysop Name changed for BBS [%s:%s] from [%s] to [%s]', Log::alert(sprintf('%s:! Sysop Name changed for BBS [%s:%s] from [%s] to [%s]',
@ -392,7 +438,6 @@ class NodelistImport implements ShouldQueue
$so->name = $system; $so->name = $system;
} }
}
// We'll search and see if we already have that system // We'll search and see if we already have that system
} else { } else {
@ -444,18 +489,6 @@ class NodelistImport implements ShouldQueue
$so->baud = $fields[6]; $so->baud = $fields[6];
*/ */
if ($so->getDirty()) {
if ($so->users->count()) {
Log::alert(sprintf('%s:! Refusing to update a system managed by a user',self::LOGKEY),['dirty'=>$so->getDirty()]);
continue;
}
if ($so->sessions()->count()) {
Log::alert(sprintf('%s:! Refusing to update a system configured here',self::LOGKEY),['dirty'=>$so->getDirty()]);
continue;
}
}
// Save the system record // Save the system record
try { try {
$so->save(); $so->save();
@ -505,16 +538,20 @@ class NodelistImport implements ShouldQueue
} }
// Remove addresses not recorded; // Remove addresses not recorded;
$remove = $zo->addresses->except($us->system->addresses->pluck('id')->toArray())->diff($no->addresses); $no->load('addresses');
$remove = $zo->addresses->diff($no->addresses)->except($our_systems->toArray())->except($our_users->toArray());
Log::alert(sprintf('%s:%% Deleting [%d] addresses [%s]',self::LOGKEY,$remove->count(),$remove->pluck('ftn2d')->join(','))); Log::alert(sprintf('%s:%% Deleting [%d] addresses [%s]',self::LOGKEY,$remove->count(),$remove->pluck('ftn2d')->join(',')));
Address::whereIN('id',$remove->pluck('id')->toArray())->update(['active'=>FALSE]); Address::whereIN('id',$remove->pluck('id')->toArray())->update(['active'=>FALSE]);
Address::whereIN('id',$remove->pluck('id')->toArray())->delete(); Address::whereIN('id',$remove->pluck('id')->toArray())->delete();
if ((! $this->testmode) && ($this->ignore_crc || ($x=crc16(substr($tocrc,0,-3))) === $file_crc)) { $crc = crc16(substr($tocrc,0,-3));
if ((! $this->testmode) && ($this->ignore_crc || ($crc === $file_crc))) {
Log::info(sprintf('%s:= Committing nodelist',self::LOGKEY)); Log::info(sprintf('%s:= Committing nodelist',self::LOGKEY));
DB::commit(); DB::commit();
} else { } else {
Log::error(sprintf('%s:! Rolling back nodelist, CRC doesnt match [%s](%s) or test mode',self::LOGKEY,$x,$file_crc)); Log::error(sprintf('%s:! Rolling back nodelist, CRC doesnt match [%s](%s) or test mode',self::LOGKEY,$crc,$file_crc));
DB::rollBack(); DB::rollBack();
} }