Beispiel #1
0
def test_get_location_old_contact_file(monkeypatch, mpatch_get_fqdn_by_host):
    """It Fails because it's not a Cylc 8 workflow."""
    contact_data = BASE_CONTACT_DATA.copy()
    contact_data['CYLC_SUITE_PUBLISH_PORT'] = '8042'
    contact_data['CYLC_VERSION'] = '5.1.2'
    monkeypatch.setattr(
        cylc.flow.network, 'load_contact_file', lambda _ : contact_data
    )
    with pytest.raises(CylcVersionError, match=r'.*5.1.2.*') as exc:
        get_location('_')
Beispiel #2
0
 def __init__(self,
              workflow: str,
              host: Optional[str] = None,
              port: Optional[int] = None,
              context: Optional[zmq.asyncio.Context] = None,
              timeout: Union[float, str, None] = None,
              srv_public_key_loc: Optional[str] = None):
     super().__init__(zmq.REQ, context=context)
     self.workflow = workflow
     if not host or not port:
         host, port, _ = get_location(workflow)
     else:
         port = int(port)
     self.host = host
     self.port = port
     if timeout is None:
         timeout = self.DEFAULT_TIMEOUT
     else:
         timeout = float(timeout)
     self.timeout = timeout * 1000
     self.timeout_handler = partial(self._timeout_handler, workflow, host,
                                    port)
     self.poller: Any = None
     # Connect the ZMQ socket on instantiation
     self.start(self.host, self.port, srv_public_key_loc)
     # gather header info post start
     self.header = self.get_header()
Beispiel #3
0
 def __init__(self,
              suite: str,
              host: str = None,
              port: int = None,
              context: object = None,
              timeout: Union[float, str] = None,
              srv_public_key_loc: str = None):
     super().__init__(zmq.REQ, context=context)
     self.suite = suite
     if not host or not port:
         host, port, _ = get_location(suite)
     else:
         port = int(port)
     self.host = host
     self.port = port
     if timeout is None:
         timeout = self.DEFAULT_TIMEOUT
     else:
         timeout = float(timeout)
     self.timeout = timeout * 1000
     self.timeout_handler = partial(self._timeout_handler, suite, host,
                                    port)
     self.poller = None
     # Connect the ZMQ socket on instantiation
     self.start(self.host, self.port, srv_public_key_loc)
     # gather header info post start
     self.header = self.get_header()
Beispiel #4
0
    def __init__(self,
                 suite: str,
                 host: str = None,
                 timeout: Union[float, str] = None):
        self.suite = suite

        if not host:
            self.host, _, _ = get_location(suite)
Beispiel #5
0
    def __init__(
            self,
            workflow: str,
            host: str = None,
            timeout: Union[float, str] = None
    ):
        self.workflow = workflow

        if not host:
            self.host, _, _ = get_location(workflow)
Beispiel #6
0
 def __init__(self,
              workflow: str,
              host: str = None,
              timeout: Union[float, str] = None):
     self.workflow = workflow
     self.SLEEP_INTERVAL = 0.1
     if not host:
         self.host, _, _ = get_location(workflow)
     # 5 min default timeout
     self.timeout = timeout if timeout is not None else 300
Beispiel #7
0
def test_get_location_ok(monkeypatch, mpatch_get_fqdn_by_host):
    """It passes when information is available."""
    contact_data = BASE_CONTACT_DATA.copy()
    contact_data[ContactFileFields.PUBLISH_PORT] = '8042'
    monkeypatch.setattr(
        cylc.flow.network, 'load_contact_file', lambda _ : contact_data
    )
    assert get_location('_') == (
        'myhost.x.y.z', 42, 8042
    )
