From 6515c91270763e99be1cd53d1c10d3adb40cdee5 Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 26 Apr 2019 14:30:00 +1000 Subject: [PATCH] Nodelist import --- app/Console/Commands/NodelistImport.php | 172 ++++++++++++++++++ app/Models/Flag.php | 10 + app/Models/Node.php | 21 +++ app/Models/Zone.php | 10 + composer.json | 3 +- composer.lock | 53 +++++- config/database.php | 19 ++ .../2019_04_16_105252_create_zt.php | 33 ++++ .../2019_04_16_105253_create_zones.php | 41 +++++ .../2019_04_16_105254_create_nodes.php | 57 ++++++ .../2019_04_16_105255_create_flags.php | 34 ++++ .../2019_04_16_105256_create_flag_node.php | 39 ++++ 12 files changed, 488 insertions(+), 4 deletions(-) create mode 100644 app/Console/Commands/NodelistImport.php create mode 100644 app/Models/Flag.php create mode 100644 app/Models/Node.php create mode 100644 app/Models/Zone.php create mode 100644 database/migrations/2019_04_16_105252_create_zt.php create mode 100644 database/migrations/2019_04_16_105253_create_zones.php create mode 100644 database/migrations/2019_04_16_105254_create_nodes.php create mode 100644 database/migrations/2019_04_16_105255_create_flags.php create mode 100644 database/migrations/2019_04_16_105256_create_flag_node.php diff --git a/app/Console/Commands/NodelistImport.php b/app/Console/Commands/NodelistImport.php new file mode 100644 index 0000000..dfef8fe --- /dev/null +++ b/app/Console/Commands/NodelistImport.php @@ -0,0 +1,172 @@ +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$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"; + } +} \ No newline at end of file diff --git a/app/Models/Flag.php b/app/Models/Flag.php new file mode 100644 index 0000000..bb4b444 --- /dev/null +++ b/app/Models/Flag.php @@ -0,0 +1,10 @@ +belongsToMany(Flag::class); + } + + public function hasFlag($relation, $model) + { + return (bool) $this->{$relation}() + ->wherePivot($model->getForeignKey(),$model->{$model->getKeyName()}) + ->count(); + } +} \ No newline at end of file diff --git a/app/Models/Zone.php b/app/Models/Zone.php new file mode 100644 index 0000000..cb47e12 --- /dev/null +++ b/app/Models/Zone.php @@ -0,0 +1,10 @@ + 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'), + ], + ], /* diff --git a/database/migrations/2019_04_16_105252_create_zt.php b/database/migrations/2019_04_16_105252_create_zt.php new file mode 100644 index 0000000..4859b05 --- /dev/null +++ b/database/migrations/2019_04_16_105252_create_zt.php @@ -0,0 +1,33 @@ +binary('id',10)->unique(); + $table->timestamps(); + $table->text('api'); + $table->binary('token',24); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('zt'); + } +} diff --git a/database/migrations/2019_04_16_105253_create_zones.php b/database/migrations/2019_04_16_105253_create_zones.php new file mode 100644 index 0000000..9e503f8 --- /dev/null +++ b/database/migrations/2019_04_16_105253_create_zones.php @@ -0,0 +1,41 @@ +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'); + } +} diff --git a/database/migrations/2019_04_16_105254_create_nodes.php b/database/migrations/2019_04_16_105254_create_nodes.php new file mode 100644 index 0000000..eb81cb1 --- /dev/null +++ b/database/migrations/2019_04_16_105254_create_nodes.php @@ -0,0 +1,57 @@ +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'); + } +} diff --git a/database/migrations/2019_04_16_105255_create_flags.php b/database/migrations/2019_04_16_105255_create_flags.php new file mode 100644 index 0000000..e6f2ff6 --- /dev/null +++ b/database/migrations/2019_04_16_105255_create_flags.php @@ -0,0 +1,34 @@ +increments('id'); + $table->timestamps(); + $table->string('flag')->unique(); + $table->string('description'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('flags'); + } +} diff --git a/database/migrations/2019_04_16_105256_create_flag_node.php b/database/migrations/2019_04_16_105256_create_flag_node.php new file mode 100644 index 0000000..b27d0f5 --- /dev/null +++ b/database/migrations/2019_04_16_105256_create_flag_node.php @@ -0,0 +1,39 @@ +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'); + } +}