Source code for cobbler.serializer

"""
Serializer code for cobbler
Now adapted to support different storage backends
"""

import fcntl
import os
import sys
import time
import traceback

from cobbler import module_loader

LOCK_ENABLED = True
LOCK_HANDLE = None


[docs]def handler(num, frame): print("Ctrl-C not allowed during writes. Please wait.", file=sys.stderr) return True
def __grab_lock(): """ Dual purpose locking: (A) flock to avoid multiple process access (B) block signal handler to avoid ctrl+c while writing YAML """ try: if LOCK_ENABLED: if not os.path.exists("/var/lib/cobbler/lock"): fd = open("/var/lib/cobbler/lock", "w+") fd.close() LOCK_HANDLE = open("/var/lib/cobbler/lock", "r") fcntl.flock(LOCK_HANDLE.fileno(), fcntl.LOCK_EX) except: # this is pretty much FATAL, avoid corruption and quit now. traceback.print_exc() sys.exit(7) def __release_lock(with_changes=False): if with_changes: # this file is used to know the time of last modification on cobbler_collections # was made -- allowing the API to work more smoothly without # a lot of unneccessary reloads. fd = open("/var/lib/cobbler/.mtime", 'w') fd.write("%f" % time.time()) fd.close() if LOCK_ENABLED: LOCK_HANDLE = open("/var/lib/cobbler/lock", "r") fcntl.flock(LOCK_HANDLE.fileno(), fcntl.LOCK_UN) LOCK_HANDLE.close()
[docs]def serialize(collection): """ Save a collection to disk @param Collection collection collection """ __grab_lock() storage_module = __get_storage_module(collection.collection_type()) storage_module.serialize(collection) __release_lock()
[docs]def serialize_item(collection, item): """ Save a collection item to disk @param Collection collection collection @param Item item collection item """ __grab_lock() storage_module = __get_storage_module(collection.collection_type()) storage_module.serialize_item(collection, item) __release_lock(with_changes=True)
[docs]def serialize_delete(collection, item): """ Delete a collection item from disk @param Collection collection collection @param Item item collection item """ __grab_lock() storage_module = __get_storage_module(collection.collection_type()) storage_module.serialize_delete(collection, item) __release_lock(with_changes=True)
[docs]def deserialize(collection, topological=True): """ Load a collection from disk @param Collection collection collection @param bool topological """ __grab_lock() storage_module = __get_storage_module(collection.collection_type()) storage_module.deserialize(collection, topological) __release_lock()
def __get_storage_module(collection_type): """ Look up serializer in /etc/cobbler/modules.conf """ return module_loader.get_module_from_file("serializers", collection_type, "serializers.file") # EOF