Skip to content

Commit

Permalink
Enable/Disable DNS. Add Tests. (#73)
Browse files Browse the repository at this point in the history
* Enable/Disable DNS. 
* Add/Improve Tests (inc. YamlBuilder).
* Cheeky refactor of the Ngrok\Open command
  • Loading branch information
Keoghan committed Oct 10, 2019
1 parent 23bbf05 commit 48eab70
Show file tree
Hide file tree
Showing 14 changed files with 498 additions and 38 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ Redis data is stored in `~/.porter/data/redis`.

## DNS

- `porter dns:on` - Turn the DNS container on (the default status).

- `porter dns:off` - Turn the DNS container off.

- `porter dns:flush` - flush your local machine's DNS in cases where it's getting a bit confused, saves you looking up the command we hope.

- `porter dns:set-host {--restore}` - see below. The `--restore` will remove the setup.
Expand Down
32 changes: 32 additions & 0 deletions app/Commands/Dns/Off.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Commands\Dns;

use App\Commands\BaseCommand;

class Off extends BaseCommand
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'dns:off';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Turn DNS container off';

/**
* Execute the console command.
*
* @return void
*/
public function handle(): void
{
$this->porter->turnOffService('dns');
}
}
32 changes: 32 additions & 0 deletions app/Commands/Dns/On.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Commands\Dns;

use App\Commands\BaseCommand;

class On extends BaseCommand
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'dns:on';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Turn DNS container on';

/**
* Execute the console command.
*
* @return void
*/
public function handle(): void
{
$this->porter->turnOnService('dns');
}
}
72 changes: 57 additions & 15 deletions app/Commands/Ngrok/Open.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class Open extends BaseCommand
*/
protected $description = 'Open ngrok connection to forward your dev environment to an external url';

/**
* Was the site secure at the start of the command?
*
* @var bool
*/
protected $wasSecure = false;

/**
* Execute the console command.
*
Expand All @@ -30,7 +37,6 @@ class Open extends BaseCommand
public function handle(): void
{
$site = Site::resolveFromPathOrCurrentWorkingDirectory($this->argument('site'));
$wasSecure = false;

if (!$site) {
$this->error('No site at this location, and no site path provided.');
Expand All @@ -42,28 +48,17 @@ public function handle(): void
return;
}

if ($site->secure) {
$this->info('Removing SSL for site (required for free ngrok version)');
$site->unsecure();
$wasSecure = true;
}
$this->removeSSLIfNeeded($site);

$this->porter->stop('ngrok');

$tls = ' -bind-tls='.($wasSecure ? 'true' : 'false');
$region = ' -region='.$this->option('region');
$inspect = ' -inspect='.($this->option('no-inspection') ? 'false' : 'true');

$this->dockerCompose
->runContainer('ngrok')
->append("ngrok http -host-header=rewrite{$region}{$tls}{$inspect} {$site->url}:80")
->append($this->constructNgrokCommand($site))
->interactive()
->perform();

if ($wasSecure) {
$this->info('Restoring SSL for site');
$site->secure();
}
$this->restoreSSLIfNeeded($site);

$this->porter->stop('ngrok');
}
Expand Down Expand Up @@ -92,4 +87,51 @@ public function checkItWillResolveProperly()

return true;
}

/**
* Remove SSL from the site if it was secured.
*
* @param Site $site
*/
protected function removeSSLIfNeeded(Site $site): void
{
if (!$site->secure) {
return;
}

$this->info('Removing SSL for site (required for free ngrok version)');
$this->wasSecure = true;
$site->unsecure();
}

/**
* Add SSL back to the site if it was previously secured.
*
* @param Site $site
*/
protected function restoreSSLIfNeeded(Site $site): void
{
if (!$this->wasSecure) {
return;
}

$this->info('Restoring SSL for site');
$site->secure();
}

/**
* Construct the ngrok command.
*
* @param Site $site
*
* @return string
*/
protected function constructNgrokCommand(Site $site): string
{
$tls = ' -bind-tls='.($this->wasSecure ? 'true' : 'false');
$region = ' -region='.$this->option('region');
$inspect = ' -inspect='.($this->option('no-inspection') ? 'false' : 'true');

return "ngrok http -host-header=rewrite{$region}{$tls}{$inspect} {$site->url}:80";
}
}
35 changes: 25 additions & 10 deletions app/Support/Console/DockerCompose/YamlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,34 @@ public function build(ImageRepository $imageSet)
{
$this->files->put(
$this->porterLibrary->dockerComposeFile(),
view("{$imageSet->getName()}::base")->with([
'home' => setting('home'),
'host_machine_name' => setting('host_machine_name'),
'activePhpVersions' => PhpVersion::active()->get(),
'useMysql' => setting('use_mysql') == 'on',
'useRedis' => setting('use_redis') == 'on',
'useBrowser' => setting('use_browser') == 'on',
'imageSet' => $imageSet,
'libraryPath' => $this->porterLibrary->path(),
])->render()
$this->renderDockerComposeFile($imageSet)
);
}

