Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
lippserd committed Dec 30, 2024
1 parent 6908c3f commit 11f9d5a
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 269 deletions.
312 changes: 114 additions & 198 deletions application/controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,10 @@ public function databaseAction(): void

public function notificationsAction()
{
// Check sources in use, maybe list them, delete unused automatically with a checkbox or something.
// Add cluster uuid to username.
$this->mergeTabs($this->Module()->getConfigTabs()->activate('notifications'));

$kconfig = [];
$q = KConfig::on(Database::connection())
->filter(Filter::equal('key', [
KConfig::NOTIFICATIONS_URL,
KConfig::NOTIFICATIONS_USERNAME,
KConfig::NOTIFICATIONS_KUBERNETES_WEB_URL
]));

foreach ($q as $r) {
$kconfig[$r['key']] = $r;
}

$sourceForm = new class (NotificationsDatabase::get()) extends SourceForm {
public function hasBeenSent(): bool
{
Expand All @@ -88,99 +78,108 @@ public function hasBeenSubmitted(): bool
}
};

$form = (new NotificationsConfigForm())
->setKConfig($kconfig)
->on(
NotificationsConfigForm::ON_SUCCESS,
function (NotificationsConfigForm $form) use ($kconfig, $sourceForm) {
if ($form->isLocked()) {
$form->addMessage($this->translate('Notifications configuration is locked.'));
try {

return;
}
$form = (new NotificationsConfigForm())
->on(
NotificationsConfigForm::ON_SUCCESS,
function (NotificationsConfigForm $form) use ($sourceForm) {
if ($form->isLocked()) {
$form->addMessage($this->translate('Notifications configuration is locked.'));

$values = $form->getValues();
return;
}

if (
! ($kconfig[KConfig::NOTIFICATIONS_USERNAME]->locked ?? false)
&& ($kconfig[KConfig::NOTIFICATIONS_USERNAME]->value ?? '') === ''
) {
try {
$values[KConfig::NOTIFICATIONS_PASSWORD] = $this->createSource($sourceForm);
} catch (Throwable $e) {
Logger::error($e);
Logger::error($e->getTraceAsString());
$clusterUuid = $form->getClusterUuid();
$kconfig = $form->getKConfig($clusterUuid);
$values = $form->getValues();

$form->addMessage($e->getMessage());
if (
! ($kconfig[KConfig::NOTIFICATIONS_USERNAME]->locked ?? false)
&& ($kconfig[KConfig::NOTIFICATIONS_USERNAME]->value ?? '') === ''
) {
try {
$values[KConfig::NOTIFICATIONS_PASSWORD] = $this->createSource($sourceForm, $clusterUuid);
} catch (Throwable $e) {
Logger::error($e);
Logger::error($e->getTraceAsString());

return;
}
$form->addMessage($e->getMessage());

return;
}

/** @var ?Source $source */
$source = Source::on(NotificationsDatabase::get())
->filter(Filter::all(
Filter::equal('name', KConfig::DEFAULT_NOTIFICATIONS_NAME),
Filter::equal('type', KConfig::DEFAULT_NOTIFICATIONS_TYPE)
))
->first();
/** @var ?Source $source */
$source = Source::on(NotificationsDatabase::get())
->filter(Filter::all(
Filter::equal('name', KConfig::DEFAULT_NOTIFICATIONS_NAME . "($clusterUuid)"),
Filter::equal('type', KConfig::DEFAULT_NOTIFICATIONS_TYPE)
))
->first();

if ($source === null) {
throw new LogicException($this->translate('Source not found'));
if ($source === null) {
throw new LogicException($this->translate('Source not found'));
}

$values[KConfig::NOTIFICATIONS_USERNAME] = "source-$source->id";
}

$values[KConfig::NOTIFICATIONS_USERNAME] = "source-$source->id";
}
try {
Database::connection()->transaction(function (Connection $db) use ($values, $clusterUuid) {
$db->delete((new KConfig())->getTableName(), [
'cluster_uuid = ?' => Uuid::fromString($clusterUuid)->getBytes(),
sprintf('%s IN (?)', $db->quoteIdentifier('key')) => array_keys($values),
'locked = ?' => 'n'
]);

try {
Database::connection()->transaction(function (Connection $db) use ($values) {
$db->delete((new KConfig())->getTableName(), [
sprintf('%s IN (?)', $db->quoteIdentifier('key')) => array_keys($values),
'locked = ?' => 'n'
]);
foreach ($values as $k => $v) {
if (empty($v)) {
continue;
}

foreach ($values as $k => $v) {
if (empty($v)) {
continue;
$db->insert((new KConfig())->getTableName(), [
'cluster_uuid' => Uuid::fromString($clusterUuid)->getBytes(),
$db->quoteIdentifier('key') => $k,
'value' => $v
]);
}
});
} catch (Throwable $e) {
Logger::error($e);
Logger::error($e->getTraceAsString());

$db->insert((new KConfig())->getTableName(), [
$db->quoteIdentifier('key') => $k,
'value' => $v,
]);
}
});
} catch (Throwable $e) {
Logger::error($e);
Logger::error($e->getTraceAsString());
$form->addMessage($e->getMessage());

return;
}

$form->addMessage($e->getMessage());
Notification::success(
$this->translate('New configuration has successfully been stored.')
);

return;
$this->redirectNow('__REFRESH__');
}
)->handleRequest($this->getServerRequest());

if (
preg_match(
'/source-(\d+)/',
$kconfig[KConfig::NOTIFICATIONS_USERNAME]->value ?? '',
$matches
) !== false
&& ! empty($matches)
) {
try {
$sourceForm->loadSource($matches[1]);

Notification::success(
$this->translate('New configuration has successfully been stored.')
);

$this->redirectNow('__REFRESH__');
// TODO(el): Check password mismatch.
} catch (HttpNotFoundException $e) {
// TODO(el): Add error box.
}
)->handleRequest($this->getServerRequest());

if (
preg_match(
'/source-(\d+)/',
$kconfig[KConfig::NOTIFICATIONS_USERNAME]->value ?? '',
$matches
) !== false
&& ! empty($matches)
) {
try {
$sourceForm->loadSource($matches[1]);

// TODO(el): Check password mismatch.
} catch (HttpNotFoundException $e) {
// TODO(el): Add error box.
}
} catch (Throwable $e) {
echo $e->getTraceAsString();
die;
}

$this->addContent($form);
Expand All @@ -190,112 +189,41 @@ public function prometheusAction()
{
$form = (new PrometheusConfigForm())
->on(PrometheusConfigForm::ON_SUCCESS, function (PrometheusConfigForm $form) {
$clusterUuid = $form->getValue('cluster_uuid');
if ($form->isLocked($clusterUuid)) {
Notification::error($this->translate('Prometheus configuration is locked'));
if ($form->isLocked()) {
$form->addMessage($this->translate('Prometheus configuration is locked.'));

return;
}

try {
$db = Database::connection();
$db->exec("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
$db->beginTransaction();

$dbConfig = KConfig::on($db)->filter(
Filter::all(
Filter::equal('cluster_uuid', Uuid::fromString($clusterUuid)->getBytes()),
Filter::any(
Filter::equal('key', self::PROMETHEUS_URL),
Filter::equal('key', self::PROMETHEUS_USERNAME),
Filter::equal('key', self::PROMETHEUS_PASSWORD)
)
)
);

$data = [];

foreach ($dbConfig as $pair) {
$data[$pair->key] = ['value' => $pair->value, 'locked' => $pair->locked];
}
$clusterUuid = $form->getClusterUuid();
$values = $form->getValues();

if (isset($data[self::PROMETHEUS_URL]) && $data[self::PROMETHEUS_URL]['locked'] !== 'y') {
$db->update(
'config',
[
'value' => $form->getValue($this->fieldForForm(self::PROMETHEUS_URL))
],
[
'cluster_uuid = ?' => Uuid::fromString($clusterUuid)->getBytes(),
$db->quoteIdentifier('key') . ' = ?' => self::PROMETHEUS_URL,
]
);
} elseif (! isset($data[self::PROMETHEUS_URL])) {
$db->insert(
'config',
[
'cluster_uuid' => Uuid::fromString($clusterUuid)->getBytes(),
$db->quoteIdentifier('key') => self::PROMETHEUS_URL,
'value' => $form->getValue(
$this->fieldForForm(self::PROMETHEUS_URL)
)
]
);
}
try {
Database::connection()->transaction(function (Connection $db) use ($values, $clusterUuid) {
$db->delete((new KConfig())->getTableName(), [
'cluster_uuid = ?' => Uuid::fromString($clusterUuid)->getBytes(),
sprintf('%s IN (?)', $db->quoteIdentifier('key')) => array_keys($values),
'locked = ?' => 'n'
]);

foreach ($values as $k => $v) {
if (empty($v)) {
continue;
}

if (isset($data[self::PROMETHEUS_USERNAME]) && $data[self::PROMETHEUS_USERNAME]['locked'] !== 'y') {
$db->update(
'config',
[
'value' => $form->getValue($this->fieldForForm(self::PROMETHEUS_USERNAME))
],
[
'cluster_uuid = ?' => Uuid::fromString($clusterUuid)->getBytes(),
$db->quoteIdentifier('key') . ' = ?' => self::PROMETHEUS_USERNAME
]
);
} elseif (! isset($data[self::PROMETHEUS_USERNAME])) {
$db->insert(
'config',
[
$db->insert((new KConfig())->getTableName(), [
'cluster_uuid' => Uuid::fromString($clusterUuid)->getBytes(),
$db->quoteIdentifier('key') => self::PROMETHEUS_USERNAME,
'value' => $form->getValue(
$this->fieldForForm(self::PROMETHEUS_USERNAME)
)
]
);
}
$db->quoteIdentifier('key') => $k,
'value' => $v
]);
}
});
} catch (Throwable $e) {
Logger::error($e);
Logger::error($e->getTraceAsString());

if (isset($data[self::PROMETHEUS_PASSWORD]) && $data[self::PROMETHEUS_PASSWORD]['locked'] !== 'y') {
$db->update(
'config',
[
'value' => $form->getValue($this->fieldForForm(self::PROMETHEUS_PASSWORD))
],
[
'cluster_uuid = ?' => Uuid::fromString($clusterUuid)->getBytes(),
$db->quoteIdentifier('key') . ' = ?' => self::PROMETHEUS_PASSWORD
]
);
} elseif (! isset($data[self::PROMETHEUS_PASSWORD])) {
$db->insert(
'config',
[
'cluster_uuid' => Uuid::fromString($clusterUuid)->getBytes(),
$db->quoteIdentifier('key') => self::PROMETHEUS_PASSWORD,
'value' => $form->getValue(
$this->fieldForForm(self::PROMETHEUS_PASSWORD)
)
]
);
}
$form->addMessage($e->getMessage());

$db->commitTransaction();
} catch (Exception $e) {
$db->rollBackTransaction();
Notification::error(
$this->translate('Failed to store new configuration') . ': ' . $e->getMessage()
);
return;
}

Expand All @@ -308,18 +236,6 @@ public function prometheusAction()
$this->addContent($form);
}

/**
* Convert database field name to form field name
*
* @param string $field
*
* @return string
*/
protected function fieldForForm(string $field): string
{
return str_replace('.', '_', $field);
}

/**
* Merge tabs with other tabs contained in this tab panel
*
Expand All @@ -332,14 +248,14 @@ protected function mergeTabs(Tabs $tabs): void
}
}

protected function createSource(SourceForm $sourceForm): string
protected function createSource(SourceForm $sourceForm, string $clusterUuid): string
{
$password = sha1(openssl_random_pseudo_bytes(16));

$formData = [
'listener_password' => $password,
'listener_password_dupe' => $password,
'name' => KConfig::DEFAULT_NOTIFICATIONS_NAME,
'name' => KConfig::DEFAULT_NOTIFICATIONS_NAME . "($clusterUuid)",
'type' => KConfig::DEFAULT_NOTIFICATIONS_TYPE,
// TODO(el): Why?
'icinga2_insecure_tls' => 'n'
Expand Down
Loading

0 comments on commit 11f9d5a

Please sign in to comment.