From 8a4aeb2df4f7f1c217834865ca297495057a82bc Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Fri, 8 Sep 2017 04:40:33 +0200 Subject: Better checks in the cron script. Check if crontab exists before trying to delete it. Fail commit if executable is not defined in the task. --- src/conf-mode/vyos-update-crontab.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/conf-mode') diff --git a/src/conf-mode/vyos-update-crontab.py b/src/conf-mode/vyos-update-crontab.py index d94235f24..ac6ae96d3 100755 --- a/src/conf-mode/vyos-update-crontab.py +++ b/src/conf-mode/vyos-update-crontab.py @@ -16,7 +16,7 @@ # # -import io +import os import re import sys @@ -75,7 +75,7 @@ def get_config(): def verify(tasks): for task in tasks: if not task["interval"] and not task["spec"]: - raise VyOSError(task, "Invalid task {0}: must define either interval or crontab-spec".format(task["name"])) + raise VyOSError("Invalid task {0}: must define either interval or crontab-spec".format(task["name"])) if task["interval"]: if task["spec"]: @@ -97,10 +97,20 @@ def verify(tasks): if value > 31: raise VyOSError("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"])) + 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"])) + def generate(tasks): crontab_header = "### Generated by vyos-update-crontab.py ###\n" if len(tasks) == 0: - os.remove(crontab_file) + if os.path.exists(crontab_file): + os.remove(crontab_file) + else: + pass else: crontab_lines = [] for task in tasks: -- cgit v1.2.3