Esempio n. 1
0
    def _update_node(self, cluster, info, data, updated, callback):
        """
        updates an individual node, this is the actual work function

        @param cluster - cluster this node is on
        @param info - info from ganeti
        @param data - data from database
        @param updated - counter object
        @param callback - callback fired when method is complete.
        """
        hostname = info['name']
        if hostname in data:
            id, mtime = data[hostname]
            if not mtime or mtime < info['mtime']:
                print '    Node (updated) : %s' % hostname
                #print '        %s :: %s' % (mtime, datetime.fromtimestamp(info['mtime']))
                # only update the whole object if it is new or modified.
                parsed = Node.parse_persistent_info(info)
                Node.objects.filter(pk=id) \
                    .update(serialized_info=cPickle.dumps(info), **parsed)
                updated += 1
        else:
            # new node
            node = Node(cluster=cluster, hostname=info['name'])
            node.info = info
            node.save()
            id = node.pk
            updated += 1

        # Updates relationships between a Node and its Primary and Secondary
        # VirtualMachines.  This always runs even when there are no updates but
        # it should execute quickly since it runs against an indexed column
        #
        # XXX this blocks so it may be worthwhile to spin this off into a
        # deferred just to break up this method.  
        VirtualMachine.objects \
            .filter(hostname__in=info['pinst_list']) \
            .update(primary_node=id)

        VirtualMachine.objects \
            .filter(hostname__in=info['sinst_list']) \
            .update(secondary_node=id)

        callback(id)