Skip to content

Commit

Permalink
deploy Wordpress sites via VitoDeploy (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
saeedvaziry authored Jan 1, 2024
1 parent 5e6d338 commit 4cda14f
Show file tree
Hide file tree
Showing 31 changed files with 443 additions and 292 deletions.
5 changes: 0 additions & 5 deletions app/Actions/Site/CreateSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ public function create(Server $server, array $input): Site
]);
}

// detect php version
if ($site->type()->language() === 'php') {
$site->php_version = $input['php_version'];
}

// validate type
$this->validateType($site, $input);

Expand Down
2 changes: 2 additions & 0 deletions app/Contracts/SiteType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ interface SiteType
{
public function language(): string;

public function supportedFeatures(): array;

public function createValidationRules(array $input): array;

public function createFields(array $input): array;
Expand Down
16 changes: 16 additions & 0 deletions app/Enums/SiteFeature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Enums;

use BenSampo\Enum\Enum;

final class SiteFeature extends Enum
{
const DEPLOYMENT = 'deployment';

const ENV = 'env';

const SSL = 'ssl';

const QUEUES = 'queues';
}
26 changes: 7 additions & 19 deletions app/Http/Livewire/Sites/CreateSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Livewire\Sites;

use App\Enums\SiteType;
use App\Exceptions\SourceControlIsNotConnected;
use App\Models\Server;
use App\Models\SourceControl;
Expand All @@ -16,23 +15,12 @@ class CreateSite extends Component

public Server $server;

public string $type = SiteType::LARAVEL;

public string $domain;

public string $alias;

public string $php_version = '';

public string $web_directory = 'public';

public string $source_control = '';

public string $repository;

public string $branch;

public bool $composer;
public array $inputs = [
'type' => '',
'web_directory' => 'public',
'source_control' => '',
'php_version' => '',
];

/**
* @throws SourceControlIsNotConnected
Expand All @@ -41,7 +29,7 @@ public function create(): void
{
$site = app(\App\Actions\Site\CreateSite::class)->create(
$this->server,
$this->all()
$this->inputs
);

$this->redirect(route('servers.sites.show', [
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/Site/InstallWordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function handle(): void
$this->site->id
);

if (! Str::contains($result, 'Wordpress installed!')) {
if (! Str::contains($result, 'Success')) {
throw new FailedToInstallWordpress($result);
}
}
Expand Down
8 changes: 4 additions & 4 deletions app/Models/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ public function server(): BelongsTo
/**
* create database on server
*/
public function createOnServer(): void
public function createOnServer(string $queue = 'ssh'): void
{
dispatch(new CreateOnServer($this))->onConnection('ssh');
dispatch(new CreateOnServer($this))->onConnection($queue);
}

/**
* delete database from server
*/
public function deleteFromServer(): void
public function deleteFromServer(string $queue = 'ssh'): void
{
$this->status = DatabaseStatus::DELETING;
$this->save();
dispatch(new DeleteFromServer($this))->onConnection('ssh');
dispatch(new DeleteFromServer($this))->onConnection($queue);
}

public function backups(): HasMany
Expand Down
16 changes: 8 additions & 8 deletions app/Models/DatabaseUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ public function scopeHasDatabase(Builder $query, string $databaseName): Builder
return $query->where('databases', 'like', "%\"$databaseName\"%");
}

public function createOnServer(): void
public function createOnServer(string $queue = 'ssh'): void
{
dispatch(new CreateOnServer($this))->onConnection('ssh');
dispatch(new CreateOnServer($this))->onConnection($queue);
}

public function deleteFromServer(): void
public function deleteFromServer(string $queue = 'ssh'): void
{
$this->status = DatabaseStatus::DELETING;
$this->save();

dispatch(new DeleteFromServer($this))->onConnection('ssh');
dispatch(new DeleteFromServer($this))->onConnection($queue);
}

public function linkNewDatabase(string $name): void
Expand All @@ -79,14 +79,14 @@ public function linkNewDatabase(string $name): void
}
}

public function linkUser(): void
public function linkUser(string $queue = 'ssh'): void
{
dispatch(new LinkUser($this))->onConnection('ssh');
dispatch(new LinkUser($this))->onConnection($queue);
}

public function unlinkUser(): void
public function unlinkUser(string $queue = 'ssh'): void
{
dispatch(new UnlinkUser($this))->onConnection('ssh');
dispatch(new UnlinkUser($this))->onConnection($queue);
}

public function getFullUserAttribute(): string
Expand Down
47 changes: 47 additions & 0 deletions app/Models/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Enums\DeploymentStatus;
use App\Enums\SiteStatus;
use App\Enums\SslStatus;
use App\Events\Broadcast;
use App\Exceptions\SourceControlIsNotConnected;
use App\Jobs\Site\ChangePHPVersion;
use App\Jobs\Site\Deploy;
Expand All @@ -18,6 +19,7 @@
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Throwable;

Expand Down Expand Up @@ -392,4 +394,49 @@ public function getSshKeyNameAttribute(): string
{
return str('site_'.$this->id)->toString();
}

public function installationFinished(): void
{
$this->update([
'status' => SiteStatus::READY,
'progress' => 100,
]);
event(
new Broadcast('install-site-finished', [
'site' => $this,
])
);
/** @todo notify */
}

/**
* @throws Throwable
*/
public function installationFailed(Throwable $e): void
{
$this->update([
'status' => SiteStatus::INSTALLATION_FAILED,
]);
event(
new Broadcast('install-site-failed', [
'site' => $this,
])
);
/** @todo notify */
Log::error('install-site-error', [
'error' => (string) $e,
]);

throw $e;
}

public function hasFeature(string $feature): bool
{
return in_array($feature, $this->type()->supportedFeatures());
}

public function isReady(): bool
{
return $this->status === SiteStatus::READY;
}
}
48 changes: 18 additions & 30 deletions app/SiteTypes/PHPSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

namespace App\SiteTypes;

use App\Enums\SiteStatus;
use App\Events\Broadcast;
use App\Enums\SiteFeature;
use App\Jobs\Site\CloneRepository;
use App\Jobs\Site\ComposerInstall;
use App\Jobs\Site\CreateVHost;
use App\Jobs\Site\DeployKey;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use Throwable;

Expand All @@ -20,12 +18,22 @@ public function language(): string
return 'php';
}

public function supportedFeatures(): array
{
return [
SiteFeature::DEPLOYMENT,
SiteFeature::ENV,
SiteFeature::SSL,
SiteFeature::QUEUES,
];
}

public function createValidationRules(array $input): array
{
return [
'php_version' => [
'required',
'in:'.implode(',', $this->site->server->installedPHPVersions()),
Rule::in($this->site->server->installedPHPVersions()),
],
'source_control' => [
'required',
Expand All @@ -44,16 +52,17 @@ public function createFields(array $input): array
{
return [
'web_directory' => $input['web_directory'] ?? '',
'source_control_id' => $input['source_control'] ?? '',
'repository' => $input['repository'] ?? '',
'branch' => $input['branch'] ?? '',
'source_control_id' => $input['source_control'],
'repository' => $input['repository'],
'branch' => $input['branch'],
];
}

public function data(array $input): array
{
return [
'composer' => (bool) $input['composer'],
'php_version' => $input['php_version'],
];
}

Expand All @@ -76,33 +85,12 @@ function () {
}

$chain[] = function () {
$this->site->update([
'status' => SiteStatus::READY,
'progress' => 100,
]);
event(
new Broadcast('install-site-finished', [
'site' => $this->site,
])
);
/** @todo notify */
$this->site->installationFinished();
};

Bus::chain($chain)
->catch(function (Throwable $e) {
$this->site->update([
'status' => SiteStatus::INSTALLATION_FAILED,
]);
event(
new Broadcast('install-site-failed', [
'site' => $this->site,
])
);
/** @todo notify */
Log::error('install-site-error', [
'error' => (string) $e,
]);
throw $e;
$this->site->installationFailed($e);
})
->onConnection('ssh-long')
->dispatch();
Expand Down
Loading

0 comments on commit 4cda14f

Please sign in to comment.