2023-03-28 20:13:08 +00:00
|
|
|
<?php
|
|
|
|
|
2024-01-07 15:23:41 +00:00
|
|
|
namespace App\Livewire\Project\Resource;
|
2023-03-28 20:13:08 +00:00
|
|
|
|
2023-09-25 18:57:52 +00:00
|
|
|
use App\Models\EnvironmentVariable;
|
2023-09-25 13:48:43 +00:00
|
|
|
use App\Models\Service;
|
2023-10-02 15:12:50 +00:00
|
|
|
use App\Models\StandaloneDocker;
|
2024-01-07 15:23:41 +00:00
|
|
|
use Livewire\Component;
|
2023-03-28 20:13:08 +00:00
|
|
|
|
2024-01-07 15:23:41 +00:00
|
|
|
class Create extends Component
|
2023-03-28 20:13:08 +00:00
|
|
|
{
|
2024-01-07 15:23:41 +00:00
|
|
|
public $type;
|
2024-02-28 12:48:39 +00:00
|
|
|
public function mount()
|
|
|
|
{
|
2024-01-07 15:23:41 +00:00
|
|
|
$type = str(request()->query('type'));
|
2023-08-09 13:57:53 +00:00
|
|
|
$destination_uuid = request()->query('destination');
|
2023-09-28 20:20:49 +00:00
|
|
|
$server_id = request()->query('server_id');
|
2023-08-09 13:57:53 +00:00
|
|
|
|
2023-08-22 15:44:49 +00:00
|
|
|
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
|
2023-04-26 11:25:41 +00:00
|
|
|
if (!$project) {
|
|
|
|
return redirect()->route('dashboard');
|
|
|
|
}
|
|
|
|
$environment = $project->load(['environments'])->environments->where('name', request()->route('environment_name'))->first();
|
|
|
|
if (!$environment) {
|
|
|
|
return redirect()->route('dashboard');
|
|
|
|
}
|
2024-05-21 08:17:32 +00:00
|
|
|
if (isset($type) && isset($destination_uuid) && isset($server_id)) {
|
|
|
|
$services = getServiceTemplates();
|
|
|
|
|
|
|
|
if (in_array($type, DATABASE_TYPES)) {
|
|
|
|
if ($type->value() === "postgresql") {
|
|
|
|
$database = create_standalone_postgresql($environment->id, $destination_uuid);
|
|
|
|
} else if ($type->value() === 'redis') {
|
|
|
|
$database = create_standalone_redis($environment->id, $destination_uuid);
|
|
|
|
} else if ($type->value() === 'mongodb') {
|
|
|
|
$database = create_standalone_mongodb($environment->id, $destination_uuid);
|
|
|
|
} else if ($type->value() === 'mysql') {
|
|
|
|
$database = create_standalone_mysql($environment->id, $destination_uuid);
|
|
|
|
} else if ($type->value() === 'mariadb') {
|
|
|
|
$database = create_standalone_mariadb($environment->id, $destination_uuid);
|
|
|
|
} else if ($type->value() === 'keydb') {
|
|
|
|
$database = create_standalone_keydb($environment->id, $destination_uuid);
|
|
|
|
} else if ($type->value() === 'dragonfly') {
|
|
|
|
$database = create_standalone_dragonfly($environment->id, $destination_uuid);
|
|
|
|
} else if ($type->value() === 'clickhouse') {
|
|
|
|
$database = create_standalone_clickhouse($environment->id, $destination_uuid);
|
2023-09-25 18:57:52 +00:00
|
|
|
}
|
2024-05-21 08:17:32 +00:00
|
|
|
return redirect()->route('project.database.configuration', [
|
2023-09-25 13:48:43 +00:00
|
|
|
'project_uuid' => $project->uuid,
|
2024-05-21 08:17:32 +00:00
|
|
|
'environment_name' => $environment->name,
|
|
|
|
'database_uuid' => $database->uuid,
|
2023-09-25 13:48:43 +00:00
|
|
|
]);
|
|
|
|
}
|
2024-05-21 08:17:32 +00:00
|
|
|
if ($type->startsWith('one-click-service-') && !is_null((int)$server_id)) {
|
|
|
|
$oneClickServiceName = $type->after('one-click-service-')->value();
|
|
|
|
$oneClickService = data_get($services, "$oneClickServiceName.compose");
|
|
|
|
$oneClickDotEnvs = data_get($services, "$oneClickServiceName.envs", null);
|
|
|
|
if ($oneClickDotEnvs) {
|
|
|
|
$oneClickDotEnvs = str(base64_decode($oneClickDotEnvs))->split('/\r\n|\r|\n/')->filter(function ($value) {
|
|
|
|
return !empty($value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if ($oneClickService) {
|
|
|
|
$destination = StandaloneDocker::whereUuid($destination_uuid)->first();
|
|
|
|
$service_payload = [
|
|
|
|
'name' => "$oneClickServiceName-" . str()->random(10),
|
|
|
|
'docker_compose_raw' => base64_decode($oneClickService),
|
|
|
|
'environment_id' => $environment->id,
|
|
|
|
'service_type' => $oneClickServiceName,
|
|
|
|
'server_id' => (int) $server_id,
|
|
|
|
'destination_id' => $destination->id,
|
|
|
|
'destination_type' => $destination->getMorphClass(),
|
|
|
|
];
|
|
|
|
if ($oneClickServiceName === 'cloudflared') {
|
|
|
|
data_set($service_payload, 'connect_to_docker_network', true);
|
|
|
|
}
|
|
|
|
$service = Service::create($service_payload);
|
|
|
|
$service->name = "$oneClickServiceName-" . $service->uuid;
|
|
|
|
$service->save();
|
|
|
|
if ($oneClickDotEnvs?->count() > 0) {
|
|
|
|
$oneClickDotEnvs->each(function ($value) use ($service) {
|
|
|
|
$key = str()->before($value, '=');
|
|
|
|
$value = str(str()->after($value, '='));
|
|
|
|
$generatedValue = $value;
|
|
|
|
if ($value->contains('SERVICE_')) {
|
|
|
|
$command = $value->after('SERVICE_')->beforeLast('_');
|
|
|
|
$generatedValue = generateEnvValue($command->value(), $service);
|
|
|
|
}
|
|
|
|
EnvironmentVariable::create([
|
|
|
|
'key' => $key,
|
|
|
|
'value' => $generatedValue,
|
|
|
|
'service_id' => $service->id,
|
|
|
|
'is_build_time' => false,
|
|
|
|
'is_preview' => false,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
$service->parse(isNew: true);
|
|
|
|
return redirect()->route('project.service.configuration', [
|
|
|
|
'service_uuid' => $service->uuid,
|
|
|
|
'environment_name' => $environment->name,
|
|
|
|
'project_uuid' => $project->uuid,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->type = $type->value();
|
2023-09-25 13:48:43 +00:00
|
|
|
}
|
2023-04-26 11:25:41 +00:00
|
|
|
}
|
2024-01-07 15:23:41 +00:00
|
|
|
public function render()
|
2023-03-28 20:13:08 +00:00
|
|
|
{
|
2024-01-07 15:23:41 +00:00
|
|
|
return view('livewire.project.resource.create');
|
2023-03-28 20:13:08 +00:00
|
|
|
}
|
|
|
|
}
|