Skip to content

Commit

Permalink
puh, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Sep 26, 2023
1 parent 03c9793 commit fabb973
Show file tree
Hide file tree
Showing 32 changed files with 386 additions and 127 deletions.
1 change: 1 addition & 0 deletions app/Actions/Service/StartService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function handle(Service $service)
{
$service->saveComposeConfigs();
$commands[] = "cd " . $service->workdir();
$commands[] = "echo '####### Saved configuration files to {$service->workdir()}.'";
$commands[] = "echo '####### Starting service {$service->name} on {$service->server->name}.'";
$commands[] = "echo '####### Pulling images.'";
$commands[] = "docker compose pull";
Expand Down
34 changes: 29 additions & 5 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,50 @@ public function new()
if ($type->startsWith('one-click-service-')) {
$oneClickServiceName = $type->after('one-click-service-')->value();
$oneClickService = data_get($services, "$oneClickServiceName.compose");
$oneClickDotEnvs = collect(data_get($services, "$oneClickServiceName.envs", []));
$oneClickDotEnvs = data_get($services, "$oneClickServiceName.envs", null);
$oneClickRequiredFqdn = data_get($services, "$oneClickServiceName.generateFqdn", []);
$oneClickRequiredFqdn = collect($oneClickRequiredFqdn);
if ($oneClickDotEnvs) {
$oneClickDotEnvs = Str::of(base64_decode($oneClickDotEnvs))->split('/\r\n|\r|\n/');
}
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,
]);
if ($oneClickDotEnvs->count() > 0) {
$oneClickDotEnvs->each(function ($value, $key) use ($service) {
$service->name = "$oneClickServiceName-" . $service->uuid;
$service->save();
if ($oneClickDotEnvs && $oneClickDotEnvs->count() > 0) {
$oneClickDotEnvs->each(function ($value) use ($service) {
$key = Str::before($value, '=');
$value = Str::of(Str::after($value, '='));
if ($value->contains('SERVICE_USER')) {
$value = Str::of(Str::random(10));
}
if ($value->contains('SERVICE_PASSWORD')) {
$value = Str::of(Str::password(symbols: false));
}
if ($value->contains('SERVICE_BASE64')) {
$length = Str::of($value)->after('SERVICE_BASE64_')->beforeLast('_')->value();
if (is_numeric($length)) {
$length = (int) $length;
} else {
$length = 1;
}
$value = Str::of(base64_encode(Str::password(length: $length, symbols: false)));
}
EnvironmentVariable::create([
'key' => $key,
'value' => $value,
'value' => $value->value(),
'service_id' => $service->id,
'is_build_time' => false,
'is_preview' => false,
]);
});
}
$service->parse(isNew: true);
$service->parse(isNew: true, requiredFqdns: $oneClickRequiredFqdn);

return redirect()->route('project.service', [
'service_uuid' => $service->uuid,
Expand Down
14 changes: 12 additions & 2 deletions app/Http/Livewire/Project/New/DockerCompose.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Livewire\Project\New;

use App\Models\EnvironmentVariable;
use App\Models\Project;
use App\Models\Service;
use Livewire\Component;
Expand All @@ -11,6 +12,7 @@
class DockerCompose extends Component
{
public string $dockerComposeRaw = '';
public string $envFile = '';
public array $parameters;
public array $query;
public function mount()
Expand Down Expand Up @@ -45,13 +47,22 @@ public function submit()

$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();

$service = Service::create([
'name' => 'service' . Str::random(10),
'docker_compose_raw' => $this->dockerComposeRaw,
'environment_id' => $environment->id,
'server_id' => (int) $server_id,
]);
$variables = parseEnvFormatToArray($this->envFile);
foreach ($variables as $key => $variable) {
EnvironmentVariable::create([
'key' => $key,
'value' => $variable,
'is_build_time' => false,
'is_preview' => false,
'service_id' => $service->id,
]);
}
$service->name = "service-$service->uuid";

$service->parse(isNew: true);
Expand All @@ -64,6 +75,5 @@ public function submit()
} catch (\Throwable $e) {
return handleError($e, $this);
}

}
}
3 changes: 2 additions & 1 deletion app/Http/Livewire/Project/New/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Select extends Component
protected $queryString = [
'server',
];

public function mount()
{
$this->parameters = get_route_parameters();
Expand All @@ -46,6 +47,7 @@ public function mount()
// return handleError($e, $this);
// }
// }

public function loadThings()
{
$this->loadServices();
Expand Down Expand Up @@ -77,7 +79,6 @@ public function loadServices(bool $forceReload = false)
});
}
$this->services = $cached;

} catch (\Throwable $e) {
ray($e);
return handleError($e, $this);
Expand Down
13 changes: 9 additions & 4 deletions app/Http/Livewire/Project/Service/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@
namespace App\Http\Livewire\Project\Service;

use App\Models\ServiceApplication;
use Illuminate\Support\Collection;
use Livewire\Component;

class Application extends Component
{
public ServiceApplication $application;
public $parameters;
public $fileStorages = null;
public $fileStorages;
protected $listeners = ["refreshFileStorages"];
protected $rules = [
'application.human_name' => 'nullable',
'application.description' => 'nullable',
'application.fqdn' => 'nullable',
'application.image_tag' => 'required',
'application.ignore_from_status' => 'required|boolean',
'application.image' => 'required',
'application.exclude_from_status' => 'required|boolean',
'application.required_fqdn' => 'required|boolean',
];
public function render()
{
return view('livewire.project.service.application');
}
public function instantSave() {
public function instantSave()
{
$this->submit();
}
public function refreshFileStorages()
Expand All @@ -42,13 +45,15 @@ public function delete()
public function mount()
{
$this->parameters = get_route_parameters();
$this->fileStorages = collect();
$this->refreshFileStorages();
}
public function submit()
{
try {
$this->validate();
$this->application->save();
switchImage($this->application);
$this->emit('success', 'Application saved successfully.');
} catch (\Throwable $e) {
ray($e);
Expand Down
15 changes: 12 additions & 3 deletions app/Http/Livewire/Project/Service/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,34 @@
class Database extends Component
{
public ServiceDatabase $database;
public $fileStorages;
protected $listeners = ["refreshFileStorages"];
protected $rules = [
'database.human_name' => 'nullable',
'database.description' => 'nullable',
'database.image_tag' => 'required',
'database.ignore_from_status' => 'required|boolean',

'database.image' => 'required',
'database.exclude_from_status' => 'required|boolean',
];
public function render()
{
return view('livewire.project.service.database');
}
public function mount() {
$this->refreshFileStorages();
}
public function instantSave() {
$this->submit();
}
public function refreshFileStorages()
{
$this->fileStorages = $this->database->fileStorages()->get();
}
public function submit()
{
try {
$this->validate();
$this->database->save();
switchImage($this->database);
$this->emit('success', 'Database saved successfully.');
} catch (\Throwable $e) {
ray($e);
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Livewire/Project/Service/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class FileStorage extends Component
public string $fs_path;

protected $rules = [
'fileStorage.is_directory' => 'required',
'fileStorage.fs_path' => 'required',
'fileStorage.mount_path' => 'required',
'fileStorage.content' => 'nullable',
Expand All @@ -39,6 +40,9 @@ public function submit()
return handleError($e, $this);
}
}
public function instantSave() {
$this->submit();
}
public function render()
{
return view('livewire.project.service.file-storage');
Expand Down
39 changes: 32 additions & 7 deletions app/Http/Livewire/Project/Service/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,57 @@
namespace App\Http\Livewire\Project\Service;

use App\Models\Service;
use App\Models\ServiceApplication;
use DanHarrin\LivewireRateLimiting\WithRateLimiting;
use Livewire\Component;

class Index extends Component
{
use WithRateLimiting;
public Service $service;
public $applications;
public $databases;
public array $parameters;
public array $query;

protected $rules = [
'service.docker_compose_raw' => 'required',
'service.docker_compose' => 'required',
'service.name' => 'required',
'service.description' => 'nullable',
];

public function manualRefreshStack() {
try {
$this->rateLimit(5);
$this->refreshStack();
} catch(\Throwable $e) {
return handleError($e, $this);
}
}
public function refreshStack()
{
$this->applications = $this->service->applications->sort();
$this->applications->each(function ($application) {
$application->fileStorages()->get()->each(function ($fileStorage) use ($application) {
if (!$fileStorage->is_directory && $fileStorage->content == null) {
$application->hasMissingFiles = true;
}
});
});
$this->databases = $this->service->databases->sort();
$this->databases->each(function ($database) {
$database->fileStorages()->get()->each(function ($fileStorage) use ($database) {
if (!$fileStorage->is_directory && $fileStorage->content == null) {
$database->hasMissingFiles = true;
}
});
});
$this->emit('success', 'Stack refreshed successfully.');
}
public function mount()
{
$this->parameters = get_route_parameters();
$this->query = request()->query();
$this->service = Service::whereUuid($this->parameters['service_uuid'])->firstOrFail();
$this->applications = $this->service->applications->sort();
$this->databases = $this->service->databases->sort();

$this->refreshStack();
}
public function render()
{
Expand All @@ -43,7 +68,7 @@ public function save()
$this->emit('refreshEnvs');
$this->emit('success', 'Service saved successfully.');
$this->service->saveComposeConfigs();
} catch(\Throwable $e) {
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
Expand Down
1 change: 1 addition & 0 deletions app/Http/Livewire/Project/Service/Navbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ public function stop()
{
StopService::run($this->service);
$this->service->refresh();
$this->emit('success', 'Service stopped successfully.');
}
}
1 change: 1 addition & 0 deletions app/Http/Livewire/Project/Shared/Danger.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function mount()

public function delete()
{
// Should be queued
try {
if ($this->resource->type() === 'service') {
$server = $this->resource->server;
Expand Down
5 changes: 3 additions & 2 deletions app/Http/Livewire/Project/Shared/EnvironmentVariable/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class Add extends Component
public $parameters;
public bool $is_preview = false;
public string $key;
public string $value;
public ?string $value = null;
public bool $is_build_time = false;

protected $listeners = ['clearAddEnv' => 'clear'];
protected $rules = [
'key' => 'required|string',
'value' => 'required|string',
'value' => 'nullable',
'is_build_time' => 'required|boolean',
];
protected $validationAttributes = [
Expand All @@ -32,6 +32,7 @@ public function mount()
public function submit()
{
$this->validate();
ray($this->key, $this->value, $this->is_build_time);
$this->emitUp('submit', [
'key' => $this->key,
'value' => $this->value,
Expand Down
17 changes: 11 additions & 6 deletions app/Http/Livewire/Project/Shared/EnvironmentVariable/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public function saveVariables($isPreview)
$this->resource->environment_variables_preview()->delete();
} else {
$variables = parseEnvFormatToArray($this->variables);
ray($variables);
$existingVariables = $this->resource->environment_variables();
$this->resource->environment_variables()->delete();
}
foreach ($variables as $key => $variable) {
ray($key, $variable);
$found = $existingVariables->where('key', $key)->first();
if ($found) {
$found->value = $variable;
Expand Down Expand Up @@ -110,11 +110,16 @@ public function submit($data)
$environment->is_build_time = $data['is_build_time'];
$environment->is_preview = $data['is_preview'];

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();
$this->refreshEnvs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Show extends Component

protected $rules = [
'env.key' => 'required|string',
'env.value' => 'required|string',
'env.value' => 'nullable',
'env.is_build_time' => 'required|boolean',
];
protected $validationAttributes = [
Expand Down
Loading

0 comments on commit fabb973

Please sign in to comment.