Changeset 463

Show
Ignore:
Timestamp:
05/16/08 11:14:07 (8 months ago)
Author:
jmowery
Message:

incremental check-in for new storage
added some node functionality

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/trunk-pmd-intproto/server/storage.py

    r461 r463  
    228228        def add_node(self, id): 
    229229                '''Add a node with the given id to the store if not already present. Create entries for each known file with an applied time of 0 for that node.''' 
     230                 
    230231                raise NotImplementedError 
    231232 
     
    677678        def add_node(self, id): 
    678679                '''Add a new node to this Layer; all controlled files have the default time of 0 until sent to the node.''' 
    679                 raise NotImplementedError 
     680                if not id: 
     681                        raise ValueError, 'Must specify a valid node id.' 
     682                if id in self.list_nodes(): 
     683                        return # already there 
     684                if not self.storage: 
     685                        raise RuntimeError, 'Cannot add node \'' + id + '\' to uncontrolled layer \'' + self.name + '\'.' 
     686        #XXX    FIXME 
     687                self.storage.add_node(id) 
    680688 
    681689        #TODO should there be a remove node? 
     
    730738class NodeLayerData: 
    731739        '''Represents the data stored on disk about the status of files sent to this node relative to a single Layer.''' 
    732         layer_name = ''  # Name of the layer represented 
     740        layer_path = ''  # Path of the layer represented 
    733741        group_name = ''  # Group name for the layer 
    734742        config_path = '' # path to the file containing the on disk record of the state of the Node's files for this layer 
    735743        config = ConfigParser.ConfigParser() # parser object for the file config_path 
    736744 
     745        def __init__(self, id, layer_path): 
     746                assert is_controlled(layer_path) 
     747                self.layer_path = layer_path 
     748                l = Layer(layer_path) 
     749                self.group_name = l.name 
     750                self.config_path = os.path.join(l.storage.nodes_dir, id) 
     751                self.config.add_section('entries') 
     752                if l.storage.path == l.root: 
     753                        self.config.add_section('general') 
     754                        self.config.add_section('bools') 
     755                self.config.read(self.config_path) 
     756 
     757        def write(self): 
     758                node_data_file = open(self.config_path, 'w') 
     759                self.config.write(node_data_file) 
     760                node_date_file.close() 
     761 
    737762class Node: 
    738763        '''The node object holds all storage relevant data about a single node connected to the store.''' 
    739         modified = False 
    740         id = '' 
    741         classification = '' 
    742         description = '' 
    743         bools = {} 
    744         entries = {} 
    745         enforcing = 'Unknown' 
    746         layers = {} 
    747  
    748         def __init__(self, id, layer=''): 
    749                 raise NotImplementedError 
     764        modified = False      # Set whenever the status data has been modified since last sync to disk 
     765        id = ''               # Id of the node (currently its IP address) 
     766        classification = ''   # Group name of the most specific layer containing the node 
     767        description = ''      # User defined string providing description of the node (possibly a name) 
     768        bools = {}            # Status of each boolean on the node system keyed by bool name value is bool not string 
     769        entries = {}          # Entry per module to be sent to the node applied time is last sent version 
     770        enforcing = 'Unknown' # Enforcing status of the node 
     771        layers = {}           # NodeLayerData per layer path for each layer in classification 
     772        root = ''             # Root of store contianing data for this node 
     773 
     774        def __init__(self, id, root): 
     775                if not id: 
     776                        raise ValueError, 'Node must have a valid id.' 
     777                self.root = find_control_root(root) #make sure root really is the root 
     778                self.classify() 
     779                self.update() 
    750780 
    751781        def __del__(self): 
     
    763793        def write(self): 
    764794                '''Write the data back to disk.''' 
    765                 raise NotImplementedError 
     795                for layer_data in self.layers.values(): 
     796                        layer_data.write() 
     797                self.modified = False 
    766798 
    767799        def set_file_status(self, name, layer, version, local=True) 
     
    770802        def update(self): 
    771803                '''Update all fields and write the node to disk if modified. This function should be called regularly and whenever the Node will go out of scope.''' 
    772                 raise NotImplementedError 
     804                if not self.modified: 
     805                        return 
     806                self.layers[self.root].config.set('general', 'description', self.description) 
     807                self.layers[self.root].config.set('general', 'classification', self.classification) 
     808                self.layers[self.root].config.set('general', 'enforcing', self.enforcing) 
     809                self.layers[self.root].config.set('general', 'root', self.root) 
     810                for b in self.bools.keys(): 
     811                        self.layers[self.root].config.set('bools', b, str(bools[b])) 
     812                for e in self.entries.keys(): 
     813                        self.layers[os.path.join(os.path.dirname(self.root), self.entries[e].group)].config.set('entries', e, pickle.dumps(self.entries[e])) 
     814                self.write() 
    773815 
    774816        def classify(self, classification=''): 
    775817                '''Place this node in the group specified by classification; if empty, attempt to determine from the store how the node is classified.''' 
     818                if not classification: 
     819                        l = Layer(self.root) 
     820                        if self.id not in l.list_nodes(): #only possible on initial addition of a node 
     821                                self.layers[self.root] = NodeLayerData(self.id, self.root) 
     822                                self.layers[self.root].write() #create empty config to be filled in by layer.add_node() 
     823                                return 
    776824                raise NotImplementedError 
    777825