Revision a8c2197d

b/Makefile.am
509 509
	lib/utils/filelock.py \
510 510
	lib/utils/hash.py \
511 511
	lib/utils/io.py \
512
	lib/utils/livelock.py \
512 513
	lib/utils/log.py \
513 514
	lib/utils/lvm.py \
514 515
	lib/utils/mlock.py \
b/lib/utils/__init__.py
47 47
from ganeti.utils.filelock import *
48 48
from ganeti.utils.hash import *
49 49
from ganeti.utils.io import *
50
from ganeti.utils.livelock import *
50 51
from ganeti.utils.log import *
51 52
from ganeti.utils.lvm import *
52 53
from ganeti.utils.mlock import *
b/lib/utils/livelock.py
1
#
2
#
3

  
4
# Copyright (C) 2014 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

  
21
"""Lockfiles to prove liveliness
22

  
23
When requesting resources, like locks, from wconfd, requesters have
24
to provide the name of a file they own an exclusive lock on, to prove
25
that they are still alive. Provide methods to obtain such a file.
26
"""
27

  
28
import fcntl
29
import os
30
import struct
31
import time
32

  
33
from ganeti import pathutils
34

  
35

  
36
class LiveLock(object):
37
  """Utility for a lockfile needed to request resources from WconfD.
38

  
39
  """
40
  def __init__(self, name=None):
41
    if name is None:
42
      name = "pid%d_" % os.getpid()
43
    # to avoid reusing existing lock files, extend name
44
    # by the current time
45
    name = "%s_%d" % (name, int(time.time()))
46
    fname = os.path.join(pathutils.LIVELOCK_DIR, name)
47
    self.lockfile = open(fname, 'w')
48
    fcntl.fcntl(self.lockfile, fcntl.F_SETLKW,
49
                struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0))
50

  
51
  def close(self):
52
    """Close the lockfile and clean it up.
53

  
54
    """
55
    self.lockfile.close()
56
    os.remove(self.lockfile.name)

Also available in: Unified diff