feat: add custom redis conf

This commit is contained in:
Andras Bacsai 2023-10-12 17:29:29 +02:00
parent beae0b545f
commit 8f9949160c
3 changed files with 33 additions and 12 deletions

View File

@ -20,6 +20,9 @@ class StartRedis
public function handle(Server $server, StandaloneRedis $database)
{
$this->database = $database;
$startCommand = "redis-server --requirepass {$this->database->redis_password} --appendonly yes";
$container_name = $this->database->uuid;
$this->configuration_dir = database_configuration_dir() . '/' . $container_name;
@ -31,12 +34,14 @@ public function handle(Server $server, StandaloneRedis $database)
$persistent_storages = $this->generate_local_persistent_volumes();
$volume_names = $this->generate_local_persistent_volumes_only_volume_names();
$environment_variables = $this->generate_environment_variables();
$this->add_custom_redis();
$docker_compose = [
'version' => '3.8',
'services' => [
$container_name => [
'image' => $this->database->image,
'command' => "redis-server --requirepass {$this->database->redis_password} --appendonly yes",
'command' => $startCommand,
'container_name' => $container_name,
'environment' => $environment_variables,
'restart' => RESTART_MODE,
@ -80,16 +85,15 @@ public function handle(Server $server, StandaloneRedis $database)
if (count($volume_names) > 0) {
$docker_compose['volumes'] = $volume_names;
}
// if (count($this->init_scripts) > 0) {
// foreach ($this->init_scripts as $init_script) {
// $docker_compose['services'][$container_name]['volumes'][] = [
// 'type' => 'bind',
// 'source' => $init_script,
// 'target' => '/docker-entrypoint-initdb.d/' . basename($init_script),
// 'read_only' => true,
// ];
// }
// }
if (!is_null($this->database->redis_conf)) {
$docker_compose['services'][$container_name]['volumes'][] = [
'type' => 'bind',
'source' => $this->configuration_dir . '/redis.conf',
'target' => '/usr/local/etc/redis/redis.conf',
'read_only' => true,
];
$docker_compose['services'][$container_name]['command'] = $startCommand . ' /usr/local/etc/redis/redis.conf';
}
$docker_compose = Yaml::dump($docker_compose, 10);
$docker_compose_base64 = base64_encode($docker_compose);
$this->commands[] = "echo '{$docker_compose_base64}' | base64 -d > $this->configuration_dir/docker-compose.yml";
@ -141,4 +145,15 @@ private function generate_environment_variables()
return $environment_variables->all();
}
private function add_custom_redis()
{
if (is_null($this->database->redis_conf)) {
return;
}
$filename = 'redis.conf';
$content = $this->database->redis_conf;
$content_base64 = base64_encode($content);
$this->commands[] = "echo '{$content_base64}' | base64 -d > $this->configuration_dir/{$filename}";
}
}

View File

@ -16,6 +16,7 @@ class General extends Component
protected $rules = [
'database.name' => 'required',
'database.description' => 'nullable',
'database.redis_conf' => 'nullable',
'database.redis_password' => 'required',
'database.image' => 'required',
'database.ports_mappings' => 'nullable',
@ -25,7 +26,8 @@ class General extends Component
protected $validationAttributes = [
'database.name' => 'Name',
'database.description' => 'Description',
'database.redis_password' => 'Postgres User',
'database.redis_conf' => 'Redis Configuration',
'database.redis_password' => 'Redis Password',
'database.image' => 'Image',
'database.ports_mappings' => 'Port Mapping',
'database.is_public' => 'Is Public',
@ -34,6 +36,9 @@ class General extends Component
public function submit() {
try {
$this->validate();
if ($this->database->redis_conf === "") {
$this->database->redis_conf = null;
}
$this->database->save();
$this->emit('success', 'Database updated successfully.');
} catch (Exception $e) {

View File

@ -23,5 +23,6 @@
</div>
<x-forms.input label="Redis URL" readonly wire:model="db_url" />
</div>
<x-forms.textarea label="Custom Redis Configuration" rows="10" id="database.redis_conf" />
</form>
</div>