From 9aef401e5787d9c00424dbdfceb577aa9ce1f012 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Fri, 5 May 2006 11:13:16 -0400 Subject: Make /etc/cobbler.conf be packaged instead of being created, proper handling of parse errors on config files, tweaking unit tests tempfile stuff since sync() test is currently broken by the tempfile changes (why??) --- cobbler/api.py | 12 +++++++++--- cobbler/cobbler.py | 37 ++++++++++++++++++++----------------- cobbler/config.py | 25 +++++++------------------ cobbler/msg.py | 6 +++--- cobbler/sync.py | 1 + 5 files changed, 40 insertions(+), 41 deletions(-) (limited to 'cobbler') diff --git a/cobbler/api.py b/cobbler/api.py index 77dd835..e48fe7a 100644 --- a/cobbler/api.py +++ b/cobbler/api.py @@ -29,13 +29,19 @@ class BootAPI: try: if self.config.files_exist(): self.config.deserialize() - except: - # traceback.print_exc() - print m("no_cfg") + except Exception, e: + # parse errors, attempt to recover + print self.last_error + if self.last_error == m("parse_error"): + # the error came from /etc/cobbler.conf, and must be fixed + # manually, CLI can't do it for policy reasons on /etc + raise Exception, "parse_error" try: self.config.serialize() except: + # shouldn't get here. File permissions issue, perhaps? traceback.print_exc() + raise Exception, "parse_error2" if not self.config.files_exist(): self.config.serialize() diff --git a/cobbler/cobbler.py b/cobbler/cobbler.py index 0b26307..fb58ff7 100755 --- a/cobbler/cobbler.py +++ b/cobbler/cobbler.py @@ -10,6 +10,7 @@ import os import sys import api import syck +import traceback from msg import * class BootCLI: @@ -59,13 +60,13 @@ class BootCLI: def run(self): """ - Run the command line + Run the command line and return system exit code """ rc = self.curry_args(self.args[1:], self.commands['toplevel']) if not rc: print self.api.last_error - return rc - + return 1 + return 0 def usage(self,args): """ @@ -294,19 +295,21 @@ def main(): # verify syck isn't busted (old syck bindings were) if not hasattr(syck,"dump"): - raise Exception("needs a more-recent PySyck") + raise Exception("needs a more-recent PySyck") if os.getuid() != 0: - # while it's true that we don't technically need root, we do need - # permissions on a relatively long list of files that ordinarily - # only root has access to, and we don't know specifically what - # files are where (other distributions in play, etc). It's - # fairly safe to assume root is required. This might be patched - # later. - print m("need_root") - sys.exit(1) - if BootCLI(sys.argv).run(): - sys.exit(0) - else: - sys.exit(1) - + # while it's true that we don't technically need root, we do need + # permissions on a relatively long list of files that ordinarily + # only root has access to, and we don't know specifically what + # files are where (other distributions in play, etc). It's + # fairly safe to assume root is required. This might be patched + # later. + print m("need_root") + sys.exit(1) + try: + cli = BootCLI(sys.argv) + except Exception, e: + if not str(e) or not str(e).startswith("parse_error"): + traceback.print_exc() + sys.exit(3) + sys.exit(cli.run()) diff --git a/cobbler/config.py b/cobbler/config.py index aa17763..197da91 100644 --- a/cobbler/config.py +++ b/cobbler/config.py @@ -15,7 +15,6 @@ global_settings_file = "/etc/cobbler.conf" global_state_file = "/var/lib/cobbler/cobbler.conf" - class BootConfig: def __init__(self,api): @@ -153,17 +152,9 @@ class BootConfig: state = None # ------ - # dump global config (pathing, urls, etc)... - try: - settings = open(self.settings_file,"w+") - except IOError: - self.api.last_error = m("cant_create: %s" % self.settings_file) - return False - data = self.to_hash(True) - settings.write(syck.dump(data)) + # dump internal state (distros, profiles, systems...) into /var/lib/... + # /etc is not serialized, it's packaged. - # ------ - # dump internal state (distros, profiles, systems...) if not os.path.isdir(os.path.dirname(self.state_file)): dirname = os.path.dirname(self.state_file) if dirname != "": @@ -194,11 +185,10 @@ class BootConfig: self.from_hash(settings,True) else: self.last_error = m("parse_error") - return False + raise Exception("parse_error") except: - traceback.print_exc() self.api.last_error = m("parse_error") - return False + raise Exception("parse_error") # ----- # load internal state(distros, systems, profiles...) @@ -207,12 +197,11 @@ class BootConfig: if state is not None: self.from_hash(state,False) else: - self.last_error = m("parse_error") - return False + self.last_error = m("parse_error2") + raise Exception("parse_error2") except: - traceback.print_exc() self.api.last_error = m("parse_error2") - return False + raise Exception("parse_error2") # all good return True diff --git a/cobbler/msg.py b/cobbler/msg.py index f50443c..1eeb8e6 100644 --- a/cobbler/msg.py +++ b/cobbler/msg.py @@ -8,8 +8,8 @@ be reused and potentially translated. msg_table = { "bad_server" : "server field in /etc/cobbler.conf must be set to something other than localhost, or kickstarts will fail", - "parse_error" : "could not parse /etc/cobbler.conf", - "parse_error2" : "could not parse /var/cobbler/cobbler.conf", + "parse_error" : "could not parse /etc/cobbler.conf, must fix manually", + "parse_error2" : "could not parse /var/lib/cobbler/cobbler.conf, replacing...", "no_create" : "cannot create: %s", "no_args" : "this command requires arguments.", "missing_options" : "cannot add, all parameters have not been set", @@ -29,7 +29,7 @@ msg_table = { "no_exist" : "%s does not exist", "no_line" : "file '%s' should have a line '%s' somewhere", "no_dir2" : "can't find %s for %s in cobbler.conf", - "no_cfg" : "could not find cobbler.conf, recreating", + "no_cfg" : "could not find a valid /etc/cobbler.conf, rebuilding", "bad_param" : "at least one parameter is missing for this function", "empty_list" : "(Empty)", "err_resolv" : "system (%s) did not resolve", diff --git a/cobbler/sync.py b/cobbler/sync.py index 80b0c67..3494382 100644 --- a/cobbler/sync.py +++ b/cobbler/sync.py @@ -101,6 +101,7 @@ class BootSync: initrd = self.api.utils.find_initrd(d.initrd) # full path if kernel is None or not os.path.isfile(kernel): self.api.last_error = "Kernel for distro (%s) cannot be found and needs to be fixed: %s" % (d.name, d.kernel) + print self.api.last_error raise "error" if initrd is None or not os.path.isfile(initrd): self.api.last_error = "Initrd for distro (%s) cannot be found and needs to be fixed: %s" % (d.name, d.initrd) -- cgit