jstore: use EnsureDirs, and add more constants
[ganeti-local] / lib / jstore.py
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     fd = open(file_name, "r")
40   except IOError, err:
41     if err.errno in (errno.ENOENT, ):
42       return None
43     raise
44
45   try:
46     return int(fd.read(128))
47   finally:
48     fd.close()
49
50
51 def ReadSerial():
52   """Read the serial file.
53
54   The queue should be locked while this function is called.
55
56   """
57   return _ReadNumericFile(constants.JOB_QUEUE_SERIAL_FILE)
58
59
60 def ReadVersion():
61   """Read the queue version.
62
63   The queue should be locked while this function is called.
64
65   """
66   return _ReadNumericFile(constants.JOB_QUEUE_VERSION_FILE)
67
68
69 def InitAndVerifyQueue(must_lock):
70   """Open and lock job queue.
71
72   If necessary, the queue is automatically initialized.
73
74   @type must_lock: bool
75   @param must_lock: Whether an exclusive lock must be held.
76   @rtype: utils.FileLock
77   @return: Lock object for the queue. This can be used to change the
78            locking mode.
79
80   """
81   dirs = [(d, constants.JOB_QUEUE_DIRS_MODE) for d in constants.JOB_QUEUE_DIRS]
82   utils.EnsureDirs(dirs)
83
84   # Lock queue
85   queue_lock = utils.FileLock.Open(constants.JOB_QUEUE_LOCK_FILE)
86   try:
87     # The queue needs to be locked in exclusive mode to write to the serial and
88     # version files.
89     if must_lock:
90       queue_lock.Exclusive(blocking=True)
91       holding_lock = True
92     else:
93       try:
94         queue_lock.Exclusive(blocking=False)
95         holding_lock = True
96       except errors.LockError:
97         # Ignore errors and assume the process keeping the lock checked
98         # everything.
99         holding_lock = False
100
101     if holding_lock:
102       # Verify version
103       version = ReadVersion()
104       if version is None:
105         # Write new version file
106         utils.WriteFile(constants.JOB_QUEUE_VERSION_FILE,
107                         data="%s\n" % constants.JOB_QUEUE_VERSION)
108
109         # Read again
110         version = ReadVersion()
111
112       if version != constants.JOB_QUEUE_VERSION:
113         raise errors.JobQueueError("Found job queue version %s, expected %s",
114                                    version, constants.JOB_QUEUE_VERSION)
115
116       serial = ReadSerial()
117       if serial is None:
118         # Write new serial file
119         utils.WriteFile(constants.JOB_QUEUE_SERIAL_FILE,
120                         data="%s\n" % 0)
121
122         # Read again
123         serial = ReadSerial()
124
125       if serial is None:
126         # There must be a serious problem
127         raise errors.JobQueueError("Can't read/parse the job queue"
128                                    " serial file")
129
130       if not must_lock:
131         # There's no need for more error handling. Closing the lock
132         # file below in case of an error will unlock it anyway.
133         queue_lock.Unlock()
134
135   except:
136     queue_lock.Close()
137     raise
138
139   return queue_lock