]> granicus.if.org Git - icinga2/commitdiff
Cli: 'repository host remove' cleans service directory, add pending changes helper
authorMichael Friedrich <michael.friedrich@netways.de>
Mon, 27 Oct 2014 18:13:33 +0000 (19:13 +0100)
committerMichael Friedrich <michael.friedrich@netways.de>
Mon, 27 Oct 2014 18:13:33 +0000 (19:13 +0100)
refs #7249
fixes #7460

lib/cli/repositoryutility.cpp
lib/cli/repositoryutility.hpp

index e33651c4bd03b5c0f716ce10713c56f195cad725..d06b778c7208034a8b344d7c1031719e4de2d4aa 100644 (file)
@@ -203,6 +203,14 @@ bool RepositoryUtility::ClearChangeLog(void)
        return true;
 }
 
+bool RepositoryUtility::ChangeLogHasPendingChanges(void)
+{
+       Array::Ptr changelog = make_shared<Array>();
+       GetChangeLog(boost::bind(RepositoryUtility::CollectChange, _1, boost::ref(changelog)));
+
+       return changelog->GetLength() > 0;
+}
+
 /* commit changelog */
 bool RepositoryUtility::CommitChangeLog(void)
 {
@@ -264,8 +272,28 @@ bool RepositoryUtility::AddObjectInternal(const String& name, const String& type
 bool RepositoryUtility::RemoveObjectInternal(const String& name, const String& type, const Dictionary::Ptr& attrs)
 {
        String path = GetRepositoryObjectConfigPath(type, attrs) + "/" + name + ".conf";
+       bool success = RemoveObjectFileInternal(path);
+
+       /* special treatment for hosts -> remove the services too */
+       if (type == "Host") {
+               path = GetRepositoryObjectConfigPath(type, attrs) + "/" + name;
+
+               std::vector<String> files;
+               Utility::GlobRecursive(path, "*.conf",
+                   boost::bind(&RepositoryUtility::CollectObjects, _1, boost::ref(files)), GlobFile);
+
+               BOOST_FOREACH(const String& file, files) {
+                       RemoveObjectFileInternal(file);
+               }
+#ifndef _WIN32
+               rmdir(path.CStr());
+#else
+               _rmdir(path.CStr());
+#endif /* _WIN32 */
+
+       }
 
-       return RemoveObjectFileInternal(path);
+       return success;
 }
 
 bool RepositoryUtility::RemoveObjectFileInternal(const String& path)
@@ -276,7 +304,7 @@ bool RepositoryUtility::RemoveObjectFileInternal(const String& path)
        }
 
        if (unlink(path.CStr()) < 0) {
-               Log(LogCritical, "cli", "Cannot remove file '" + path +
+               Log(LogCritical, "cli", "Cannot remove path '" + path +
                    "'. Failed with error code " + Convert::ToString(errno) + ", \"" + Utility::FormatErrorNumber(errno) + "\".");
                return false;
        }
index 2d413f171cbf362dd8c15d88d09b09687c8f31e7..c999599406f9b48e42ac1fcd0004f670c7712460 100644 (file)
@@ -57,6 +57,7 @@ public:
 
        static bool CommitChangeLog(void);
        static bool ClearChangeLog(void);
+       static bool ChangeLogHasPendingChanges(void);
 
        static std::vector<String> GetObjects(void);
 private: