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

141 lines
4.9 KiB
PHP
Raw Normal View History

2024-01-06 05:24:57 +00:00
<?php
namespace App\Livewire\Project\Database;
use App\Models\Server;
2024-01-10 14:42:54 +00:00
use Illuminate\Support\Facades\Storage;
2024-06-10 20:43:34 +00:00
use Livewire\Component;
2024-01-06 05:24:57 +00:00
class Import extends Component
{
public bool $unsupported = false;
2024-06-10 20:43:34 +00:00
2024-01-06 05:24:57 +00:00
public $resource;
2024-06-10 20:43:34 +00:00
2024-01-06 05:24:57 +00:00
public $parameters;
2024-06-10 20:43:34 +00:00
2024-01-10 14:42:54 +00:00
public $containers;
2024-06-10 20:43:34 +00:00
2024-01-06 05:24:57 +00:00
public bool $scpInProgress = false;
2024-06-10 20:43:34 +00:00
2024-01-06 05:24:57 +00:00
public bool $importRunning = false;
2024-04-11 10:13:11 +00:00
public ?string $filename = null;
2024-06-10 20:43:34 +00:00
2024-04-11 10:13:11 +00:00
public ?string $filesize = null;
2024-06-10 20:43:34 +00:00
2024-04-11 10:13:11 +00:00
public bool $isUploading = false;
2024-06-10 20:43:34 +00:00
2024-04-11 10:13:11 +00:00
public int $progress = 0;
2024-06-10 20:43:34 +00:00
2024-04-11 10:13:11 +00:00
public bool $error = false;
2024-01-06 05:24:57 +00:00
public Server $server;
2024-06-10 20:43:34 +00:00
2024-01-06 05:24:57 +00:00
public string $container;
2024-06-10 20:43:34 +00:00
2024-01-06 05:24:57 +00:00
public array $importCommands = [];
2024-06-10 20:43:34 +00:00
2024-01-10 14:42:54 +00:00
public string $postgresqlRestoreCommand = 'pg_restore -U $POSTGRES_USER -d $POSTGRES_DB';
2024-06-10 20:43:34 +00:00
2024-02-24 20:12:34 +00:00
public string $mysqlRestoreCommand = 'mysql -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE';
2024-06-10 20:43:34 +00:00
2024-02-24 20:12:34 +00:00
public string $mariadbRestoreCommand = 'mariadb -u $MARIADB_USER -p$MARIADB_PASSWORD $MARIADB_DATABASE';
2024-06-10 20:43:34 +00:00
2024-04-25 21:44:55 +00:00
public string $mongodbRestoreCommand = 'mongorestore --authenticationDatabase=admin --username $MONGO_INITDB_ROOT_USERNAME --password $MONGO_INITDB_ROOT_PASSWORD --uri mongodb://localhost:27017 --gzip --archive=';
2024-01-06 05:24:57 +00:00
2024-01-10 14:42:54 +00:00
public function getListeners()
{
$userId = auth()->user()->id;
2024-06-10 20:43:34 +00:00
2024-01-10 14:42:54 +00:00
return [
"echo-private:user.{$userId},DatabaseStatusChanged" => '$refresh',
];
}
2024-06-10 20:43:34 +00:00
2024-01-06 05:24:57 +00:00
public function mount()
{
$this->parameters = get_route_parameters();
$this->getContainers();
}
public function getContainers()
{
$this->containers = collect();
2024-06-10 20:43:34 +00:00
if (! data_get($this->parameters, 'database_uuid')) {
2024-01-06 05:24:57 +00:00
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' ||
2024-04-14 20:31:55 +00:00
$this->resource->getMorphClass() == 'App\Models\StandaloneClickhouse'
2024-01-10 14:42:54 +00:00
) {
$this->unsupported = true;
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.');
2024-06-10 20:43:34 +00:00
2024-04-11 10:13:11 +00:00
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-06-10 20:43:34 +00:00
if (! Storage::exists($uploadedFilename)) {
2024-04-11 10:13:11 +00:00
$this->dispatch('error', 'The file does not exist or has been deleted.');
2024-06-10 20:43:34 +00:00
2024-04-11 10:13:11 +00:00
return;
}
2024-06-10 20:43:34 +00:00
$tmpPath = '/tmp/'.basename($uploadedFilename);
2024-01-06 05:24:57 +00:00
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-04-14 20:31:55 +00:00
case 'App\Models\StandaloneMongodb':
2024-04-25 21:44:55 +00:00
$this->importCommands[] = "docker exec {$this->container} sh -c '{$this->mongodbRestoreCommand}{$tmpPath}'";
2024-04-14 20:31:55 +00:00
$this->importCommands[] = "rm {$tmpPath}";
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 $?\"'";
2024-06-10 20:43:34 +00:00
if (! empty($this->importCommands)) {
2024-01-06 05:24:57 +00:00
$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
}
}
}