Beispiel #8
0
def main(_, options, *args):
    suite = args[0]

    if len(args) > 1:
        try:
            user_at_host, options.port = args[1].split(':')
            options.owner, options.host = user_at_host.split('@')
        except ValueError:
            print(('USER_AT_HOST must take the form '
                   '"user@host:port"'),
                  file=sys.stderr)
            sys.exit(1)
    elif options.host is None or options.port is None:
        try:
            while True:
                try:
                    options.host, _, options.port = get_location(
                        suite, options.owner, options.host)
                except (ClientError, IOError, TypeError, ValueError):
                    time.sleep(3)
                    continue
                break
        except KeyboardInterrupt:
            exit()

    print(f'Connecting to tcp://{options.host}:{options.port}')
    topic_set = set()
    topic_set.add(b'shutdown')
    for topic in options.topics.split(','):
        topic_set.add(topic.encode('utf-8'))

    subscriber = WorkflowSubscriber(suite,
                                    host=options.host,
                                    port=options.port,
                                    topics=topic_set)

    subscriber.loop.create_task(
        subscriber.subscribe(process_delta_msg,
                             func=print_message,
                             subscriber=subscriber,
                             once=options.once))

    # run Python run
    try:
        subscriber.loop.run_forever()
    except (KeyboardInterrupt, SystemExit):
        print('\nDisconnecting')
        subscriber.stop()
        exit()
Beispiel #9
0
 def __init__(self,
              suite: str,
              owner: str = None,
              host: str = None,
              port: Union[int, str] = None,
              context: object = None,
              srv_public_key_loc: str = None,
              topics: Iterable[bytes] = None):
     super().__init__(zmq.SUB, context=context)
     self.suite = suite
     if port:
         port = int(port)
     if not (host and port):
         host, _, port = get_location(suite, owner, host)
     if topics is None:
         self.topics = set(b'')
     self.topics = topics
     # Connect the ZMQ socket on instantiation
     self.start(host, port, srv_public_key_loc)
Beispiel #10
0
def main(_, options, *args):
    suite = args[0]

    try:
        while True:
            try:
                host, _, port = get_location(suite)
            except (ClientError, IOError, TypeError, ValueError) as exc:
                print(exc)
                time.sleep(3)
                continue
            break
    except KeyboardInterrupt:
        sys.exit()

    print(f'Connecting to tcp://{host}:{port}')
    topic_set = set()
    topic_set.add(b'shutdown')
    for topic in options.topics.split(','):
        topic_set.add(topic.encode('utf-8'))

    subscriber = WorkflowSubscriber(suite,
                                    host=host,
                                    port=port,
                                    topics=topic_set)

    subscriber.loop.create_task(
        subscriber.subscribe(process_delta_msg,
                             func=print_message,
                             subscriber=subscriber,
                             once=options.once))

    # run Python run
    try:
        subscriber.loop.run_forever()
    except (KeyboardInterrupt, SystemExit):
        print('\nDisconnecting')
        subscriber.stop()
        sys.exit()
Beispiel #11
0
    async def async_request(
            self,
            command: str,
            args: Optional[Dict[str, Any]] = None,
            timeout: Optional[float] = None,
            req_meta: Optional[Dict[str, Any]] = None) -> object:
        """Send an asynchronous request using asyncio.

        Has the same arguments and return values as ``serial_request``.

        """
        timeout = (float(timeout) * 1000 if timeout else None) or self.timeout
        if not args:
            args = {}

        # Note: we are using CurveZMQ to secure the messages (see
        # self.curve_auth, self.socket.curve_...key etc.). We have set up
        # public-key cryptography on the ZMQ messaging and sockets, so
        # there is no need to encrypt messages ourselves before sending.

        # send message
        msg: Dict[str, Any] = {'command': command, 'args': args}
        msg.update(self.header)
        # add the request metadata
        if req_meta:
            msg['meta'].update(req_meta)
        LOG.debug('zmq:send %s', msg)
        message = encode_(msg)
        self.socket.send_string(message)

        # receive response
        if self.poller.poll(timeout):
            res = await self.socket.recv()
        else:
            if callable(self.timeout_handler):
                self.timeout_handler()
            host, port, _ = get_location(self.workflow)
            if host != self.host or port != self.port:
                raise WorkflowStopped(self.workflow)
            raise ClientTimeout(
                'Timeout waiting for server response.'
                ' This could be due to network or server issues.'
                ' Check the workflow log.')

        if msg['command'] in PB_METHOD_MAP:
            response = {'data': res}
        else:
            response = decode_(res.decode())
        LOG.debug('zmq:recv %s', response)

        try:
            return response['data']
        except KeyError:
            error = response.get(
                'error',
                {'message': f'Received invalid response: {response}'},
            )
            raise ClientError(
                error.get('message'),
                error.get('traceback'),
            )