summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Plugin.py
blob: 28299d8c7c396679f510db244d9d633dd709eefc (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
"""This module provides the baseclass for Bcfg2 Server Plugins."""
__revision__ = '$Revision$'

import copy
import logging
import lxml.etree
import os
import pickle
import posixpath
import re
import sys
import threading
import Bcfg2.Server
from Bcfg2.Bcfg2Py3k import ConfigParser

import Bcfg2.Options

# py3k compatibility
if sys.hexversion >= 0x03000000:
    from functools import reduce
    from io import FileIO as BUILTIN_FILE_TYPE
else:
    BUILTIN_FILE_TYPE = file
from Bcfg2.Bcfg2Py3k import Queue
from Bcfg2.Bcfg2Py3k import Empty
from Bcfg2.Bcfg2Py3k import Full

# grab default metadata info from bcfg2.conf
opts = {'owner': Bcfg2.Options.MDATA_OWNER,
        'group': Bcfg2.Options.MDATA_GROUP,
        'important': Bcfg2.Options.MDATA_IMPORTANT,
        'perms': Bcfg2.Options.MDATA_PERMS,
        'paranoid': Bcfg2.Options.MDATA_PARANOID,
        'sensitive': Bcfg2.Options.MDATA_SENSITIVE}
mdata_setup = Bcfg2.Options.OptionParser(opts)
mdata_setup.parse([])
del mdata_setup['args']

logger = logging.getLogger('Bcfg2.Server.Plugin')

default_file_metadata = mdata_setup

info_regex = re.compile( \
    'encoding:(\s)*(?P<encoding>\w+)|' +
    'group:(\s)*(?P<group>\S+)|' +
    'important:(\s)*(?P<important>\S+)|' +
    'mtime:(\s)*(?P<mtime>\w+)|' +
    'owner:(\s)*(?P<owner>\S+)|' +
    'paranoid:(\s)*(?P<paranoid>\S+)|' +
    'perms:(\s)*(?P<perms>\w+)|' +
    'sensitive:(\s)*(?P<sensitive>\S+)|')

def bind_info(entry, metadata, infoxml=None, default=default_file_metadata):
    for attr, val in list(default.items()):
        entry.set(attr, val)
    if infoxml:
        mdata = dict()
        infoxml.pnode.Match(metadata, mdata, entry=entry)
        if 'Info' not in mdata:
            msg = "Failed to set metadata for file %s" % entry.get('name')
            logger.error(msg)
            raise PluginExecutionError(msg)
        for attr, val in list(mdata['Info'][None].items()):
            entry.set(attr, val)


class PluginInitError(Exception):
    """Error raised in cases of Plugin initialization errors."""
    pass


class PluginExecutionError(Exception):
    """Error raised in case of Plugin execution errors."""
    pass


class Debuggable(object):
    __rmi__ = ['toggle_debug']

    def __init__(self, name=None):
        if name is None:
            name = "%s.%s" % (self.__class__.__module__,
                              self.__class__.__name__)
        self.debug_flag = False
        self.logger = logging.getLogger(name)
        
    def toggle_debug(self):
        self.debug_flag = not self.debug_flag

    def debug_log(self, message, flag=None):
        if (flag is None and self.debug_flag) or flag:
            self.logger.error(message)


class Plugin(Debuggable):
    """This is the base class for all Bcfg2 Server plugins.
    Several attributes must be defined in the subclass:
    name : the name of the plugin
    __version__ : a version string
    __author__ : the author/contact for the plugin

    Plugins can provide three basic types of functionality:
      - Structure creation (overloading BuildStructures)
      - Configuration entry binding (overloading HandlesEntry, or loads the Entries table)
      - Data collection (overloading GetProbes/ReceiveData)
    """
    name = 'Plugin'
    __version__ = '$Id$'
    __author__ = 'bcfg-dev@mcs.anl.gov'
    experimental = False
    deprecated = False
    conflicts = []

    # Default sort_order to 500. Plugins of the same type are
    # processed in order of ascending sort_order value. Plugins with
    # the same sort_order are sorted alphabetically by their name.
    sort_order = 500

    def __init__(self, core, datastore):
        """Initialize the plugin.
        
        :param core: the Bcfg2.Server.Core initializing the plugin
        :param datastore: the filesystem path of Bcfg2's repository
        """
        object.__init__(self)
        self.Entries = {}
        self.core = core
        self.data = "%s/%s" % (datastore, self.name)
        self.running = True
        Debuggable.__init__(self, name=self.name)

    @classmethod
    def init_repo(cls, repo):
        path = "%s/%s" % (repo, cls.name)
        os.makedirs(path)

    def shutdown(self):
        self.running = False


class Generator(object):
    """Generator plugins contribute to literal client configurations."""
    def HandlesEntry(self, entry, metadata):
        """This is the slow path method for routing configuration binding requests."""
        return False

    def HandleEntry(self, entry, metadata):
        """This is the slow-path handler for configuration entry binding."""
        raise PluginExecutionError


class Structure(object):
    """Structure Plugins contribute to abstract client configurations."""
    def BuildStructures(self, metadata):
        """Return a list of abstract goal structures for client."""
        raise PluginExecutionError


class Metadata(object):
    """Signal metadata capabilities for this plugin"""
    def add_client(self, client_name, attribs):
        """Add client."""
        pass

    def remove_client(self, client_name):
        """Remove client."""
        pass

    def viz(self, hosts, bundles, key, colors):
        """Create viz str for viz admin mode."""
        pass

    def get_initial_metadata(self, client_name):
        raise PluginExecutionError

    def merge_additional_data(self, imd, source, groups, data):
        raise PluginExecutionError


class Connector(object):
    """Connector Plugins augment client metadata instances."""
    def get_additional_groups(self, metadata):
        """Determine additional groups for metadata."""
        return list()

    def get_additional_data(self, metadata):
        """Determine additional data for metadata instances."""
        return dict()


class Probing(object):
    """Signal probe capability for this plugin."""
    def GetProbes(self, _):
        """Return a set of probes for execution on client."""
        return []

    def ReceiveData(self, _, dummy):
        """Receive probe results pertaining to client."""
        pass


class Statistics(object):
    """Signal statistics handling capability."""
    def process_statistics(self, client, xdata):
        pass


class ThreadedStatistics(Statistics,
                         threading.Thread):
    """Threaded statistics handling capability."""
    def __init__(self, core, datastore):
        Statistics.__init__(self)
        threading.Thread.__init__(self)
        # Event from the core signaling an exit
        self.terminate = core.terminate
        self.work_queue = Queue(100000)
        self.pending_file = "%s/etc/%s.pending" % (datastore, self.__class__.__name__)
        self.daemon = True
        self.start()

    def save(self):
        """Save any pending data to a file."""
        pending_data = []
        try:
            while not self.work_queue.empty():
                (metadata, data) = self.work_queue.get_nowait()
                try:
                    pending_data.append((metadata.hostname, lxml.etree.tostring(data)))
                except:
                    self.logger.warning("Dropping interaction for %s" % metadata.hostname)
        except Empty:
            pass

        try:
            savefile = open(self.pending_file, 'w')
            pickle.dump(pending_data, savefile)
            savefile.close()
            self.logger.info("Saved pending %s data" % self.__class__.__name__)
        except:
            self.logger.warning("Failed to save pending data")

    def load(self):
        """Load any pending data to a file."""
        if not os.path.exists(self.pending_file):
            return True
        pending_data = []
        try:
            savefile = open(self.pending_file, 'r')
            pending_data = pickle.load(savefile)
            savefile.close()
        except Exception:
            e = sys.exc_info()[1]
            self.logger.warning("Failed to load pending data: %s" % e)
        for (pmetadata, pdata) in pending_data:
            # check that shutdown wasnt called early
            if self.terminate.isSet():
                return False

            try:
                while True:
                    try:
                        metadata = self.core.build_metadata(pmetadata)
                        break
                    except Bcfg2.Server.Plugins.Metadata.MetadataRuntimeError:
                        pass

                    self.terminate.wait(5)
                    if self.terminate.isSet():
                        return False

                self.work_queue.put_nowait((metadata,
                                            lxml.etree.XML(pdata,
                                                           parser=Bcfg2.Server.XMLParser)))
            except Full:
                self.logger.warning("Queue.Full: Failed to load queue data")
                break
            except lxml.etree.LxmlError:
                lxml_error = sys.exc_info()[1]
                self.logger.error("Unable to load save interaction: %s" % lxml_error)
            except Bcfg2.Server.Plugins.Metadata.MetadataConsistencyError:
                self.logger.error("Unable to load metadata for save interaction: %s" % pmetadata)
        try:
            os.unlink(self.pending_file)
        except:
            self.logger.error("Failed to unlink save file: %s" % self.pending_file)
        self.logger.info("Loaded pending %s data" % self.__class__.__name__)
        return True

    def run(self):
        if not self.load():
            return
        while not self.terminate.isSet() and self.work_queue != None:
            try:
                (xdata, client) = self.work_queue.get(block=True, timeout=2)
            except Empty:
                continue
            except Exception:
                e = sys.exc_info()[1]
                self.logger.error("ThreadedStatistics: %s" % e)
                continue
            self.handle_statistic(xdata, client)
        if self.work_queue != None and not self.work_queue.empty():
            self.save()

    def process_statistics(self, metadata, data):
        warned = False
        try:
            self.work_queue.put_nowait((metadata, copy.copy(data)))
            warned = False
        except Full:
            if not warned:
                self.logger.warning("%s: Queue is full.  Dropping interactions." % self.__class__.__name__)
            warned = True

    def handle_statistics(self, metadata, data):
        """Handle stats here."""
        pass


class PullSource(object):
    def GetExtra(self, client):
        return []

    def GetCurrentEntry(self, client, e_type, e_name):
        raise PluginExecutionError


class PullTarget(object):
    def AcceptChoices(self, entry, metadata):
        raise PluginExecutionError

    def AcceptPullData(self, specific, new_entry, verbose):
        """This is the null per-plugin implementation
        of bcfg2-admin pull."""
        raise PluginExecutionError


class Decision(object):
    """Signal decision handling capability."""
    def GetDecisions(self, metadata, mode):
        return []


class ValidationError(Exception):
    pass


class StructureValidator(object):
    """Validate/modify goal structures."""
    def validate_structures(self, metadata, structures):
        raise ValidationError("not implemented")


class GoalValidator(object):
    """Validate/modify configuration goals."""
    def validate_goals(self, metadata, goals):
        raise ValidationError("not implemented")


class Version(object):
    """Interact with various version control systems."""
    def get_revision(self):
        return []

    def commit_data(self, file_list, comment=None):
        pass


# the rest of the file contains classes for coherent file caching

class FileBacked(object):
    """This object caches file data in memory.
    HandleEvent is called whenever fam registers an event.
    Index can parse the data into member data as required.
    This object is meant to be used as a part of DirectoryBacked.
    """

    def __init__(self, name):
        object.__init__(self)
        self.data = ''
        self.name = name
        
    def HandleEvent(self, event=None):
        """Read file upon update."""
        if event and event.code2str() not in ['exists', 'changed', 'created']:
            return
        try:
            self.data = BUILTIN_FILE_TYPE(self.name).read()
            self.Index()
        except IOError:
            err = sys.exc_info()[1]
            logger.error("Failed to read file %s: %s" % (self.name, err))

    def Index(self):
        """Update local data structures based on current file state"""
        pass

    def __repr__(self):
        return "%s: %s" % (self.__class__.__name__, str(self))

    def __str__(self):
        return "%s: %s" % (self.name, self.data)


class DirectoryBacked(object):
    """This object is a coherent cache for a filesystem hierarchy of files."""
    __child__ = FileBacked
    patterns = re.compile('.*')

    def __init__(self, data, fam):
        """Initialize the DirectoryBacked object.

        :param self: the object being initialized.
        :param data: the path to the data directory that will be
        monitored.
        :param fam: The FileMonitor object used to receive
        notifications of changes.  
        """
        object.__init__(self)

        self.data = os.path.normpath(data)
        self.fam = fam

        # self.entries contains information about the files monitored
        # by this object.... The keys of the dict are the relative
        # paths to the files. The values are the objects (of type
        # __child__) that handle their contents.
        self.entries = {}

        # self.handles contains information about the directories
        # monitored by this object. The keys of the dict are the
        # values returned by the initial fam.AddMonitor() call (which
        # appear to be integers). The values are the relative paths of
        # the directories.
        self.handles = {}

        # Monitor everything in the plugin's directory
        self.add_directory_monitor('')

    def __getitem__(self, key):
        return self.entries[key]

    def __iter__(self):
        return iter(list(self.entries.items()))

    def add_directory_monitor(self, relative):
        """Add a new directory to FAM structures for monitoring.

        :param relative: Path name to monitor. This must be relative
        to the plugin's directory. An empty string value ("") will
        cause the plugin directory itself to be monitored.
        """
        dirpathname = os.path.join(self.data, relative)
        if relative not in self.handles.values():
            if not posixpath.isdir(dirpathname):
                logger.error("Failed to open directory %s" % (dirpathname))
                return
            reqid = self.fam.AddMonitor(dirpathname, self)
            self.handles[reqid] = relative

    def add_entry(self, relative, event):
        """Add a new file to our structures for monitoring.

        :param relative: Path name to monitor. This must be relative
        to the plugin's directory.
        :param event: File Monitor event that caused this entry to be
        added.
        """
        self.entries[relative] = self.__child__(os.path.join(self.data,
                                                             relative))
        self.entries[relative].HandleEvent(event)

    def HandleEvent(self, event):
        """Handle FAM/Gamin events.
        
        This method is invoked by FAM/Gamin when it detects a change
        to a filesystem object we have requsted to be monitored.

        This method manages the lifecycle of events related to the
        monitored objects, adding them to our indiciess and creating
        objects of type __child__ that actually do the domain-specific
        processing. When appropriate, it propogates events those
        objects by invoking their HandleEvent in turn.
        """
        action = event.code2str()

        # Clean up the absolute path names passed in
        event.filename = os.path.normpath(event.filename)
        if event.filename.startswith(self.data):
            event.filename = event.filename[len(self.data)+1:]

        # Exclude events for actions we don't care about
        if action == 'endExist':
            return

        if event.requestID not in self.handles:
            logger.warn("Got %s event with unknown handle (%s) for %s"
                        % (action, event.requestID, abspath))
            return

        # Calculate the absolute and relative paths this event refers to
        abspath = os.path.join(self.data, self.handles[event.requestID],
                               event.filename)
        relpath = os.path.join(self.handles[event.requestID], event.filename)

        if action == 'deleted':
            for key in self.entries.keys():
                if key.startswith(relpath):
                    del self.entries[key]
            # We remove values from self.entries, but not
            # self.handles, because the FileMonitor doesn't stop
            # watching a directory just because it gets deleted. If it
            # is recreated, we will start getting notifications for it
            # again without having to add a new monitor.
        elif posixpath.isdir(abspath):
            # Deal with events for directories
            if action in ['exists', 'created']:
                self.add_directory_monitor(relpath)
            elif action == 'changed':
                if relpath in self.entries:
                    # Ownerships, permissions or timestamps changed on
                    # the directory. None of these should affect the
                    # contents of the files, though it could change
                    # our ability to access them.
                    #
                    # It seems like the right thing to do is to cancel
                    # monitoring the directory and then begin
                    # monitoring it again. But the current FileMonitor
                    # class doesn't support canceling, so at least let
                    # the user know that a restart might be a good
                    # idea.
                    logger.warn("Directory properties for %s changed, please " +
                                " consider restarting the server" % (abspath))
                else:
                    # Got a "changed" event for a directory that we
                    # didn't know about. Go ahead and treat it like a
                    # "created" event, but log a warning, because this
                    # is unexpected.
                    logger.warn("Got %s event for unexpected dir %s" % (action,
                                                                        abspath))
                    self.add_directory_monitor(relpath)
            else:
                logger.warn("Got unknown dir event %s %s %s" % (event.requestID,
                                                                event.code2str(),
                                                                abspath))
        else:
            # Deal with events for non-directories
            if ((event.filename[-1] == '~') or
                (event.filename[:2] == '.#') or
                (event.filename[-4:] == '.swp') or
                (event.filename in ['SCCS', '.svn', '4913']) or
                (not self.patterns.match(event.filename))):
                return
            if action in ['exists', 'created']:
                self.add_entry(relpath, event)
            elif action == 'changed':
                if relpath in self.entries:
                    self.entries[relpath].HandleEvent(event)
                else:
                    # Got a "changed" event for a file that we didn't
                    # know about. Go ahead and treat it like a
                    # "created" event, but log a warning, because this
                    # is unexpected.
                    logger.warn("Got %s event for unexpected file %s" % (action,
                                                                         abspath))
                    self.add_entry(relpath, event)
            else:
                logger.warn("Got unknown file event %s %s %s" % (event.requestID,
                                                                 event.code2str(),
                                                                 abspath))


class XMLFileBacked(FileBacked):
    """
    This object is a coherent cache for an XML file to be used as a
    part of DirectoryBacked.
    """
    __identifier__ = 'name'

    def __init__(self, filename):
        self.label = "dummy"
        self.entries = []
        FileBacked.__init__(self, filename)

    def Index(self):
        """Build local data structures."""
        try:
            self.xdata = lxml.etree.XML(self.data,
                                        parser=Bcfg2.Server.XMLParser)
        except lxml.etree.XMLSyntaxError:
            logger.error("Failed to parse %s" % (self.name))
            return
        self.entries = self.xdata.getchildren()
        if self.__identifier__ is not None:
            self.label = self.xdata.attrib[self.__identifier__]

    def __iter__(self):
        return iter(self.entries)

    def __str__(self):
        return "%s: %s" % (self.name, lxml.etree.tostring(self.xdata))


class SingleXMLFileBacked(XMLFileBacked):
    """This object is a coherent cache for an independent XML file."""
    def __init__(self, filename, fam):
        XMLFileBacked.__init__(self, filename)
        self.extras = []
        self.fam = fam
        self.fam.AddMonitor(filename, self)

    def Index(self):
        """Build local data structures."""
        try:
            self.xdata = lxml.etree.XML(self.data, base_url=self.name,
                                        parser=Bcfg2.Server.XMLParser)
        except lxml.etree.XMLSyntaxError:
            err = sys.exc_info()[1]
            logger.error("Failed to parse %s: %s" % (self.name, err))
            raise Bcfg2.Server.Plugin.PluginInitError

        included = [ent.get('href')
                    for ent in self.xdata.findall('./{http://www.w3.org/2001/XInclude}include')]
        if included:
            for name in included:
                if name not in self.extras:
                    self.fam.AddMonitor(os.path.join(os.path.dirname(self.name),
                                                     name),
                                        self)
                    self.extras.append(name)
            try:
                self.xdata.getroottree().xinclude()
            except lxml.etree.XIncludeError:
                err = sys.exc_info()[1]
                logger.error("XInclude failed on %s: %s" % (self.name, err))


        self.entries = self.xdata.getchildren()
        if self.__identifier__ is not None:
            self.label = self.xdata.attrib[self.__identifier__]


class StructFile(XMLFileBacked):
    """This file contains a set of structure file formatting logic."""
    __identifier__ = None
    
    def __init__(self, name):
        XMLFileBacked.__init__(self, name)

    def _match(self, item, metadata):
        """ recursive helper for Match() """
        if isinstance(item, lxml.etree._Comment):
            return []
        elif item.tag == 'Group':
            rv = []
            if ((item.get('negate', 'false').lower() == 'true' and
                 item.get('name') not in metadata.groups) or
                (item.get('negate', 'false').lower() == 'false' and
                 item.get('name') in metadata.groups)):
                for child in item.iterchildren():
                    rv.extend(self._match(child, metadata))
            return rv
        elif item.tag == 'Client':
            rv = []
            if ((item.get('negate', 'false').lower() == 'true' and
                 item.get('name') != metadata.hostname) or
                (item.get('negate', 'false').lower() == 'false' and
                 item.get('name') == metadata.hostname)):
                for child in item.iterchildren():
                    rv.extend(self._match(child, metadata))
            return rv
        else:
            rv = copy.copy(item)
            for child in rv.iterchildren():
                rv.remove(child)
            for child in item.iterchildren():
                rv.extend(self._match(child, metadata))
            return [rv]
            
    def Match(self, metadata):
        """Return matching fragments of independent."""
        rv = []
        for child in self.entries:
            rv.extend(self._match(child, metadata))
        return rv


class INode:
    """
    LNodes provide lists of things available at a particular
    group intersection.
    """
    raw = {'Client': "lambda m, e:'%(name)s' == m.hostname and predicate(m, e)",
           'Group': "lambda m, e:'%(name)s' in m.groups and predicate(m, e)"}
    nraw = {'Client': "lambda m, e:'%(name)s' != m.hostname and predicate(m, e)",
            'Group': "lambda m, e:'%(name)s' not in m.groups and predicate(m, e)"}
    containers = ['Group', 'Client']
    ignore = []

    def __init__(self, data, idict, parent=None):
        self.data = data
        self.contents = {}
        if parent == None:
            self.predicate = lambda m, d: True
        else:
            predicate = parent.predicate
            if data.get('negate', 'false') in ['true', 'True']:
                psrc = self.nraw
            else:
                psrc = self.raw
            if data.tag in list(psrc.keys()):
                self.predicate = eval(psrc[data.tag] %
                                      {'name': data.get('name')},
                                      {'predicate': predicate})
            else:
                raise Exception
        mytype = self.__class__
        self.children = []
        for item in data.getchildren():
            if item.tag in self.ignore:
                continue
            elif item.tag in self.containers:
                self.children.append(mytype(item, idict, self))
            else:
                try:
                    self.contents[item.tag][item.get('name')] = item.attrib
                except KeyError:
                    self.contents[item.tag] = {item.get('name'): item.attrib}
                if item.text:
                    self.contents[item.tag]['__text__'] = item.text
                try:
                    idict[item.tag].append(item.get('name'))
                except KeyError:
                    idict[item.tag] = [item.get('name')]

    def Match(self, metadata, data, entry=lxml.etree.Element("None")):
        """Return a dictionary of package mappings."""
        if self.predicate(metadata, entry):
            for key in self.contents:
                try:
                    data[key].update(self.contents[key])
                except:
                    data[key] = {}
                    data[key].update(self.contents[key])
            for child in self.children:
                child.Match(metadata, data, entry=entry)


class InfoNode (INode):
    """ INode implementation that includes <Path> tags """
    raw = {'Client': "lambda m, e:'%(name)s' == m.hostname and predicate(m, e)",
           'Group': "lambda m, e:'%(name)s' in m.groups and predicate(m, e)",
           'Path': "lambda m, e:('%(name)s' == e.get('name') or '%(name)s' == e.get('realname')) and predicate(m, e)"}
    nraw = {'Client': "lambda m, e:'%(name)s' != m.hostname and predicate(m, e)",
            'Group': "lambda m, e:'%(name)s' not in m.groups and predicate(m, e)",
            'Path': "lambda m, e:('%(name)s' != e.get('name') and '%(name)s' != e.get('realname')) and predicate(m, e)"}
    containers = ['Group', 'Client', 'Path']


class XMLSrc(XMLFileBacked):
    """XMLSrc files contain a LNode hierarchy that returns matching entries."""
    __node__ = INode
    __cacheobj__ = dict

    def __init__(self, filename, noprio=False):
        XMLFileBacked.__init__(self, filename)
        self.items = {}
        self.cache = None
        self.pnode = None
        self.priority = -1
        self.noprio = noprio

    def HandleEvent(self, _=None):
        """Read file upon update."""
        try:
            data = BUILTIN_FILE_TYPE(self.name).read()
        except IOError:
            logger.error("Failed to read file %s" % (self.name))
            return
        self.items = {}
        try:
            xdata = lxml.etree.XML(data, parser=Bcfg2.Server.XMLParser)
        except lxml.etree.XMLSyntaxError:
            logger.error("Failed to parse file %s" % (self.name))
            return
        self.pnode = self.__node__(xdata, self.items)
        self.cache = None
        try:
            self.priority = int(xdata.get('priority'))
        except (ValueError, TypeError):
            if not self.noprio:
                logger.error("Got bogus priority %s for file %s" %
                             (xdata.get('priority'), self.name))
        del xdata, data

    def Cache(self, metadata):
        """Build a package dict for a given host."""
        if self.cache == None or self.cache[0] != metadata:
            cache = (metadata, self.__cacheobj__())
            if self.pnode == None:
                logger.error("Cache method called early for %s; forcing data load" % (self.name))
                self.HandleEvent()
                return
            self.pnode.Match(metadata, cache[1])
            self.cache = cache

    def __str__(self):
        return str(self.items)


class InfoXML (XMLSrc):
    __node__ = InfoNode


class XMLDirectoryBacked(DirectoryBacked):
    """Directorybacked for *.xml."""
    patterns = re.compile('.*\.xml')


class PrioDir(Plugin, Generator, XMLDirectoryBacked):
    """This is a generator that handles package assignments."""
    name = 'PrioDir'
    __child__ = XMLSrc

    def __init__(self, core, datastore):
        Plugin.__init__(self, core, datastore)
        Generator.__init__(self)
        try:
            XMLDirectoryBacked.__init__(self, self.data, self.core.fam)
        except OSError:
            self.logger.error("Failed to load %s indices" % (self.name))
            raise PluginInitError

    def HandleEvent(self, event):
        """Handle events and update dispatch table."""
        XMLDirectoryBacked.HandleEvent(self, event)
        self.Entries = {}
        for src in list(self.entries.values()):
            for itype, children in list(src.items.items()):
                for child in children:
                    try:
                        self.Entries[itype][child] = self.BindEntry
                    except KeyError:
                        self.Entries[itype] = {child: self.BindEntry}

    def _matches(self, entry, metadata, rules):
        return entry.get('name') in rules

    def BindEntry(self, entry, metadata):
        attrs = self.get_attrs(entry, metadata)
        for key, val in list(attrs.items()):
            entry.attrib[key] = val
        
    def get_attrs(self, entry, metadata):
        """ get a list of attributes to add to the entry during the bind """
        for src in self.entries.values():
            src.Cache(metadata)

        matching = [src for src in list(self.entries.values())
                    if (src.cache and
                        entry.tag in src.cache[1] and
                        self._matches(entry, metadata,
                                      src.cache[1][entry.tag]))]
        if len(matching) == 0:
            raise PluginExecutionError('No matching source for entry when retrieving attributes for %s(%s)' % (entry.tag, entry.attrib.get('name')))
        elif len(matching) == 1:
            index = 0
        else:
            prio = [int(src.priority) for src in matching]
            if prio.count(max(prio)) > 1:
                self.logger.error("Found conflicting sources with "
                                  "same priority for %s, %s %s" %
                                  (metadata.hostname,
                                   entry.tag.lower(), entry.get('name')))
                self.logger.error([item.name for item in matching])
                self.logger.error("Priority was %s" % max(prio))
                raise PluginExecutionError
            index = prio.index(max(prio))

        for rname in list(matching[index].cache[1][entry.tag].keys()):
            if self._matches(entry, metadata, [rname]):
                data = matching[index].cache[1][entry.tag][rname]
                break
        else:
            # Fall back on __getitem__. Required if override used
            data = matching[index].cache[1][entry.tag][entry.get('name')]
        if '__text__' in data:
            entry.text = data['__text__']
        if '__children__' in data:
            [entry.append(copy.copy(item)) for item in data['__children__']]

        return dict([(key, data[key])
                     for key in list(data.keys())
                     if not key.startswith('__')])


# new unified EntrySet backend
class SpecificityError(Exception):
    """Thrown in case of filename parse failure."""
    pass


class Specificity:

    def __init__(self, all=False, group=False, hostname=False, prio=0, delta=False):
        self.hostname = hostname
        self.all = all
        self.group = group
        self.prio = prio
        self.delta = delta

    def __lt__(self, other):
        return self.__cmp__(other) < 0

    def matches(self, metadata):
        return self.all or \
               self.hostname == metadata.hostname or \
               self.group in metadata.groups

    def __cmp__(self, other):
        """Sort most to least specific."""
        if self.all:
            return 1
        if self.group:
            if other.hostname:
                return 1
            if other.group and other.prio > self.prio:
                return 1
            if other.group and other.prio == self.prio:
                return 0
        return -1

    def more_specific(self, other):
        """Test if self is more specific than other."""
        if self.all:
            True
        elif self.group:
            if other.hostname:
                return True
            elif other.group and other.prio > self.prio:
                return True
        return False


class SpecificData(object):
    def __init__(self, name, specific, encoding):
        self.name = name
        self.specific = specific

    def handle_event(self, event):
        if event.code2str() == 'deleted':
            return
        try:
            self.data = open(self.name).read()
        except:
            logger.error("Failed to read file %s" % self.name)


class EntrySet:
    """Entry sets deal with the host- and group-specific entries."""
    ignore = re.compile("^(\.#.*|.*~|\\..*\\.(sw[px])|.*\\.genshi_include)$")

    def __init__(self, basename, path, entry_type, encoding):
        self.path = path
        self.entry_type = entry_type
        self.entries = {}
        self.metadata = default_file_metadata.copy()
        self.infoxml = None
        self.encoding = encoding
        pattern = '(.*/)?%s(\.((H_(?P<hostname>\S+))|' % basename
        pattern += '(G(?P<prio>\d+)_(?P<group>\S+))))?$'
        self.specific = re.compile(pattern)

    def get_matching(self, metadata):
        return [item for item in list(self.entries.values())
                if item.specific.matches(metadata)]

    def best_matching(self, metadata):
        """ Return the appropriate interpreted template from the set of
        available templates. """
        matching = self.get_matching(metadata)

        hspec = [ent for ent in matching if ent.specific.hostname]
        if hspec:
            return hspec[0]

        gspec = [ent for ent in matching if ent.specific.group]
        if gspec:
            gspec.sort(self.group_sortfunc)
            return gspec[-1]

        aspec = [ent for ent in matching if ent.specific.all]
        if aspec:
            return aspec[0]

        raise PluginExecutionError

    def handle_event(self, event):
        """Handle FAM events for the TemplateSet."""
        action = event.code2str()

        if event.filename in ['info', 'info.xml', ':info']:
            if action in ['exists', 'created', 'changed']:
                self.update_metadata(event)
            elif action == 'deleted':
                self.reset_metadata(event)
            return

        if action in ['exists', 'created']:
            self.entry_init(event)
        else:
            if event.filename not in self.entries:
                logger.warning("Got %s event for unknown file %s" %
                               (action, event.filename))
                if action == 'changed':
                    # received a bogus changed event; warn, but treat
                    # it like a created event
                    self.entry_init(event)
                return
            if action == 'changed':
                self.entries[event.filename].handle_event(event)
            elif action == 'deleted':
                del self.entries[event.filename]

    def entry_init(self, event):
        """Handle template and info file creation."""
        if event.filename in self.entries:
            logger.warn("Got duplicate add for %s" % event.filename)
        else:
            fpath = "%s/%s" % (self.path, event.filename)
            try:
                spec = self.specificity_from_filename(event.filename)
            except SpecificityError:
                if not self.ignore.match(event.filename):
                    logger.error("Could not process filename %s; ignoring" % fpath)
                return
            self.entries[event.filename] = self.entry_type(fpath,
                                                           spec, self.encoding)
        self.entries[event.filename].handle_event(event)

    def specificity_from_filename(self, fname):
        """Construct a specificity instance from a filename and regex."""
        data = self.specific.match(fname)
        if not data:
            raise SpecificityError(fname)
        kwargs = {}
        if data.group('hostname'):
            kwargs['hostname'] = data.group('hostname')
        elif data.group('group'):
            kwargs['group'] = data.group('group')
            kwargs['prio'] = int(data.group('prio'))
        else:
            kwargs['all'] = True
        if 'delta' in data.groupdict():
            kwargs['delta'] = data.group('delta')
        return Specificity(**kwargs)

    def update_metadata(self, event):
        """Process info and info.xml files for the templates."""
        fpath = "%s/%s" % (self.path, event.filename)
        if event.filename == 'info.xml':
            if not self.infoxml:
                self.infoxml = InfoXML(fpath, True)
            self.infoxml.HandleEvent(event)
        elif event.filename in [':info', 'info']:
            for line in open(fpath).readlines():
                match = info_regex.match(line)
                if not match:
                    logger.warning("Failed to match line in %s: %s" % (fpath,
                                                                       line))
                    continue
                else:
                    mgd = match.groupdict()
                    for key, value in list(mgd.items()):
                        if value:
                            self.metadata[key] = value
                    if len(self.metadata['perms']) == 3:
                        self.metadata['perms'] = "0%s" % \
                                                 (self.metadata['perms'])

    def reset_metadata(self, event):
        """Reset metadata to defaults if info or info.xml removed."""
        if event.filename == 'info.xml':
            self.infoxml = None
        elif event.filename in [':info', 'info']:
            self.metadata = default_file_metadata.copy()

    def group_sortfunc(self, x, y):
        """sort groups by their priority"""
        return cmp(x.specific.prio, y.specific.prio)

    def bind_info_to_entry(self, entry, metadata):
        bind_info(entry, metadata, infoxml=self.infoxml, default=self.metadata)

    def bind_entry(self, entry, metadata):
        """Return the appropriate interpreted template from the set of available templates."""
        self.bind_info_to_entry(entry, metadata)
        return self.best_matching(metadata).bind_entry(entry, metadata)


class GroupSpool(Plugin, Generator):
    """Unified interface for handling group-specific data (e.g. .G## files)."""
    name = 'GroupSpool'
    __version__ = '$Id$'
    __author__ = 'bcfg-dev@mcs.anl.gov'
    filename_pattern = ""
    es_child_cls = object
    es_cls = EntrySet

    def __init__(self, core, datastore):
        Plugin.__init__(self, core, datastore)
        Generator.__init__(self)
        if self.data[-1] == '/':
            self.data = self.data[:-1]
        self.Entries['Path'] = {}
        self.entries = {}
        self.handles = {}
        self.AddDirectoryMonitor('')
        self.encoding = core.encoding

    def add_entry(self, event):
        epath = self.event_path(event)
        ident = self.event_id(event)
        if posixpath.isdir(epath):
            self.AddDirectoryMonitor(epath[len(self.data):])
        if ident not in self.entries and posixpath.isfile(epath):
            dirpath = "".join([self.data, ident])
            self.entries[ident] = self.es_cls(self.filename_pattern,
                                              dirpath,
                                              self.es_child_cls,
                                              self.encoding)
            self.Entries['Path'][ident] = self.entries[ident].bind_entry
        if not posixpath.isdir(epath):
            # do not pass through directory events
            self.entries[ident].handle_event(event)

    def event_path(self, event):
        return "".join([self.data, self.handles[event.requestID],
                        event.filename])

    def event_id(self, event):
        epath = self.event_path(event)
        if posixpath.isdir(epath):
            return self.handles[event.requestID] + event.filename
        else:
            return self.handles[event.requestID][:-1]

    def HandleEvent(self, event):
        """Unified FAM event handler for GroupSpool."""
        action = event.code2str()
        if event.filename[0] == '/':
            return
        ident = self.event_id(event)

        if action in ['exists', 'created']:
            self.add_entry(event)
        if action == 'changed':
            if ident in self.entries:
                self.entries[ident].handle_event(event)
            else:
                # got a changed event for a file we didn't know
                # about. go ahead and process this as a 'created', but
                # warn
                self.logger.warning("Got changed event for unknown file %s" %
                                    ident)
                self.add_entry(event)
        elif action == 'deleted':
            fbase = self.handles[event.requestID] + event.filename
            if fbase in self.entries:
                # a directory was deleted
                del self.entries[fbase]
                del self.Entries['Path'][fbase]
            elif ident in self.entries:
                self.entries[ident].handle_event(event)
            elif ident not in self.entries:
                self.logger.warning("Got deleted event for unknown file %s" %
                                    ident)

    def AddDirectoryMonitor(self, relative):
        """Add new directory to FAM structures."""
        if not relative.endswith('/'):
            relative += '/'
        name = self.data + relative
        if relative not in list(self.handles.values()):
            if not posixpath.isdir(name):
                print("Failed to open directory %s" % (name))
                return
            reqid = self.core.fam.AddMonitor(name, self)
            self.handles[reqid] = relative

class SimpleConfig(FileBacked,
                   ConfigParser.SafeConfigParser):
    ''' a simple plugin config using ConfigParser '''
    _required = True
    
    def __init__(self, plugin):
        filename = os.path.join(plugin.data, plugin.name.lower() + ".conf")
        self.plugin = plugin
        self.fam = self.plugin.core.fam
        self.read_files = set()
        Bcfg2.Server.Plugin.FileBacked.__init__(self, filename)
        ConfigParser.SafeConfigParser.__init__(self)

        if (self._required or
            (not self._required and os.path.exists(self.name))):
            self.fam.AddMonitor(self.name, self)

    def Index(self):
        """ Build local data structures """
        for section in self.sections():
            self.remove_section(section)
        self.read_files.update(self.read(self.name))

    def get(self, section, option, **kwargs):
        """ convenience method for getting config items """
        default = None
        if 'default' in kwargs:
            default = kwargs['default']
            del kwargs['default']
        try:
            return ConfigParser.SafeConfigParser.get(self, section, option,
                                                     **kwargs)
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            if default is not None:
                return default
            else:
                raise

    def getboolean(self, section, option, **kwargs):
        """ convenience method for getting boolean config items """
        default = None
        if 'default' in kwargs:
            default = kwargs['default']
            del kwargs['default']
        try:
            return ConfigParser.SafeConfigParser.getboolean(self, section,
                                                            option, **kwargs)
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError,
                ValueError):
            if default is not None:
                return default
            else:
                raise

    @property
    def loaded(self):
        if os.path.exists(self.name):
            return self.name in self.read_files
        else:
            return True