2023-10-12 07:12:46 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project\Service;
|
2023-10-12 07:12:46 +00:00
|
|
|
|
2024-03-21 11:44:32 +00:00
|
|
|
use App\Models\Service;
|
2024-04-15 14:54:03 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2023-10-12 07:12:46 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class StackForm extends Component
|
|
|
|
{
|
2024-03-21 11:44:32 +00:00
|
|
|
public Service $service;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-04-15 14:54:03 +00:00
|
|
|
public Collection $fields;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
|
|
|
protected $listeners = ['saveCompose'];
|
|
|
|
|
2023-11-13 10:09:21 +00:00
|
|
|
public $rules = [
|
2023-10-12 07:12:46 +00:00
|
|
|
'service.docker_compose_raw' => 'required',
|
|
|
|
'service.docker_compose' => 'required',
|
|
|
|
'service.name' => 'required',
|
|
|
|
'service.description' => 'nullable',
|
2024-01-21 13:30:03 +00:00
|
|
|
'service.connect_to_docker_network' => 'nullable',
|
2023-10-12 07:12:46 +00:00
|
|
|
];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-11-13 10:09:21 +00:00
|
|
|
public $validationAttributes = [];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-11-13 10:09:21 +00:00
|
|
|
public function mount()
|
|
|
|
{
|
2024-04-15 14:54:03 +00:00
|
|
|
$this->fields = collect([]);
|
2023-11-13 10:09:21 +00:00
|
|
|
$extraFields = $this->service->extraFields();
|
|
|
|
foreach ($extraFields as $serviceName => $fields) {
|
|
|
|
foreach ($fields as $fieldKey => $field) {
|
|
|
|
$key = data_get($field, 'key');
|
|
|
|
$value = data_get($field, 'value');
|
2023-11-24 20:38:39 +00:00
|
|
|
$rules = data_get($field, 'rules', 'nullable');
|
2023-11-13 10:09:21 +00:00
|
|
|
$isPassword = data_get($field, 'isPassword');
|
2024-04-15 14:54:03 +00:00
|
|
|
$this->fields->put($key, [
|
2024-06-10 20:43:34 +00:00
|
|
|
'serviceName' => $serviceName,
|
|
|
|
'key' => $key,
|
|
|
|
'name' => $fieldKey,
|
|
|
|
'value' => $value,
|
|
|
|
'isPassword' => $isPassword,
|
|
|
|
'rules' => $rules,
|
2024-04-15 14:54:03 +00:00
|
|
|
]);
|
|
|
|
|
2023-11-13 10:09:21 +00:00
|
|
|
$this->rules["fields.$key.value"] = $rules;
|
|
|
|
$this->validationAttributes["fields.$key.value"] = $fieldKey;
|
|
|
|
}
|
2023-11-11 20:32:41 +00:00
|
|
|
}
|
2024-05-31 10:55:14 +00:00
|
|
|
$this->fields = $this->fields->sortBy('name');
|
2023-11-11 20:32:41 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-10-12 07:12:46 +00:00
|
|
|
public function saveCompose($raw)
|
|
|
|
{
|
|
|
|
$this->service->docker_compose_raw = $raw;
|
|
|
|
$this->submit();
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-15 19:44:01 +00:00
|
|
|
public function instantSave()
|
|
|
|
{
|
2024-01-21 13:30:03 +00:00
|
|
|
$this->service->save();
|
2024-05-27 22:41:42 +00:00
|
|
|
$this->dispatch('success', 'Service settings saved.');
|
2024-01-21 13:30:03 +00:00
|
|
|
}
|
2023-10-12 07:12:46 +00:00
|
|
|
|
|
|
|
public function submit()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->validate();
|
2024-03-14 08:21:48 +00:00
|
|
|
$isValid = validateComposeFile($this->service->docker_compose_raw, $this->service->server->id);
|
|
|
|
if ($isValid !== 'OK') {
|
|
|
|
throw new \Exception("Invalid docker-compose file.\n$isValid");
|
|
|
|
}
|
2023-10-12 07:12:46 +00:00
|
|
|
$this->service->save();
|
2023-11-13 10:09:21 +00:00
|
|
|
$this->service->saveExtraFields($this->fields);
|
2023-10-12 07:12:46 +00:00
|
|
|
$this->service->parse();
|
|
|
|
$this->service->refresh();
|
|
|
|
$this->service->saveComposeConfigs();
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('refreshEnvs');
|
2024-02-22 13:53:42 +00:00
|
|
|
$this->dispatch('success', 'Service saved.');
|
2023-10-12 07:12:46 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return handleError($e, $this);
|
2024-04-12 10:44:49 +00:00
|
|
|
} finally {
|
|
|
|
if (is_null($this->service->config_hash)) {
|
|
|
|
$this->service->isConfigurationChanged(true);
|
|
|
|
} else {
|
|
|
|
$this->dispatch('configurationChanged');
|
|
|
|
}
|
2023-10-12 07:12:46 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-10-12 07:12:46 +00:00
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
return view('livewire.project.service.stack-form');
|
|
|
|
}
|
|
|
|
}
|