Nodelist import

This commit is contained in:
Deon George 2019-04-26 14:30:00 +10:00
parent 5753982a8d
commit 6515c91270
12 changed files with 488 additions and 4 deletions

View File

@ -0,0 +1,172 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\{Flag,Node,Zone};
class NodelistImport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'nodelist:import {file : Nodelist File}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import Nodelist';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$file = $this->argument('file');
$lines = intval(exec("wc -l '$file'"));
$file = fopen($file,"r");
$bar = $this->output->createProgressBar($lines);
$bar->setFormat("%current%/%max% [%bar%] %percent:3s%% (%memory%) (%remaining%) ");
$bar->setRedrawFrequency(100);
$bar->start();
$zone = $region = $host = NULL;
while (! feof($file)) {
$line = stream_get_line($file, 0, "\r\n");
// Lines beginning with a semicolon(;) are comments
if (preg_match('/^;/',$line) OR ($line == chr(0x1A)))
continue;
$fields = explode(',',$line);
$o = new Node();
// First field is either zone,region,host,hub,down,pvt (or blank)
if ($fields[0] AND ! in_array($fields[0],['Zone','Region','Host','Hub','Pvt','Down']))
{
$this->error(sprintf('Invalid field zero [%s] - IGNORING record (%s)',$fields[0],$line));
$bar->advance();
continue;
}
$status = NULL;
$node = 0;
switch ($fields[0])
{
case 'Zone': $zone = $fields[1];
$zo = Zone::firstOrCreate([
'id'=>$zone
]);
break;
case 'Host': $host = $fields[1]; break;
case 'Region': $region = $fields[1]; break;
case 'Down':
case 'Pvt': $status = $fields[0];
case '':
$node = $fields[1];
break;
default:
$this->error(sprintf('Unhandled first field [%s]',$fields[0]));
$bar->advance();
continue;
}
if (! $zone)
{
$this->error('Zone NOT set, ignoring record...');
$bar->advance();
continue;
}
if (! $host)
{
$host = $zone;
}
if (! $region)
{
$region = 0 ;
}
$o = Node::firstOrNew([
'zone_id'=>$zo->id,
'host_id'=>$host,
'node_id'=>$node,
]);
$o->region_id = $region;
$o->system = $fields[2];
$o->location = $fields[3];
$o->sysop = $fields[4];
$o->active = TRUE;
$o->status = $status;
if (! in_array($fields[5],['-Unpublished-']))
$o->phone = $fields[5];
$o->baud = $fields[6];
$o->save();
// Fields 7 onwards are flags
$flags = collect();
for ($i=7;$i<count($fields);$i++)
{
$value = NULL;
if (preg_match('/^[A-Z]+:/',$fields[$i]))
{
list($flag,$value) = explode(':',$fields[$i],2);
} else {
$flag = $fields[$i];
}
$fo = Flag::firstOrNew(
['flag'=>$flag]
);
if (! $flag)
{
$this->warn('Ignoring blank flag on: '.$line);
continue;
}
if (! $fo->exists)
{
$fo->description = 'Auto Created on Import';
$fo->save();
}
$flags->put($fo->id,['arguments'=>$value]);
}
$o->flags()->sync($flags);
$bar->advance();
}
fclose($file);
$bar->finish();
echo "\n";
}
}

10
app/Models/Flag.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Flag extends Model
{
protected $fillable = ['flag'];
}

21
app/Models/Node.php Normal file
View File

@ -0,0 +1,21 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Node extends Model
{
protected $fillable = ['zone_id','host_id','node_id'];
public function flags() {
return $this->belongsToMany(Flag::class);
}
public function hasFlag($relation, $model)
{
return (bool) $this->{$relation}()
->wherePivot($model->getForeignKey(),$model->{$model->getKeyName()})
->count();
}
}

10
app/Models/Zone.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Zone extends Model
{
protected $fillable = ['id'];
}

View File

