4 # Copyright (C) 2006, 2007 Google Inc.
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.
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.
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
22 """Module implementing the job queue handling."""
26 from ganeti import constants
27 from ganeti import errors
28 from ganeti import runtime
29 from ganeti import utils
32 def _ReadNumericFile(file_name):
33 """Reads a file containing a number.
36 @return: None if file is not found, otherwise number
40 return int(utils.ReadFile(file_name))
41 except EnvironmentError, err:
42 if err.errno in (errno.ENOENT, ):
48 """Read the serial file.
50 The queue should be locked while this function is called.
53 return _ReadNumericFile(constants.JOB_QUEUE_SERIAL_FILE)
57 """Read the queue version.
59 The queue should be locked while this function is called.
62 return _ReadNumericFile(constants.JOB_QUEUE_VERSION_FILE)
65 def InitAndVerifyQueue(must_lock):
66 """Open and lock job queue.
68 If necessary, the queue is automatically initialized.
71 @param must_lock: Whether an exclusive lock must be held.
72 @rtype: utils.FileLock
73 @return: Lock object for the queue. This can be used to change the
77 getents = runtime.GetEnts()
80 queue_lock = utils.FileLock.Open(constants.JOB_QUEUE_LOCK_FILE)
82 # The queue needs to be locked in exclusive mode to write to the serial and
85 queue_lock.Exclusive(blocking=True)
89 queue_lock.Exclusive(blocking=False)
91 except errors.LockError:
92 # Ignore errors and assume the process keeping the lock checked
98 version = ReadVersion()
100 # Write new version file
101 utils.WriteFile(constants.JOB_QUEUE_VERSION_FILE,
102 uid=getents.masterd_uid, gid=getents.masterd_gid,
103 data="%s\n" % constants.JOB_QUEUE_VERSION)
106 version = ReadVersion()
108 if version != constants.JOB_QUEUE_VERSION:
109 raise errors.JobQueueError("Found job queue version %s, expected %s",
110 version, constants.JOB_QUEUE_VERSION)
112 serial = ReadSerial()
114 # Write new serial file
115 utils.WriteFile(constants.JOB_QUEUE_SERIAL_FILE,
116 uid=getents.masterd_uid, gid=getents.masterd_gid,
120 serial = ReadSerial()
123 # There must be a serious problem
124 raise errors.JobQueueError("Can't read/parse the job queue"
128 # There's no need for more error handling. Closing the lock
129 # file below in case of an error will unlock it anyway.