lasthourcloud/app/Livewire/Project/Database/Import.php

118 lines
4.5 KiB
PHP
Raw Normal View History

2024-01-06 05:24:57 +00:00
<?php
namespace App\Livewire\Project\Database;
use Livewire\Component;
use App\Models\Server;
2024-01-10 14:42:54 +00:00
use Illuminate\Support\Facades\Storage;
2024-01-06 05:24:57 +00:00
class Import extends Component
{
public $resource;
public $parameters;
2024-01-10 14:42:54 +00:00
public $containers;
2024-01-06 05:24:57 +00:00
public bool $scpInProgress = false;
public bool $importRunning = false;
2024-04-11 10:13:11 +00:00
public ?string $filename = null;
public ?string $filesize = null;
public bool $isUploading = false;
public int $progress = 0;
public bool $error = false;
2024-01-06 05:24:57 +00:00
public Server $server;
public string $container;
public array $importCommands = [];
2024-01-10 14:42:54 +00:00
public string $postgresqlRestoreCommand = 'pg_restore -U $POSTGRES_USER -d $POSTGRES_DB';
2024-02-24 20:12:34 +00:00
public string $mysqlRestoreCommand = 'mysql -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE';
public string $mariadbRestoreCommand = 'mariadb -u $MARIADB_USER -p$MARIADB_PASSWORD $MARIADB_DATABASE';
2024-01-06 05:24:57 +00:00
2024-01-10 14:42:54 +00:00
public function getListeners()
{
$userId = auth()->user()->id;
return [
"echo-private:user.{$userId},DatabaseStatusChanged" => '$refresh',
];
}
2024-01-06 05:24:57 +00:00
public function mount()
{
$this->parameters = get_route_parameters();
$this->getContainers();
}
public function getContainers()
{
$this->containers = collect();
if (!data_get($this->parameters, 'database_uuid')) {
abort(404);
}
2024-04-11 10:13:11 +00:00
$resource = getResourceByUuid($this->parameters['database_uuid'], data_get(auth()->user()->currentTeam(), 'id'));
2024-01-06 05:24:57 +00:00
if (is_null($resource)) {
2024-04-10 13:00:46 +00:00
abort(404);
2024-01-06 05:24:57 +00:00
}
$this->resource = $resource;
$this->server = $this->resource->destination->server;
$this->container = $this->resource->uuid;
2024-01-10 14:42:54 +00:00
if (str(data_get($this, 'resource.status'))->startsWith('running')) {
2024-01-06 05:24:57 +00:00
$this->containers->push($this->container);
}
2024-01-10 14:42:54 +00:00
if (
2024-04-10 13:00:46 +00:00
$this->resource->getMorphClass() == 'App\Models\StandaloneRedis' ||
$this->resource->getMorphClass() == 'App\Models\StandaloneKeydb' ||
$this->resource->getMorphClass() == 'App\Models\StandaloneDragonfly' ||
$this->resource->getMorphClass() == 'App\Models\StandaloneClickhouse' ||
$this->resource->getMorphClass() == 'App\Models\StandaloneMongodb'
2024-01-10 14:42:54 +00:00
) {
2024-04-11 10:13:11 +00:00
$this->dispatch('error', 'Import is not supported for this resource.');
2024-01-06 05:24:57 +00:00
}
}
2024-01-10 14:42:54 +00:00
2024-01-06 05:24:57 +00:00
public function runImport()
{
2024-04-11 10:13:11 +00:00
if ($this->filename == '') {
$this->dispatch('error', 'Please select a file to import.');
return;
}
2024-01-06 05:24:57 +00:00
try {
2024-04-11 10:13:11 +00:00
$uploadedFilename = "upload/{$this->resource->uuid}/restore";
2024-01-10 14:42:54 +00:00
$path = Storage::path($uploadedFilename);
2024-04-11 10:13:11 +00:00
if (!Storage::exists($uploadedFilename)) {
$this->dispatch('error', 'The file does not exist or has been deleted.');
return;
}
2024-01-06 05:24:57 +00:00
$tmpPath = '/tmp/' . basename($uploadedFilename);
instant_scp($path, $tmpPath, $this->server);
2024-04-11 10:13:11 +00:00
Storage::delete($uploadedFilename);
2024-01-06 05:24:57 +00:00
$this->importCommands[] = "docker cp {$tmpPath} {$this->container}:{$tmpPath}";
switch ($this->resource->getMorphClass()) {
case 'App\Models\StandaloneMariadb':
2024-01-10 14:42:54 +00:00
$this->importCommands[] = "docker exec {$this->container} sh -c '{$this->mariadbRestoreCommand} < {$tmpPath}'";
2024-01-06 05:24:57 +00:00
$this->importCommands[] = "rm {$tmpPath}";
2024-01-10 14:42:54 +00:00
break;
2024-01-06 05:24:57 +00:00
case 'App\Models\StandaloneMysql':
2024-01-10 14:42:54 +00:00
$this->importCommands[] = "docker exec {$this->container} sh -c '{$this->mysqlRestoreCommand} < {$tmpPath}'";
2024-01-06 05:24:57 +00:00
$this->importCommands[] = "rm {$tmpPath}";
2024-01-10 14:42:54 +00:00
break;
2024-01-06 05:24:57 +00:00
case 'App\Models\StandalonePostgresql':
2024-01-10 14:42:54 +00:00
$this->importCommands[] = "docker exec {$this->container} sh -c '{$this->postgresqlRestoreCommand} {$tmpPath}'";
2024-01-06 05:24:57 +00:00
$this->importCommands[] = "rm {$tmpPath}";
2024-01-10 14:42:54 +00:00
break;
2024-01-06 05:24:57 +00:00
}
$this->importCommands[] = "docker exec {$this->container} sh -c 'rm {$tmpPath}'";
$this->importCommands[] = "docker exec {$this->container} sh -c 'echo \"Import finished with exit code $?\"'";
if (!empty($this->importCommands)) {
$activity = remote_process($this->importCommands, $this->server, ignore_errors: true);
$this->dispatch('activityMonitor', $activity->id);
2024-01-06 05:24:57 +00:00
}
} catch (\Throwable $e) {
2024-04-11 10:13:11 +00:00
return handleError($e, $this);
2024-01-06 05:24:57 +00:00
}
}
}