mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-04 07:22:34 +00:00
Merge (#127)
This commit is contained in:
30
app/Actions/Server/CheckConnection.php
Normal file
30
app/Actions/Server/CheckConnection.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Server;
|
||||
|
||||
use App\Facades\Notifier;
|
||||
use App\Models\Server;
|
||||
use App\Notifications\ServerDisconnected;
|
||||
use Throwable;
|
||||
|
||||
class CheckConnection
|
||||
{
|
||||
public function check(Server $server): Server
|
||||
{
|
||||
$status = $server->status;
|
||||
try {
|
||||
$server->ssh()->connect();
|
||||
$server->refresh();
|
||||
if ($status == 'disconnected') {
|
||||
$server->status = 'ready';
|
||||
$server->save();
|
||||
}
|
||||
} catch (Throwable) {
|
||||
$server->status = 'disconnected';
|
||||
$server->save();
|
||||
Notifier::send($server, new ServerDisconnected($server));
|
||||
}
|
||||
|
||||
return $server;
|
||||
}
|
||||
}
|
@ -3,13 +3,19 @@
|
||||
namespace App\Actions\Server;
|
||||
|
||||
use App\Enums\FirewallRuleStatus;
|
||||
use App\Enums\ServerProvider;
|
||||
use App\Enums\ServerStatus;
|
||||
use App\Exceptions\ServerProviderError;
|
||||
use App\Jobs\Installation\ContinueInstallation;
|
||||
use App\Facades\Notifier;
|
||||
use App\Models\Server;
|
||||
use App\Models\User;
|
||||
use App\Notifications\ServerInstallationFailed;
|
||||
use App\Notifications\ServerInstallationSucceed;
|
||||
use App\ValidationRules\RestrictedIPAddressesRule;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
@ -30,7 +36,7 @@ public function create(User $creator, array $input): Server
|
||||
'user_id' => $creator->id,
|
||||
'name' => $input['name'],
|
||||
'ssh_user' => config('core.server_providers_default_user')[$input['provider']][$input['os']],
|
||||
'ip' => $input['ip'],
|
||||
'ip' => $input['ip'] ?? '',
|
||||
'port' => $input['port'] ?? 22,
|
||||
'os' => $input['os'],
|
||||
'type' => $input['type'],
|
||||
@ -71,14 +77,8 @@ public function create(User $creator, array $input): Server
|
||||
$server->type()->createServices($input);
|
||||
|
||||
// install server
|
||||
if ($server->provider == 'custom') {
|
||||
$server->install();
|
||||
} else {
|
||||
$server->progress_step = __('Installation will begin in 3 minutes!');
|
||||
$server->save();
|
||||
dispatch(new ContinueInstallation($server))
|
||||
->delay(now()->addMinutes(2));
|
||||
}
|
||||
$this->install($server);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $server;
|
||||
@ -88,12 +88,44 @@ public function create(User $creator, array $input): Server
|
||||
if ($e instanceof ServerProviderError) {
|
||||
throw ValidationException::withMessages([
|
||||
'provider' => __('Provider Error: ').$e->getMessage(),
|
||||
])->errorBag('createServer');
|
||||
]);
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
private function install(Server $server): void
|
||||
{
|
||||
$bus = Bus::chain([
|
||||
function () use ($server) {
|
||||
if (! $server->provider()->isRunning()) {
|
||||
sleep(2);
|
||||
}
|
||||
$server->type()->install();
|
||||
$server->update([
|
||||
'status' => ServerStatus::READY,
|
||||
]);
|
||||
Notifier::send($server, new ServerInstallationSucceed($server));
|
||||
},
|
||||
])->catch(function (Throwable $e) use ($server) {
|
||||
$server->update([
|
||||
'status' => ServerStatus::INSTALLATION_FAILED,
|
||||
]);
|
||||
Notifier::send($server, new ServerInstallationFailed($server));
|
||||
Log::error('server-installation-error', [
|
||||
'error' => (string) $e,
|
||||
]);
|
||||
});
|
||||
|
||||
if ($server->provider != ServerProvider::CUSTOM) {
|
||||
$server->progress_step = 'waiting-for-provider';
|
||||
$server->save();
|
||||
$bus->delay(now()->addMinutes(3));
|
||||
}
|
||||
|
||||
$bus->onConnection('ssh')->dispatch();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
@ -136,7 +168,7 @@ private function validateInputs(array $input): void
|
||||
*/
|
||||
private function validateType(Server $server, array $input): void
|
||||
{
|
||||
Validator::make($input, $server->type()->createValidationRules($input))
|
||||
Validator::make($input, $server->type()->createRules($input))
|
||||
->validate();
|
||||
}
|
||||
|
||||
@ -145,7 +177,7 @@ private function validateType(Server $server, array $input): void
|
||||
*/
|
||||
private function validateProvider(Server $server, array $input): void
|
||||
{
|
||||
Validator::make($input, $server->provider()->createValidationRules($input))
|
||||
Validator::make($input, $server->provider()->createRules($input))
|
||||
->validate();
|
||||
}
|
||||
|
||||
|
@ -35,7 +35,7 @@ public function edit(Server $server, array $input): Server
|
||||
$server->save();
|
||||
|
||||
if ($checkConnection) {
|
||||
$server->checkConnection();
|
||||
return $server->checkConnection();
|
||||
}
|
||||
|
||||
return $server;
|
||||
|
@ -1,14 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Server;
|
||||
|
||||
use App\Models\Server;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class GetServers
|
||||
{
|
||||
public function handle(): Collection
|
||||
{
|
||||
return Server::query()->latest()->get();
|
||||
}
|
||||
}
|
23
app/Actions/Server/RebootServer.php
Normal file
23
app/Actions/Server/RebootServer.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Server;
|
||||
|
||||
use App\Enums\ServerStatus;
|
||||
use App\Models\Server;
|
||||
use Throwable;
|
||||
|
||||
class RebootServer
|
||||
{
|
||||
public function reboot(Server $server): Server
|
||||
{
|
||||
try {
|
||||
$server->os()->reboot();
|
||||
$server->status = ServerStatus::DISCONNECTED;
|
||||
$server->save();
|
||||
} catch (Throwable) {
|
||||
$server = $server->checkConnection();
|
||||
}
|
||||
|
||||
return $server;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user