Skip to content
This repository has been archived by the owner on Feb 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #136 from dbpolito/fwd_tools
Browse files Browse the repository at this point in the history
Deploy to fwd.tools
  • Loading branch information
dbpolito authored May 25, 2020
2 parents 02018d9 + 131c4c1 commit adb43a7
Show file tree
Hide file tree
Showing 6 changed files with 1,283 additions and 163 deletions.
4 changes: 4 additions & 0 deletions .fwd
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ DB_PASSWORD="secret"
COMPOSE_API_VERSION=1.25
FWD_COMPOSE_VERSION=3.7
# FWD_NETWORK="custom-network" # by default will be generated on the fly

# Deploy
# FWD_TOOLS_TOKEN=
# FWD_TOOLS_SLUG=
32 changes: 32 additions & 0 deletions app/Commands/Deploy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Commands;

use App\Tasks\Deploy as DeployTask;

class Deploy extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'deploy';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Deploy your app to fwd.tools.';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
return DeployTask::make($this)->run();
}
}
2 changes: 2 additions & 0 deletions app/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class Environment
'COMPOSE_API_VERSION',
'FWD_COMPOSE_VERSION',
'FWD_NETWORK',
'FWD_TOOLS_TOKEN',
'FWD_TOOLS_SLUG',
];

/** @var Xdg $xdg */
Expand Down
109 changes: 109 additions & 0 deletions app/Tasks/Deploy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace App\Tasks;

use App\Builder\Builder;
use App\Environment;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

class Deploy extends Task
{
const URL = 'https://fwd.tools';

protected $file;
protected $deploy;

public function run(...$args): int
{
try {
$commands = [
[$this, 'createReleaseFile'],
[$this, 'sendReleaseFile'],
[$this, 'building'],
];

if ($exitCode = $this->runCallables($commands)) {
return $exitCode;
}

$this->command->info('Access URL: ' . $this->deploy->url);

return 0;
} catch (RequestException $exception) {
$this->command->error('Deploy failed');

if ($exception->response->status() === 422) {
$this->command->error(
collect($exception->response->object()->errors)->flatten()->implode(PHP_EOL)
);
}

return 1;
} catch (\Exception $exception) {
$this->command->error('Deploy failed');
$this->command->error($exception->getMessage());

return 1;
} finally {
$environment = resolve(Environment::class);
File::delete($environment->getContextFile($this->file));
}
}

public function createReleaseFile() : int
{
return $this->runTask('Create Release File', function () {
$this->file = sprintf('%s.tgz', Str::random(10));

$this->runCommandWithoutOutput(
Builder::make('git', 'archive', '--format=tar.gz', 'HEAD', '-o', $this->file)
);

return 0;
});
}

public function sendReleaseFile() : int
{
return $this->runTask('Send Release File', function () {
$environment = resolve(Environment::class);
$file = fopen($environment->getContextFile($this->file), 'r');
$data = ['slug' => env('FWD_TOOLS_SLUG')];

$this->deploy = $this->http()
->attach('deploy', $file, 'deploy.tgz')
->post(self::URL . '/api/deploy/create', $data)
->throw()
->object();

return 0;
});
}

public function building() : int
{
return $this->runTask('Building', function () {
return $this->runCallableWaitFor(function () {
$this->deploy = $this->http()
->get(self::URL . sprintf('/api/deploy/%s/status', $this->deploy->id))
->throw()
->object();

if ($this->deploy->status === 'failed') {
throw new \Exception('Failed');
}

return $this->deploy->status === 'success' ? 0 : 1;
}, 600);
});
}

protected function http()
{
return Http::withToken(env('FWD_TOOLS_TOKEN'))
->withHeaders(['Accept' => 'application/json']);
}
}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
"require": {
"php": "^7.2.5",
"dnoegel/php-xdg-base-dir": "^0.1.1",
"guzzlehttp/guzzle": "^6.3.1",
"illuminate/http": "^7.0",
"illuminate/view": "^7.0",
"laravel-zero/framework": "^7.0",
"padraic/phar-updater": "^1.0.6"
},
"require-dev": {
"mockery/mockery": "^1.0",
"mockery/mockery": "1.3.*",
"phpunit/phpunit": "^8.0"
},
"autoload": {
Expand Down
Loading

0 comments on commit adb43a7

Please sign in to comment.