Ejemplo n.º 1
0
    def update_node(self, node_id, data):
        """
        Update Node information on server.  Currently only "active" and "sync" fields should be updated.
        :param data: Dictionary with values for "active" and/or "sync" node fields.
        :return Node:
        """
        try:
            # Reply contains single Node record
            reply, status_code, rq = self._make_json_request(
                'PUT', '/api/nodes/{0}/'.format(node_id), data)

            # _logger.debug('Node Update Request: {0}: {1}'.format(status_code, reply))

            if reply is not None and status_code is not None:

                # 200 means the node record was update successfully.
                if status_code == requests.codes.ok:
                    return Node(reply), status_code

            _logger.debug('Update node failed ({0}).'.format(status_code))
            return None, status_code

        except:
            raise
            # _logger.debug('ex: {0}: {1}'.format(status_code, reply))
            pass

        return None, requests.codes.teapot
Ejemplo n.º 2
0
    def register_node(self, node):
        """
        Register Node on server.
        :param node:
        :return Node:
        """
        try:
            node_d = node.to_dict()
        except AttributeError:
            _logger.critical(
                'Node parameter is not valid in register_node method.')
            return None, requests.codes.teapot

        try:
            # Reply contains single Node record
            reply, status_code, rq = self._make_json_request(
                'POST', '/api/nodes/', node_d)

            if reply is not None and status_code is not None:

                # 201 means the node record was created successfully.
                if status_code == requests.codes.created or status_code == requests.codes.ok:
                    return Node(reply), status_code

            _logger.debug('Register node failed ({0}).'.format(status_code))
            return None, status_code

        except:
            pass

        return None, requests.codes.teapot
Ejemplo n.º 3
0
    def get_node_by_machine_id(self, machine_id):
        """
        Request Node object from server filtered by machine_id value.
        :param machine_id:
        :return Node object:
        """

        url = '/api/nodes/?machine_id={0}'.format(machine_id)

        # Reply contains dict array of Node records.  Reply array should be empty or contain one Node record.
        reply, status_code, rq = self._make_json_request('GET', url)

        # If we have a good code and no data, then the node has not been registered yet.
        if status_code == requests.codes.ok:
            if reply is not None and len(reply) != 0:
                return Node(reply[0]), status_code

        _logger.error('Node lookup request failed.')
        return None, status_code
    def _register_node(self):
        """
        Contact the server to register this node with the server.
        """
        # Look for existing Node record.
        self._node, status_code = self._sds_conn.get_node_by_machine_id(
            self.node_info.machine_id)

        if status_code == requests.codes.ok and self._node and self._node.id:
            _logger.warning(
                '{0}: Node already registered, using previously registered node info.'
                .format(self.get_name()))
            return True

        self.cwrite('{0}: Registering Node...  '.format(self.get_name()))

        node = Node(
            platform=self.node_info.firewall_platform,
            os=platform.system().lower(),
            dist=platform.dist()[0],
            dist_version=platform.dist()[1],
            hostname=socket.gethostname(),
            python_version=sys.version.replace('\n', ''),
            machine_id=self.node_info.machine_id,
            fernet_key='',  # Fernet.generate_key().decode('UTF-8'),
            polling_interval=self._globals.polling_interval)

        # Attempt to register this node on the SD server.
        self._node, status_code = self._sds_conn.register_node(node)

        if status_code != requests.codes.created or not self._node or self._node.id is None:
            self.cwriteline('[Failed]',
                            'Register Node failed, unknown reason.')
            return False

        self.cwriteline('[OK]', 'Node successfully registered.')

        return True