utils/Makefile | 2
utils/migrate-system-profile | 2
utils/rhnLockfile.py | 112 +++++++++++++++++++++++++++++++++++++++++++
utils/rhn_fcntl.py | 44 ++++++++++++++++
utils/spacewalk-utils.spec | 5 +
5 files changed, 163 insertions(+), 2 deletions(-)
New commits:
commit c6aaa1f5c5fe577bf66c83779202134094317ae1
Merge: 1b32acc... e294153...
Author: Pradeep Kilambi <pkilambi(a)redhat.com>
Date: Fri Jul 31 14:24:05 2009 -0400
Merge branch 'master' of ssh://pkilambi@git.fedorahosted.org/git/spacewalk
commit 1b32acc1b98f6c72e42e9abbd595a8b93ea24cb5
Author: Pradeep Kilambi <pkilambi(a)redhat.com>
Date: Fri Jul 31 14:23:46 2009 -0400
514970 - removing the spacewalk common dependency on utils package so it can run on
client. Added the locking supported moduled to utils package.
diff --git a/utils/Makefile b/utils/Makefile
index fa6ceb0..834eee7 100644
--- a/utils/Makefile
+++ b/utils/Makefile
@@ -6,7 +6,7 @@ NAME := spacewalk-utils
SUBDIR = utils
-FILES = __init__ systemSnapshot migrateSystemProfile
+FILES = __init__ systemSnapshot migrateSystemProfile rhnLockfile rhn_fcntl
SCRIPTS = sw-system-snapshot migrate-system-profile spacewalk-api
DOCBOOK = $(wildcard /usr/bin/docbook2man)
diff --git a/utils/migrate-system-profile b/utils/migrate-system-profile
index 02da0fd..76daa6f 100755
--- a/utils/migrate-system-profile
+++ b/utils/migrate-system-profile
@@ -53,7 +53,7 @@ if os.getuid() != 0:
try:
- from common import rhnLockfile, CFG, fetchTraceback
+ from utils import rhnLockfile
except KeyboardInterrupt:
systemExit(0, "\nUser interrupted process.")
except ImportError:
diff --git a/utils/rhnLockfile.py b/utils/rhnLockfile.py
new file mode 100644
index 0000000..e6e6cfb
--- /dev/null
+++ b/utils/rhnLockfile.py
@@ -0,0 +1,112 @@
+#
+# Copyright (c) 2008 Red Hat, Inc.
+#
+# This software is licensed to you under the GNU General Public License,
+# version 2 (GPLv2). There is NO WARRANTY for this software, express or
+# implied, including the implied warranties of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
+# along with this software; if not, see
+#
http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
+#
+# Red Hat trademarks are not licensed under GPLv2. No permission is
+# granted to use or replicate Red Hat trademarks that are incorporated
+# in this software or its documentation.
+#
+
+import os
+import sys
+import fcntl
+from errno import EWOULDBLOCK, EEXIST
+import rhn_fcntl
+
+# NOTE: rhn_fcntl is deprecated in Python 2.2, just use fcntl
+
+class LockfileLockedException(Exception):
+ """thrown ONLY when pid file is locked."""
+ pass
+
+class Lockfile:
+
+ """class that provides simple access to a PID-style lockfile.
+
+ methods: __init__(lockfile), acquire(), and release()
+ NOTE: currently acquires upon init
+ The *.pid file will be acquired, or an LockfileLockedException is raised.
+ """
+
+ def __init__(self, lockfile, pid=None):
+ """create (if need be), and acquire lock on lockfile
+
+ lockfile example: '/var/run/up2date.pid'
+ """
+
+ # cleanup the path and assign it.
+ self.lockfile = os.path.abspath(
+ os.path.expanduser(
+ os.path.expandvars(lockfile)))
+
+ self.pid = pid
+ if not self.pid:
+ self.pid = os.getpid()
+
+ # create the directory structure
+ dirname = os.path.dirname(self.lockfile)
+ if not os.path.exists(dirname):
+ try:
+ os.makedirs(dirname)
+ except OSError, e:
+ if hasattr(e, 'errno') and e.errno == EEXIST:
+ # race condition... dirname exists now.
+ pass
+ else:
+ raise
+
+ # open the file -- non-destructive read-write, unless it needs
+ # to be created XXX: potential race condition upon create?
+ self.f = os.open(self.lockfile, os.O_RDWR|os.O_CREAT|os.O_SYNC)
+ self.acquire()
+
+ def acquire(self):
+ """acquire the lock; else raise
LockfileLockedException."""
+
+ try:
+ fcntl.flock(self.f, fcntl.LOCK_EX|fcntl.LOCK_NB)
+ except IOError, e:
+ if e.errno == EWOULDBLOCK:
+ raise LockfileLockedException(
+ "cannot acquire lock on %s." % self.lockfile)
+ else:
+ raise
+ # unlock upon exit
+ fcntl.fcntl(self.f, rhn_fcntl.F_SETFD, 1)
+ # truncate and write the pid
+ os.ftruncate(self.f, 0)
+ os.write(self.f, str(self.pid) + '\n')
+
+ def release(self):
+ # Remove the lock file
+ os.unlink(self.lockfile)
+ fcntl.flock(self.f, fcntl.LOCK_UN)
+ os.close(self.f)
+
+
+def main():
+ """test code"""
+
+ try:
+ L = Lockfile('./test.pid')
+ except LockfileLockedException, e:
+ sys.stderr.write("%s\n" % e)
+ sys.exit(-1)
+ else:
+ print "lock acquired "
+ print "...sleeping for 10 seconds"
+ import time
+ time.sleep(10)
+ L.release()
+ print "lock released "
+
+if __name__ == '__main__':
+ # test code
+ sys.exit(main() or 0)
+
diff --git a/utils/rhn_fcntl.py b/utils/rhn_fcntl.py
new file mode 100644
index 0000000..c1ba42f
--- /dev/null
+++ b/utils/rhn_fcntl.py
@@ -0,0 +1,44 @@
+#!/usr/bin/python
+#
+# Copyright (c) 2008 Red Hat, Inc.
+#
+# This software is licensed to you under the GNU General Public License,
+# version 2 (GPLv2). There is NO WARRANTY for this software, express or
+# implied, including the implied warranties of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
+# along with this software; if not, see
+#
http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
+#
+# Red Hat trademarks are not licensed under GPLv2. No permission is
+# granted to use or replicate Red Hat trademarks that are incorporated
+# in this software or its documentation.
+#
+
+
+import fcntl
+
+
+symbols = [
+ 'F_RDLCK', 'F_WRLCK', 'F_UNLCK', 'F_SETLKW',
+ 'F_SETFD', 'F_GETFD',
+]
+
+
+if hasattr(fcntl, symbols[0]):
+ module = fcntl
+else:
+ import FCNTL
+ module = FCNTL
+ del FCNTL
+del fcntl
+
+
+dict = globals()
+for symbol in symbols:
+ dict[symbol] = getattr(module, symbol)
+
+
+del dict
+del symbol
+del symbols
+del module
diff --git a/utils/spacewalk-utils.spec b/utils/spacewalk-utils.spec
index 6c946db..a34247a 100644
--- a/utils/spacewalk-utils.spec
+++ b/utils/spacewalk-utils.spec
@@ -50,10 +50,15 @@ rm -rf $RPM_BUILD_ROOT
%{rhnroot}/utils/__init__.py*
%{rhnroot}/utils/systemSnapshot.py*
%{rhnroot}/utils/migrateSystemProfile.py*
+%{rhnroot}/utils/rhnLockfile.py*
+%{rhnroot}/utils/rhn_fcntl.py*
%{_mandir}/man8/*
%changelog
+* Fri Jul 31 2009 Pradeep Kilambi <pkilambi(a)redhat.com>
+- removing common module dep and adding locking to utils package.
+
* Wed Jul 15 2009 Miroslav Suchý <msuchy(a)redhat.com> 0.6.5-1
- add spacewalk-api script, which can interact with API from command line