@ -8,7 +8,8 @@
"php": "^7.1.3", "php": "^7.1.3",
"fideloper/proxy": "^4.0", "fideloper/proxy": "^4.0",
"laravel/framework": "5.7.*", "laravel/framework": "5.7.*",
"laravel/tinker": "^1.0" "laravel/tinker": "^1.0",
"nbj/cockroachdb-laravel": "^0.0.6@alpha"
}, },
"require-dev": { "require-dev": {
"beyondcode/laravel-dump-server": "^1.0", "beyondcode/laravel-dump-server": "^1.0",

53
composer.lock generated
View File

@ -1,10 +1,10 @@
{ {
"_readme": [ "_readme": [
"This file locks the dependencies of your project to a known state", "This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "66ca7343889332c2b8ecca11c251430c", "content-hash": "f4fa68dee51390d0fc4c5887b35c10f6",
"packages": [ "packages": [
{ {
"name": "dnoegel/php-xdg-base-dir", "name": "dnoegel/php-xdg-base-dir",
@ -821,6 +821,51 @@
], ],
"time": "2018-11-05T09:00:11+00:00" "time": "2018-11-05T09:00:11+00:00"
}, },
{
"name": "nbj/cockroachdb-laravel",
"version": "0.0.6-alpha",
"source": {
"type": "git",
"url": "https://github.com/nbj/cockroachdb-laravel.git",
"reference": "02f3239ca87dd1ee5aecf1eb8b0ddbd5819ffb09"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nbj/cockroachdb-laravel/zipball/02f3239ca87dd1ee5aecf1eb8b0ddbd5819ffb09",
"reference": "02f3239ca87dd1ee5aecf1eb8b0ddbd5819ffb09",
"shasum": ""
},
"type": "package",
"extra": {
"laravel": {
"providers": [
"Nbj\\Cockroach\\CockroachServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Nbj\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nikolaj Boel Jensen",
"email": "nbj@cego.dk"
}
],
"description": "CockroachDB driver for Laravel 5.6",
"keywords": [
"cockroach",
"cockroachdb",
"laravel"
],
"time": "2019-01-22T10:01:40+00:00"
},
{ {
"name": "nesbot/carbon", "name": "nesbot/carbon",
"version": "1.34.4", "version": "1.34.4",
@ -4125,7 +4170,9 @@
], ],
"aliases": [], "aliases": [],
"minimum-stability": "dev", "minimum-stability": "dev",
"stability-flags": [], "stability-flags": {
"nbj/cockroachdb-laravel": 15
},
"prefer-stable": true, "prefer-stable": true,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {

View File

@ -82,6 +82,25 @@ return [
'prefix_indexes' => true, 'prefix_indexes' => true,
], ],
'cockroach' => [
'driver' => 'cockroach',
'host' => env('DB_HOST', 'HOSTNAME-OF-COCKROACH-SERVER'),
'port' => env('DB_PORT', '26257'),
'database' => env('DB_DATABASE', 'DATABASE-NAME'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
// Only set these keys if you want to run en secure mode
// otherwise you can them out of the configuration array
#'sslcert' => env('DB_SSLCERT', 'client.crt'),
#'sslkey' => env('DB_SSLKEY', 'client.key'),
#'sslrootcert' => env('DB_SSLROOTCERT', 'ca.crt'),
],
], ],
/* /*

View File

@ -0,0 +1,33 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateZt extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('zt', function (Blueprint $table) {
$table->binary('id',10)->unique();
$table->timestamps();
$table->text('api');
$table->binary('token',24);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('zt');
}
}

View File

@ -0,0 +1,41 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateZones extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('zones', function (Blueprint $table) {
$table->integer('id')->primary();
$table->timestamps();
$table->binary('zt_id',10)->nullable();
$table->binary('ztnet',6)->nullable();
$table->ipAddress('ipv4')->nullable();
$table->integer('ipv4_mask')->nullable();
$table->ipAddress('ipv6')->nullable();
$table->integer('ipv6_mask')->nullable();
$table->unique('zt_id');
$table->foreign('zt_id')->references('id')->on('zt');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('zones');
}
}

View File

@ -0,0 +1,57 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateNodes extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('nodes', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('zone_id')->index();
$table->integer('region_id');
$table->integer('host_id')->index();
$table->integer('hub_id')->nullable()->index();
$table->integer('node_id')->index();
$table->binary('active');
$table->string('status')->nullable();
$table->string('system');
$table->string('sysop');
$table->string('location');
$table->integer('baud');
$table->string('phone')->nullable();
$table->binary('zt',10)->nullable();
$table->unique(['zone_id','region_id','id']);
$table->unique(['zone_id','zt']);
// $table->index('zone_id');
$table->foreign('zone_id')->references('id')->on('zones');
$table->unique(['zone_id','host_id','id']);
// $table->index(['zone_id','host_id']);
// $table->index(['zone_id','id']);
// $table->foreign(['zone_id','host_id'])->references(['zone_id','id'])->on('nodes');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('nodes');
}
}

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFlags extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::dropIfExists('flags');
Schema::create('flags', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('flag')->unique();
$table->string('description');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('flags');
}
}

View File

@ -0,0 +1,39 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFlagNode extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::dropIfExists('flag_node');
Schema::create('flag_node', function (Blueprint $table) {
$table->integer('flag_id');
$table->integer('node_id');
$table->string('arguments')->nullable();
$table->index('node_id');
$table->index('flag_id');
$table->unique(['node_id','flag_id']);
$table->foreign('node_id')->references('id')->on('nodes');
$table->foreign('flag_id')->references('id')->on('flags');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('flag_node');
}
}