summaryrefslogtreecommitdiffstats
path: root/cobbler/serializer.py
blob: f12a15d90a4e3def65b45ade2f665ad017e1b92d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
Serializer code for cobbler
Now adapted to support different storage backends

Copyright 2006-2008, Red Hat, Inc
Michael DeHaan <mdehaan@redhat.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301  USA
"""

import errno
import os
from utils import _
import fcntl
import traceback
import sys
import signal
import time

from cexceptions import *
import api as cobbler_api

LOCK_ENABLED = True
LOCK_HANDLE = None
BLOCK_SIGNAL = True

def handler(num,frame): 
   print sys.stderr, "Ctrl-C not allowed during writes.  Please wait."
   return True
    
def no_ctrl_c():
   signal.signal(signal.SIGINT, handler)
   return True

def ctrl_c_ok():
   signal.signal(signal.SIGINT, signal.default_int_handler)
   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)
        if BLOCK_SIGNAL:
            no_ctrl_c()
        return True
    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 when the last config change
        # 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()
    if BLOCK_SIGNAL:
        ctrl_c_ok()
    return True

def serialize(obj):
    """
    Save a collection to disk or other storage.  
    """
    __grab_lock()
    storage_module = __get_storage_module(obj.collection_type())
    storage_module.serialize(obj)
    __release_lock()
    return True

def serialize_item(collection, item):
    """
    Save an item.
    """
    __grab_lock()
    storage_module = __get_storage_module(collection.collection_type())
    save_fn = getattr(storage_module, "serialize_item", None)
    if save_fn is None:
        rc = storage_module.serialize(collection)
    else:
        rc = save_fn(collection,item)
    __release_lock(with_changes=True)
    return rc

def serialize_delete(collection, item):
    """
    Delete an object from a saved state.
    """
    __grab_lock()
    storage_module = __get_storage_module(collection.collection_type())
    delete_fn = getattr(storage_module, "serialize_delete", None)
    if delete_fn is None:
        rc = storage_module.serialize(collection)
    else:
        rc = delete_fn(collection,item)
    __release_lock(with_changes=True)
    return rc

def deserialize(obj,topological=True):
    """
    Fill in an empty collection from disk or other storage
    """
    __grab_lock()
    storage_module = __get_storage_module(obj.collection_type())
    rc = storage_module.deserialize(obj,topological)
    __release_lock()
    return rc

def deserialize_raw(collection_type):
    """
    Return the datastructure corresponding to the serialized
    disk state, without going through the Cobbler object system.
    Much faster, when you don't need the objects.
    """
    __grab_lock()
    storage_module = __get_storage_module(collection_type)
    rc = storage_module.deserialize_raw(collection_type)
    __release_lock()
    return rc

def deserialize_item(collection_type, item_name):
    """
    Get a specific record.
    """
    __grab_lock()
    storage_module = __get_storage_module(collection_type)
    rc = storage_module.deserialize_item(collection_type, item_name)
    __release_lock()
    return rc

def deserialize_item_raw(collection_type, item_name):
    __grab_lock()
    storage_module = __get_storage_module(collection_type)
    rc = storage_module.deserialize_item_raw(collection_type, item_name)
    __release_lock()
    return rc

def __get_storage_module(collection_type):
    """
    Look up serializer in /etc/cobbler/modules.conf
    """    
    capi = cobbler_api.BootAPI()
    return capi.get_module_from_file("serializers",collection_type,"serializer_yaml")

if __name__ == "__main__":
    __grab_lock()
    __release_lock()