def __push_stream_end_point_MS(self, t_addr, t_port, data, image_name):
        """
        Create a client socket to connect to server
        :param target: Tuple with three parameter from the endpoint request
        :param data: ByteArray which holds the content to be streamed to the batch.
        :return: Boolean return status
        """

        try:
            s = None
            for res in socket.getaddrinfo(t_addr, t_port, socket.AF_UNSPEC,
                                          socket.SOCK_STREAM):
                af, socktype, proto, canonname, sa = res
                try:
                    s = socket.socket(af, socktype, proto)
                except OSError as msg:
                    print(msg)
                    s = None
                    continue
                try:
                    s.connect(sa)
                except OSError as msg:
                    print(msg)
                    s.close()
                    s = None
                    continue
                break
            if s is None:
                SysOut.warn_string("Cannot connect to " + t_addr + ":" +
                                   str(t_port) + "!")
                return False

            # Generate header string
            image_name_b = bytes(image_name, 'UTF-8')
            image_name_l = str(len(image_name_b))

            while len(image_name_l) < 3:
                image_name_l = "0" + image_name_l

            image_name_t = bytes(image_name_l, 'UTF-8') + image_name_b

            with s:
                # Identifying object id
                s.sendall(image_name_t)
                s.sendall(data)
                s.sendall(b'')
                s.close()

            return True

        except:
            SysOut.warn_string("Cannot stream data to an end point!")
Exemple #2
0
        def update_job(request):
            job_id = request.get('job_id')
            if not job_id in LookUpTable.Jobs.__jobs:
                SysOut.warn_string(
                    "Couldn't update job, no existing job matching ID!")
                return False

            tkn = request.get(Definition.get_str_token())
            if not tkn == LookUpTable.Jobs.__jobs[job_id]['user_token']:
                SysOut.warn_string("Incorrect token, refusing update.")
                return False

            old_job = LookUpTable.Jobs.__jobs[job_id]
            old_job['job_status'] = request.get('job_status')

            return True
    def __get_stream_end_point(self, container_name, container_os, priority,
                               digest):
        """
        Request for the stream end point from the master.
        :return: Boolean(False) when the system is busy.
                 Tuple(batch_addr, batch_port, tuple_id) if the batch or messaging system is available.
        """

        if not priority:
            priority = 0
        else:
            if not isinstance(priority, int):
                LocalError.err_invalid_priority_type()

        try:

            url = self.__str_push_request + Definition.Master.get_str_push_req_container_ext(
                container_name, container_os, priority, self.__source_name,
                digest)

            print('Sending request..')
            print(url)

            response = self.__connector.request('GET', url)
            # print(response.status)
            # print(response.text)

            if response.status == 406:
                # Messages in queue is full. Result in queue lock.
                SysOut.warn_string("Queue in master is full.")
                return False

            if response.status == 500:
                SysOut.warn_string(
                    "System internal error! Please consult documentation.")
                return False

            elif response.status != 200:
                SysOut.warn_string("something else went wrong")
                return False

        except Exception as ex:
            print(ex)
            SysOut.err_string(
                "Couldn't connect to the master at {0}:{1}.".format(
                    self.__master_addr, self.__master_port))
            return False

        try:
            content = eval(response.data.decode('utf-8'))
            return content

        except:
            SysOut.warn_string("JSON content error from the master!")
            return False
Exemple #4
0
        def new_job(request):
            new_item = {}
            new_id = request.get('job_id')
            if not new_id:
                SysOut.warn_string("Couldn't create job, no ID provided!")
                return False

            if new_id in LookUpTable.Jobs.__jobs:
                SysOut.warn_string(
                    "Job already exists in system, can't create!")
                return False

            new_item['job_id'] = new_id
            new_item['job_status'] = request.get('job_status')
            new_item[
                Definition.Container.get_str_con_image_name()] = request.get(
                    Definition.Container.get_str_con_image_name())
            new_item['user_token'] = request.get(Definition.get_str_token())
            new_item['volatile'] = request.get('volatile')
            LookUpTable.Jobs.__jobs[new_id] = new_item

            return True