Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes with JupyterHub 2.3.1 #45

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions scripts/get_port.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@

import argparse
import socket

def main():
args = parse_arguments()
if args.ip:
print("{} {}".format(port(), ip()))
else:
print(port())

def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("--ip", "-i",
help="Include IP address in output",
action="store_true")
return parser.parse_args()

def port():
s = socket.socket()
s.bind(('', 0))
s.bind(("", 0))
port = s.getsockname()[1]
s.close()
return port


def ip(address=("8.8.8.8", 80)):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(address)
ip = s.getsockname()[0]
s.close()
return ip


if __name__ == "__main__":
main()
print(f"{ip()} {port()}")
18 changes: 14 additions & 4 deletions sshspawner/sshspawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class SSHSpawner(Spawner):
# http://traitlets.readthedocs.io/en/stable/migration.html#separation-of-metadata-and-keyword-arguments-in-traittype-contructors
# config is an unrecognized keyword

# Change default ip value for SSHSpawners
ip = "0.0.0.0"

remote_hosts = List(trait=Unicode(),
help="Possible remote hosts from which to choose remote_host.",
config=True)
Expand Down Expand Up @@ -114,7 +117,7 @@ async def start(self):
c = asyncssh.read_certificate(cf)

self.remote_host = self.choose_remote_host()

self.remote_ip, port = await self.remote_random_port()
if self.remote_ip is None or port is None or port == 0:
return False
Expand Down Expand Up @@ -149,9 +152,16 @@ async def start(self):
for index, value in enumerate(cmd):
if value == old:
cmd[index] = new

has_port = False
for index, value in enumerate(cmd):
if value[0:6] == '--port':
cmd[index] = '--port=%d' % (port)
has_port = True

# Always supply port option!
if not has_port:
cmd.append(f"--port={port}")

remote_cmd = ' '.join(cmd)

Expand Down Expand Up @@ -211,8 +221,8 @@ def _log_remote_ip(self, change):

# FIXME this needs to now return IP and port too
async def remote_random_port(self):
"""Select unoccupied port on the remote host and return it.
"""Select unoccupied port on the remote host and return it.

If this fails for some reason return `None`."""

username = self.get_remote_user(self.user.name)
Expand Down Expand Up @@ -257,7 +267,7 @@ async def exec_notebook(self, command):
for item in env.items():
# item is a (key, value) tuple
# command = ('export %s=%s;' % item) + command
bash_script_str += 'export %s=%s\n' % item
bash_script_str += "export %s='%s'\n" % item
bash_script_str += 'unset XDG_RUNTIME_DIR\n'

bash_script_str += 'touch .jupyter.log\n'
Expand Down