Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forget modified attribute on config change #8739

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ set(base_SOURCES
scriptframe.cpp scriptframe.hpp
scriptglobal.cpp scriptglobal.hpp
scriptutils.cpp scriptutils.hpp
serializer.cpp serializer.hpp
serializer.cpp serializer.hpp serializer-script.cpp
shared.hpp
shared-object.hpp
singleton.hpp
Expand Down
19 changes: 19 additions & 0 deletions lib/base/configobject-script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,28 @@
#include "base/dictionary.hpp"
#include "base/function.hpp"
#include "base/functionwrapper.hpp"
#include "base/reference.hpp"
#include "base/scriptframe.hpp"

using namespace icinga;

static bool ConfigObjectGetAttribute(const String& attr, const Reference::Ptr& result)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
ConfigObject::Ptr self = vframe->Self;

REQUIRE_NOT_NULL(self);

Value temp;
bool ok = self->GetAttribute(attr, &temp);

if (ok && result) {
result->Set(temp);
}

return ok;
}

static void ConfigObjectModifyAttribute(const String& attr, const Value& value)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Expand All @@ -27,6 +45,7 @@ static void ConfigObjectRestoreAttribute(const String& attr)
Object::Ptr ConfigObject::GetPrototype()
{
static Dictionary::Ptr prototype = new Dictionary({
{ "get_attribute", new Function("ConfigObject#get_attribute", ConfigObjectGetAttribute, { "attr", "result" }, false) },
{ "modify_attribute", new Function("ConfigObject#modify_attribute", ConfigObjectModifyAttribute, { "attr", "value" }, false) },
{ "restore_attribute", new Function("ConfigObject#restore_attribute", ConfigObjectRestoreAttribute, { "attr", "value" }, false) }
});
Expand Down
32 changes: 30 additions & 2 deletions lib/base/configobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,34 @@ class ModAttrValidationUtils final : public ValidationUtils
}
};

bool ConfigObject::GetAttribute(const String& attr, Value *result)
{
std::vector<String> tokens = attr.Split(".");
auto current (tokens.begin());
auto end (tokens.end());
Object::Ptr subTree (this);
Value branch;

for (;;) {
if (!subTree->GetOwnField(*current, &branch)) {
return false;
}

if (++current == end) {
break;
}

if (!branch.IsObject()) {
return false;
}

subTree = branch.Get<Object::Ptr>();
}

*result = std::move(branch);
return true;
}

void ConfigObject::ModifyAttribute(const String& attr, const Value& value, bool updateVersion)
{
Dictionary::Ptr original_attributes = GetOriginalAttributes();
Expand Down Expand Up @@ -621,7 +649,7 @@ void ConfigObject::StopObjects()
}
}

