def run(self):
        """
        Listen for new docker containers, and store in self.known_containers
        when a new container is discovered, if a container hasnt sent a heartbeat in a while, then remove it from our list or mark it
        """
        app.logger.info('Starting ContainerListener...')
        init_db()
        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)#todo: examine SocketServer
        server_address = (MASTER_HOST, MASTER_PORT)
        server.bind(server_address)
        server.listen(5)
        app.logger.info('Listening...')

        while True:
            #try:
            (clientsocket, address) = server.accept()
            data = clientsocket.recv(1024)
            if data:
                ip_addr = address[0]
                rpc_port = data.rstrip()#jsonrpc listening port
                #if new:
                if not Container.query.with_entities(Container.ip_addr).filter(Container.ip_addr == ip_addr).all():
                    #SELECT container.ip_addr from containers where container.ip_addr == ip_addr
                    new_container = Container(ip_addr, rpc_port)
                    db_session.add(new_container)
                    db_session.commit()
                    app.logger.info('New container detected: '+ip_addr+':'+rpc_port)
                    #create poller for this container after insert, to avoid race condition
                    dp = DevicePoller.DevicePoller(ip_addr, rpc_port)
                    dp.start()
            #finally:
            #    server.close()


            app.logger.info('known containers'+ str(self.known_containers))
Exemple #2
0
    def run(self):
        """
        Poll known_containers for their devices
        and update the respected value in known_containers
        """
        try:
            # try:
            self.db_container_id = (
                Container.query.filter(Container.ip_addr == self.ip_addr, Container.port == self.rpc_port)
                .with_entities(Container.id)
                .first()[0]
            )
            app.logger.info(
                "DevicePoller started for container: {0}:{1} {2}".format(
                    self.ip_addr, self.rpc_port, self.db_container_id
                )
            )

            while True:
                if not self.rpc_server.is_server_running():  # check if ADB server is running on RPC server host
                    self.rpc_server.start_server()  # todo change to restart_server

                device_serials = self.rpc_server.get_devices()[0]
                known_device_serials = [
                    serial[0] for serial in Device.query.with_entities(Device.serial_number).all()
                ]  # SELECT device.serial from devices
                app.logger.info("[{0}]Known devices: {1}".format(self.db_container_id, known_device_serials))
                # 1st, see if there are any new devices
                for device_serial in device_serials:
                    if device_serial not in known_device_serials:
                        app.logger.info("[{0}]New device found: {1}".format(self.db_container_id, device_serial))
                        new_device = Device(device_serial, self.db_container_id)
                        db_session.add(new_device)
                        db_session.commit()

                # 2nd, check to see if any of the devices in the db are not connected anymore, and remove them from the db if so
                for known_device_serial in known_device_serials:
                    if known_device_serial not in device_serials:
                        app.logger.info("[{0}]Removing device: {1}".format(self.db_container_id, known_device_serial))
                        device_to_delete = Device.query.filter(Device.serial_number == known_device_serial).first()
                        # SELECT device.serial_number from devices where device.serial_number == known_device_serial_number
                        db_session.delete(device_to_delete)
                        db_session.commit()

                time.sleep(5)
                app.logger.info("[{0}] Refreshing...".format(self.db_container_id))

        except Exception as e:
            # else:
            app.logger.debug("[{0}]{1}".format(self.db_container_id, e))
            app.logger.critical("[{0}]Cleaning up...".format(self.db_container_id))
            # cleanup dependent devices first
            devices_to_delete = Device.query.filter(Device.container_id == self.db_container_id).all()
            # SELECT device.container_id from devices where device.container_id == container_id
            for device_to_delete in devices_to_delete:
                db_session.delete(device_to_delete)
            db_session.commit()
            # cleanup container this instance is responsible for (should just be 1)
            container_to_delete = Container.query.filter(Container.id == self.db_container_id).first()
            db_session.delete(container_to_delete)
            db_session.commit()
            return