Skip to content

Instantly share code, notes, and snippets.

@twiecki
Created December 10, 2012 17:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save twiecki/4251856 to your computer and use it in GitHub Desktop.
Save twiecki/4251856 to your computer and use it in GitHub Desktop.
Relevant changes to starcluster ipcluster.py code to start multiple ipython nbs on multiple ports
def _start_notebook(self, master, user, profile_dir, profile_name='default', notebook_port=8888):
master.ssh.execute('ipython profile create %s' % profile_name)
log.info("Setting up IPython web notebook for user: %s" % user)
user_cert = posixpath.join(profile_dir, '%s.pem' % user)
ssl_cert = posixpath.join(profile_dir, '%s.pem' % user)
if not master.ssh.isfile(user_cert):
log.info("Creating SSL certificate for user %s" % user)
ssl_subj = "/C=US/ST=SC/L=STAR/O=Dis/CN=%s" % master.dns_name
master.ssh.execute(
"openssl req -new -newkey rsa:4096 -days 365 "
'-nodes -x509 -subj %s -keyout %s -out %s' %
(ssl_subj, ssl_cert, ssl_cert))
else:
log.info("Using existing SSL certificate...")
f = master.ssh.remote_file('%s/ipython_notebook_config.py' %
profile_dir)
#notebook_port = 8888
sha1py = 'from IPython.lib import passwd; print passwd("%s")'
sha1cmd = "python -c '%s'" % sha1py
sha1pass = master.ssh.execute(sha1cmd % self.notebook_passwd)[0]
f.write('\n'.join([
"c = get_config()",
"c.IPKernelApp.pylab = 'inline'",
"c.NotebookApp.certfile = u'%s'" % ssl_cert,
"c.NotebookApp.ip = '*'",
"c.NotebookApp.open_browser = False",
"c.NotebookApp.password = u'%s'" % sha1pass,
"c.NotebookApp.port = %d" % notebook_port,
]))
f.close()
master.ssh.execute_async("ipython notebook --profile %s" % profile_name)
group = master.cluster_groups[0]
world_cidr = '0.0.0.0/0'
port_open = master.ec2.has_permission(group, 'tcp', notebook_port,
notebook_port, world_cidr)
if not port_open:
log.info("Authorizing tcp port %s on %s" %
(notebook_port, world_cidr))
group.authorize('tcp', notebook_port, notebook_port, world_cidr)
log.info("IPython notebook URL: https://%s:%s" %
(master.dns_name, notebook_port))
log.info("The notebook password is: %s" % self.notebook_passwd)
log.warn("Please check your local firewall settings if you're having "
"issues connecting to the IPython notebook",
extra=dict(__textwrap__=True))
@print_timing("IPCluster")
def run(self, nodes, master, user, user_shell, volumes):
n = sum([node.num_processors for node in nodes]) - 1
user_home = node.getpwnam(user).pw_dir
profile_dir = posixpath.join(user_home, '.ipython', 'profile_default')
master.ssh.switch_user(user)
self._write_config(master, user, profile_dir)
cfile = self._start_cluster(master, n, profile_dir)
if self.enable_notebook:
ports = range(8888, 8958)
for port in ports:
self._start_notebook(master, user, profile_dir+str(port), profile_name='default%i'%port, notebook_port=port)
log.info(STARTED_MSG_11 % dict(cluster=master.parent_cluster,
user=user, connector_file=cfile,
key_location=master.key_location))
master.ssh.switch_user('root')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment