945113
# State module allowing to lock simultaneous executions - helpful in Orchestration states
945113
# Georg Pfuetzenreuter <mail+opensuse@georg-pfuetzenreuter.net>
945113
945113
from pathlib import Path
945113
945113
def lock(name, path='/var/lib/salt/'):
945113
    ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''}
945113
    lockfile = path + name
945113
    if Path(lockfile).exists():
945113
        if __opts__["test"]:
945113
            ret["comment"] = "Would have complained about {0} already existing".format(lockfile)
945113
            ret["result"] = None
945113
        else:
945113
            ret['comment'] = 'Lockfile {0} already exists'.format(lockfile)
945113
        return(ret)
945113
    if __opts__["test"]:
945113
        ret["comment"] = "Lockfile {0} would have been created".format(lockfile)
945113
        ret["result"] = None
945113
        return(ret)
945113
    try:
945113
        Path(lockfile).touch(exist_ok=False)
945113
    except FileExistsError as error:
945113
        ret['comment'] = 'Failed to create lockfile {0}, it already exists'.format(lockfile)
945113
        return(ret)
945113
    except Exception as error:
945113
        ret['comment'] = 'Failed to create lockfile {0}, error: {1}'.format(lockfile, error)
945113
        return(ret)
945113
    if Path(lockfile).exists():
945113
        ret['comment'] = 'Lockfile {0} created'.format(lockfile)
945113
        ret['result'] = True
945113
    else:
945113
        ret['comment'] = 'Failed to create lockfile {0}'.format(lockfile)
945113
    return(ret)
945113
945113
def unlock(name, path='/var/lib/salt/'):
945113
    ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''}
945113
    lockfile = path + name
945113
    if not Path(lockfile).exists():
945113
        if __opts__["test"]:
945113
            ret['comment'] = 'Lockfile {0} would have been removed if it existed'.format(lockfile)
945113
            ret["result"] = None
945113
        else:
945113
            ret['comment'] = 'Lockfile {0} does not exist'.format(lockfile)
945113
        return(ret)
945113
    if __opts__["test"]:
945113
        ret["comment"] = "Lockfile {0} would have been removed".format(lockfile)
945113
        ret["result"] = None
945113
        return(ret)
945113
    try:
945113
        Path(lockfile).unlink()
945113
    except Exception as error:
945113
        ret['comment'] = 'Failed to delete lockfile {0}, error: {1}'.format(lockfile, error)
945113
        return(ret)
945113
    if not Path(lockfile).exists():
945113
        ret['comment'] = 'Lockfile {0} deleted'.format(lockfile)
945113
        ret['result'] = True
945113
    else:
945113
        ret['comment'] = 'Failed to delete lockfile {0}'.format(lockfile)
945113
    return(ret)
945113
945113
def check(name, path='/var/lib/salt/'):
945113
    ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''}
945113
    lockfile = path + name
945113
    if __opts__["test"]:
945113
        ret["comment"] = "Would have checked for existence of lockfile {0}".format(lockfile)
945113
        ret["result"] = None
945113
        return(ret)
945113
    if Path(lockfile).exists():
945113
        ret['comment'] = 'Deployment of {0} is locked via {1} - maybe there is an existing execution'.format(name, lockfile)
945113
    else:
945113
        ret['comment'] = '{0} is not locked'.format(name)
945113
        ret['result'] = True
945113
    return(ret)
945113