Skip to content

Commit

Permalink
Merge pull request #48 from Shadercloud/master
Browse files Browse the repository at this point in the history
Add tunneler:reset Command
  • Loading branch information
bubba-h57 authored May 19, 2021
2 parents 1c0b8ec + ddbce69 commit 62d09bb
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
45 changes: 45 additions & 0 deletions src/Console/TunnelerReset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
namespace STS\Tunneler\Console;

use Illuminate\Console\Command;
use STS\Tunneler\Jobs\CreateTunnel;

class TunnelerReset extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'tunneler:reset';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Destroy and reconnect the SSH tunnel';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$tunnel = new CreateTunnel();
$tunnel->destoryTunnel();

\Artisan::call('tunneler:activate');
}
}
13 changes: 11 additions & 2 deletions src/Jobs/CreateTunnel.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public function handle(): int
}

$this->createTunnel();

$tries = config('tunneler.tries');
for ($i = 0; $i < $tries; $i++) {
if ($this->verifyTunnel()) {
return 2;
}

// Wait a bit until next iteration
usleep(config('tunneler.wait'));
}
Expand Down Expand Up @@ -101,6 +101,15 @@ protected function verifyTunnel()
return $this->runCommand($this->ncCommand);
}

/*
* Use pkill to kill the SSH tunnel
*/

public function destoryTunnel(){
$ssh_command = preg_replace('/[\s]{2}[\s]*/',' ',$this->sshCommand);
return $this->runCommand('pkill -f "'.$ssh_command.'"');
}

/**
* Runs a command and converts the exit code to a boolean
* @param $command
Expand Down
9 changes: 9 additions & 0 deletions src/TunnelerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Support\ServiceProvider;
use STS\Tunneler\Console\TunnelerCommand;
use STS\Tunneler\Console\TunnelerReset;
use STS\Tunneler\Jobs\CreateTunnel;


Expand Down Expand Up @@ -50,6 +51,14 @@ function ($app) {
);

$this->commands('command.tunneler.activate');

$this->app->singleton('command.tunneler.reset',
function ($app) {
return new TunnelerReset();
}
);

$this->commands('command.tunneler.reset');
}

/**
Expand Down

0 comments on commit 62d09bb

Please sign in to comment.