From 94511341c93c57275037cea114edffa906ef7fa1 Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Dec 27 2022 22:03:03 +0000 Subject: Init lock state module Signed-off-by: Georg Pfuetzenreuter --- diff --git a/salt/_states/lock.py b/salt/_states/lock.py new file mode 100644 index 0000000..3a9fd20 --- /dev/null +++ b/salt/_states/lock.py @@ -0,0 +1,74 @@ +# State module allowing to lock simultaneous executions - helpful in Orchestration states +# Georg Pfuetzenreuter + +from pathlib import Path + +def lock(name, path='/var/lib/salt/'): + ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''} + lockfile = path + name + if Path(lockfile).exists(): + if __opts__["test"]: + ret["comment"] = "Would have complained about {0} already existing".format(lockfile) + ret["result"] = None + else: + ret['comment'] = 'Lockfile {0} already exists'.format(lockfile) + return(ret) + if __opts__["test"]: + ret["comment"] = "Lockfile {0} would have been created".format(lockfile) + ret["result"] = None + return(ret) + try: + Path(lockfile).touch(exist_ok=False) + except FileExistsError as error: + ret['comment'] = 'Failed to create lockfile {0}, it already exists'.format(lockfile) + return(ret) + except Exception as error: + ret['comment'] = 'Failed to create lockfile {0}, error: {1}'.format(lockfile, error) + return(ret) + if Path(lockfile).exists(): + ret['comment'] = 'Lockfile {0} created'.format(lockfile) + ret['result'] = True + else: + ret['comment'] = 'Failed to create lockfile {0}'.format(lockfile) + return(ret) + +def unlock(name, path='/var/lib/salt/'): + ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''} + lockfile = path + name + if not Path(lockfile).exists(): + if __opts__["test"]: + ret['comment'] = 'Lockfile {0} would have been removed if it existed'.format(lockfile) + ret["result"] = None + else: + ret['comment'] = 'Lockfile {0} does not exist'.format(lockfile) + return(ret) + if __opts__["test"]: + ret["comment"] = "Lockfile {0} would have been removed".format(lockfile) + ret["result"] = None + return(ret) + try: + Path(lockfile).unlink() + except Exception as error: + ret['comment'] = 'Failed to delete lockfile {0}, error: {1}'.format(lockfile, error) + return(ret) + if not Path(lockfile).exists(): + ret['comment'] = 'Lockfile {0} deleted'.format(lockfile) + ret['result'] = True + else: + ret['comment'] = 'Failed to delete lockfile {0}'.format(lockfile) + return(ret) + +def check(name, path='/var/lib/salt/'): + ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''} + lockfile = path + name + if __opts__["test"]: + ret["comment"] = "Would have checked for existence of lockfile {0}".format(lockfile) + ret["result"] = None + return(ret) + if Path(lockfile).exists(): + ret['comment'] = 'Deployment of {0} is locked via {1} - maybe there is an existing execution'.format(name, lockfile) + else: + ret['comment'] = '{0} is not locked'.format(name) + ret['result'] = True + return(ret) +