def public_url(app, user_or_service=None, path=''): """Return the full, public base URL (including prefix) of the given JupyterHub instance.""" if user_or_service: if app.subdomain_host: host = user_or_service.host else: host = public_host(app) prefix = user_or_service.prefix else: host = public_host(app) prefix = Server.from_url(app.proxy.public_url).base_url if path: return host + ujoin(prefix, path) else: return host + prefix
async def test_url_changed(kube_ns, kube_client, config, hub_pod, hub): user = MockUser(name="url") config.KubeSpawner.pod_connect_ip = ( "jupyter-{username}--{servername}.foo.example.com") spawner = KubeSpawner(hub=hub, user=user, config=config) spawner.db = Mock() # start the spawner res = await spawner.start() pod_host = "http://jupyter-url.foo.example.com:8888" assert res == pod_host # Mock an incorrect value in the db # Can occur e.g. by interrupting a launch with a hub restart # or possibly weird network things in kubernetes spawner.server = Server.from_url(res + "/users/url/") spawner.server.ip = "1.2.3.4" spawner.server.port = 0 assert spawner.server.host == "http://1.2.3.4:0" assert spawner.server.base_url == "/users/url/" # poll checks the url, and should restore the correct value await spawner.poll() # verify change noticed and persisted to db assert spawner.server.host == pod_host assert spawner.db.commit.call_count == 1 # base_url should be left alone assert spawner.server.base_url == "/users/url/" previous_commit_count = spawner.db.commit.call_count # run it again, to make sure we aren't incorrectly detecting and committing # changes on every poll await spawner.poll() assert spawner.db.commit.call_count == previous_commit_count await spawner.stop()
def public_host(app): """Return the public *host* (no URL prefix) of the given JupyterHub instance.""" if app.subdomain_host: return app.subdomain_host else: return Server.from_url(app.proxy.public_url).host
def start_tb_proxy(self): public_server = Server.from_url(self.tb_public_url) api_server = Server.from_url(self.tb_api_url) env = os.environ.copy() env['CONFIGPROXY_AUTH_TOKEN'] = self.auth_token cmd = self.command + [ '--ip', public_server.ip, '--port', str(public_server.port), '--api-ip', api_server.ip, '--api-port', str(api_server.port), '--error-target', url_path_join(self.hub.url, 'error'), '--no-include-prefix', ] print('cmdcmdcmdcmd', cmd, self.auth_token) if self.app.subdomain_host: cmd.append('--host-routing') if self.debug: cmd.extend(['--log-level', 'debug']) if self.ssl_key: cmd.extend(['--ssl-key', self.ssl_key]) if self.ssl_cert: cmd.extend(['--ssl-cert', self.ssl_cert]) if self.app.statsd_host: cmd.extend([ '--statsd-host', self.app.statsd_host, '--statsd-port', str(self.app.statsd_port), '--statsd-prefix', self.app.statsd_prefix + '.chp' ]) # Warn if SSL is not used if ' --ssl' not in ' '.join(cmd): self.log.warning( "Running JupyterHub without SSL." " I hope there is SSL termination happening somewhere else..." ) self.log.info("Starting proxy @ %s", public_server.bind_url) self.log.debug("Proxy cmd: %s", cmd) shell = os.name == 'nt' try: self.proxy_process = Popen(cmd, env=env, start_new_session=True, shell=shell) except FileNotFoundError as e: self.log.error( "Failed to find proxy %r\n" "The proxy can be installed with `npm install -g configurable-http-proxy`" % self.command) raise def _check_process(): status = self.proxy_process.poll() if status is not None: e = RuntimeError("Proxy failed to start with exit code %i" % status) # py2-compatible `raise e from None` e.__cause__ = None raise e for server in (public_server, api_server): for i in range(10): _check_process() try: yield server.wait_up(1) except TimeoutError: continue else: break yield server.wait_up(1) _check_process() self.log.debug("Proxy started and appears to be up") pc = PeriodicCallback(self.check_running, 1e3 * self.check_running_interval) pc.start()
def start(self): self.server = Server.from_url("http://127.0.0.1:%i" % randint(1025, 65535))