Skip to content

Instantly share code, notes, and snippets.

@marius311
Created October 11, 2012 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save marius311/3876001 to your computer and use it in GitHub Desktop.
Save marius311/3876001 to your computer and use it in GitHub Desktop.
Get an IPython notebook running on Carver.
#!/usr/bin/env python
#Specify this:
username = ''
"""
Get an IPython notebook running on Carver.
Usage:
ipyn.carv --dir /some/path --port ####
"""
from subprocess import Popen, PIPE, call
import sys
import webbrowser
from getopt import getopt
import time
args = dict(getopt(sys.argv[1:],[],['port=','dir=','inline=','usplanck='])[0])
port = args.get('--port',8888)
dir = args.get('--dir',None)
inline = (args.get('--inline','True')=='True')
usplanck = (args.get('--usplanck','True')=='True')
def readwhile(stream,func):
while True:
line = stream.readline()
if line!='':
print line[:-1]
if func(line): break
else:
raise Exception("Disconnected unexpectedly.")
pqsub=Popen(['ssh','-t','-t','-4','%s@carver.nersc.gov'%username],stdin=PIPE,stdout=PIPE,stderr=PIPE)
pqsub.stdin.write('qsub -I -V ')
if usplanck: pqsub.stdin.write('-q usplanck -l nodes=1:ppn=1,pvmem=20gb -l walltime=12:00:00')
pqsub.stdin.write('\necho HOSTNAME=`hostname`\n')
def gethostname(line):
global hostname
if line.startswith('HOSTNAME'):
hostname = line.split('=')[1].strip()
return True
readwhile(pqsub.stdout, gethostname)
if dir:
pqsub.stdin.write('cd %s\n'%dir)
pqsub.stdin.write('echo CD\n')
readwhile(pqsub.stdout, lambda line: line.startswith('CD'))
pqsub.stdin.write('ipython notebook --pylab%s --port=%s\n'%('=inline' if inline else '',port))
readwhile(pqsub.stdout, lambda line: line.startswith('[NotebookApp]'))
tunnel = ['ssh','-4', '-t', '-Y', 'carver.nersc.gov', '-L', '%s:localhost:%s'%(port,port), 'ssh', '-t', '-Y', hostname, '-L', '%s:localhost:%s'%(port,port)]
print ' '.join(tunnel)
ptunnel = Popen(tunnel,stdout=PIPE,stdin=PIPE)
ptunnel.stdin.write('echo TUNNEL\n')
readwhile(ptunnel.stdout,lambda line: line.startswith('TUNNEL'))
webbrowser.open('http://localhost:%s'%port)
print "Succesfully opened notebook!"
print "Kill this process to end your notebook connection."
time.sleep(12*60**2)
pqsub.kill()
ptunnel.kill()
print "Succesfully cleanup up connections."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment