예제 #1
0
    def initialize(self,
                   apiurl,
                   auth_backend,
                   deployer,
                   tokens,
                   ws_source_template,
                   ws_target_template,
                   io_loop=None):
        """Initialize the WebSocket server.

        Create a new WebSocket client and connect it to the Juju API.
        Set up the authentication system.
        Handle the queued messages.
        """
        if io_loop is None:
            io_loop = IOLoop.current()
        self._io_loop = io_loop
        self._summary = request_summary(self.request) + ' '
        logging.info(self._summary + 'client connected')
        self.connected = True
        self.juju_connected = False
        self._juju_message_queue = queue = deque()
        # Set up the authentication infrastructure.
        self.tokens = tokens
        write_message = wrap_write_message(self)
        self.user = User()
        self.auth = AuthMiddleware(self.user, auth_backend, tokens,
                                   write_message)
        # Set up the bundle deployment and change set infrastructure.
        self.deployment = DeployMiddleware(self.user, deployer, write_message)
        self.changeset = ChangeSetMiddleware(self.user, write_message)
        apiurl = get_juju_api_url(self.request.path, ws_source_template,
                                  ws_target_template, apiurl)
        # Juju requires the Origin header to be included in the WebSocket
        # client handshake request. Propagate the client origin if present;
        # use the Juju API server as origin otherwise.
        headers = get_headers(self.request, apiurl)
        # Connect the WebSocket client to the Juju API server.
        self._juju_connected_future = websocket_connect(io_loop,
                                                        apiurl,
                                                        self.on_juju_message,
                                                        headers=headers)
        try:
            self.juju_connection = yield self._juju_connected_future
        except Exception as err:
            logging.error(self._summary + 'unable to connect to the Juju API')
            logging.exception(err)
            self.connected = False
            return
        # At this point the Juju API is successfully connected.
        self.juju_connected = True
        logging.info(self._summary + 'Juju API connected: {}'.format(apiurl))
        # Send all the messages that have been enqueued before the connection
        # to the Juju API server was established.
        while self.connected and self.juju_connected and len(queue):
            message = queue.popleft()
            encoded = message.encode('utf-8')
            logging.debug(self._summary + 'queue -> juju: {}'.format(encoded))
            self.juju_connection.write_message(message)
예제 #2
0
    def initialize(
            self, apiurl, auth_backend, deployer, tokens, ws_source_template,
            ws_target_template, io_loop=None):
        """Initialize the WebSocket server.

        Create a new WebSocket client and connect it to the Juju API.
        Set up the authentication system.
        Handle the queued messages.
        """
        if io_loop is None:
            io_loop = IOLoop.current()
        self._io_loop = io_loop
        self._summary = request_summary(self.request) + ' '
        logging.info(self._summary + 'client connected')
        self.connected = True
        self.juju_connected = False
        self._juju_message_queue = queue = deque()
        # Set up the authentication infrastructure.
        self.tokens = tokens
        write_message = wrap_write_message(self)
        self.user = User()
        self.auth = AuthMiddleware(
            self.user, auth_backend, tokens, write_message)
        # Set up the bundle deployment and change set infrastructure.
        self.deployment = DeployMiddleware(self.user, deployer, write_message)
        self.changeset = ChangeSetMiddleware(self.user, write_message)
        apiurl = get_juju_api_url(
            self.request.path, ws_source_template, ws_target_template, apiurl)
        # Juju requires the Origin header to be included in the WebSocket
        # client handshake request. Propagate the client origin if present;
        # use the Juju API server as origin otherwise.
        headers = get_headers(self.request, apiurl)
        # Connect the WebSocket client to the Juju API server.
        self._juju_connected_future = websocket_connect(
            io_loop, apiurl, self.on_juju_message, headers=headers)
        try:
            self.juju_connection = yield self._juju_connected_future
        except Exception as err:
            logging.error(self._summary + 'unable to connect to the Juju API')
            logging.exception(err)
            self.connected = False
            return
        # At this point the Juju API is successfully connected.
        self.juju_connected = True
        logging.info(self._summary + 'Juju API connected: {}'.format(apiurl))
        # Send all the messages that have been enqueued before the connection
        # to the Juju API server was established.
        while self.connected and self.juju_connected and len(queue):
            message = queue.popleft()
            encoded = message.encode('utf-8')
            logging.debug(self._summary + 'queue -> juju: {}'.format(encoded))
            self.juju_connection.write_message(message)
예제 #3
0
 def test_prefixed_match(self):
     # The expected URL is returned when the path also includes a prefix.
     path = '/my/prefix/api/1.2.3.4/47/uuid'
     url = utils.get_juju_api_url(path, self.template, self.default)
     self.assertEqual('wss://1.2.3.4:47/environment/uuid/api', url)
예제 #4
0
 def test_precise_match(self):
     # The expected URL is returned when the path matches precisely.
     path = '/api/1.2.3.4/4242/my-uuid'
     url = utils.get_juju_api_url(path, self.template, self.default)
     self.assertEqual('wss://1.2.3.4:4242/environment/my-uuid/api', url)
예제 #5
0
 def test_no_match(self):
     # The default URL is returned if there is no match.
     url = utils.get_juju_api_url('/ws', self.template, self.default)
     self.assertEqual(self.default, url)
예제 #6
0
 def call(self, path):
     """Call the get_juju_api_url function using the given path."""
     return utils.get_juju_api_url(path, self.source_template, self.target_template, self.default)
예제 #7
0
 def call(self, path):
     """Call the get_juju_api_url function using the given path."""
     return utils.get_juju_api_url(path, self.source_template,
                                   self.target_template, self.default)