Skip to content

Commit 62d09bb

Browse files
authored
Merge pull request #48 from Shadercloud/master
Add tunneler:reset Command
2 parents 1c0b8ec + ddbce69 commit 62d09bb

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

src/Console/TunnelerReset.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
namespace STS\Tunneler\Console;
3+
4+
use Illuminate\Console\Command;
5+
use STS\Tunneler\Jobs\CreateTunnel;
6+
7+
class TunnelerReset extends Command
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'tunneler:reset';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Destroy and reconnect the SSH tunnel';
22+
23+
/**
24+
* Create a new command instance.
25+
*
26+
* @return void
27+
*/
28+
public function __construct()
29+
{
30+
parent::__construct();
31+
}
32+
33+
/**
34+
* Execute the console command.
35+
*
36+
* @return int
37+
*/
38+
public function handle()
39+
{
40+
$tunnel = new CreateTunnel();
41+
$tunnel->destoryTunnel();
42+
43+
\Artisan::call('tunneler:activate');
44+
}
45+
}

src/Jobs/CreateTunnel.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ public function handle(): int
5858
}
5959

6060
$this->createTunnel();
61-
61+
6262
$tries = config('tunneler.tries');
6363
for ($i = 0; $i < $tries; $i++) {
6464
if ($this->verifyTunnel()) {
6565
return 2;
6666
}
67-
67+
6868
// Wait a bit until next iteration
6969
usleep(config('tunneler.wait'));
7070
}
@@ -101,6 +101,15 @@ protected function verifyTunnel()
101101
return $this->runCommand($this->ncCommand);
102102
}
103103

104+
/*
105+
* Use pkill to kill the SSH tunnel
106+
*/
107+
108+
public function destoryTunnel(){
109+
$ssh_command = preg_replace('/[\s]{2}[\s]*/',' ',$this->sshCommand);
110+
return $this->runCommand('pkill -f "'.$ssh_command.'"');
111+
}
112+
104113
/**
105114
* Runs a command and converts the exit code to a boolean
106115
* @param $command

src/TunnelerServiceProvider.php

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Illuminate\Support\ServiceProvider;
44
use STS\Tunneler\Console\TunnelerCommand;
5+
use STS\Tunneler\Console\TunnelerReset;
56
use STS\Tunneler\Jobs\CreateTunnel;
67

78

@@ -50,6 +51,14 @@ function ($app) {
5051
);
5152

5253
$this->commands('command.tunneler.activate');
54+
55+
$this->app->singleton('command.tunneler.reset',
56+
function ($app) {
57+
return new TunnelerReset();
58+
}
59+
);
60+
61+
$this->commands('command.tunneler.reset');
5362
}
5463

5564
/**

0 commit comments

Comments
 (0)