From 372fb5a9bc2f1289d2fda7b979796ac442d71ab9 Mon Sep 17 00:00:00 2001
From: Piotr Bartosiewicz
Date: Thu, 12 Feb 2015 17:22:01 +0100
Subject: [PATCH 01/16] Code refactor/cleanup
[Bug/Feature] N/A
[Cause] N/A
[Solution] N/A
[Verification] Build, run tests
Change-Id: I30a1358f9163c78afa44f6ea0731003171f0f9a7
---
server/dynamic-config-scheme.hpp | 51 ++++++++++++++++
server/exception.hpp | 8 +++
server/zone-connection-transport.cpp | 2 +
server/zone.cpp | 4 +-
server/zones-manager.cpp | 81 ++++++++++++++------------
server/zones-manager.hpp | 6 +-
tests/unit_tests/lxc/templates/minimal-dbus.sh | 3 +
tests/unit_tests/server/ut-server.cpp | 11 ++--
tests/unit_tests/server/ut-zone-admin.cpp | 9 +--
tests/unit_tests/server/ut-zone.cpp | 9 +--
tests/unit_tests/server/ut-zones-manager.cpp | 12 ++--
11 files changed, 136 insertions(+), 60 deletions(-)
create mode 100644 server/dynamic-config-scheme.hpp
diff --git a/server/dynamic-config-scheme.hpp b/server/dynamic-config-scheme.hpp
new file mode 100644
index 0000000..90c80d5
--- /dev/null
+++ b/server/dynamic-config-scheme.hpp
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Piotr Bartosiewicz
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+/**
+ * @file
+ * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
+ * @brief Dynamic config helper functions
+ */
+
+
+#ifndef SERVER_DYNAMIC_CONFIG_SCHEME_HPP
+#define SERVER_DYNAMIC_CONFIG_SCHEME_HPP
+
+#include
+
+namespace vasum {
+
+/**
+ * Gets db prefix for vasum config
+ */
+inline std::string getVasumDbPrefix()
+{
+ return "vasum";
+}
+
+/**
+ * Gets db prefix for zone config
+ */
+inline std::string getZoneDbPrefix(const std::string& id)
+{
+ return "zone." + id;
+}
+
+} // namespace vasum
+
+#endif // SERVER_DYNAMIC_CONFIG_SCHEME_HPP
diff --git a/server/exception.hpp b/server/exception.hpp
index 09eaaef..cb358b1 100644
--- a/server/exception.hpp
+++ b/server/exception.hpp
@@ -50,6 +50,14 @@ struct ZoneOperationException: public ServerException {
};
/**
+ * Invalid zone id
+ */
+struct InvalidZoneIdException : public ServerException {
+
+ InvalidZoneIdException(const std::string& error = "") : ServerException(error) {}
+};
+
+/**
* Exception during performing an operation on a zone connection
*/
struct ZoneConnectionException: public ServerException {
diff --git a/server/zone-connection-transport.cpp b/server/zone-connection-transport.cpp
index 0757241..0a8f8a0 100644
--- a/server/zone-connection-transport.cpp
+++ b/server/zone-connection-transport.cpp
@@ -74,6 +74,7 @@ ZoneConnectionTransport::ZoneConnectionTransport(const std::string& runMountPoin
LOGE("Initialization failed: could not mount " << runMountPoint);
throw ZoneConnectionException("Could not mount: " + runMountPoint);
}
+ LOGI("Mounted: " << runMountPoint);
}
// if there is no systemd in the zone this dir won't be created automatically
@@ -95,6 +96,7 @@ ZoneConnectionTransport::~ZoneConnectionTransport()
if (!utils::umount(mRunMountPoint)) {
LOGE("Deinitialization failed: could not umount " << mRunMountPoint);
}
+ LOGI("Unmounted: " << mRunMountPoint);
}
}
}
diff --git a/server/zone.cpp b/server/zone.cpp
index 1617bd6..0c2eca7 100644
--- a/server/zone.cpp
+++ b/server/zone.cpp
@@ -25,6 +25,7 @@
#include "config.hpp"
#include "zone.hpp"
+#include "dynamic-config-scheme.hpp"
#include "base-exception.hpp"
#include "logger/logger.hpp"
@@ -48,7 +49,6 @@ typedef std::lock_guard Lock;
// TODO: move constants to the config file when default values are implemented there
const int RECONNECT_RETRIES = 15;
const int RECONNECT_DELAY = 1 * 1000;
-const std::string DB_PREFIX = "zone.";
} // namespace
@@ -77,7 +77,7 @@ Zone::Zone(const utils::Worker::Pointer& worker,
const fs::path zonePath = fs::path(zonesPath) / mAdmin->getId();
mRootPath = (zonePath / fs::path("rootfs")).string();
- const std::string dbPrefix = DB_PREFIX + mAdmin->getId();
+ const std::string dbPrefix = getZoneDbPrefix(mAdmin->getId());
config::loadFromKVStoreWithJsonFile(dbPath, zoneConfigPath, mDynamicConfig, dbPrefix);
mProvision.reset(new ZoneProvision(mRootPath, zoneConfigPath, dbPath, dbPrefix, mConfig.validLinkPrefixes));
diff --git a/server/zones-manager.cpp b/server/zones-manager.cpp
index 6acc128..3baa2a2 100644
--- a/server/zones-manager.cpp
+++ b/server/zones-manager.cpp
@@ -27,6 +27,7 @@
#include "host-dbus-definitions.hpp"
#include "common-dbus-definitions.hpp"
#include "zone-dbus-definitions.hpp"
+#include "dynamic-config-scheme.hpp"
#include "zones-manager.hpp"
#include "zone-admin.hpp"
#include "lxc/cgroup.hpp"
@@ -64,7 +65,6 @@ bool regexMatchVector(const std::string& str, const std::vector& v
return false;
}
-const std::string DB_PREFIX = "daemon";
const std::string HOST_ID = "host";
const std::string ENABLED_FILE_NAME = "enabled";
@@ -124,7 +124,10 @@ ZonesManager::ZonesManager(const std::string& configPath)
LOGD("Instantiating ZonesManager object...");
config::loadFromJsonFile(configPath, mConfig);
- config::loadFromKVStoreWithJsonFile(mConfig.dbPath, configPath, mDynamicConfig, DB_PREFIX);
+ config::loadFromKVStoreWithJsonFile(mConfig.dbPath,
+ configPath,
+ mDynamicConfig,
+ getVasumDbPrefix());
mProxyCallPolicy.reset(new ProxyCallPolicy(mConfig.proxyCallRules));
@@ -187,7 +190,7 @@ ZonesManager::ZonesManager(const std::string& configPath)
this, _1, _2, _3));
for (const auto& zoneConfig : mDynamicConfig.zoneConfigs) {
- createZone(utils::createFilePath(mConfig.zoneNewConfigPrefix, zoneConfig));
+ insertZone(utils::createFilePath(mConfig.zoneNewConfigPrefix, zoneConfig));
}
updateDefaultId();
@@ -233,14 +236,14 @@ Zone& ZonesManager::getZone(const std::string& id)
{
auto iter = findZone(id);
if (iter == mZones.end()) {
- throw std::out_of_range("id not found");
+ throw InvalidZoneIdException("Zone id not found");
}
return get(iter);
}
void ZonesManager::saveDynamicConfig()
{
- config::saveToKVStore(mConfig.dbPath, mDynamicConfig, DB_PREFIX);
+ config::saveToKVStore(mConfig.dbPath, mDynamicConfig, getVasumDbPrefix());
}
void ZonesManager::updateDefaultId()
@@ -266,7 +269,7 @@ void ZonesManager::updateDefaultId()
saveDynamicConfig();
}
-void ZonesManager::createZone(const std::string& zoneConfigPath)
+void ZonesManager::insertZone(const std::string& zoneConfigPath)
{
LOGT("Creating Zone " << zoneConfigPath);
std::unique_ptr zone(new Zone(mWorker->createSubWorker(),
@@ -277,7 +280,10 @@ void ZonesManager::createZone(const std::string& zoneConfigPath)
mConfig.runMountPointPrefix));
const std::string id = zone->getId();
if (id == HOST_ID) {
- throw ZoneOperationException("Cannot use reserved zone ID");
+ throw InvalidZoneIdException("Cannot use reserved zone ID");
+ }
+ if (findZone(id) != mZones.end()) {
+ throw InvalidZoneIdException("Zone already exists");
}
using namespace std::placeholders;
@@ -296,11 +302,6 @@ void ZonesManager::createZone(const std::string& zoneConfigPath)
zone->setDbusStateChangedCallback(bind(&ZonesManager::handleDbusStateChanged,
this, id, _1));
- Lock lock(mMutex);
-
- if (findZone(id) != mZones.end()) {
- throw ZoneOperationException("Zone already exists");
- }
mZones.push_back(std::move(zone));
// after zone is created successfully, put a file informing that zones are enabled
@@ -319,7 +320,7 @@ void ZonesManager::destroyZone(const std::string& zoneId)
auto iter = findZone(zoneId);
if (iter == mZones.end()) {
LOGE("Failed to destroy zone " << zoneId << ": no such zone");
- throw ZoneOperationException("No such zone");
+ throw InvalidZoneIdException("No such zone");
}
get(iter).setDestroyOnExit();
@@ -444,7 +445,7 @@ bool ZonesManager::isPaused(const std::string& zoneId)
auto iter = findZone(zoneId);
if (iter == mZones.end()) {
LOGE("No such zone id: " << zoneId);
- throw ZoneOperationException("No such zone");
+ throw InvalidZoneIdException("No such zone");
}
return get(iter).isPaused();
@@ -457,7 +458,7 @@ bool ZonesManager::isRunning(const std::string& zoneId)
auto iter = findZone(zoneId);
if (iter == mZones.end()) {
LOGE("No such zone id: " << zoneId);
- throw ZoneOperationException("No such zone");
+ throw InvalidZoneIdException("No such zone");
}
return get(iter).isRunning();
}
@@ -803,7 +804,7 @@ void ZonesManager::handleDeclareFileCall(const std::string& zone,
const std::string id = getZone(zone).declareFile(type, path, flags, mode);
result->set(g_variant_new("(s)", id.c_str()));
- } catch (const std::out_of_range&) {
+ } catch (const InvalidZoneIdException&) {
LOGE("No zone with id=" << zone);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
} catch (const config::ConfigException& ex) {
@@ -827,7 +828,7 @@ void ZonesManager::handleDeclareMountCall(const std::string& source,
const std::string id = getZone(zone).declareMount(source, target, type, flags, data);
result->set(g_variant_new("(s)", id.c_str()));
- } catch (const std::out_of_range&) {
+ } catch (const InvalidZoneIdException&) {
LOGE("No zone with id=" << zone);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
} catch (const config::ConfigException& ex) {
@@ -847,7 +848,7 @@ void ZonesManager::handleDeclareLinkCall(const std::string& source,
const std::string id = getZone(zone).declareLink(source, target);
result->set(g_variant_new("(s)", id.c_str()));
- } catch (const std::out_of_range&) {
+ } catch (const InvalidZoneIdException&) {
LOGE("No zone with id=" << zone);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
} catch (const config::ConfigException& ex) {
@@ -874,7 +875,7 @@ void ZonesManager::handleGetDeclarationsCall(const std::string& zone,
out.data(),
out.size());
result->set(g_variant_new("(@as)", array));
- } catch (const std::out_of_range&) {
+ } catch (const InvalidZoneIdException&) {
LOGE("No zone with id=" << zone);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
} catch (const VasumException& ex) {
@@ -895,7 +896,7 @@ void ZonesManager::handleRemoveDeclarationCall(const std::string& zone,
getZone(zone).removeDeclaration(declarationId);
result->setVoid();
- } catch (const std::out_of_range&) {
+ } catch (const InvalidZoneIdException&) {
LOGE("No zone with id=" << zone);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
} catch (const VasumException& ex) {
@@ -997,14 +998,12 @@ int ZonesManager::getVTForNewZone()
return *candidates.begin();
}
-void ZonesManager::handleCreateZoneCall(const std::string& id,
- const std::string& templateName,
- dbus::MethodResultBuilder::Pointer result)
+void ZonesManager::createZone(const std::string& id,
+ const std::string& templateName)
{
- if (id.empty()) {
+ if (id.empty()) { // TODO validate id (no spaces, slashes etc)
LOGE("Failed to add zone - invalid name.");
- result->setError(api::ERROR_INVALID_ID, "Invalid name");
- return;
+ throw InvalidZoneIdException("Invalid name");
}
LOGI("Creating zone " << id);
@@ -1018,8 +1017,7 @@ void ZonesManager::handleCreateZoneCall(const std::string& id,
// check if zone does not exist
if (findZone(id) != mZones.end()) {
LOGE("Cannot create " << id << " zone - already exists!");
- result->setError(api::ERROR_INVALID_ID, "Already exists");
- return;
+ throw InvalidZoneIdException("Already exists");
}
const std::string zonePathStr = utils::createFilePath(mConfig.zonesPath, id, "/");
@@ -1033,8 +1031,7 @@ void ZonesManager::handleCreateZoneCall(const std::string& id,
if (!utils::launchAsRoot(copyImageContentsWrapper)) {
LOGE("Failed to copy zone image.");
- result->setError(api::ERROR_INTERNAL, "Failed to copy zone image.");
- return;
+ throw ZoneOperationException("Failed to copy zone image.");
}
}
@@ -1062,25 +1059,35 @@ void ZonesManager::handleCreateZoneCall(const std::string& id,
} catch (VasumException& e) {
LOGE("Generate config failed: " << e.what());
utils::launchAsRoot(std::bind(removeAllWrapper, zonePathStr));
- result->setError(api::ERROR_INTERNAL, "Failed to generate config");
- return;
+ throw e;
}
LOGT("Creating new zone");
try {
- createZone(newConfigPath);
+ insertZone(newConfigPath);
} catch (VasumException& e) {
LOGE("Creating new zone failed: " << e.what());
utils::launchAsRoot(std::bind(removeAllWrapper, zonePathStr));
- result->setError(api::ERROR_INTERNAL, "Failed to create zone");
- return;
+ throw e;
}
mDynamicConfig.zoneConfigs.push_back(newConfigName);
saveDynamicConfig();
updateDefaultId();
+}
- result->setVoid();
+void ZonesManager::handleCreateZoneCall(const std::string& id,
+ const std::string& templateName,
+ dbus::MethodResultBuilder::Pointer result)
+{
+ try {
+ createZone(id, templateName);
+ result->setVoid();
+ } catch (const InvalidZoneIdException& e) {
+ result->setError(api::ERROR_INVALID_ID, "Existing or invalid zone id");
+ } catch (const VasumException& e) {
+ result->setError(api::ERROR_INTERNAL, "Failed to create zone");
+ }
}
void ZonesManager::handleDestroyZoneCall(const std::string& id,
@@ -1091,7 +1098,7 @@ void ZonesManager::handleDestroyZoneCall(const std::string& id,
LOGI("Destroying zone " << id);
destroyZone(id);
- } catch (const ZoneOperationException& e) {
+ } catch (const InvalidZoneIdException&) {
LOGE("Failed to destroy zone - no such zone id: " << id);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
} catch (const VasumException& e) {
diff --git a/server/zones-manager.hpp b/server/zones-manager.hpp
index 99dfa4b..529ef8b 100644
--- a/server/zones-manager.hpp
+++ b/server/zones-manager.hpp
@@ -49,9 +49,10 @@ public:
/**
* Create new zone.
*
- * @param zoneConfigPath config of new zone
+ * @param zoneId id of new zone
+ * @param templateName a template name
*/
- void createZone(const std::string& zoneConfigPath);
+ void createZone(const std::string& zoneId, const std::string& templateName);
/**
* Destroy zone.
@@ -137,6 +138,7 @@ private:
const std::string& templatePath,
const std::string& resultPath);
int getVTForNewZone();
+ void insertZone(const std::string& zoneConfigPath);
void notifyActiveZoneHandler(const std::string& caller,
const std::string& appliaction,
diff --git a/tests/unit_tests/lxc/templates/minimal-dbus.sh b/tests/unit_tests/lxc/templates/minimal-dbus.sh
index fb61eb3..a8ff5b9 100755
--- a/tests/unit_tests/lxc/templates/minimal-dbus.sh
+++ b/tests/unit_tests/lxc/templates/minimal-dbus.sh
@@ -56,6 +56,9 @@ lxc.haltsignal = SIGTERM
lxc.pts = 256
lxc.tty = 0
+#lxc.loglevel = TRACE
+#lxc.logfile = /tmp/${name}.log
+
lxc.cgroup.devices.deny = a
lxc.mount.auto = proc sys cgroup
diff --git a/tests/unit_tests/server/ut-server.cpp b/tests/unit_tests/server/ut-server.cpp
index 3ed7319..3cd3e51 100644
--- a/tests/unit_tests/server/ut-server.cpp
+++ b/tests/unit_tests/server/ut-server.cpp
@@ -35,6 +35,12 @@
#include
namespace {
+
+const std::string CONFIG_DIR = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-server";
+const std::string TEST_CONFIG_PATH = CONFIG_DIR + "/test-daemon.conf";
+const std::string BUGGY_CONFIG_PATH = CONFIG_DIR + "/buggy-daemon.conf";
+const std::string MISSING_CONFIG_PATH = CONFIG_DIR + "/missing-daemon.conf";
+
const std::string ZONES_PATH = "/tmp/ut-zones"; // the same as in daemon.conf
const bool AS_ROOT = true;
@@ -52,11 +58,6 @@ BOOST_FIXTURE_TEST_SUITE(ServerSuite, Fixture)
using namespace vasum;
using namespace config;
-const std::string TEST_CONFIG_PATH = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-server/test-daemon.conf";
-const std::string BUGGY_CONFIG_PATH = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-server/buggy-daemon.conf";
-const std::string MISSING_CONFIG_PATH = "/this/is/a/missing/file/path/missing-daemon.conf";
-
-
BOOST_AUTO_TEST_CASE(ConstructorDestructorTest)
{
std::unique_ptr s;
diff --git a/tests/unit_tests/server/ut-zone-admin.cpp b/tests/unit_tests/server/ut-zone-admin.cpp
index 8dc3eb8..ba21ea9 100644
--- a/tests/unit_tests/server/ut-zone-admin.cpp
+++ b/tests/unit_tests/server/ut-zone-admin.cpp
@@ -37,10 +37,11 @@ using namespace vasum;
namespace {
-const std::string TEST_CONFIG_PATH = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zone-admin/zones/test.conf";
-const std::string TEST_NO_SHUTDOWN_CONFIG_PATH = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zone-admin/zones/test-no-shutdown.conf";
-const std::string BUGGY_CONFIG_PATH = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zone-admin/zones/buggy.conf";
-const std::string MISSING_CONFIG_PATH = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zone-admin/zones/missing.conf";
+const std::string ZONES_CONFIG_DIR = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zone-admin/zones";
+const std::string TEST_CONFIG_PATH = ZONES_CONFIG_DIR + "/test.conf";
+const std::string TEST_NO_SHUTDOWN_CONFIG_PATH = ZONES_CONFIG_DIR + "/test-no-shutdown.conf";
+const std::string BUGGY_CONFIG_PATH = ZONES_CONFIG_DIR + "/buggy.conf";
+const std::string MISSING_CONFIG_PATH = ZONES_CONFIG_DIR + "/missing.conf";
const std::string ZONES_PATH = "/tmp/ut-zones";
const std::string LXC_TEMPLATES_PATH = VSM_TEST_LXC_TEMPLATES_INSTALL_DIR;
diff --git a/tests/unit_tests/server/ut-zone.cpp b/tests/unit_tests/server/ut-zone.cpp
index 783fbf8..646a0a8 100644
--- a/tests/unit_tests/server/ut-zone.cpp
+++ b/tests/unit_tests/server/ut-zone.cpp
@@ -45,10 +45,11 @@ using namespace config;
namespace {
-const std::string TEST_CONFIG_PATH = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zone/zones/test.conf";
-const std::string TEST_DBUS_CONFIG_PATH = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zone/zones/test-dbus.conf";
-const std::string BUGGY_CONFIG_PATH = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zone/zones/buggy.conf";
-const std::string MISSING_CONFIG_PATH = "/this/is/a/missing/file/path/config.conf";
+const std::string ZONES_CONFIG_DIR = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zone/zones";
+const std::string TEST_CONFIG_PATH = ZONES_CONFIG_DIR + "/test.conf";
+const std::string TEST_DBUS_CONFIG_PATH = ZONES_CONFIG_DIR + "/test-dbus.conf";
+const std::string BUGGY_CONFIG_PATH = ZONES_CONFIG_DIR + "/buggy.conf";
+const std::string MISSING_CONFIG_PATH = ZONES_CONFIG_DIR + "/missing.conf";
const std::string ZONES_PATH = "/tmp/ut-zones";
const std::string LXC_TEMPLATES_PATH = VSM_TEST_LXC_TEMPLATES_INSTALL_DIR;
const std::string DB_PATH = ZONES_PATH + "/vasum.db";
diff --git a/tests/unit_tests/server/ut-zones-manager.cpp b/tests/unit_tests/server/ut-zones-manager.cpp
index b0ab2e2..a32aaa7 100644
--- a/tests/unit_tests/server/ut-zones-manager.cpp
+++ b/tests/unit_tests/server/ut-zones-manager.cpp
@@ -61,12 +61,12 @@ using namespace dbus;
namespace {
-const std::string TEST_CONFIG_PATH = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zones-manager/test-daemon.conf";
-const std::string TEST_DBUS_CONFIG_PATH = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zones-manager/test-dbus-daemon.conf";
-const std::string EMPTY_DBUS_CONFIG_PATH = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zones-manager/empty-dbus-daemon.conf";
-const std::string BUGGY_CONFIG_PATH = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zones-manager/buggy-daemon.conf";
-const std::string TEST_ZONE_CONF_PATH = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zones-manager/zones/";
-const std::string MISSING_CONFIG_PATH = "/this/is/a/missing/file/path/missing-daemon.conf";
+const std::string CONFIG_DIR = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zones-manager";
+const std::string TEST_CONFIG_PATH = CONFIG_DIR + "/test-daemon.conf";
+const std::string TEST_DBUS_CONFIG_PATH = CONFIG_DIR + "/test-dbus-daemon.conf";
+const std::string EMPTY_DBUS_CONFIG_PATH = CONFIG_DIR + "/empty-dbus-daemon.conf";
+const std::string BUGGY_CONFIG_PATH = CONFIG_DIR + "/buggy-daemon.conf";
+const std::string MISSING_CONFIG_PATH = CONFIG_DIR + "/missing-daemon.conf";
const int EVENT_TIMEOUT = 5000;
const int UNEXPECTED_EVENT_TIMEOUT = EVENT_TIMEOUT / 5;
const int TEST_DBUS_CONNECTION_ZONES_COUNT = 3;
--
2.7.4
From 9dfee0f5df9bc2a0ef5b97550bb6a1c69cc0176e Mon Sep 17 00:00:00 2001
From: Piotr Bartosiewicz
Date: Thu, 12 Feb 2015 10:21:18 +0100
Subject: [PATCH 02/16] Don't store dynamic config in conf files
[Bug/Feature] Per-zone mutable config fields (ip, vt and more in
the future) goes to dynamic config.
[Cause] N/A
[Solution] N/A
[Verification] Build, run tests
Change-Id: I2a59b0292fc326a689e3ff9375da1199dc8b7618
---
server/configs/daemon.conf.in | 3 +-
server/configs/templates/default.conf | 3 +-
server/server.cpp | 10 -
server/zone-admin.cpp | 24 +-
server/zone-admin.hpp | 10 +-
server/zone-config.hpp | 77 ++++---
server/zone.cpp | 25 +-
server/zone.hpp | 7 +-
server/zones-manager-config.hpp | 13 +-
server/zones-manager.cpp | 119 +++++-----
server/zones-manager.hpp | 6 +-
tests/unit_tests/client/configs/CMakeLists.txt | 18 +-
.../ut-client/templates/console-dbus.conf.in} | 1 -
.../configs/ut-client/test-daemon.conf.in} | 5 +-
.../configs/ut-client/test-dbus-daemon.conf.in | 25 --
.../configs/ut-client/zones/console1-dbus.conf.in | 18 --
.../configs/ut-client/zones/console2-dbus.conf.in | 18 --
.../configs/ut-client/zones/console3-dbus.conf.in | 18 --
tests/unit_tests/client/ut-client.cpp | 37 +--
tests/unit_tests/server/configs/CMakeLists.txt | 60 ++---
.../server/configs/ut-server/buggy-daemon.conf.in | 18 --
.../{zones/zone1.conf => templates/default.conf} | 1 -
.../server/configs/ut-server/test-daemon.conf.in | 7 +-
.../server/configs/ut-server/zones/zone3.conf | 18 --
.../ut-zone-admin/{zones => templates}/buggy.conf | 1 -
.../{zones => templates}/missing.conf | 1 -
.../{zones => templates}/test-no-shutdown.conf | 1 -
.../templates/test.conf} | 1 -
.../server/configs/ut-zone-admin/zones/test.conf | 18 --
.../ut-zone/{zones => templates}/buggy.conf | 1 -
.../ut-zone/{zones => templates}/test-dbus.conf.in | 3 +-
.../configs/ut-zone/{zones => templates}/test.conf | 1 -
.../configs/ut-zones-manager/buggy-daemon.conf.in | 18 --
.../console-dbus.conf.in} | 3 +-
.../console1.conf => templates/console.conf} | 1 -
.../configs/ut-zones-manager/test-daemon.conf.in | 14 +-
.../ut-zones-manager/test-dbus-daemon.conf.in | 25 --
.../ut-zones-manager/zones/console1-dbus.conf.in | 18 --
.../ut-zones-manager/zones/console2-dbus.conf.in | 18 --
.../configs/ut-zones-manager/zones/console2.conf | 18 --
.../configs/ut-zones-manager/zones/console3.conf | 18 --
tests/unit_tests/server/ut-server.cpp | 26 ++-
tests/unit_tests/server/ut-zone-admin.cpp | 24 +-
tests/unit_tests/server/ut-zone.cpp | 15 +-
tests/unit_tests/server/ut-zones-manager.cpp | 253 ++++++++++++---------
45 files changed, 394 insertions(+), 625 deletions(-)
rename tests/unit_tests/{server/configs/ut-zones-manager/templates/default.conf.in => client/configs/ut-client/templates/console-dbus.conf.in} (96%)
rename tests/unit_tests/{server/configs/ut-zones-manager/empty-dbus-daemon.conf.in => client/configs/ut-client/test-daemon.conf.in} (80%)
delete mode 100644 tests/unit_tests/client/configs/ut-client/test-dbus-daemon.conf.in
delete mode 100644 tests/unit_tests/client/configs/ut-client/zones/console1-dbus.conf.in
delete mode 100644 tests/unit_tests/client/configs/ut-client/zones/console2-dbus.conf.in
delete mode 100644 tests/unit_tests/client/configs/ut-client/zones/console3-dbus.conf.in
delete mode 100644 tests/unit_tests/server/configs/ut-server/buggy-daemon.conf.in
rename tests/unit_tests/server/configs/ut-server/{zones/zone1.conf => templates/default.conf} (93%)
delete mode 100644 tests/unit_tests/server/configs/ut-server/zones/zone3.conf
rename tests/unit_tests/server/configs/ut-zone-admin/{zones => templates}/buggy.conf (92%)
rename tests/unit_tests/server/configs/ut-zone-admin/{zones => templates}/missing.conf (92%)
rename tests/unit_tests/server/configs/ut-zone-admin/{zones => templates}/test-no-shutdown.conf (92%)
rename tests/unit_tests/server/configs/{ut-server/zones/zone2.conf => ut-zone-admin/templates/test.conf} (93%)
delete mode 100644 tests/unit_tests/server/configs/ut-zone-admin/zones/test.conf
rename tests/unit_tests/server/configs/ut-zone/{zones => templates}/buggy.conf (94%)
rename tests/unit_tests/server/configs/ut-zone/{zones => templates}/test-dbus.conf.in (85%)
rename tests/unit_tests/server/configs/ut-zone/{zones => templates}/test.conf (94%)
delete mode 100644 tests/unit_tests/server/configs/ut-zones-manager/buggy-daemon.conf.in
rename tests/unit_tests/server/configs/ut-zones-manager/{zones/console3-dbus.conf.in => templates/console-dbus.conf.in} (83%)
rename tests/unit_tests/server/configs/ut-zones-manager/{zones/console1.conf => templates/console.conf} (91%)
delete mode 100644 tests/unit_tests/server/configs/ut-zones-manager/test-dbus-daemon.conf.in
delete mode 100644 tests/unit_tests/server/configs/ut-zones-manager/zones/console1-dbus.conf.in
delete mode 100644 tests/unit_tests/server/configs/ut-zones-manager/zones/console2-dbus.conf.in
delete mode 100644 tests/unit_tests/server/configs/ut-zones-manager/zones/console2.conf
delete mode 100644 tests/unit_tests/server/configs/ut-zones-manager/zones/console3.conf
diff --git a/server/configs/daemon.conf.in b/server/configs/daemon.conf.in
index 327e470..a6359fa 100644
--- a/server/configs/daemon.conf.in
+++ b/server/configs/daemon.conf.in
@@ -1,10 +1,9 @@
{
"dbPath" : "/usr/dbspace/vasum.db",
- "zoneConfigs" : [],
+ "zoneIds" : [],
"zonesPath" : "${DATA_DIR}/.zones",
"zoneImagePath" : "",
"zoneTemplateDir" : "/etc/vasum/templates/",
- "zoneNewConfigPrefix" : "/var/lib/vasum",
"runMountPointPrefix" : "/var/run/zones",
"defaultId" : "",
"lxcTemplatePrefix" : "/etc/vasum/lxc-templates",
diff --git a/server/configs/templates/default.conf b/server/configs/templates/default.conf
index 9690a87..2aa7a66 100644
--- a/server/configs/templates/default.conf
+++ b/server/configs/templates/default.conf
@@ -1,5 +1,4 @@
{
- "name" : "~NAME~",
"lxcTemplate" : "tizen-common-wayland.sh",
"initWithArgs" : [],
"ipv4Gateway" : "10.0.~IP~.1",
@@ -7,7 +6,7 @@
"cpuQuotaForeground" : -1,
"cpuQuotaBackground" : 1000,
"privilege" : 10,
- "vt" : ~VT~,
+ "vt" : 0,
"switchToDefaultAfterTimeout" : true,
"enableDbusIntegration" : true,
"runMountPoint" : "~NAME~/run",
diff --git a/server/server.cpp b/server/server.cpp
index 874b02f..b4e2344 100644
--- a/server/server.cpp
+++ b/server/server.cpp
@@ -165,16 +165,6 @@ bool Server::prepareEnvironment(const std::string& configPath, bool runAsRoot)
}
}
- // create directory for additional zone data (if needed)
- if (!config.zoneNewConfigPrefix.empty()) {
- if (!utils::createDir(config.zoneNewConfigPrefix, uid, gid,
- fs::perms::owner_all |
- fs::perms::group_read | fs::perms::group_exe |
- fs::perms::others_read | fs::perms::others_exe)) {
- return false;
- }
- }
-
// Omit supplementaty group setup and root drop if the user is already switched.
// This situation will happen during daemon update triggered by SIGUSR1.
if (!runAsRoot && geteuid() == uid) {
diff --git a/server/zone-admin.cpp b/server/zone-admin.cpp
index 46c4974..32a18d2 100644
--- a/server/zone-admin.cpp
+++ b/server/zone-admin.cpp
@@ -46,12 +46,14 @@ const int SHUTDOWN_WAIT = 10;
const std::uint64_t DEFAULT_CPU_SHARES = 1024;
const std::uint64_t DEFAULT_VCPU_PERIOD_MS = 100000;
-ZoneAdmin::ZoneAdmin(const std::string& zonesPath,
- const std::string& lxcTemplatePrefix,
- const ZoneConfig& config)
+ZoneAdmin::ZoneAdmin(const std::string& zoneId,
+ const std::string& zonesPath,
+ const std::string& lxcTemplatePrefix,
+ const ZoneConfig& config,
+ const ZoneDynamicConfig& dynamicConfig)
: mConfig(config),
- mZone(zonesPath, config.name),
- mId(mZone.getName()),
+ mZone(zonesPath, zoneId),
+ mId(zoneId),
mDetachOnExit(false),
mDestroyOnExit(false)
{
@@ -63,16 +65,16 @@ ZoneAdmin::ZoneAdmin(const std::string& zonesPath,
lxcTemplatePrefix);
LOGI(mId << ": Creating zone from template: " << lxcTemplate);
utils::CStringArrayBuilder args;
- if (!config.ipv4Gateway.empty()) {
+ if (!dynamicConfig.ipv4Gateway.empty()) {
args.add("--ipv4-gateway");
- args.add(config.ipv4Gateway.c_str());
+ args.add(dynamicConfig.ipv4Gateway.c_str());
}
- if (!config.ipv4.empty()) {
+ if (!dynamicConfig.ipv4.empty()) {
args.add("--ipv4");
- args.add(config.ipv4.c_str());
+ args.add(dynamicConfig.ipv4.c_str());
}
- const std::string vt = std::to_string(config.vt);
- if (config.vt > 0) {
+ const std::string vt = std::to_string(dynamicConfig.vt);
+ if (dynamicConfig.vt > 0) {
args.add("--vt");
args.add(vt.c_str());
}
diff --git a/server/zone-admin.hpp b/server/zone-admin.hpp
index 1d31ab4..10b7f02 100644
--- a/server/zone-admin.hpp
+++ b/server/zone-admin.hpp
@@ -44,13 +44,17 @@ public:
/**
* ZoneAdmin constructor
+ * @param zoneId zone id
* @param zonesPath directory where zones are defined (lxc configs, rootfs etc)
* @param lxcTemplatePrefix directory where templates are stored
* @param config zones config
+ * @param dynamicConfig zones dynamic config
*/
- ZoneAdmin(const std::string& zonesPath,
- const std::string& lxcTemplatePrefix,
- const ZoneConfig& config);
+ ZoneAdmin(const std::string& zoneId,
+ const std::string& zonesPath,
+ const std::string& lxcTemplatePrefix,
+ const ZoneConfig& config,
+ const ZoneDynamicConfig& dynamicConfig);
virtual ~ZoneAdmin();
/**
diff --git a/server/zone-config.hpp b/server/zone-config.hpp
index 02db1c4..da4b570 100644
--- a/server/zone-config.hpp
+++ b/server/zone-config.hpp
@@ -38,11 +38,6 @@ namespace vasum {
struct ZoneConfig {
/**
- * Zone name
- */
- std::string name;
-
- /**
* Lxc template name (relative to lxcTemplatePrefix)
*/
std::string lxcTemplate;
@@ -53,27 +48,12 @@ struct ZoneConfig {
std::vector initWithArgs;
/**
- * IP v4 gateway address
- */
- std::string ipv4Gateway;
-
- /**
- * IP v4 address
- */
- std::string ipv4;
-
- /**
* Privilege of the zone.
* The smaller the value the more important the zone
*/
int privilege;
/**
- * Number of virtual terminal used by xserver inside zone
- */
- int vt;
-
- /**
* Allow switching to default zone after timeout.
* Setting this to false will disable switching to default zone after timeout.
*/
@@ -96,11 +76,6 @@ struct ZoneConfig {
std::int64_t cpuQuotaBackground;
/**
- * Path to zones dbus unix socket
- */
- std::string runMountPoint;
-
- /**
* When you move a file out of the zone (by move request)
* its path must match at least one of the regexps in this vector.
*/
@@ -119,27 +94,57 @@ struct ZoneConfig {
CONFIG_REGISTER
(
- name,
lxcTemplate,
initWithArgs,
- ipv4Gateway,
- ipv4,
- privilege,
- vt,
- switchToDefaultAfterTimeout,
+ privilege, // TODO not needed?
+ switchToDefaultAfterTimeout, // TODO move to dynamic and add an API to change
enableDbusIntegration,
cpuQuotaForeground,
cpuQuotaBackground,
- runMountPoint,
- permittedToSend,
- permittedToRecv,
+ permittedToSend, // TODO move to dynamic and add an API to change
+ permittedToRecv, // TODO move to dynamic and add an API to change
validLinkPrefixes
)
};
struct ZoneDynamicConfig {
- //TODO a place for zone dynamic config (other than provisioning which has its own struct)
- CONFIG_REGISTER_EMPTY
+
+ /**
+ * IP v4 gateway address
+ */
+ std::string ipv4Gateway;
+
+ /**
+ * IP v4 address
+ */
+ std::string ipv4;
+
+ /**
+ * Number of virtual terminal used by xserver inside zone
+ */
+ int vt;
+
+ /**
+ * Path to zones dbus unix socket
+ */
+ std::string runMountPoint;
+
+ CONFIG_REGISTER
+ (
+ ipv4Gateway,
+ ipv4,
+ vt,
+ runMountPoint
+ )
+};
+
+struct ZoneTemplatePathConfig {
+ /**
+ * A path to zone template config (containing default values)
+ */
+ std::string zoneTemplatePath;
+
+ CONFIG_REGISTER(zoneTemplatePath)
};
} // namespace vasum
diff --git a/server/zone.cpp b/server/zone.cpp
index 0c2eca7..e8580e1 100644
--- a/server/zone.cpp
+++ b/server/zone.cpp
@@ -53,14 +53,17 @@ const int RECONNECT_DELAY = 1 * 1000;
} // namespace
Zone::Zone(const utils::Worker::Pointer& worker,
+ const std::string& zoneId,
const std::string& zonesPath,
- const std::string& zoneConfigPath,
+ const std::string& zoneTemplatePath,
const std::string& dbPath,
const std::string& lxcTemplatePrefix,
const std::string& baseRunMountPointPath)
: mWorker(worker)
{
- config::loadFromJsonFile(zoneConfigPath, mConfig);
+ const std::string dbPrefix = getZoneDbPrefix(zoneId);
+ config::loadFromKVStoreWithJsonFile(dbPath, zoneTemplatePath, mConfig, dbPrefix);
+ config::loadFromKVStoreWithJsonFile(dbPath, zoneTemplatePath, mDynamicConfig, dbPrefix);
for (std::string r: mConfig.permittedToSend) {
mPermittedToSend.push_back(boost::regex(r));
@@ -69,18 +72,16 @@ Zone::Zone(const utils::Worker::Pointer& worker,
mPermittedToRecv.push_back(boost::regex(r));
}
- if (!mConfig.runMountPoint.empty()) {
- mRunMountPoint = fs::absolute(mConfig.runMountPoint, baseRunMountPointPath).string();
+ if (!mDynamicConfig.runMountPoint.empty()) {
+ mRunMountPoint = fs::absolute(mDynamicConfig.runMountPoint, baseRunMountPointPath).string();
}
- mAdmin.reset(new ZoneAdmin(zonesPath, lxcTemplatePrefix, mConfig));
+ mAdmin.reset(new ZoneAdmin(zoneId, zonesPath, lxcTemplatePrefix, mConfig, mDynamicConfig));
- const fs::path zonePath = fs::path(zonesPath) / mAdmin->getId();
+ const fs::path zonePath = fs::path(zonesPath) / zoneId;
mRootPath = (zonePath / fs::path("rootfs")).string();
- const std::string dbPrefix = getZoneDbPrefix(mAdmin->getId());
- config::loadFromKVStoreWithJsonFile(dbPath, zoneConfigPath, mDynamicConfig, dbPrefix);
- mProvision.reset(new ZoneProvision(mRootPath, zoneConfigPath, dbPath, dbPrefix, mConfig.validLinkPrefixes));
+ mProvision.reset(new ZoneProvision(mRootPath, zoneTemplatePath, dbPath, dbPrefix, mConfig.validLinkPrefixes));
}
Zone::~Zone()
@@ -187,7 +188,7 @@ std::string Zone::getDbusAddress() const
int Zone::getVT() const
{
- return mConfig.vt;
+ return mDynamicConfig.vt;
}
std::string Zone::getRootPath() const
@@ -199,8 +200,8 @@ bool Zone::activateVT()
{
Lock lock(mReconnectMutex);
- if (mConfig.vt >= 0) {
- return utils::activateVT(mConfig.vt);
+ if (mDynamicConfig.vt >= 0) {
+ return utils::activateVT(mDynamicConfig.vt);
}
return true;
diff --git a/server/zone.hpp b/server/zone.hpp
index dbffc22..b708811 100644
--- a/server/zone.hpp
+++ b/server/zone.hpp
@@ -47,14 +47,17 @@ class Zone {
public:
/**
* Zone constructor
+ * @param zoneId zone id
* @param zonesPath directory where zones are defined (lxc configs, rootfs etc)
- * @param zoneConfigPath path for zones config
+ * @param zoneTemplatePath path for zones config template
+ * @param dbPath path to dynamic config db file
* @param lxcTemplatePrefix directory where templates are stored
* @param baseRunMountPointPath base directory for run mount point
*/
Zone(const utils::Worker::Pointer& worker,
+ const std::string& zoneId,
const std::string& zonesPath,
- const std::string& zoneConfigPath,
+ const std::string& zoneTemplatePath,
const std::string& dbPath,
const std::string& lxcTemplatePrefix,
const std::string& baseRunMountPointPath);
diff --git a/server/zones-manager-config.hpp b/server/zones-manager-config.hpp
index 3ae6e5a..bfc1b25 100644
--- a/server/zones-manager-config.hpp
+++ b/server/zones-manager-config.hpp
@@ -60,11 +60,6 @@ struct ZonesManagerConfig {
std::string zoneTemplateDir;
/**
- * Prefix added to a path for new zone configuration files
- */
- std::string zoneNewConfigPrefix;
-
- /**
* Path prefix for lxc templates
*/
std::string lxcTemplatePrefix;
@@ -95,7 +90,6 @@ struct ZonesManagerConfig {
zonesPath,
zoneImagePath,
zoneTemplateDir,
- zoneNewConfigPrefix,
lxcTemplatePrefix,
availableVTs,
inputConfig,
@@ -107,10 +101,9 @@ struct ZonesManagerConfig {
struct ZonesManagerDynamicConfig {
/**
- * List of zones' configs that we manage.
- * File paths can be relative to the ZoneManager config file.
+ * A list of created zones.
*/
- std::vector zoneConfigs;
+ std::vector zoneIds;
/**
* An ID of default zone.
@@ -119,7 +112,7 @@ struct ZonesManagerDynamicConfig {
CONFIG_REGISTER
(
- zoneConfigs,
+ zoneIds,
defaultId
)
};
diff --git a/server/zones-manager.cpp b/server/zones-manager.cpp
index 3baa2a2..bccfd21 100644
--- a/server/zones-manager.cpp
+++ b/server/zones-manager.cpp
@@ -70,15 +70,9 @@ const std::string ENABLED_FILE_NAME = "enabled";
const boost::regex ZONE_NAME_REGEX("~NAME~");
const boost::regex ZONE_IP_THIRD_OCTET_REGEX("~IP~");
-const boost::regex ZONE_VT_REGEX("~VT~");
const unsigned int ZONE_IP_BASE_THIRD_OCTET = 100;
-std::string getConfigName(const std::string& zoneId)
-{
- return "zones/" + zoneId + ".conf";
-}
-
template
void remove(std::vector& v, const T& item)
{
@@ -189,8 +183,8 @@ ZonesManager::ZonesManager(const std::string& configPath)
mHostConnection.setRevokeDeviceCallback(bind(&ZonesManager::handleRevokeDeviceCall,
this, _1, _2, _3));
- for (const auto& zoneConfig : mDynamicConfig.zoneConfigs) {
- insertZone(utils::createFilePath(mConfig.zoneNewConfigPrefix, zoneConfig));
+ for (const auto& zoneId : mDynamicConfig.zoneIds) {
+ insertZone(zoneId, getTemplatePathForExistingZone(zoneId));
}
updateDefaultId();
@@ -269,38 +263,46 @@ void ZonesManager::updateDefaultId()
saveDynamicConfig();
}
-void ZonesManager::insertZone(const std::string& zoneConfigPath)
+std::string ZonesManager::getTemplatePathForExistingZone(const std::string& id)
{
- LOGT("Creating Zone " << zoneConfigPath);
+ ZoneTemplatePathConfig config;
+ config::loadFromKVStore(mConfig.dbPath, config, getZoneDbPrefix(id));
+ return config.zoneTemplatePath;
+}
+
+void ZonesManager::insertZone(const std::string& zoneId, const std::string& zoneTemplatePath)
+{
+ if (zoneId == HOST_ID) {
+ throw InvalidZoneIdException("Cannot use reserved zone ID");
+ }
+ if (findZone(zoneId) != mZones.end()) {
+ throw InvalidZoneIdException("Zone already exists");
+ }
+
+ LOGT("Creating Zone " << zoneId);
std::unique_ptr zone(new Zone(mWorker->createSubWorker(),
+ zoneId,
mConfig.zonesPath,
- zoneConfigPath,
+ zoneTemplatePath,
mConfig.dbPath,
mConfig.lxcTemplatePrefix,
mConfig.runMountPointPrefix));
- const std::string id = zone->getId();
- if (id == HOST_ID) {
- throw InvalidZoneIdException("Cannot use reserved zone ID");
- }
- if (findZone(id) != mZones.end()) {
- throw InvalidZoneIdException("Zone already exists");
- }
using namespace std::placeholders;
zone->setNotifyActiveZoneCallback(bind(&ZonesManager::notifyActiveZoneHandler,
- this, id, _1, _2));
+ this, zoneId, _1, _2));
zone->setDisplayOffCallback(bind(&ZonesManager::displayOffHandler,
- this, id));
+ this, zoneId));
zone->setFileMoveRequestCallback(bind(&ZonesManager::handleZoneMoveFileRequest,
- this, id, _1, _2, _3));
+ this, zoneId, _1, _2, _3));
zone->setProxyCallCallback(bind(&ZonesManager::handleProxyCall,
- this, id, _1, _2, _3, _4, _5, _6, _7));
+ this, zoneId, _1, _2, _3, _4, _5, _6, _7));
zone->setDbusStateChangedCallback(bind(&ZonesManager::handleDbusStateChanged,
- this, id, _1));
+ this, zoneId, _1));
mZones.push_back(std::move(zone));
@@ -333,7 +335,7 @@ void ZonesManager::destroyZone(const std::string& zoneId)
}
// update dynamic config
- remove(mDynamicConfig.zoneConfigs, getConfigName(zoneId));
+ remove(mDynamicConfig.zoneIds, zoneId);
saveDynamicConfig();
updateDefaultId();
@@ -932,52 +934,40 @@ void ZonesManager::handleSetActiveZoneCall(const std::string& id,
void ZonesManager::generateNewConfig(const std::string& id,
- const std::string& templatePath,
- const std::string& resultPath)
+ const std::string& templatePath)
{
- // TODO Do not store new config at all, use template and dynamic config instead
- namespace fs = boost::filesystem;
+ const std::string dbPrefix = getZoneDbPrefix(id);
+ ZoneDynamicConfig dynamicConfig;
+ config::loadFromKVStoreWithJsonFile(mConfig.dbPath, templatePath, dynamicConfig, dbPrefix);
- if (fs::exists(resultPath)) {
- LOGT(resultPath << " already exists, removing");
- fs::remove(resultPath);
- } else {
- std::string resultFileDir = utils::dirName(resultPath);
- if (!utils::createDirs(resultFileDir, fs::perms::owner_all |
- fs::perms::group_read | fs::perms::group_exe |
- fs::perms::others_read | fs::perms::others_exe)) {
- LOGE("Unable to create directory for new config.");
- throw ZoneOperationException("Unable to create directory for new config.");
- }
- }
-
- std::string config;
- if (!utils::readFileContent(templatePath, config)) {
- LOGE("Failed to read template config file.");
- throw ZoneOperationException("Failed to read template config file.");
- }
-
- std::string resultConfig = boost::regex_replace(config, ZONE_NAME_REGEX, id);
+ // update mount point path
+ dynamicConfig.runMountPoint = boost::regex_replace(dynamicConfig.runMountPoint,
+ ZONE_NAME_REGEX,
+ id);
// generate first free VT number
const int freeVT = getVTForNewZone();
LOGD("VT number: " << freeVT);
- resultConfig = boost::regex_replace(resultConfig, ZONE_VT_REGEX, std::to_string(freeVT));
+ dynamicConfig.vt = freeVT;
// generate third IP octet for network config
std::string thirdOctetStr = std::to_string(ZONE_IP_BASE_THIRD_OCTET + freeVT);
LOGD("IP third octet: " << thirdOctetStr);
- resultConfig = boost::regex_replace(resultConfig, ZONE_IP_THIRD_OCTET_REGEX, thirdOctetStr);
+ dynamicConfig.ipv4Gateway = boost::regex_replace(dynamicConfig.ipv4Gateway,
+ ZONE_IP_THIRD_OCTET_REGEX,
+ thirdOctetStr);
+ dynamicConfig.ipv4 = boost::regex_replace(dynamicConfig.ipv4,
+ ZONE_IP_THIRD_OCTET_REGEX,
+ thirdOctetStr);
- if (!utils::saveFileContent(resultPath, resultConfig)) {
- LOGE("Faield to save new config file.");
- throw ZoneOperationException("Failed to save new config file.");
- }
+ // save dynamic config
+ config::saveToKVStore(mConfig.dbPath, dynamicConfig, dbPrefix);
+
+ // save zone template path
+ ZoneTemplatePathConfig templatePathConfig;
+ templatePathConfig.zoneTemplatePath = templatePath;
+ config::saveToKVStore(mConfig.dbPath, templatePathConfig, dbPrefix);
- // restrict new config file so that only owner (vasum) can write it
- fs::permissions(resultPath, fs::perms::owner_read | fs::perms::owner_write |
- fs::perms::group_read |
- fs::perms::others_read);
}
int ZonesManager::getVTForNewZone()
@@ -1035,10 +1025,6 @@ void ZonesManager::createZone(const std::string& id,
}
}
- // generate paths to new configuration files
- std::string newConfigName = getConfigName(id);
- std::string newConfigPath = utils::createFilePath(mConfig.zoneNewConfigPrefix, newConfigName);
-
auto removeAllWrapper = [](const std::string& path) -> bool {
try {
LOGD("Removing copied data");
@@ -1053,9 +1039,8 @@ void ZonesManager::createZone(const std::string& id,
templateName + ".conf");
try {
- LOGI("Generating config from " << zoneTemplatePath << " to " << newConfigPath);
- generateNewConfig(id, zoneTemplatePath, newConfigPath);
-
+ LOGI("Generating config from " << zoneTemplatePath);
+ generateNewConfig(id, zoneTemplatePath);
} catch (VasumException& e) {
LOGE("Generate config failed: " << e.what());
utils::launchAsRoot(std::bind(removeAllWrapper, zonePathStr));
@@ -1064,14 +1049,14 @@ void ZonesManager::createZone(const std::string& id,
LOGT("Creating new zone");
try {
- insertZone(newConfigPath);
+ insertZone(id, zoneTemplatePath);
} catch (VasumException& e) {
LOGE("Creating new zone failed: " << e.what());
utils::launchAsRoot(std::bind(removeAllWrapper, zonePathStr));
throw e;
}
- mDynamicConfig.zoneConfigs.push_back(newConfigName);
+ mDynamicConfig.zoneIds.push_back(id);
saveDynamicConfig();
updateDefaultId();
}
diff --git a/server/zones-manager.hpp b/server/zones-manager.hpp
index 529ef8b..6453278 100644
--- a/server/zones-manager.hpp
+++ b/server/zones-manager.hpp
@@ -135,10 +135,10 @@ private:
void refocus();
void switchingSequenceMonitorNotify();
void generateNewConfig(const std::string& id,
- const std::string& templatePath,
- const std::string& resultPath);
+ const std::string& templatePath);
+ std::string getTemplatePathForExistingZone(const std::string& id);
int getVTForNewZone();
- void insertZone(const std::string& zoneConfigPath);
+ void insertZone(const std::string& zoneId, const std::string& templatePath);
void notifyActiveZoneHandler(const std::string& caller,
const std::string& appliaction,
diff --git a/tests/unit_tests/client/configs/CMakeLists.txt b/tests/unit_tests/client/configs/CMakeLists.txt
index 8742612..5587d11 100644
--- a/tests/unit_tests/client/configs/CMakeLists.txt
+++ b/tests/unit_tests/client/configs/CMakeLists.txt
@@ -20,20 +20,16 @@
MESSAGE(STATUS "Installing configs for the Client Unit Tests to " ${VSM_TEST_CONFIG_INSTALL_DIR})
## Generate ####################################################################
-CONFIGURE_FILE(ut-client/test-dbus-daemon.conf.in
- ${CMAKE_BINARY_DIR}/ut-client/test-dbus-daemon.conf @ONLY)
+CONFIGURE_FILE(ut-client/test-daemon.conf.in
+ ${CMAKE_BINARY_DIR}/ut-client/test-daemon.conf @ONLY)
FILE(GLOB client_manager_CONF_GEN ${CMAKE_BINARY_DIR}/ut-client/*.conf)
-CONFIGURE_FILE(ut-client/zones/console1-dbus.conf.in
- ${CMAKE_BINARY_DIR}/ut-client/zones/console1-dbus.conf @ONLY)
-CONFIGURE_FILE(ut-client/zones/console2-dbus.conf.in
- ${CMAKE_BINARY_DIR}/ut-client/zones/console2-dbus.conf @ONLY)
-CONFIGURE_FILE(ut-client/zones/console3-dbus.conf.in
- ${CMAKE_BINARY_DIR}/ut-client/zones/console3-dbus.conf @ONLY)
-FILE(GLOB client_zone_CONF_GEN ${CMAKE_BINARY_DIR}/ut-client/zones/*.conf)
+CONFIGURE_FILE(ut-client/templates/console-dbus.conf.in
+ ${CMAKE_BINARY_DIR}/ut-client/templates/console-dbus.conf @ONLY)
+FILE(GLOB client_templates_CONF_GEN ${CMAKE_BINARY_DIR}/ut-client/templates/*.conf)
## Install #####################################################################
INSTALL(FILES ${client_manager_CONF_GEN}
DESTINATION ${VSM_TEST_CONFIG_INSTALL_DIR}/client/ut-client)
-INSTALL(FILES ${client_zone_CONF_GEN}
- DESTINATION ${VSM_TEST_CONFIG_INSTALL_DIR}/client/ut-client/zones)
+INSTALL(FILES ${client_templates_CONF_GEN}
+ DESTINATION ${VSM_TEST_CONFIG_INSTALL_DIR}/client/ut-client/templates)
diff --git a/tests/unit_tests/server/configs/ut-zones-manager/templates/default.conf.in b/tests/unit_tests/client/configs/ut-client/templates/console-dbus.conf.in
similarity index 96%
rename from tests/unit_tests/server/configs/ut-zones-manager/templates/default.conf.in
rename to tests/unit_tests/client/configs/ut-client/templates/console-dbus.conf.in
index 28879db..0bd0e03 100644
--- a/tests/unit_tests/server/configs/ut-zones-manager/templates/default.conf.in
+++ b/tests/unit_tests/client/configs/ut-client/templates/console-dbus.conf.in
@@ -1,5 +1,4 @@
{
- "name" : "~NAME~",
"lxcTemplate" : "minimal-dbus.sh",
"initWithArgs" : ["/bin/sh", "-c", "trap exit SIGTERM; /usr/bin/dbus-daemon --config-file=@VSM_TEST_CONFIG_INSTALL_DIR@/server/ut-zones-manager/ut-dbus.conf --fork; read"],
"ipv4Gateway" : "",
diff --git a/tests/unit_tests/server/configs/ut-zones-manager/empty-dbus-daemon.conf.in b/tests/unit_tests/client/configs/ut-client/test-daemon.conf.in
similarity index 80%
rename from tests/unit_tests/server/configs/ut-zones-manager/empty-dbus-daemon.conf.in
rename to tests/unit_tests/client/configs/ut-client/test-daemon.conf.in
index 83b1124..6346df3 100644
--- a/tests/unit_tests/server/configs/ut-zones-manager/empty-dbus-daemon.conf.in
+++ b/tests/unit_tests/client/configs/ut-client/test-daemon.conf.in
@@ -1,11 +1,10 @@
{
"dbPath" : "/tmp/ut-zones/vasum.db",
- "zoneConfigs" : [],
+ "zoneIds" : [],
"defaultId" : "",
"zonesPath" : "/tmp/ut-zones",
"zoneImagePath" : "",
- "zoneTemplateDir" : "@VSM_TEST_CONFIG_INSTALL_DIR@/server/ut-zones-manager/templates/",
- "zoneNewConfigPrefix" : "/tmp/ut-zones/generated-configs/",
+ "zoneTemplateDir" : "@VSM_TEST_CONFIG_INSTALL_DIR@/client/ut-client/templates/",
"runMountPointPrefix" : "",
"lxcTemplatePrefix" : "@VSM_TEST_LXC_TEMPLATES_INSTALL_DIR@",
"availableVTs" : [],
diff --git a/tests/unit_tests/client/configs/ut-client/test-dbus-daemon.conf.in b/tests/unit_tests/client/configs/ut-client/test-dbus-daemon.conf.in
deleted file mode 100644
index b51ee9a..0000000
--- a/tests/unit_tests/client/configs/ut-client/test-dbus-daemon.conf.in
+++ /dev/null
@@ -1,25 +0,0 @@
-{
- "dbPath" : "/tmp/ut-zones/vasum.db",
- "zoneConfigs" : ["zones/console1-dbus.conf",
- "zones/console2-dbus.conf",
- "zones/console3-dbus.conf"],
- "defaultId" : "ut-zones-manager-console1-dbus",
- "zonesPath" : "/tmp/ut-zones",
- "zoneImagePath" : "",
- "zoneTemplateDir" : "no_need_for_templates_in_this_test",
- "zoneNewConfigPrefix" : "@VSM_TEST_CONFIG_INSTALL_DIR@/client/ut-client/",
- "runMountPointPrefix" : "",
- "lxcTemplatePrefix" : "@VSM_TEST_LXC_TEMPLATES_INSTALL_DIR@",
- "availableVTs" : [],
- "inputConfig" : {"enabled" : false,
- "device" : "/dev/doesnotexist",
- "code" : 139,
- "numberOfEvents" : 2,
- "timeWindowMs" : 500},
- "proxyCallRules" : [{"caller" : "*",
- "target" : "*",
- "targetBusName" : "org.tizen.vasum.tests",
- "targetObjectPath" : "*",
- "targetInterface" : "*",
- "targetMethod" : "*"}]
-}
diff --git a/tests/unit_tests/client/configs/ut-client/zones/console1-dbus.conf.in b/tests/unit_tests/client/configs/ut-client/zones/console1-dbus.conf.in
deleted file mode 100644
index 097480d..0000000
--- a/tests/unit_tests/client/configs/ut-client/zones/console1-dbus.conf.in
+++ /dev/null
@@ -1,18 +0,0 @@
-{
- "name" : "ut-zones-manager-console1-dbus",
- "lxcTemplate" : "minimal-dbus.sh",
- "initWithArgs" : ["/bin/sh", "-c", "trap exit SIGTERM; /usr/bin/dbus-daemon --config-file=@VSM_TEST_CONFIG_INSTALL_DIR@/server/ut-zones-manager/ut-dbus.conf --fork; read"],
- "ipv4Gateway" : "",
- "ipv4" : "",
- "privilege" : 20,
- "vt" : -1,
- "switchToDefaultAfterTimeout" : true,
- "enableDbusIntegration" : true,
- "cpuQuotaForeground" : -1,
- "cpuQuotaBackground" : 1000,
- "runMountPoint" : "/tmp/ut-run/ut-zones-manager-console1-dbus",
- "provisions" : [],
- "permittedToSend" : [ "/tmp/.*", "/etc/secret2" ],
- "permittedToRecv" : [ "/tmp/.*" ],
- "validLinkPrefixes" : []
-}
diff --git a/tests/unit_tests/client/configs/ut-client/zones/console2-dbus.conf.in b/tests/unit_tests/client/configs/ut-client/zones/console2-dbus.conf.in
deleted file mode 100644
index 440772b..0000000
--- a/tests/unit_tests/client/configs/ut-client/zones/console2-dbus.conf.in
+++ /dev/null
@@ -1,18 +0,0 @@
-{
- "name" : "ut-zones-manager-console2-dbus",
- "lxcTemplate" : "minimal-dbus.sh",
- "initWithArgs" : ["/bin/sh", "-c", "trap exit SIGTERM; /usr/bin/dbus-daemon --config-file=@VSM_TEST_CONFIG_INSTALL_DIR@/server/ut-zones-manager/ut-dbus.conf --fork; read"],
- "ipv4Gateway" : "",
- "ipv4" : "",
- "privilege" : 20,
- "vt" : -1,
- "switchToDefaultAfterTimeout" : false,
- "enableDbusIntegration" : true,
- "cpuQuotaForeground" : -1,
- "cpuQuotaBackground" : 1000,
- "runMountPoint" : "/tmp/ut-run/ut-zones-manager-console2-dbus",
- "provisions" : [],
- "permittedToSend" : [ "/tmp/.*" ],
- "permittedToRecv" : [ "/tmp/.*", "/etc/secret1" ],
- "validLinkPrefixes" : []
-}
diff --git a/tests/unit_tests/client/configs/ut-client/zones/console3-dbus.conf.in b/tests/unit_tests/client/configs/ut-client/zones/console3-dbus.conf.in
deleted file mode 100644
index 2f28ee1..0000000
--- a/tests/unit_tests/client/configs/ut-client/zones/console3-dbus.conf.in
+++ /dev/null
@@ -1,18 +0,0 @@
-{
- "name" : "ut-zones-manager-console3-dbus",
- "lxcTemplate" : "minimal-dbus.sh",
- "initWithArgs" : ["/bin/sh", "-c", "trap exit SIGTERM; /usr/bin/dbus-daemon --config-file=@VSM_TEST_CONFIG_INSTALL_DIR@/server/ut-zones-manager/ut-dbus.conf --fork; read"],
- "ipv4Gateway" : "",
- "ipv4" : "",
- "privilege" : 20,
- "vt" : -1,
- "switchToDefaultAfterTimeout" : true,
- "enableDbusIntegration" : true,
- "cpuQuotaForeground" : -1,
- "cpuQuotaBackground" : 1000,
- "runMountPoint" : "/tmp/ut-run/ut-zones-manager-console3-dbus",
- "provisions" : [],
- "permittedToSend" : [ "/tmp/.*" ],
- "permittedToRecv" : [ "/tmp/.*" ],
- "validLinkPrefixes" : []
-}
diff --git a/tests/unit_tests/client/ut-client.cpp b/tests/unit_tests/client/ut-client.cpp
index a188a9d..1c55668 100644
--- a/tests/unit_tests/client/ut-client.cpp
+++ b/tests/unit_tests/client/ut-client.cpp
@@ -31,6 +31,7 @@
#include "utils/scoped-dir.hpp"
#include "zones-manager.hpp"
#include "zone-dbus-definitions.hpp"
+#include "logger/logger.hpp"
#include