Ejemplo n.º 1
0
 def __contains__(self, item):
     if isinstance(item, tuple):
         result = Node.select().where(Node.hostname == item[1]).exists()
     else:
         result = Node.select().where((Node.id == item)
                                      | (Node.hostname == item)).exists()
     return result
Ejemplo n.º 2
0
 def __getitem__(self, key):
     if isinstance(key, tuple):
         result = Node.select().where((Node.id == key[0])
                                      & (Node.hostname == key[1])).limit(1)
     else:
         result = Node.select().where((Node.id == key)
                                      | (Node.hostname == key)).limit(1)
     return result.first() if result else None
Ejemplo n.º 3
0
 def peek(self, status=None):
     if status:
         result = Node.select().where(Node.status == status).first()
     else:
         result = Node.select().first()
     if not result:
         raise Exception('node not found')
     return result
Ejemplo n.º 4
0
 def __delitem__(self, key, safe=True):
     if isinstance(key, tuple):
         if safe:
             query = (Node.id == key[0]) & (Node.hostname == key[1]) & (
                 Node.status == NodeState.OFFLINE)
         else:
             query = (Node.id == key[0]) & (Node.hostname == key[1])
     else:
         if safe:
             query = ((Node.id == key) |
                      (Node.hostname == key)) & (Node.status
                                                 == NodeState.OFFLINE)
         else:
             query = (Node.id == key) | (Node.hostname == key)
     Node.delete().where(query).execute()
Ejemplo n.º 5
0
 def get_silent_periods(self, key):
     if self.__contains__(key):
         result = Node.select().where((Node.id == key)
                                      | (Node.hostname == key)).first()
         if result:
             return json.loads(result.silent_periods)
         else:
             raise Exception(
                 "no silent periods configured for node [{}]".format(key))
     else:
         raise Exception('node not found')
Ejemplo n.º 6
0
    def __setitem__(self, key, status):
        now = datetime.datetime.now()
        set_fields = {'status': status}

        if status == NodeState.ONLINE:
            set_fields['date_become_online'] = now
            set_fields['date_become_offline'] = None
        elif status == NodeState.OFFLINE:
            set_fields['date_become_online'] = None
            set_fields['date_become_offline'] = now

        if self.__contains__(key):
            if isinstance(key, tuple):
                Node.update(set_fields).where((Node.id == key[1]) | (
                    Node.hostname == key[1])).execute()
            else:
                Node.update(set_fields).where((Node.id == key) | (
                    Node.hostname == key)).execute()
        else:
            if isinstance(key, tuple):
                info = cpuinfo.get_cpu_info()
                set_fields['id'] = key[0]
                set_fields['hostname'] = key[1]
                set_fields['cpu_threads'] = info['count']
                set_fields['cpu_details'] = info['brand']
                Node.create(**set_fields)
            else:
                raise Exception(
                    'node doesn\'t exist, you must provide both id and hostname'
                )
Ejemplo n.º 7
0
 def __iter__(self):
     return Node.select().iterator()
Ejemplo n.º 8
0
 def serialize(self):
     result = []
     for node in Node.select().iterator():
         result.append(node.dict())
     return result
Ejemplo n.º 9
0
 def __repr__(self):
     result = []
     for node in Node.select().iterator():
         result.append(node)
     return "Nodes({})".format(result)
Ejemplo n.º 10
0
 def __len__(self):
     return Node.select().count()
Ejemplo n.º 11
0
 def clear_silent_periods(self, key):
     if self.__contains__(key):
         Node.update(silent_periods=None).where(Node.id == key).execute()
     else:
         raise Exception('node not found')
Ejemplo n.º 12
0
 def __create_table(self):
     Node.create_table(True)
Ejemplo n.º 13
0
 def set_silent_periods(self, key, silent_periods):
     if self.__contains__(key):
         Node.update(silent_periods=json.dumps(silent_periods)).where((
             Node.id == key) | (Node.hostname == key)).execute()
     else:
         raise Exception('node not found')
Ejemplo n.º 14
0
 def clear(self, safe=True):
     if safe:
         Node.delete().where(Node.status != NodeState.ONLINE).execute()
     else:
         Node.delete().execute()