lasthourcloud/app/Http/Controllers/ProjectController.php

153 lines
6.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
2023-09-25 18:57:52 +00:00
use App\Models\EnvironmentVariable;
2023-05-22 13:47:40 +00:00
use App\Models\Project;
use App\Models\Server;
2023-09-25 13:48:43 +00:00
use App\Models\Service;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
class ProjectController extends Controller
{
2023-05-22 13:47:40 +00:00
public function all()
{
return view('projects', [
'projects' => Project::ownedByCurrentTeam()->get(),
'servers' => Server::ownedByCurrentTeam()->count(),
]);
2023-05-22 13:47:40 +00:00
}
2023-06-22 07:33:26 +00:00
public function edit()
{
$projectUuid = request()->route('project_uuid');
2023-08-22 15:44:49 +00:00
$teamId = currentTeam()->id;
2023-06-22 07:33:26 +00:00
$project = Project::where('team_id', $teamId)->where('uuid', $projectUuid)->first();
if (!$project) {
return redirect()->route('dashboard');
}
return view('project.edit', ['project' => $project]);
}
2023-05-22 13:47:40 +00:00
public function show()
{
2023-06-15 07:15:41 +00:00
$projectUuid = request()->route('project_uuid');
2023-08-22 15:44:49 +00:00
$teamId = currentTeam()->id;
2023-05-22 13:47:40 +00:00
2023-06-15 07:15:41 +00:00
$project = Project::where('team_id', $teamId)->where('uuid', $projectUuid)->first();
if (!$project) {
2023-04-25 12:43:35 +00:00
return redirect()->route('dashboard');
}
2023-04-12 20:13:17 +00:00
$project->load(['environments']);
2023-05-22 13:47:40 +00:00
return view('project.show', ['project' => $project]);
}
2023-03-31 11:32:07 +00:00
2023-05-11 13:20:02 +00:00
public function new()
2023-04-26 11:25:41 +00:00
{
2023-09-25 13:48:43 +00:00
$services = Cache::get('services', []);
$type = Str::of(request()->query('type'));
$destination_uuid = request()->query('destination');
2023-09-25 13:48:43 +00:00
$server_id = request()->query('server');
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)) {
$standalone_postgresql = create_standalone_postgresql($environment->id, $destination_uuid);
return redirect()->route('project.database.configuration', [
'project_uuid' => $project->uuid,
'environment_name' => $environment->name,
'database_uuid' => $standalone_postgresql->uuid,
]);
}
2023-09-25 13:48:43 +00:00
if ($type->startsWith('one-click-service-')) {
$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) {
$oneClickDotEnvs = Str::of(base64_decode($oneClickDotEnvs))->split('/\r\n|\r|\n/');
}
2023-09-27 13:48:19 +00:00
ray($oneClickDotEnvs);
2023-09-25 13:48:43 +00:00
if ($oneClickService) {
$service = Service::create([
'name' => "$oneClickServiceName-" . Str::random(10),
'docker_compose_raw' => base64_decode($oneClickService),
'environment_id' => $environment->id,
'server_id' => (int) $server_id,
]);
2023-09-26 12:45:52 +00:00
$service->name = "$oneClickServiceName-" . $service->uuid;
$service->save();
2023-09-27 13:48:19 +00:00
if ($oneClickDotEnvs->count() > 0) {
2023-09-26 12:45:52 +00:00
$oneClickDotEnvs->each(function ($value) use ($service) {
$key = Str::before($value, '=');
$value = Str::of(Str::after($value, '='));
2023-09-27 13:48:19 +00:00
$generatedValue = $value;
if ($value->contains('SERVICE_')) {
$command = $value->after('SERVICE_')->beforeLast('_');
switch ($command->value()) {
case 'PASSWORD':
$generatedValue = Str::password(symbols: false);
break;
case 'PASSWORD_64':
$generatedValue = Str::password(length: 64, symbols: false);
break;
case 'BASE64_64':
$generatedValue = Str::random(64);
break;
case 'BASE64_128':
$generatedValue = Str::random(128);
break;
case 'BASE64':
$generatedValue = Str::random(32);
break;
case 'USER':
$generatedValue = Str::random(16);
break;
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);
2023-09-25 13:48:43 +00:00
return redirect()->route('project.service', [
'service_uuid' => $service->uuid,
'environment_name' => $environment->name,
'project_uuid' => $project->uuid,
]);
}
}
2023-05-11 13:20:02 +00:00
return view('project.new', [
2023-09-25 13:48:43 +00:00
'type' => $type->value()
2023-05-11 13:20:02 +00:00
]);
2023-04-26 11:25:41 +00:00
}
public function resources()
{
2023-08-22 15:44:49 +00:00
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
2023-04-25 12:43:35 +00:00
return redirect()->route('dashboard');
}
2023-03-30 17:50:27 +00:00
$environment = $project->load(['environments'])->environments->where('name', request()->route('environment_name'))->first();
if (!$environment) {
2023-04-25 12:43:35 +00:00
return redirect()->route('dashboard');
2023-03-30 17:50:27 +00:00
}
2023-05-16 13:27:47 +00:00
return view('project.resources', [
'project' => $project,
'environment' => $environment
]);
}
}