[icinga-checkins] icinga.org: icinga2/support/2.2: Cli: Ensure that repository path is created on 'node add' command

git at icinga.org git at icinga.org
Thu Nov 27 18:16:15 CET 2014


Module: icinga2
Branch: support/2.2
Commit: bea8a58d44b472e2871953a282ba03e61ea5e933
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=bea8a58d44b472e2871953a282ba03e61ea5e933

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Thu Nov 27 18:15:14 2014 +0100

Cli: Ensure that repository path is created on 'node add' command

fixes #7743

---

 lib/cli/nodeupdateconfigcommand.cpp |    6 +++++-
 lib/cli/nodeutility.cpp             |   19 +++++++++++++++++++
 lib/cli/nodeutility.hpp             |    1 +
 3 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/lib/cli/nodeupdateconfigcommand.cpp b/lib/cli/nodeupdateconfigcommand.cpp
index e8104de..10c464f 100644
--- a/lib/cli/nodeupdateconfigcommand.cpp
+++ b/lib/cli/nodeupdateconfigcommand.cpp
@@ -21,9 +21,12 @@
 #include "cli/nodeutility.hpp"
 #include "cli/repositoryutility.hpp"
 #include "base/logger.hpp"
+#include "base/console.hpp"
 #include "base/application.hpp"
-#include "base/objectlock.hpp"
+#include "base/tlsutility.hpp"
+#include "base/scriptvariable.hpp"
 #include "base/json.hpp"
+#include "base/objectlock.hpp"
 #include <boost/foreach.hpp>
 #include <boost/algorithm/string/join.hpp>
 #include <boost/algorithm/string/replace.hpp>
@@ -383,6 +386,7 @@ int NodeUpdateConfigCommand::Run(const boost::program_options::variables_map& vm
 	RepositoryUtility::CommitChangeLog();
 
 	/* store the new inventory for next run */
+	NodeUtility::CreateRepositoryPath();
 	Utility::SaveJsonFile(inventory_path, inventory);
 
 	std::cout << "Make sure to reload Icinga 2 for these changes to take effect." << std::endl;
diff --git a/lib/cli/nodeutility.cpp b/lib/cli/nodeutility.cpp
index c529168..e3387e8 100644
--- a/lib/cli/nodeutility.cpp
+++ b/lib/cli/nodeutility.cpp
@@ -24,6 +24,7 @@
 #include "base/tlsutility.hpp"
 #include "base/convert.hpp"
 #include "base/utility.hpp"
+#include "base/scriptvariable.hpp"
 #include "base/json.hpp"
 #include "base/netstring.hpp"
 #include "base/stdiostream.hpp"
@@ -55,6 +56,20 @@ String NodeUtility::GetNodeSettingsFile(const String& name)
 	return GetRepositoryPath() + "/" + SHA256(name) + ".settings";
 }
 
+void NodeUtility::CreateRepositoryPath(void)
+{
+	if (!Utility::PathExists(GetRepositoryPath()))
+		Utility::MkDirP(GetRepositoryPath(), 0750);
+
+	String user = ScriptVariable::Get("RunAsUser");
+        String group = ScriptVariable::Get("RunAsGroup");
+
+        if (!Utility::SetFileOwnership(GetRepositoryPath(), user, group)) {
+                Log(LogWarning, "cli")
+                    << "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << GetRepositoryPath() << "'. Verify it yourself!";
+	}
+}
+
 std::vector<String> NodeUtility::GetNodeCompletionSuggestions(const String& word)
 {
 	std::vector<String> suggestions;
@@ -147,6 +162,7 @@ void NodeUtility::AddNode(const String& name)
 	node->Set("zone", name);
 	node->Set("repository", Empty);
 
+	CreateRepositoryPath();
 	Utility::SaveJsonFile(path, node);
 }
 
@@ -159,6 +175,7 @@ void NodeUtility::AddNodeSettings(const String& name, const String& host,
 	settings->Set("port", port);
 	settings->Set("log_duration", log_duration);
 
+	CreateRepositoryPath();
 	Utility::SaveJsonFile(GetNodeSettingsFile(name), settings);
 }
 
@@ -444,6 +461,7 @@ int NodeUtility::UpdateBlackAndWhiteList(const String& type, const String& zone_
 	lists->Add(new_filter);
 
 	String list_path = GetBlackAndWhiteListPath(type);
+	CreateRepositoryPath();
 	Utility::SaveJsonFile(list_path, lists);
 
 	return 0;
@@ -488,6 +506,7 @@ int NodeUtility::RemoveBlackAndWhiteList(const String& type, const String& zone_
 	}
 
 	String list_path = GetBlackAndWhiteListPath(type);
+	CreateRepositoryPath();
 	Utility::SaveJsonFile(list_path, lists);
 
 	return 0;
diff --git a/lib/cli/nodeutility.hpp b/lib/cli/nodeutility.hpp
index 9fff77a..f2c748d 100644
--- a/lib/cli/nodeutility.hpp
+++ b/lib/cli/nodeutility.hpp
@@ -39,6 +39,7 @@ public:
 	static String GetRepositoryPath(void);
 	static String GetNodeRepositoryFile(const String& name);
 	static String GetNodeSettingsFile(const String& name);
+	static void CreateRepositoryPath(void);
 	static std::vector<String> GetNodeCompletionSuggestions(const String& word);
 
 	static void PrintNodes(std::ostream& fp);



More information about the icinga-checkins mailing list