From 8a14d554c7de6fe0e238b55b9acf235a35714198 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Fri, 8 Sep 2017 07:37:28 +0200 Subject: Separate VyOSError and ConfigError. VyOSError is now only raised on improper config operations and internal errors, such as trying to use a function on a wrong kind of node. ConfigError should be used by scripts to indicate configuration mistakes and error conditions. --- python/vyos/util.py | 22 ++++++++++++++++++++++ src/conf-mode/vyos-update-crontab.py | 21 +++++++++++---------- 2 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 python/vyos/util.py diff --git a/python/vyos/util.py b/python/vyos/util.py new file mode 100644 index 000000000..a1a835de6 --- /dev/null +++ b/python/vyos/util.py @@ -0,0 +1,22 @@ +# Copyright (c) 2017 VyOS maintainers and contributors +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the Software +# is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +class ConfigError(Exception): + pass diff --git a/src/conf-mode/vyos-update-crontab.py b/src/conf-mode/vyos-update-crontab.py index ac6ae96d3..2d15de8ea 100755 --- a/src/conf-mode/vyos-update-crontab.py +++ b/src/conf-mode/vyos-update-crontab.py @@ -20,7 +20,8 @@ import os import re import sys -from vyos.config import VyOSError, Config +from vyos.config import Config +from vyos.util import ConfigError crontab_file = "/etc/cron.d/vyos-crontab" @@ -75,34 +76,34 @@ def get_config(): def verify(tasks): for task in tasks: if not task["interval"] and not task["spec"]: - raise VyOSError("Invalid task {0}: must define either interval or crontab-spec".format(task["name"])) + raise ConfigError("Invalid task {0}: must define either interval or crontab-spec".format(task["name"])) if task["interval"]: if task["spec"]: - raise VyOSError("Invalid task {0}: cannot use interval and crontab-spec at the same time".format(task["name"])) + raise ConfigError("Invalid task {0}: cannot use interval and crontab-spec at the same time".format(task["name"])) if not re.match(r"^\d+[mdh]?$", task["interval"]): - raise(VyOSError("Invalid interval {0} in task {1}: interval should be a number optionally followed by m, h, or d".format(task["name"], task["interval"]))) + raise(ConfigError("Invalid interval {0} in task {1}: interval should be a number optionally followed by m, h, or d".format(task["name"], task["interval"]))) else: # Check if values are within allowed range value, suffix = split_interval(task["interval"]) if not suffix or suffix == "m": if value > 60: - raise VyOSError("Invalid task {0}: interval in minutes must not exceed 60".format(task["name"])) + raise ConfigError("Invalid task {0}: interval in minutes must not exceed 60".format(task["name"])) elif suffix == "h": if value > 24: - raise VyOSError("Invalid task {0}: interval in hours must not exceed 24".format(task["name"])) + raise ConfigError("Invalid task {0}: interval in hours must not exceed 24".format(task["name"])) elif suffix == "d": if value > 31: - raise VyOSError("Invalid task {0}: interval in days must not exceed 31".format(task["name"])) + raise ConfigError("Invalid task {0}: interval in days must not exceed 31".format(task["name"])) if not task["executable"]: - raise VyOSError("Invalid task {0}: executable is not defined".format(task["name"])) + raise ConfigError("Invalid task {0}: executable is not defined".format(task["name"])) else: # Check if executable exists and is executable if not (os.path.isfile(task["executable"]) and os.access(task["executable"], os.X_OK)): - raise VyOSError("Invalid task {0}: file {1} does not exist or is not executable".format(task["name"], task["executable"])) + raise ConfigError("Invalid task {0}: file {1} does not exist or is not executable".format(task["name"], task["executable"])) def generate(tasks): crontab_header = "### Generated by vyos-update-crontab.py ###\n" @@ -142,6 +143,6 @@ if __name__ == '__main__': verify(c) generate(c) apply(c) - except VyOSError as e: + except ConfigError as e: print(e) sys.exit(1) -- cgit v1.2.3