Statistics
| Branch: | Tag: | Revision:

root / lib / jstore.py @ 84a12e40

History | View | Annotate | Download (3.6 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007 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

    
22
"""Module implementing the job queue handling."""
23

    
24
import errno
25

    
26
from ganeti import constants
27
from ganeti import errors
28
from ganeti import utils
29

    
30

    
31
def _ReadNumericFile(file_name):
32
  """Reads a file containing a number.
33

34
  @rtype: None or int
35
  @return: None if file is not found, otherwise number
36

37
  """
38
  try:
39
    return int(utils.ReadFile(file_name))
40
  except EnvironmentError, err:
41
    if err.errno in (errno.ENOENT, ):
42
      return None
43
    raise
44

    
45

    
46
def ReadSerial():
47
  """Read the serial file.
48

49
  The queue should be locked while this function is called.
50

51
  """
52
  return _ReadNumericFile(constants.JOB_QUEUE_SERIAL_FILE)
53

    
54

    
55
def ReadVersion():
56
  """Read the queue version.
57

58
  The queue should be locked while this function is called.
59

60
  """
61
  return _ReadNumericFile(constants.JOB_QUEUE_VERSION_FILE)
62

    
63

    
64
def InitAndVerifyQueue(must_lock):
65
  """Open and lock job queue.
66

67
  If necessary, the queue is automatically initialized.
68

69
  @type must_lock: bool
70
  @param must_lock: Whether an exclusive lock must be held.
71
  @rtype: utils.FileLock
72
  @return: Lock object for the queue. This can be used to change the
73
           locking mode.
74

75
  """
76
  dirs = [(d, constants.JOB_QUEUE_DIRS_MODE) for d in constants.JOB_QUEUE_DIRS]
77
  utils.EnsureDirs(dirs)
78

    
79
  # Lock queue
80
  queue_lock = utils.FileLock.Open(constants.JOB_QUEUE_LOCK_FILE)
81
  try:
82
    # The queue needs to be locked in exclusive mode to write to the serial and
83
    # version files.
84
    if must_lock:
85
      queue_lock.Exclusive(blocking=True)
86
      holding_lock = True
87
    else:
88
      try:
89
        queue_lock.Exclusive(blocking=False)
90
        holding_lock = True
91
      except errors.LockError:
92
        # Ignore errors and assume the process keeping the lock checked
93
        # everything.
94
        holding_lock = False
95

    
96
    if holding_lock:
97
      # Verify version
98
      version = ReadVersion()
99
      if version is None:
100
        # Write new version file
101
        utils.WriteFile(constants.JOB_QUEUE_VERSION_FILE,
102
                        data="%s\n" % constants.JOB_QUEUE_VERSION)
103

    
104
        # Read again
105
        version = ReadVersion()
106

    
107
      if version != constants.JOB_QUEUE_VERSION:
108
        raise errors.JobQueueError("Found job queue version %s, expected %s",
109
                                   version, constants.JOB_QUEUE_VERSION)
110

    
111
      serial = ReadSerial()
112
      if serial is None:
113
        # Write new serial file
114
        utils.WriteFile(constants.JOB_QUEUE_SERIAL_FILE,
115
                        data="%s\n" % 0)
116

    
117
        # Read again
118
        serial = ReadSerial()
119

    
120
      if serial is None:
121
        # There must be a serious problem
122
        raise errors.JobQueueError("Can't read/parse the job queue"
123
                                   " serial file")
124

    
125
      if not must_lock:
126
        # There's no need for more error handling. Closing the lock
127
        # file below in case of an error will unlock it anyway.
128
        queue_lock.Unlock()
129

    
130
  except:
131
    queue_lock.Close()
132
    raise
133

    
134
  return queue_lock