Beispiel #1
0
    def storage(self, data_list, page):

        if data_list is None:
            return None

        for row in data_list:
            ip = row["ip"]
            host = Host.query.filter_by(ip=ip).first()
            if host is None:
                host = Host()
                host.page = int(page)
                host.ip = ip
                host.app = row["app"]
                host.port = row["port"]
                host.service = row["service"]
                host.device = row["device"]
                host.hostname = row["hostname"]
                host.os = row["os"]
                host.city_cn = row["city_cn"]
                host.city_en = row["city_en"]
                host.country_cn = row["country_cn"]
                host.country_en = row["country_en"]

                db.session.add(host)
                db.session.commit()
Beispiel #2
0
 def scale_vm(self, cu):
     self.hosts.sort(key=lambda host: host.cu_remain, reverse=False)
     for host in self.hosts:
         if host.cu_remain >= cu:
             host.cu_used = host.cu_used + cu
             host.cu_remain = host.cu_remain - cu
             return True
     host = Host()
     if host.cu_remain >= cu:
         host.cu_used = host.cu_used + cu
         host.cu_remain = host.cu_remain - cu
         self.hosts.append(host)
         return True
     else:
         return False
Beispiel #3
0
    def new_agent(self, hostname: str, api_key: str, ip: str) -> dict:
        if not self.db.session.query(Host).filter_by(
                hostname=hostname).count() < 1:
            return {'code': 1}

        info = (hostname + api_key + ip).encode('utf-8')
        # cálculo de hash com informações do agent
        sha = hashlib.sha1(info).hexdigest()

        host_temp = Host(hostname=hostname, apikey=api_key, ip=ip, sha=sha)
        logging.warning(' - user "%s:%s" add in database SQLite' %
                        (host_temp.hostname, host_temp.ip))
        self.db.session.add(host_temp)

        try:
            self.db.session.commit()
            return {'code': 0}
        except Exception as err:
            self.db.session.rollback()
            logging.error(' - error to add user in database: %s' % err)
        return {'code': 2}