File: example.py

package info (click to toggle)
sanlock 4.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,012 kB
  • sloc: ansic: 29,026; sh: 1,192; python: 1,067; makefile: 359
file content (91 lines) | stat: -rw-r--r-- 2,704 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Copyright (C) 2019 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

from __future__ import print_function

import pwd
import grp
import os
import time
import signal
import tempfile
import sanlock

HOST_ID = 1
LOCKSPACE_NAME = "lockspace1"
RESOURCE_NAME = "resource1"


def sigTermHandler():
    print("SIGTERM signal received")


def main():
    signal.signal(signal.SIGTERM, sigTermHandler)

    print("Creating the sanlock disk")
    fd, disk = tempfile.mkstemp()
    os.close(fd)

    os.chown(
        disk, pwd.getpwnam("sanlock").pw_uid, grp.getgrnam("sanlock").gr_gid)
    offset = sanlock.get_alignment(disk)

    SNLK_DISKS = [(disk, offset)]

    print("Registering to sanlock")
    fd = sanlock.register()

    print("Initializing '%s'" % (LOCKSPACE_NAME,))
    sanlock.write_lockspace(LOCKSPACE_NAME, disk, align=1048576, sector=512)

    print("Initializing '%s' on '%s'" % (RESOURCE_NAME, LOCKSPACE_NAME))
    sanlock.write_resource(
        LOCKSPACE_NAME, RESOURCE_NAME, SNLK_DISKS, align=1048576, sector=512)

    print("Acquiring the id '%i' on '%s'" % (HOST_ID, LOCKSPACE_NAME))
    sanlock.add_lockspace(LOCKSPACE_NAME, HOST_ID, disk)

    try:
        print("Acquiring '%s' on '%s'" % (RESOURCE_NAME, LOCKSPACE_NAME))
        sanlock.acquire(
            LOCKSPACE_NAME, RESOURCE_NAME, SNLK_DISKS, slkfd=fd, version=0)

        while True:
            print("Trying to get lockspace '%s' hosts" % LOCKSPACE_NAME)
            try:
                hosts_list = sanlock.get_hosts(LOCKSPACE_NAME)
            except sanlock.SanlockException as e:
                if e.errno != os.errno.EAGAIN:
                    raise
            else:
                print("Lockspace '%s' hosts: " % LOCKSPACE_NAME, hosts_list)
                break
            time.sleep(5)

        owners = sanlock.read_resource_owners(
            LOCKSPACE_NAME,
            RESOURCE_NAME,
            SNLK_DISKS,
            align=1048576,
            sector=512)
        print("Resource '%s' owners: %s" % (RESOURCE_NAME, owners))

        print("Releasing '%s' on '%s'" % (RESOURCE_NAME, LOCKSPACE_NAME))
        sanlock.release(LOCKSPACE_NAME, RESOURCE_NAME, SNLK_DISKS, slkfd=fd)
    except Exception as e:
        print("Exception: ", e)
    finally:
        print("Releasing the id '%i' on '%s'" % (HOST_ID, LOCKSPACE_NAME))
        sanlock.rem_lockspace(LOCKSPACE_NAME, HOST_ID, disk)

    print("Removing the sanlock disk")
    os.remove(disk)


if __name__ == '__main__':
    main()