void ConfigObject::DumpModifiedAttributes(const std::function<void(const ConfigObject::Ptr&, const String&, const Value&)>& callback)
void ConfigObject::DumpModifiedAttributes(const std::function<void(const ConfigObject::Ptr&, const String&, const Value&, const Value&)>& callback)
{
for (const Type::Ptr& type : Type::GetAllTypes()) {
auto *dtype = dynamic_cast<ConfigType *>(type.get());
Expand Down Expand Up @@ -675,7 +703,7 @@ void ConfigObject::DumpModifiedAttributes(const std::function<void(const ConfigO
} else
modifiedValue = currentValue;

callback(object, key, modifiedValue);
callback(object, key, kv.second, modifiedValue);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/base/configobject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ConfigObject : public ObjectImpl<ConfigObject>

ConfigObject::Ptr GetZone() const;

bool GetAttribute(const String& attr, Value *result);
void ModifyAttribute(const String& attr, const Value& value, bool updateVersion = true);
void RestoreAttribute(const String& attr, bool updateVersion = true);
bool IsAttributeModified(const String& attr) const;
Expand Down Expand Up @@ -75,7 +76,7 @@ class ConfigObject : public ObjectImpl<ConfigObject>
static void RestoreObjects(const String& filename, int attributeTypes = FAState);
static void StopObjects();

static void DumpModifiedAttributes(const std::function<void(const ConfigObject::Ptr&, const String&, const Value&)>& callback);
static void DumpModifiedAttributes(const std::function<void(const ConfigObject::Ptr&, const String&, const Value&, const Value&)>& callback);

static Object::Ptr GetPrototype();

Expand Down
8 changes: 8 additions & 0 deletions lib/base/serializer-script.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* Icinga 2 | (c) 2021 Icinga GmbH | GPLv2+ */

#include "base/function.hpp"
#include "base/serializer.hpp"

using namespace icinga;

REGISTER_FUNCTION(Internal, serialize, &Serialize, "value:attribute_types");
35 changes: 28 additions & 7 deletions lib/icinga/icingaapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "base/initialize.hpp"
#include "base/statsfunction.hpp"
#include "base/loader.hpp"
#include "base/json.hpp"
#include "base/serializer.hpp"
#include <fstream>

using namespace icinga;
Expand Down Expand Up @@ -121,7 +123,7 @@ void IcingaApplication::OnShutdown()
DumpProgramState();
}

static void PersistModAttrHelper(std::fstream& fp, ConfigObject::Ptr& previousObject, const ConfigObject::Ptr& object, const String& attr, const Value& value)
static void PersistModAttrHelper(std::fstream& fp, ConfigObject::Ptr& previousObject, const ConfigObject::Ptr& object, const String& attr, const Value& original, const Value& modified)
{
if (object != previousObject) {
if (previousObject) {
Expand All @@ -138,18 +140,37 @@ static void PersistModAttrHelper(std::fstream& fp, ConfigObject::Ptr& previousOb
});
ConfigWriter::EmitFunctionCall(fp, "get_object", args1);

ConfigWriter::EmitRaw(fp, "\nif (obj) {\n");
ConfigWriter::EmitRaw(fp, "\nif (obj) {\n\tvar result\n");
}

ConfigWriter::EmitRaw(fp, "\tobj.");
ConfigWriter::EmitRaw(fp, "\tif (");

if (original.GetType() == ValueEmpty) {
ConfigWriter::EmitRaw(fp, "!obj.get_attribute(");
ConfigWriter::EmitString(fp, attr);
ConfigWriter::EmitRaw(fp, ", &result) || Json.encode(Internal.serialize(result, ");
ConfigWriter::EmitNumber(fp, FAConfig);
ConfigWriter::EmitRaw(fp, ")) == ");
ConfigWriter::EmitString(fp, "null");
} else {
ConfigWriter::EmitRaw(fp, "obj.get_attribute(");
ConfigWriter::EmitString(fp, attr);
ConfigWriter::EmitRaw(fp, ", &result) && Json.encode(Internal.serialize(result, ");
ConfigWriter::EmitNumber(fp, FAConfig);
ConfigWriter::EmitRaw(fp, ")) == Json.encode(");
ConfigWriter::EmitValue(fp, 1, Serialize(original, FAConfig));
ConfigWriter::EmitRaw(fp, ")");
}

ConfigWriter::EmitRaw(fp, ") {\n\t\tobj.");

Array::Ptr args2 = new Array({
attr,
value
modified
});
ConfigWriter::EmitFunctionCall(fp, "modify_attribute", args2);

ConfigWriter::EmitRaw(fp, "\n");
ConfigWriter::EmitRaw(fp, "\n\t}\n");

previousObject = object;
}
Expand All @@ -175,8 +196,8 @@ void IcingaApplication::DumpModifiedAttributes()
fp.exceptions(std::ofstream::failbit | std::ofstream::badbit);

ConfigObject::Ptr previousObject;
ConfigObject::DumpModifiedAttributes([&fp, &previousObject](const ConfigObject::Ptr& object, const String& attr, const Value& value) {
PersistModAttrHelper(fp, previousObject, object, attr, value);
ConfigObject::DumpModifiedAttributes([&fp, &previousObject](const ConfigObject::Ptr& object, const String& attr, const Value& original, const Value& modified) {
PersistModAttrHelper(fp, previousObject, object, attr, original, modified);
});

if (previousObject) {
Expand Down