lasthourcloud/app/Http/Livewire/Project/New/DockerCompose.php

75 lines
2.2 KiB
PHP
Raw Normal View History

2023-09-19 13:51:13 +00:00
<?php
namespace App\Http\Livewire\Project\New;
use App\Models\Project;
2023-09-20 13:42:41 +00:00
use App\Models\Service;
2023-09-19 13:51:13 +00:00
use Livewire\Component;
use Illuminate\Support\Str;
class DockerCompose extends Component
{
public string $dockercompose = '';
public array $parameters;
public array $query;
public function mount()
{
$this->parameters = get_route_parameters();
$this->query = request()->query();
if (isDev()) {
$this->dockercompose = 'services:
ghost:
2023-09-22 10:08:51 +00:00
documentation: https://ghost.org/docs/config
2023-09-19 13:51:13 +00:00
image: ghost:5
volumes:
- ghost-content-data:/var/lib/ghost/content
environment:
- url=$SERVICE_FQDN_GHOST
- database__client=mysql
- database__connection__host=mysql
- database__connection__user=$SERVICE_USER_MYSQL
- database__connection__password=$SERVICE_PASSWORD_MYSQL
- database__connection__database=${MYSQL_DATABASE-ghost}
depends_on:
- mysql
mysql:
documentation: https://hub.docker.com/_/mysql
image: mysql:8.0
volumes:
- ghost-mysql-data:/var/lib/mysql
environment:
- MYSQL_USER=${SERVICE_USER_MYSQL}
- MYSQL_PASSWORD=${SERVICE_PASSWORD_MYSQL}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_ROOT_PASSWORD=${SERVICE_PASSWORD_MYSQL_ROOT}
';
}
}
public function submit()
{
$this->validate([
'dockercompose' => 'required'
]);
2023-09-21 15:48:31 +00:00
$server_id = $this->query['server_id'];
2023-09-19 13:51:13 +00:00
$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
2023-09-21 15:48:31 +00:00
$service = Service::create([
'name' => 'service' . Str::random(10),
'docker_compose_raw' => $this->dockercompose,
'environment_id' => $environment->id,
'server_id' => (int) $server_id,
]);
$service->name = "service-$service->uuid";
$service->parse(isNew: true);
2023-09-19 13:51:13 +00:00
2023-09-20 13:42:41 +00:00
return redirect()->route('project.service', [
'service_uuid' => $service->uuid,
2023-09-19 13:51:13 +00:00
'environment_name' => $environment->name,
'project_uuid' => $project->uuid,
]);
}
}