lasthourcloud/app/Livewire/Project/Resource/Create.php

99 lines
4.5 KiB
PHP
Raw Normal View History

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