2024-01-22 15:08:18 +00:00
< ? php
namespace App\Livewire\Project\Shared ;
use App\Models\Environment ;
use App\Models\Project ;
use App\Models\StandaloneDocker ;
use App\Models\SwarmDocker ;
use Livewire\Component ;
use Visus\Cuid2\Cuid2 ;
class ResourceOperations extends Component
{
public $resource ;
2024-06-10 20:43:34 +00:00
2024-01-22 15:08:18 +00:00
public $projectUuid ;
2024-06-10 20:43:34 +00:00
2024-11-22 15:03:20 +00:00
public $environmentUuid ;
2024-06-10 20:43:34 +00:00
2024-01-22 15:08:18 +00:00
public $projects ;
2024-06-10 20:43:34 +00:00
2024-01-22 15:08:18 +00:00
public $servers ;
public function mount ()
{
$parameters = get_route_parameters ();
2024-01-31 13:22:48 +00:00
$this -> projectUuid = data_get ( $parameters , 'project_uuid' );
2024-11-22 15:03:20 +00:00
$this -> environmentUuid = data_get ( $parameters , 'environment_uuid' );
2024-01-22 15:08:18 +00:00
$this -> projects = Project :: ownedByCurrentTeam () -> get ();
$this -> servers = currentTeam () -> servers ;
}
2024-06-10 20:43:34 +00:00
2025-01-07 18:26:25 +00:00
public function cloneTo ( $destination_id ) // issues is applications table stuff is replciated but not the application_settings table stuff and also application_preveiws is not replicated, Also only volumes but not files an directory mounts are cloned
// Also the next issue is that the thing is not coloned to the correct server
2024-01-22 15:08:18 +00:00
{
2025-01-07 14:31:43 +00:00
$new_destination = StandaloneDocker :: find ( $destination_id );
2024-06-10 20:43:34 +00:00
if ( ! $new_destination ) {
2025-01-07 14:31:43 +00:00
$new_destination = SwarmDocker :: find ( $destination_id );
2024-01-22 15:08:18 +00:00
}
2024-06-10 20:43:34 +00:00
if ( ! $new_destination ) {
2024-01-22 15:08:18 +00:00
return $this -> addError ( 'destination_id' , 'Destination not found.' );
}
2024-07-25 11:31:59 +00:00
$uuid = ( string ) new Cuid2 ;
2024-01-22 15:08:18 +00:00
$server = $new_destination -> server ;
2025-01-07 18:26:25 +00:00
2025-01-07 14:31:43 +00:00
if ( $this -> resource -> getMorphClass () === \App\Models\Application :: class ) {
2024-12-16 09:25:56 +00:00
$name = 'clone-of-' . str ( $this -> resource -> name ) -> limit ( 20 ) . '-' . $uuid ;
2025-01-07 14:31:43 +00:00
2025-01-07 16:18:04 +00:00
$new_resource = $this -> resource -> replicate ([
'id' ,
'created_at' ,
'updated_at' ,
'additional_servers_count' ,
'additional_networks_count' ,
]) -> fill ([
2024-01-22 15:08:18 +00:00
'uuid' => $uuid ,
2024-12-16 09:25:56 +00:00
'name' => $name ,
2024-01-22 15:08:18 +00:00
'fqdn' => generateFqdn ( $server , $uuid ),
'status' => 'exited' ,
'destination_id' => $new_destination -> id ,
]);
$new_resource -> save ();
2024-03-11 14:08:05 +00:00
if ( $new_resource -> destination -> server -> proxyType () !== 'NONE' ) {
2024-06-11 09:36:42 +00:00
$customLabels = str ( implode ( '|coolify|' , generateLabelsApplication ( $new_resource ))) -> replace ( '|coolify|' , " \n " );
2024-01-31 08:03:54 +00:00
$new_resource -> custom_labels = base64_encode ( $customLabels );
$new_resource -> save ();
}
2024-01-22 15:08:18 +00:00
$environmentVaribles = $this -> resource -> environment_variables () -> get ();
foreach ( $environmentVaribles as $environmentVarible ) {
2025-01-07 16:18:04 +00:00
$newEnvironmentVariable = $environmentVarible -> replicate ([
'id' ,
'created_at' ,
'updated_at' ,
2025-01-07 18:26:25 +00:00
'additional_servers_count' , // not needed because it only is computed for the application
2025-01-07 16:18:04 +00:00
'additional_networks_count' ,
]) -> fill ([
2024-12-17 09:38:32 +00:00
'resourceable_id' => $new_resource -> id ,
'resourceable_type' => $new_resource -> getMorphClass (),
2024-01-22 15:08:18 +00:00
]);
$newEnvironmentVariable -> save ();
}
$persistentVolumes = $this -> resource -> persistentStorages () -> get ();
2025-01-07 14:31:43 +00:00
foreach ( $persistentVolumes as $volume ) {
$volumeName = str ( $volume -> name ) -> replace ( $this -> resource -> uuid , $new_resource -> uuid ) -> value ();
if ( $volumeName === $volume -> name ) {
$volumeName = $new_resource -> uuid . '-' . str ( $volume -> name ) -> afterLast ( '-' );
2024-12-16 09:25:56 +00:00
}
2025-01-07 16:18:04 +00:00
$newPersistentVolume = $volume -> replicate ([
'id' ,
'created_at' ,
'updated_at' ,
'additional_servers_count' ,
'additional_networks_count' ,
]) -> fill ([
2024-12-16 09:25:56 +00:00
'name' => $volumeName ,
2024-01-22 15:08:18 +00:00
'resource_id' => $new_resource -> id ,
]);
$newPersistentVolume -> save ();
}
$route = route ( 'project.application.configuration' , [
'project_uuid' => $this -> projectUuid ,
2024-11-22 15:03:20 +00:00
'environment_uuid' => $this -> environmentUuid ,
2024-01-22 15:08:18 +00:00
'application_uuid' => $new_resource -> uuid ,
2024-06-10 20:43:34 +00:00
]) . '#resource-operations' ;
2024-01-22 15:08:18 +00:00
return redirect () -> to ( $route );
2025-01-07 14:31:43 +00:00
} elseif (
$this -> resource -> getMorphClass () === \App\Models\StandalonePostgresql :: class ||
$this -> resource -> getMorphClass () === \App\Models\StandaloneMongodb :: class ||
$this -> resource -> getMorphClass () === \App\Models\StandaloneMysql :: class ||
$this -> resource -> getMorphClass () === \App\Models\StandaloneMariadb :: class ||
$this -> resource -> getMorphClass () === \App\Models\StandaloneRedis :: class ||
$this -> resource -> getMorphClass () === \App\Models\StandaloneKeydb :: class ||
$this -> resource -> getMorphClass () === \App\Models\StandaloneDragonfly :: class ||
$this -> resource -> getMorphClass () === \App\Models\StandaloneClickhouse :: class
) {
2024-07-25 11:31:59 +00:00
$uuid = ( string ) new Cuid2 ;
2025-01-07 16:18:04 +00:00
$new_resource = $this -> resource -> replicate ([
'id' ,
'created_at' ,
'updated_at' ,
'additional_servers_count' ,
'additional_networks_count' ,
]) -> fill ([
2024-01-22 15:08:18 +00:00
'uuid' => $uuid ,
2024-06-10 20:43:34 +00:00
'name' => $this -> resource -> name . '-clone-' . $uuid ,
2024-01-22 15:08:18 +00:00
'status' => 'exited' ,
'started_at' => null ,
'destination_id' => $new_destination -> id ,
]);
$new_resource -> save ();
$environmentVaribles = $this -> resource -> environment_variables () -> get ();
foreach ( $environmentVaribles as $environmentVarible ) {
$payload = [];
if ( $this -> resource -> type () === 'standalone-postgresql' ) {
$payload [ 'standalone_postgresql_id' ] = $new_resource -> id ;
2024-06-10 20:43:34 +00:00
} elseif ( $this -> resource -> type () === 'standalone-redis' ) {
2024-01-22 15:08:18 +00:00
$payload [ 'standalone_redis_id' ] = $new_resource -> id ;
2024-06-10 20:43:34 +00:00
} elseif ( $this -> resource -> type () === 'standalone-mongodb' ) {
2024-01-22 15:08:18 +00:00
$payload [ 'standalone_mongodb_id' ] = $new_resource -> id ;
2024-06-10 20:43:34 +00:00
} elseif ( $this -> resource -> type () === 'standalone-mysql' ) {
2024-01-22 15:08:18 +00:00
$payload [ 'standalone_mysql_id' ] = $new_resource -> id ;
2024-06-10 20:43:34 +00:00
} elseif ( $this -> resource -> type () === 'standalone-mariadb' ) {
2024-01-22 15:08:18 +00:00
$payload [ 'standalone_mariadb_id' ] = $new_resource -> id ;
}
2025-01-07 16:18:04 +00:00
$newEnvironmentVariable = $environmentVarible -> replicate ([
'id' ,
'created_at' ,
'updated_at' ,
'additional_servers_count' ,
'additional_networks_count' ,
]) -> fill ( $payload );
2024-01-22 15:08:18 +00:00
$newEnvironmentVariable -> save ();
}
$route = route ( 'project.database.configuration' , [
'project_uuid' => $this -> projectUuid ,
2024-11-22 15:03:20 +00:00
'environment_uuid' => $this -> environmentUuid ,
2024-01-22 15:08:18 +00:00
'database_uuid' => $new_resource -> uuid ,
2024-06-10 20:43:34 +00:00
]) . '#resource-operations' ;
2024-01-22 15:08:18 +00:00
return redirect () -> to ( $route );
2025-01-07 14:31:43 +00:00
} elseif ( $this -> resource -> type () === 'service' ) {
2024-07-25 11:31:59 +00:00
$uuid = ( string ) new Cuid2 ;
2025-01-07 16:18:04 +00:00
$new_resource = $this -> resource -> replicate ([
'id' ,
'created_at' ,
'updated_at' ,
'additional_servers_count' ,
'additional_networks_count' ,
]) -> fill ([
2024-01-22 15:08:18 +00:00
'uuid' => $uuid ,
2024-06-10 20:43:34 +00:00
'name' => $this -> resource -> name . '-clone-' . $uuid ,
2024-01-22 15:08:18 +00:00
'destination_id' => $new_destination -> id ,
2025-01-07 18:26:25 +00:00
'destination_type' => $new_destination -> getMorphClass (),
'server_id' => $new_destination -> server_id , // server_id is probably not needed anymore because of the new polymorphic relationships (here it is needed for clone to work - but maybe we can drop the column)
2024-01-22 15:08:18 +00:00
]);
2025-01-07 18:26:25 +00:00
2024-01-22 15:08:18 +00:00
$new_resource -> save ();
foreach ( $new_resource -> applications () as $application ) {
$application -> update ([
'status' => 'exited' ,
]);
}
foreach ( $new_resource -> databases () as $database ) {
$database -> update ([
'status' => 'exited' ,
]);
}
$new_resource -> parse ();
$route = route ( 'project.service.configuration' , [
'project_uuid' => $this -> projectUuid ,
2024-11-22 15:03:20 +00:00
'environment_uuid' => $this -> environmentUuid ,
2024-01-22 15:08:18 +00:00
'service_uuid' => $new_resource -> uuid ,
2024-06-10 20:43:34 +00:00
]) . '#resource-operations' ;
2024-01-22 15:08:18 +00:00
return redirect () -> to ( $route );
}
}
2024-06-10 20:43:34 +00:00
2024-01-22 15:08:18 +00:00
public function moveTo ( $environment_id )
{
try {
2025-01-07 14:31:43 +00:00
$new_environment = Environment :: findOrFail ( $environment_id );
2024-01-22 15:08:18 +00:00
$this -> resource -> update ([
2024-06-10 20:43:34 +00:00
'environment_id' => $environment_id ,
2024-01-22 15:08:18 +00:00
]);
if ( $this -> resource -> type () === 'application' ) {
$route = route ( 'project.application.configuration' , [
'project_uuid' => $new_environment -> project -> uuid ,
2024-11-22 15:03:20 +00:00
'environment_uuid' => $new_environment -> uuid ,
2024-01-22 15:08:18 +00:00
'application_uuid' => $this -> resource -> uuid ,
2024-06-10 20:43:34 +00:00
]) . '#resource-operations' ;
2024-01-22 15:08:18 +00:00
return redirect () -> to ( $route );
2025-01-07 14:31:43 +00:00
} elseif ( str ( $this -> resource -> type ()) -> startsWith ( 'standalone-' )) {
2024-01-22 15:08:18 +00:00
$route = route ( 'project.database.configuration' , [
'project_uuid' => $new_environment -> project -> uuid ,
2024-11-22 15:03:20 +00:00
'environment_uuid' => $new_environment -> uuid ,
2024-01-22 15:08:18 +00:00
'database_uuid' => $this -> resource -> uuid ,
2024-06-10 20:43:34 +00:00
]) . '#resource-operations' ;
2024-01-22 15:08:18 +00:00
return redirect () -> to ( $route );
2025-01-07 14:31:43 +00:00
} elseif ( $this -> resource -> type () === 'service' ) {
2024-01-22 15:08:18 +00:00
$route = route ( 'project.service.configuration' , [
'project_uuid' => $new_environment -> project -> uuid ,
2024-11-22 15:03:20 +00:00
'environment_uuid' => $new_environment -> uuid ,
2024-01-22 15:08:18 +00:00
'service_uuid' => $this -> resource -> uuid ,
2024-06-10 20:43:34 +00:00
]) . '#resource-operations' ;
2024-01-22 15:08:18 +00:00
return redirect () -> to ( $route );
}
2025-01-07 14:31:43 +00:00
} catch ( \Throwable $e ) {
2024-01-22 15:08:18 +00:00
return handleError ( $e , $this );
}
}
2024-06-10 20:43:34 +00:00
2024-01-22 15:08:18 +00:00
public function render ()
{
return view ( 'livewire.project.shared.resource-operations' );
}
}