Elektra  0.8.18
Introduction

Applications should use Elektra to read (and store) configurations for a overall better integrated system. A light integration would be to write parsers for the configuration files of your application as Elektra plugin. This yields the following advantages:

For full integration, however, the application needs to be patched to directly access Elektra's key database. When the application is fully integrated in Elektra's ecosystem additional benefits arise:

Elektrify

The process to make applications aware of other's configuration is called "to elektrify". This tutorial is both for new and existing applications.

When a software starts to use Elektra, it is called to be elektrified. The places where configuration is parsed or generated must be located. Afterwards, Elektra's data structures must be used instead at these locations. In Elektra 0.7, that was nearly everything the software developer could do.

Now more optional possibilities are open. First, the parser and generator code can be moved to a storage plugin without having to rewrite additional parts. Doing this, the syntax of the configuration file stays exactly the same as it was before. The application immediately profits from Elektra's infrastructure solving basic issues like update and conflict detection and resolving of the file name.

Moreover, a huge variety of plugins can be utilised to extend the functionality of the configuration system and the programmer can write supplementary plugins. They can do syntactic checks, write out events in their log files and notify users if configuration has changed. So we get a bunch of plugins that are reusable, and we gain a lot because every other program can also access the configuration of this software.

For new software the situation is similar. Additionally, there is the option to reuse mature and well-tested plugins to achieve the optimal result for configuration. New applications simply do not have the burden to stay compatible with the configuration system they had to before. But they can also use self-written plugins for adding needed behaviour or cross-cutting concerns.

To sum up, if a developer wants to elektrify software, he or she can do that without any need for changes to the outside world regarding the format and semantics of the configuration. But in the interconnected world it is only a matter of time until other software also wants to access the configuration, and with elektrified software it is possible for every application to do so.

Get Started

As first step in a C-application you need to create an in-memory Key. Such a Key is Elektra's atomic unit and consists of:

Keys are either associated with entries in configuration files or used as arguments in the API to transport some information.

Thus a key is only in-memory and does not need any of the other Elektra's objects. We always can create one:

    Key *parentKey = keyNew("/sw/org/myapp/#0/current",
            KEY_CACADING_NAME,
            KEY_END);

Now we have the Key we will use to pass as argument. First we open our key database (KDB). This is done by:

    KDB *repo = kdbOpen(parentKey);

A Key is seldom alone, but they are often found in groups, e.g. in configuration files. To represent many keys (a set of keys) Elektra has the data structure KeySet. Because the Key's name is unique we can easily lookup keys in a KeySet without ambiguity. Additionally, we can can iterate over all Keys in a KeySet without a hassle. To create an empty KeySet we use:

    KeySet *conf = ksNew(200,
            KS_END);

Now we have everything ready to fetch the latest configuration:

    kdbGet(repo, conf, parentKey);

Lookup

To lookup a key, we simply use, e.g.:

    Key *k = ksLookupByName(conf,
            "/sw/org/myapp/#0/current/section/subsection/key",
            0);

We see in that example that only Elektra paths are hardcoded in the application, no configuration file or similar. Thus we are interested in the value we use:

    char *val = keyString(k);

Finally, we need to convert the configuration value to the datatype we need.

Obviously, to do this manually has severe drawbacks:

So (larger) applications should not directly use KeySet, but instead use code generation to provide a type-safe front-end.

For more information about that, continue reading here.

Specification

Now, we have a fully working configuration system without any hard-coded information (such as configuration files). So we already gained something. But, we did not discuss how we can actually achieve application integration, the goal of Elektra.

Elektra 0.8.11 introduces the so called specification for the application's configuration. It is located below its own namespace spec (next to user and system).

Keys in spec allow us to specify which keys are read by the application, which fallback they might have and which is the default value using meta data. The implementation of these features happened in ksLookup. When cascading keys (those starting with /) are used following features are now available (in the meta data of respective spec-keys):

They can be used like this:

    kdb set /overrides/test "example override"
    sudo kdb setmeta spec/test override/#0 /overrides/test

This technique provides complete transparency how a program will fetch a configuration value. In practice that means that:

kdb get "/sw/org/myapp/#0/current/section/subsection/key",

will give you the exact same value as the application gets when it uses the above lookup C-code.

What we do not see in the program above are the default values and fallbacks. They are only present in the so specification (namespace spec). Luckily, the specification consists of (meta) key/value pairs, too. So we do not have to learn something new.

So lets say, that another application otherapp exactly has the value we actually want. We want to better integrate the system, that in the case we do not have a value for /sw/org/myapp/#0/current/section/subsection/key we want to use /sw/otherorg/otherapp/#0/current/section/subsection/key.

So we specify:

kdb setmeta spec/sw/org/myapp/#0/current/section/subsection/key "fallback/#0" /sw/otherorg/otherapp/#0/current/section/subsection/key

Voila, we have done a system integration between myapp and otherapp!

Note that the fallback, override and cascading works on key level, and not like most other systems have implemented, on configuration file level.

Conclusion

Elektra does not hardcode any configuration data in your application. Using the default specification, we even can startup applications without any configuration file at all and still do not have anything hardcoded in the applications binary. Furthermore, by only using cascading keys for kdbGet() and ksLookup() Elektra gives you the possibility to specify how configuration data should be retrieved. In this specification you can define, that configuration data from other applications or shared places should be considered or even preferred. Doing so, we can achieve configuration integration.

SEE ALSO