wip: ui for services

This commit is contained in:
Andras Bacsai
2023-09-22 11:23:49 +02:00
parent 4ae7e46e81
commit 53d1fa0331
32 changed files with 575 additions and 250 deletions

View File

@@ -18,6 +18,7 @@ class StartService
$docker_compose_base64 = base64_encode($service->docker_compose);
$commands[] = "echo $docker_compose_base64 | base64 -d > docker-compose.yml";
$envs = $service->environment_variables()->get();
$commands[] = "rm -f .env || true";
foreach ($envs as $env) {
$commands[] = "echo '{$env->key}={$env->value}' >> .env";
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Http\Livewire\Project\Service;
use App\Models\ServiceApplication;
use Livewire\Component;
class Application extends Component
{
public ServiceApplication $application;
protected $rules = [
'application.human_name' => 'nullable',
'application.fqdn' => 'nullable',
];
public function render()
{
return view('livewire.project.service.application');
}
public function submit()
{
try {
$this->validate();
$this->application->save();
} catch (\Throwable $e) {
ray($e);
} finally {
$this->emit('generateDockerCompose');
}
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Http\Livewire\Project\Service;
use App\Models\ServiceApplication;
use App\Models\ServiceDatabase;
use Livewire\Component;
class Database extends Component
{
public ServiceDatabase $database;
protected $rules = [
'database.human_name' => 'nullable',
];
public function render()
{
return view('livewire.project.service.database');
}
public function submit()
{
try {
$this->validate();
$this->database->save();
} catch (\Throwable $e) {
ray($e);
} finally {
$this->emit('generateDockerCompose');
}
}
}

View File

@@ -15,73 +15,25 @@ class Index extends Component
public array $parameters;
public array $query;
public Collection $services;
protected $listeners = ['serviceStatusUpdated'];
protected $rules = [
'services.*.fqdn' => 'nullable',
'service.docker_compose_raw' => 'required',
'service.docker_compose' => 'required',
];
public function mount()
{
$this->services = collect([]);
$this->parameters = get_route_parameters();
$this->query = request()->query();
$this->service = Service::whereUuid($this->parameters['service_uuid'])->firstOrFail();
foreach ($this->service->applications as $application) {
$this->services->put($application->name, [
'fqdn' => $application->fqdn,
]);
}
// foreach ($this->service->databases as $database) {
// $this->services->put($database->name, $database->fqdn);
// }
}
public function render()
{
return view('livewire.project.service.index')->layout('layouts.app');
return view('livewire.project.service.index');
}
public function serviceStatusUpdated() {
ray('serviceStatusUpdated');
$this->check_status();
}
public function check_status()
{
dispatch_sync(new ContainerStatusJob($this->service->server));
$this->service->refresh();
}
public function submit()
{
try {
if ($this->services->count() === 0) {
return;
}
foreach ($this->services as $name => $value) {
$foundService = $this->service->applications()->whereName($name)->first();
if ($foundService) {
$foundService->fqdn = data_get($value, 'fqdn');
$foundService->save();
return;
}
$foundService = $this->service->databases()->whereName($name)->first();
if ($foundService) {
// $foundService->save();
return;
}
}
} catch (\Throwable $e) {
ray($e);
} finally {
$this->service->parse();
}
}
public function deploy()
{
public function save() {
$this->service->save();
$this->service->parse();
$activity = StartService::run($this->service);
$this->emit('newMonitorActivity', $activity->id);
}
public function stop() {
StopService::run($this->service);
$this->service->refresh();
$this->emit('refreshEnvs');
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace App\Http\Livewire\Project\Service;
use App\Actions\Service\StartService;
use App\Actions\Service\StopService;
use App\Jobs\ContainerStatusJob;
use App\Models\Service;
use Livewire\Component;
class Navbar extends Component
{
public Service $service;
public array $parameters;
public array $query;
protected $listeners = ['serviceStatusUpdated'];
public function render()
{
return view('livewire.project.service.navbar');
}
public function serviceStatusUpdated()
{
ray('serviceStatusUpdated');
$this->check_status();
}
public function check_status()
{
dispatch_sync(new ContainerStatusJob($this->service->server));
$this->service->refresh();
}
public function deploy()
{
$this->service->parse();
$activity = StartService::run($this->service);
$this->emit('newMonitorActivity', $activity->id);
}
public function stop()
{
StopService::run($this->service);
$this->service->refresh();
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Http\Livewire\Project\Service;
use App\Models\Service;
use App\Models\ServiceApplication;
use App\Models\ServiceDatabase;
use Illuminate\Support\Collection;
use Livewire\Component;
class Show extends Component
{
public Service $service;
public ServiceApplication $serviceApplication;
public ServiceDatabase $serviceDatabase;
public array $parameters;
public array $query;
public Collection $services;
protected $listeners = ['generateDockerCompose'];
public function mount()
{
$this->services = collect([]);
$this->parameters = get_route_parameters();
$this->query = request()->query();
$this->service = Service::whereUuid($this->parameters['service_uuid'])->firstOrFail();
$service = $this->service->applications()->whereName($this->parameters['service_name'])->first();
if ($service) {
$this->serviceApplication = $service;
} else {
$this->serviceDatabase = $this->service->databases()->whereName($this->parameters['service_name'])->first();
}
}
public function generateDockerCompose()
{
$this->service->parse();
}
public function render()
{
return view('livewire.project.service.show');
}
}

View File

@@ -19,13 +19,24 @@ class Danger extends Component
public function delete()
{
$destination = $this->resource->destination->getMorphClass()::where('id', $this->resource->destination->id)->first();
instant_remote_process(["docker rm -f {$this->resource->uuid}"], $destination->server);
$this->resource->delete();
return redirect()->route('project.resources', [
'project_uuid' => $this->parameters['project_uuid'],
'environment_name' => $this->parameters['environment_name']
]);
try {
if ($this->resource->type() === 'service') {
$server = $this->resource->server;
} else {
$destination = data_get($this->resource, 'destination');
if ($destination) {
$destination = $this->resource->destination->getMorphClass()::where('id', $this->resource->destination->id)->first();
$server = $destination->server;
}
}
instant_remote_process(["docker rm -f {$this->resource->uuid}"], $server);
$this->resource->delete();
return redirect()->route('project.resources', [
'project_uuid' => $this->parameters['project_uuid'],
'environment_name' => $this->parameters['environment_name']
]);
} catch (\Throwable $e) {
return handleError($e);
}
}
}

View File

@@ -31,7 +31,6 @@ class Add extends Component
public function submit()
{
ray('submitting');
$this->validate();
$this->emitUp('submit', [
'key' => $this->key,

View File

@@ -53,6 +53,7 @@ class All extends Component
$this->resource->environment_variables_preview()->delete();
} else {
$variables = parseEnvFormatToArray($this->variables);
ray($variables);
$existingVariables = $this->resource->environment_variables();
$this->resource->environment_variables()->delete();
}
@@ -68,11 +69,16 @@ class All extends Component
$environment->value = $variable;
$environment->is_build_time = false;
$environment->is_preview = $isPreview ? true : false;
if ($this->resource->type() === 'application') {
$environment->application_id = $this->resource->id;
}
if ($this->resource->type() === 'standalone-postgresql') {
$environment->standalone_postgresql_id = $this->resource->id;
switch ($this->resource->type()) {
case 'application':
$environment->application_id = $this->resource->id;
break;
case 'standalone-postgresql':
$environment->standalone_postgresql_id = $this->resource->id;
break;
case 'service':
$environment->service_id = $this->resource->id;
break;
}
$environment->save();
}

View File

@@ -10,7 +10,9 @@ class Show extends Component
{
public $parameters;
public ModelsEnvironmentVariable $env;
public string|null $modalId = null;
public ?string $modalId = null;
public string $type;
protected $rules = [
'env.key' => 'required|string',
'env.value' => 'required|string',
@@ -37,6 +39,7 @@ class Show extends Component
$this->validate();
$this->env->save();
$this->emit('success', 'Environment variable updated successfully.');
$this->emit('refreshEnvs');
}
public function delete()

View File

@@ -2,13 +2,16 @@
namespace App\Http\Livewire\Project\Shared\Storages;
use App\Models\LocalPersistentVolume;
use Livewire\Component;
use Visus\Cuid2\Cuid2;
class Show extends Component
{
public $storage;
public string|null $modalId = null;
public LocalPersistentVolume $storage;
public bool $isReadOnly = false;
public ?string $modalId = null;
protected $rules = [
'storage.name' => 'required|string',
'storage.mount_path' => 'required|string',

View File

@@ -170,40 +170,32 @@ class ContainerStatusJob implements ShouldQueue, ShouldBeEncrypted
if (in_array("$service->id-$app->name", $foundServices)) {
continue;
} else {
$exitedServices->push($service);
$app->update(['status' => 'exited']);
$exitedServices->push($app);
}
}
foreach ($dbs as $db) {
if (in_array("$service->id-$db->name", $foundServices)) {
continue;
} else {
$exitedServices->push($service);
$db->update(['status' => 'exited']);
$exitedServices->push($db);
}
}
}
}
$exitedServices = $exitedServices->unique('id');
ray($exitedServices);
// ray($exitedServices);
// foreach ($serviceIds as $serviceId) {
// $service = $services->where('id', $serviceId)->first();
// if ($service->status === 'exited') {
// continue;
// }
foreach ($exitedServices as $exitedService) {
if ($exitedService->status === 'exited') {
continue;
}
$name = data_get($exitedService, 'name');
$fqdn = data_get($exitedService, 'fqdn');
$containerName = $name ? "$name ($fqdn)" : $fqdn;
$project = data_get($service, 'environment.project');
$environment = data_get($service, 'environment');
// $name = data_get($service, 'name');
// $fqdn = data_get($service, 'fqdn');
// $containerName = $name ? "$name ($fqdn)" : $fqdn;
// $project = data_get($service, 'environment.project');
// $environment = data_get($service, 'environment');
// $url = base_url() . '/project/' . $project->uuid . "/" . $environment->name . "/service/" . $service->uuid;
// $this->server->team->notify(new ContainerStopped($containerName, $this->server, $url));
// }
$url = base_url() . '/project/' . $project->uuid . "/" . $environment->name . "/service/" . $service->uuid;
$this->server->team->notify(new ContainerStopped($containerName, $this->server, $url));
$exitedService->update(['status' => 'exited']);
}
$notRunningApplications = $applications->pluck('id')->diff($foundApplications);
foreach ($notRunningApplications as $applicationId) {

View File

@@ -3,7 +3,6 @@
namespace App\Models;
use App\Enums\ProxyTypes;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
@@ -14,27 +13,26 @@ class Service extends BaseModel
{
use HasFactory;
protected $guarded = [];
public function persistentStorages()
{
return $this->morphMany(LocalPersistentVolume::class, 'resource');
}
public function portsMappingsArray(): Attribute
{
return Attribute::make(
get: fn () => is_null($this->ports_mappings)
? []
: explode(',', $this->ports_mappings),
);
}
public function portsExposesArray(): Attribute
protected static function booted()
{
return Attribute::make(
get: fn () => is_null($this->ports_exposes)
? []
: explode(',', $this->ports_exposes)
);
static::deleted(function ($service) {
foreach($service->applications()->get() as $application) {
$application->persistentStorages()->delete();
}
foreach($service->databases()->get() as $database) {
$database->persistentStorages()->delete();
}
$service->environment_variables()->delete();
$service->applications()->delete();
$service->databases()->delete();
});
}
public function type()
{
return 'service';
}
public function applications()
{
return $this->hasMany(ServiceApplication::class);
@@ -47,10 +45,10 @@ class Service extends BaseModel
{
return $this->belongsTo(Environment::class);
}
public function server() {
public function server()
{
return $this->belongsTo(Server::class);
}
public function byName(string $name)
{
$app = $this->applications()->whereName($name)->first();
@@ -70,7 +68,6 @@ class Service extends BaseModel
public function parse(bool $isNew = false): Collection
{
// ray()->clearAll();
ray('Service parse');
if ($this->docker_compose_raw) {
$yaml = Yaml::parse($this->docker_compose_raw);
@@ -138,8 +135,8 @@ class Service extends BaseModel
}
}
}
$savedService->ports_exposes = $ports->implode(',');
$savedService->save();
// $savedService->ports_exposes = $ports->implode(',');
// $savedService->save();
}
// Collect volumes
$serviceVolumes = collect(data_get($service, 'volumes', []));
@@ -158,17 +155,38 @@ class Service extends BaseModel
return $key == $volumeName;
});
if (!$volumeExists) {
if (!Str::startsWith($volumeName, '/')) {
if (Str::startsWith($volumeName, '/')) {
$volumes->put($volumeName, $volumePath);
LocalPersistentVolume::updateOrCreate(
[
'mount_path' => $volumePath,
'resource_id' => $savedService->id,
'resource_type' => get_class($savedService)
],
[
'name' => Str::slug($volumeName, '-'),
'mount_path' => $volumePath,
'host_path' => $volumeName,
'resource_id' => $savedService->id,
'resource_type' => get_class($savedService)
]
);
} else {
$composeVolumes->put($volumeName, null);
}
$volumes->put($volumeName, $volumePath);
if ($isNew) {
LocalPersistentVolume::create([
'name' => $volumeName,
'mount_path' => $volumePath,
'resource_id' => $savedService->id,
'resource_type' => get_class($savedService)
]);
LocalPersistentVolume::updateOrCreate(
[
'mount_path' => $volumePath,
'resource_id' => $savedService->id,
'resource_type' => get_class($savedService)
],
[
'name' => $volumeName,
'mount_path' => $volumePath,
'host_path' => null,
'resource_id' => $savedService->id,
'resource_type' => get_class($savedService)
]
);
}
}
}
@@ -229,25 +247,26 @@ class Service extends BaseModel
}
if (!$envs->has($nakedName->value())) {
$envs->put($nakedName->value(), $nakedValue->value());
if ($isNew) {
EnvironmentVariable::create([
'key' => $nakedName->value(),
'value' => $nakedValue->value(),
'is_build_time' => false,
'service_id' => $this->id,
'is_preview' => false,
]);
}
EnvironmentVariable::updateOrCreate([
'key' => $nakedName->value(),
'service_id' => $this->id,
], [
'value' => $nakedValue->value(),
'is_build_time' => false,
'service_id' => $this->id,
'is_preview' => false,
]);
}
} else {
if (!$envs->has($nakedName->value())) {
$envs->put($nakedName->value(), null);
if ($isNew) {
$envExists = EnvironmentVariable::where('service_id', $this->id)->where('key', $nakedName->value())->exists();
if (!$envExists) {
EnvironmentVariable::create([
'key' => $nakedName->value(),
'value' => null,
'is_build_time' => false,
'service_id' => $this->id,
'is_build_time' => false,
'is_preview' => false,
]);
}
@@ -259,31 +278,31 @@ class Service extends BaseModel
$generatedValue = null;
if ($variableName->startsWith('SERVICE_USER')) {
$generatedValue = Str::random(10);
if ($isNew) {
if (!$envs->has($variableName->value())) {
$envs->put($variableName->value(), $generatedValue);
EnvironmentVariable::create([
'key' => $variableName->value(),
'value' => $generatedValue,
'is_build_time' => false,
'service_id' => $this->id,
'is_preview' => false,
]);
}
if (!$envs->has($variableName->value())) {
$envs->put($variableName->value(), $generatedValue);
EnvironmentVariable::updateOrCreate([
'key' => $variableName->value(),
'service_id' => $this->id,
], [
'value' => $generatedValue,
'is_build_time' => false,
'service_id' => $this->id,
'is_preview' => false,
]);
}
} else if ($variableName->startsWith('SERVICE_PASSWORD')) {
$generatedValue = Str::password(symbols: false);
if ($isNew) {
if (!$envs->has($variableName->value())) {
$envs->put($variableName->value(), $generatedValue);
EnvironmentVariable::create([
'key' => $variableName->value(),
'value' => $generatedValue,
'is_build_time' => false,
'service_id' => $this->id,
'is_preview' => false,
]);
}
if (!$envs->has($variableName->value())) {
$envs->put($variableName->value(), $generatedValue);
EnvironmentVariable::updateOrCreate([
'key' => $variableName->value(),
'service_id' => $this->id,
], [
'value' => $generatedValue,
'is_build_time' => false,
'service_id' => $this->id,
'is_preview' => false,
]);
}
} else if ($variableName->startsWith('SERVICE_FQDN')) {
if ($fqdn) {
@@ -324,20 +343,6 @@ class Service extends BaseModel
data_forget($service, 'documentation');
return $service;
});
// $services = $services->map(function ($service, $serviceName) {
// $dependsOn = collect(data_get($service, 'depends_on', []));
// $dependsOn = $dependsOn->map(function ($value) {
// return "$value-{$this->uuid}";
// });
// data_set($service, 'depends_on', $dependsOn->toArray());
// return $service;
// });
// $renamedServices = collect([]);
// collect($services)->map(function ($service, $serviceName) use ($renamedServices) {
// $newServiceName = "$serviceName-$this->uuid";
// $renamedServices->put($newServiceName, $service);
// });
$finalServices = [
'version' => $dockerComposeVersion,
'services' => $services->toArray(),

View File

@@ -2,6 +2,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class ServiceApplication extends BaseModel
@@ -9,4 +10,12 @@ class ServiceApplication extends BaseModel
use HasFactory;
protected $guarded = [];
public function type()
{
return 'service';
}
public function persistentStorages()
{
return $this->morphMany(LocalPersistentVolume::class, 'resource');
}
}

View File

@@ -9,4 +9,12 @@ class ServiceDatabase extends BaseModel
use HasFactory;
protected $guarded = [];
public function type()
{
return 'service';
}
public function persistentStorages()
{
return $this->morphMany(LocalPersistentVolume::class, 'resource');
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace App\View\Components\Services;
use App\Models\Service;
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Illuminate\View\Component;
class Links extends Component
{
public Collection $links;
public function __construct(public Service $service)
{
$this->links = collect([]);
$service->applications()->get()->map(function ($application) {
$this->links->push($application->fqdn);
});
}
/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
return view('components.services.links');
}
}