Commit 268affd1 authored by Aard's avatar Aard

Make ssu.ini versioned, allow adding new keys/update unchanged default values

parent b1410bd7
......@@ -14,6 +14,8 @@
#define SSU_CONFIGURATION "/etc/ssu/ssu.ini"
/// Path to the main SSU configuration file
#define SSU_REPO_CONFIGURATION "/usr/share/ssu/repos.ini"
/// Path to the main SSU configuration file
#define SSU_DEFAULT_CONFIGURATION "/usr/share/ssu/ssu-defaults.ini"
/// The SSU protocol version used by the ssu client libraries
#define SSU_PROTOCOL_VERSION "1"
#endif
......@@ -31,12 +31,69 @@ Ssu::Ssu(): QObject(){
settings = new QSettings(SSU_CONFIGURATION, QSettings::IniFormat);
repoSettings = new QSettings(SSU_REPO_CONFIGURATION, QSettings::IniFormat);
bool initialized=settings->value("initialized").toBool();
if (!initialized){
// FIXME, there's currently no fallback for missing configuration
settings->setValue("initialized", true);
settings->setValue("flavour", "testing");
QSettings defaultSettings(SSU_DEFAULT_CONFIGURATION, QSettings::IniFormat);
int configVersion=0;
int defaultConfigVersion=0;
if (settings->contains("configVersion"))
configVersion = settings->value("configVersion").toInt();
if (defaultSettings.contains("configVersion"))
defaultConfigVersion = defaultSettings.value("configVersion").toInt();
if (configVersion < defaultConfigVersion){
qDebug() << "Configuration is outdated, updating from " << configVersion
<< " to " << defaultConfigVersion;
for (int i=configVersion+1;i<=defaultConfigVersion;i++){
QStringList defaultKeys;
QString currentSection = QString("%1/").arg(i);
qDebug() << "Processing configuration version " << i;
defaultSettings.beginGroup(currentSection);
defaultKeys = defaultSettings.allKeys();
defaultSettings.endGroup();
foreach (const QString &key, defaultKeys){
if (!settings->contains(key)){
// Add new keys..
settings->setValue(key, defaultSettings.value(currentSection + key));
qDebug() << "Adding new key: " << key;
} else {
// ... or update the ones where default values has changed.
QVariant oldValue;
// check if an old value exists in an older configuration version
for (int j=i-1;j>0;j--){
if (defaultSettings.contains(QString("%1/").arg(j)+key)){
oldValue = defaultSettings.value(QString("%1/").arg(j)+key);
break;
}
}
// skip updating if there is no old value, since we can't check if the
// default value has changed
if (oldValue.isNull())
continue;
QVariant newValue = defaultSettings.value(currentSection + key);
if (oldValue == newValue){
// old and new value match, no need to do anything, apart from beating the
// person who added a useless key
continue;
} else {
// default value has changed, so check if the configuration is still
// using the old default value...
QVariant currentValue = settings->value(key);
// testcase: handles properly default update of thing with changed value in ssu.ini?
if (currentValue == oldValue){
// ...and update the key if it does
settings->setValue(key, newValue);
qDebug() << "Updating " << key << " from " << currentValue << " to " << newValue;
}
}
}
}
settings->setValue("configVersion", i);
}
}
#ifdef TARGET_ARCH
......
[General]
configVersion=2
[1]
flavour=testing
registered=false
rndRelease=latest
release=
adaptation=
ca-certificate=
credentials-url=https://example.com/ssu/device/%1/credentials.xml
register-url=https://example.com/ssu/device/%1/register.xml
credentials-scope=example
[2]
release=latest
......@@ -20,7 +20,7 @@ tests.depends = libssu
config.files = ssu.ini
config.path = /etc/ssu
static_config.files = repos.ini
static_config.files = repos.ini ssu-defaults.ini
static_config.path = /usr/share/ssu
INSTALLS += config static_config
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment