From 571996035b62895d0ac93e0afd053c2885dac72c Mon Sep 17 00:00:00 2001 From: Christian Boltz Date: Dec 27 2022 22:47:38 +0000 Subject: Merge branch 'lock-module' into production https://gitlab.infra.opensuse.org/infra/salt/-/merge_requests/604 --- diff --git a/bin/test_extension.sh b/bin/test_extension.sh index 1c5ac34..240d706 100755 --- a/bin/test_extension.sh +++ b/bin/test_extension.sh @@ -1,7 +1,7 @@ #!/bin/bash NON_SLS_PILLAR=$(find pillar -type f | egrep -v "(macros.jinja|(FORMULAS|valid_custom_grains)\.yaml|\.sls)$|/README\.md$") -NON_SLS_SALT=$(find salt -type f | egrep -v "/(files|templates)/|(pillar\.example|\.sls|/README\.md)$") +NON_SLS_SALT=$(find salt -type f | egrep -v "/(files|templates)/|(pillar\.example|\.sls|/README\.md)$|/(_modules|_states)/\w+\.py$") NON_SLS=( ${NON_SLS_PILLAR} ${NON_SLS_SALT} ) if [[ -n $NON_SLS ]]; then 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) +