Module: check_mk
Branch: master
Commit: 912ea916bf31dddd792be4236503d1477fb07692
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=912ea916bf31dd…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Wed Dec 16 12:17:57 2015 +0100
New automation for removing MKPs
---
modules/automation.py | 11 +++++++++++
modules/packaging.py | 14 +++++++++++---
2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/modules/automation.py b/modules/automation.py
index ee0f1af..29036e2 100644
--- a/modules/automation.py
+++ b/modules/automation.py
@@ -48,6 +48,8 @@ def do_automation(cmd, args):
result = automation_get_package_info(args)
elif cmd == "get-package":
result = automation_get_package(args)
+ elif cmd == "remove-package":
+ result = automation_remove_package(args)
elif cmd == "notification-get-bulks":
result = automation_get_bulks(args)
else:
@@ -1312,3 +1314,12 @@ def automation_get_package(args):
output_file = fake_file()
create_mkp_file(package, file_object=output_file)
return package, output_file.content()
+
+
+def automation_remove_package(args):
+ load_module("packaging")
+ package_name = args[0]
+ package = read_package_info(package_name)
+ if not package:
+ raise MKAutomationError("Package not installed or corrupt")
+ remove_package(package)
diff --git a/modules/packaging.py b/modules/packaging.py
index 31610fd..0f262b9 100644
--- a/modules/packaging.py
+++ b/modules/packaging.py
@@ -314,6 +314,11 @@ def package_remove(args):
raise PackageException("No such package %s." % pacname)
verbose("Removing package %s...\n" % pacname)
+ remove_package(package)
+ verbose("Successfully removed package %s.\n" % pacname)
+
+
+def remove_package(package):
for part, title, perm, dir in package_parts:
filenames = package["files"].get(part, [])
if len(filenames) > 0:
@@ -325,9 +330,11 @@ def package_remove(args):
os.remove(path)
verbose("\n")
except Exception, e:
- sys.stderr.write("cannot remove %s: %s\n" % (path, e))
- os.remove(pac_dir + pacname)
- verbose("Successfully removed package %s.\n" % pacname)
+ if opt_debug:
+ raise
+ raise Exception("Cannot remove %s: %s\n" % (path, e))
+
+ os.remove(pac_dir + package["name"])
def package_install(args):
@@ -473,6 +480,7 @@ def packaged_files_in_dir(part):
def read_package_info(pacname):
try:
package = eval(file(pac_dir + pacname).read())
+ package["name"] = pacname # do not trust package content
num_files = sum([len(fl) for fl in package["files"].values() ])
package["num_files"] = num_files
return package