Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/Models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,15 @@ public static function boot(): void
$server->workers()->delete();
$server->daemons()->delete();
$server->sshKeys()->detach();
if ($server->deleteFromProvider) {
$server->provider()->delete();
}
if (File::exists($server->sshKey()['public_key_path'])) {
File::delete($server->sshKey()['public_key_path']);
}
if (File::exists($server->sshKey()['private_key_path'])) {
File::delete($server->sshKey()['private_key_path']);
}
if ($server->deleteFromProvider) {
$server->provider()->delete();
}
DB::commit();
} catch (Throwable $e) {
DB::rollBack();
Expand Down
33 changes: 33 additions & 0 deletions app/Providers/ServerProviderServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
use App\DTOs\DynamicField;
use App\DTOs\DynamicForm;
use App\Enums\OperatingSystem;
use App\Models\Service;
use App\Plugins\RegisterServerProvider;
use App\ServerProviders\AWS;
use App\ServerProviders\Custom;
use App\ServerProviders\DigitalOcean;
use App\ServerProviders\Hetzner;
use App\ServerProviders\Lightsail;
use App\ServerProviders\Linode;
use App\ServerProviders\Proxmox;
use App\ServerProviders\Vultr;
use App\Services\Firewall\Ufw;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;

class ServerProviderServiceProvider extends ServiceProvider
Expand All @@ -23,6 +27,7 @@ public function boot(): void
{
$this->custom();
$this->aws();
$this->lightsail();
$this->hetzner();
$this->digitalOcean();
$this->linode();
Expand Down Expand Up @@ -58,6 +63,34 @@ private function aws(): void
->register();
}

private function lightsail(): void
{
Event::listen('service.installed', function (Service $service): void {
if ($service->name === Ufw::id() && $service->server->provider === Lightsail::id()) {
$provider = $service->server->provider();
if ($provider instanceof Lightsail) {
$provider->configureFirewall();
}
}
});

RegisterServerProvider::make(Lightsail::id())
->label('AWS Lightsail')
->handler(Lightsail::class)
->form(
DynamicForm::make([
DynamicField::make('key')
->text()
->label('Access Key'),
DynamicField::make('secret')
->password()
->label('Secret Access Key'),
])
)
->defaultUser('ubuntu')
->register();
}

private function hetzner(): void
{
RegisterServerProvider::make(Hetzner::id())
Expand Down
291 changes: 291 additions & 0 deletions app/ServerProviders/Lightsail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
<?php

namespace App\ServerProviders;

use App\Exceptions\ServerProviderError;
use Aws\Exception\AwsException;
use Aws\Lightsail\LightsailClient;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use phpseclib3\Crypt\RSA;
use SensitiveParameter;

class Lightsail extends AbstractProvider
{
private const MAX_PAGES = 100;

public static function id(): string
{
return 'lightsail';
}

public function createRules(array $input): array
{
return [
'plan' => ['required', 'string'],
'region' => ['required', 'string', 'regex:/^[a-z]{2}(?:-[a-z]+)+-\d+$/'],
];
}

public function credentialValidationRules(array $input): array
{
return [
'key' => ['required', 'string'],
'secret' => ['required', 'string'],
];
}

public function credentialData(array $input): array
{
return [
'key' => $input['key'],
'secret' => $input['secret'],
];
}

public function data(array $input): array
{
return [
'plan' => $input['plan'],
'region' => $input['region'],
];
}

public function connect(#[SensitiveParameter] array $credentials): bool
{
$this->request('GetRegions', credentials: $this->credentialData($credentials));

return true;
}

public function regions(): array
{
return collect($this->request('GetRegions')['regions'] ?? [])
->mapWithKeys(fn (array $region): array => [
$region['name'] => $region['displayName'].' ('.$region['name'].')',
])
->all();
}

public function plans(?string $region): array
{
if (! $region) {
return [];
}

return collect($this->paginate('GetBundles', 'bundles', $region))
->filter(fn (array $bundle): bool => ($bundle['isActive'] ?? false)
&& in_array('LINUX_UNIX', $bundle['supportedPlatforms'] ?? [], true)
&& ($bundle['publicIpv4AddressCount'] ?? 0) > 0)
->mapWithKeys(fn (array $bundle): array => [
$bundle['bundleId'] => __('server_providers.plan', [
'name' => $bundle['name'],
'cpu' => $bundle['cpuCount'],
'memory' => $bundle['ramSizeInGb'] * 1024,
'disk' => $bundle['diskSizeInGb'],
]).' ('.number_format($bundle['price'], 2).'/mo)',
])
->all();
}

public function create(): void
{
$region = $this->server->provider_data['region'];
$regions = $this->request('GetRegions', ['includeAvailabilityZones' => true]);
$location = collect($regions['regions'] ?? [])->firstWhere('name', $region);
$zone = $location['availabilityZones'][0]['zoneName'] ?? null;

if (! $zone) {
throw new ServerProviderError('The selected AWS Lightsail region is unavailable.');
}

if (! isset($this->plans($region)[$this->server->provider_data['plan']])) {
throw new ServerProviderError('The selected AWS Lightsail plan is unavailable.');
}

$blueprint = collect($this->paginate('GetBlueprints', 'blueprints', $region))
->first(fn (array $blueprint): bool => ($blueprint['isActive'] ?? false)
&& in_array($blueprint['group'] ?? '', ['ubuntu', $this->server->os->value], true)
&& ($blueprint['type'] ?? '') === 'os'
&& str_starts_with($blueprint['version'] ?? '', $this->server->os->getVersion()));

if (! $blueprint) {
throw new ServerProviderError('The selected Ubuntu version is unavailable on AWS Lightsail.');
}

$name = 'vito-'.$this->server->id.'-'.Str::lower(Str::random(12));
$keyName = $name.'-key';
$this->generateKeyPair();
$this->server->jsonUpdate('provider_data', 'ssh_key_name', $keyName);
$this->request('ImportKeyPair', [
'keyPairName' => $keyName,
'publicKeyBase64' => $this->server->sshKey()['public_key'],
]);

$this->server->jsonUpdate('provider_data', 'instance_name', $name);
$this->request('CreateInstances', [
'instanceNames' => [$name],
'availabilityZone' => $zone,
'blueprintId' => $blueprint['blueprintId'],
'bundleId' => $this->server->provider_data['plan'],
'keyPairName' => $keyName,
'ipAddressType' => 'ipv4',
]);
}

public function generateKeyPair(): void
{
$key = RSA::createKey(2048);
/** @var FilesystemAdapter $disk */
$disk = Storage::disk(config('core.key_pairs_disk'));
$disk->put((string) $this->server->id, $key->toString('PKCS8'));
chmod($disk->path((string) $this->server->id), 0400);
$disk->put($this->server->id.'.pub', $key->getPublicKey()->toString('OpenSSH'));
}

public function isRunning(): bool
{
if (! isset($this->server->provider_data['instance_name'])) {
return false;
}

$result = $this->request('GetInstance', [
'instanceName' => $this->server->provider_data['instance_name'],
]);
$instance = $result['instance'] ?? [];

if (($instance['state']['name'] ?? '') !== 'running' || empty($instance['publicIpAddress'])) {
return false;
}

if (! ($this->server->provider_data['ssh_access_configured'] ?? false)
&& ! ($this->server->provider_data['firewall_configured'] ?? false)) {
$this->request('PutInstancePublicPorts', [
'instanceName' => $this->server->provider_data['instance_name'],
'portInfos' => [[
'fromPort' => 22,
'toPort' => 22,
'protocol' => 'tcp',
'cidrs' => ['0.0.0.0/0'],
]],
Comment thread
coderabbitai[bot] marked this conversation as resolved.
]);
$this->server->jsonUpdate('provider_data', 'ssh_access_configured', true, false);
}

$this->server->ip = $instance['publicIpAddress'];
$this->server->local_ip = $instance['privateIpAddress'] ?? null;
$this->server->save();

return true;
}

public function configureFirewall(): void
{
if ($this->server->provider_data['firewall_configured'] ?? false) {
return;
}

$this->request('PutInstancePublicPorts', [
'instanceName' => $this->server->provider_data['instance_name'],
'portInfos' => [[
'fromPort' => 0,
'toPort' => 65535,
'protocol' => 'all',
'cidrs' => ['0.0.0.0/0'],
]],
]);
$this->server->jsonUpdate('provider_data', 'firewall_configured', true);
}

public function delete(): void
{
if (isset($this->server->provider_data['instance_name'])) {
$this->request('DeleteInstance', [
'instanceName' => $this->server->provider_data['instance_name'],
]);
}

if (isset($this->server->provider_data['ssh_key_name'])) {
$this->request('DeleteKeyPair', [
'keyPairName' => $this->server->provider_data['ssh_key_name'],
]);
}
}

/**
* @return array<int, array<string, mixed>>
*/
private function paginate(string $operation, string $key, string $region): array
{
$items = [];
$parameters = ['includeInactive' => false];
$page = 0;
$seenTokens = [];

do {
if (++$page > self::MAX_PAGES) {
throw new ServerProviderError('AWS Lightsail returned too many pages for '.$operation.'.');
}

$result = $this->request($operation, $parameters, $region);
$items = array_merge($items, $result[$key] ?? []);
$parameters['pageToken'] = $result['nextPageToken'] ?? null;

if (isset($seenTokens[$parameters['pageToken']])) {
throw new ServerProviderError('AWS Lightsail returned an invalid page token for '.$operation.'.');
}
if ($parameters['pageToken'] !== null) {
$seenTokens[$parameters['pageToken']] = true;
}
} while ($parameters['pageToken'] !== null && $parameters['pageToken'] !== '');

return $items;
}

/**
* @param array<string, mixed> $parameters
* @param array{key: string, secret: string}|null $credentials
* @return array<string, mixed>
*/
private function request(string $operation, array $parameters = [], ?string $region = null, #[SensitiveParameter] ?array $credentials = null): array
{
$region ??= $this->server->provider_data['region'] ?? 'us-east-1';

if (! preg_match('/^[a-z]{2}(?:-[a-z]+)+-\d+$/', $region)) {
throw ValidationException::withMessages(['region' => 'Invalid AWS Lightsail region.']);
}

try {
$client = app(LightsailClient::class, ['args' => [
'version' => '2016-11-28',
'region' => $region,
'credentials' => $credentials ?? $this->serverProvider->getCredentials(),
]]);
$result = $client->execute($client->getCommand($operation, $parameters))->toArray();
} catch (AwsException $exception) {
$code = $exception->getAwsErrorCode();

if (in_array($code, ['NotFoundException', 'DoesNotExist'], true)
&& in_array($operation, ['GetInstance', 'DeleteInstance', 'DeleteKeyPair'], true)) {
return [];
}

throw new ServerProviderError('AWS Lightsail could not complete '.$operation.($code ? ' ('.$code.')' : '').'.');
}

foreach ($result['operations'] ?? (isset($result['operation']) ? [$result['operation']] : []) as $operationResult) {
if (($operationResult['status'] ?? '') === 'Failed') {
throw new ServerProviderError('AWS Lightsail could not complete '.$operation.'.');
}

if (in_array($operation, ['DeleteInstance', 'DeleteKeyPair'], true)
&& ! in_array($operationResult['status'] ?? '', ['Succeeded', 'Completed'], true)) {
throw new ServerProviderError('AWS Lightsail deletion is still in progress. Wait for it to finish, then retry deleting the server.');
}
}

return $result;
}
}
Loading
Loading