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