/**
* Render the docker compose file.
*
* @param ImageRepository $imageSet
*
* @throws \Throwable
*
* @return string
*/
public function renderDockerComposeFile(ImageRepository $imageSet)
{
return view("{$imageSet->getName()}::base")->with([
'home' => setting('home'),
'host_machine_name' => setting('host_machine_name'),
'activePhpVersions' => PhpVersion::active()->get(),
'useMysql' => setting('use_mysql') === 'on',
'useRedis' => setting('use_redis') === 'on',
'useBrowser' => setting('use_browser') === 'on',
'useDns' => setting('use_dns') === 'on' || setting_missing('use_dns'),
'imageSet' => $imageSet,
'libraryPath' => $this->porterLibrary->path(),
])->render();
}

/**
* Destroy the docker-compose.yaml file.
*/
Expand Down
28 changes: 26 additions & 2 deletions app/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @param string|null $key
* @param string|null $default
*
* @return mixed
* @return mixed|\Illuminate\Support\Collection
*/
function setting($key = null, $default = null)
{
Expand All @@ -19,10 +19,34 @@ function setting($key = null, $default = null)
return Setting::where('name', $key)->value('value') ?? $default;
}

/**
* Check if a setting exists.
*
* @param string $key
*
* @return bool
*/
function setting_exists($key)
{
return !is_null(setting($key));
}

/**
* Check if a setting is missing.
*
* @param string $key
*
* @return bool
*/
function setting_missing($key)
{
return !setting_exists($key);
}

/**
* Check if we're running tests since environment is limited to production/development.
*/
function running_tests()
{
return config('app.running_tests');
return (bool) config('app.running_tests');
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

services:

@include("{$imageSet->getName()}::dns")
@includeWhen($useDns, "{$imageSet->getName()}::dns")

@include("{$imageSet->getName()}::mailhog")

Expand All @@ -26,14 +26,8 @@
# END PHP version {!! $version->version_number !!}
@endforeach

@if($useMysql)
@include("{$imageSet->getName()}::mysql")
@endif
@includeWhen($useMysql, "{$imageSet->getName()}::mysql")

@if($useRedis)
@include("{$imageSet->getName()}::redis")
@endif
@includeWhen($useRedis, "{$imageSet->getName()}::redis")

@if ($useBrowser)
@include("{$imageSet->getName()}::browser")
@endif
@includeWhen($useBrowser, "{$imageSet->getName()}::browser")
2 changes: 1 addition & 1 deletion tests/Unit/Commands/Browser/OffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class OffTest extends BaseTestCase
use MocksPorter;

/** @test */
public function it_turns_the_browser_on()
public function it_turns_the_browser_off()
{
$this->porter->shouldReceive('turnOffService')->with('browser')->once();

Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/Commands/Dns/FlushTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Tests\Unit\Commands\Dns;

use App\PorterLibrary;
use App\Support\Mechanics\Mechanic;
use Mockery;
use Tests\BaseTestCase;

class FlushTest extends BaseTestCase
{
/** @test */
public function it_will_flush_the_dns()
{
$mechanicMock = Mockery::mock(Mechanic::class);
$mechanicMock
->shouldIgnoreMissing()
->shouldReceive('flushDns')->withNoArgs()->once();

$this->app->instance(Mechanic::class, $mechanicMock);
$this->app->get(PorterLibrary::class)->setMechanic($mechanicMock);

$this->artisan('dns:flush');
}
}
19 changes: 19 additions & 0 deletions tests/Unit/Commands/Dns/OffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tests\Unit\Commands\Dns;

use Tests\BaseTestCase;
use Tests\Unit\Support\Concerns\MocksPorter;

class OffTest extends BaseTestCase
{
use MocksPorter;

/** @test */
public function it_turns_the_dns_off()
{
$this->porter->shouldReceive('turnOffService')->with('dns')->once();

$this->artisan('dns:off');
}
}
19 changes: 19 additions & 0 deletions tests/Unit/Commands/Dns/OnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tests\Unit\Commands\Dns;

use Tests\BaseTestCase;
use Tests\Unit\Support\Concerns\MocksPorter;

class OnTest extends BaseTestCase
{
use MocksPorter;

/** @test */
public function it_turns_the_dns_on()
{
$this->porter->shouldReceive('turnOnService')->with('dns')->once();

$this->artisan('dns:on');
}
}

0 comments on commit 48eab70

Please sign in to comment.