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

[ENH]: Guided create bucket finalized #33

Merged
merged 15 commits into from
Jun 14, 2019
Merged
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
106 changes: 82 additions & 24 deletions resen/Resen.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ def start_bucket(self,bucket_name):
def stop_bucket(self,bucket_name):
return self.bucket_manager.stop_bucket(bucket_name)

def start_jupyter(self,bucket_name,local,container,lab=True):
return self.bucket_manager.start_jupyter(bucket_name,local,container,lab=lab)
def start_jupyter(self,bucket_name,local,container):
return self.bucket_manager.start_jupyter(bucket_name,local,container)

def stop_jupyter(self,bucket_name):
return self.bucket_manager.stop_jupyter(bucket_name)

def _get_config_dir(self):
appname = 'resen'
Expand Down Expand Up @@ -187,6 +190,9 @@ def create_bucket(self,bucket_name):
params['docker']['port'] = list()
params['docker']['storage'] = list()
params['docker']['status'] = None
params['docker']['jupyter'] = dict()
params['docker']['jupyter']['token'] = None
params['docker']['jupyter']['port'] = None

# now add the new bucket to the self.buckets config and then update the config file
self.buckets.append(params)
Expand Down Expand Up @@ -523,7 +529,7 @@ def execute_command(self,bucket_name,command,detach=True):

if bucket['docker']['status'] in ['running']:
# then we can start the container and update status
result = self.dockerhelper.execute_command(bucket['docker']['container'],command)
result = self.dockerhelper.execute_command(bucket['docker']['container'],command,detach=detach)
status, output = result
if (detach and status is None) or (not detach and status==0):
return True
Expand All @@ -535,49 +541,101 @@ def execute_command(self,bucket_name,command,detach=True):
print('ERROR: Bucket %s is not running!' % (bucket['bucket']['name']))
return False

def start_jupyter(self,bucket_name,local_port,container_port,lab=True):
def start_jupyter(self,bucket_name,local_port,container_port):
if not bucket_name in self.bucket_names:
print("ERROR: Bucket with name: %s does not exist!" % bucket_name)
return False

if lab:
style = 'lab'
else:
style = 'notebook'

token = '%048x' % random.randrange(16**48)
ind = self.bucket_names.index(bucket_name)
bucket = self.buckets[ind]
pid = self.get_jupyter_pid(bucket['docker']['container'])

command = "bash -cl 'source activate py36 && jupyter %s --no-browser --ip 0.0.0.0 --port %s --NotebookApp.token=%s --KernelSpecManager.ensure_native_kernel=False'"
command = command % (style, container_port, token)
if not pid is None:
port = bucket['docker']['jupyter']['port']
token = bucket['docker']['jupyter']['token']
url = 'http://localhost:%s/?token=%s' % (port,token)
print("Jupyter lab is already running and can be accessed in a browser at: %s" % (url))
return True


token = '%048x' % random.randrange(16**48)
command = "bash -cl 'source activate py36 && jupyter lab --no-browser --ip 0.0.0.0 --port %s --NotebookApp.token=%s --KernelSpecManager.ensure_native_kernel=False'"
command = command % (container_port, token)

status = self.execute_command(bucket_name,command,detach=True)
if status == False:
return False
time.sleep(0.1)

# now check that jupyter is running
self.update_bucket_statuses()
ind = self.bucket_names.index(bucket_name)
bucket = self.buckets[ind]
result = self.dockerhelper.execute_command(bucket['docker']['container'],'ps -ef',detach=False)
output = result[1].decode('utf-8').split('\n')
pid = self.get_jupyter_pid(bucket['docker']['container'])

pid = None
for line in output:
if 'jupyter' in line and token in line:
parsed_line = [x for x in line.split(' ') if x != '']
pid = parsed_line[1]
break

if pid is not None:
self.buckets[ind]['docker']['jupyter']['token'] = token
self.buckets[ind]['docker']['jupyter']['port'] = local_port
self.save_config()
url = 'http://localhost:%s/?token=%s' % (local_port,token)
print("Jupyter %s can be accessed in a browser at: %s" % (style, url))
print("Jupyter lab can be accessed in a browser at: %s" % (url))
time.sleep(3)
webbrowser.open(url)
return True
else:
print("ERROR: Failed to start jupyter server!")
return False

def stop_jupyter(self,bucket_name):
if not bucket_name in self.bucket_names:
print("ERROR: Bucket with name: %s does not exist!" % bucket_name)
return False

ind = self.bucket_names.index(bucket_name)
bucket = self.buckets[ind]
if not bucket['docker']['status'] in ['running']:
return True

pid = self.get_jupyter_pid(bucket['docker']['container'])
if pid is None:
return True

port = bucket['docker']['jupyter']['port']
python_cmd = 'from notebook.notebookapp import shutdown_server, list_running_servers; '
python_cmd += 'svrs = [x for x in list_running_servers() if x[\\\"port\\\"] == %s]; ' % (port)
python_cmd += 'sts = True if len(svrs) == 0 else shutdown_server(svrs[0]); print(sts)'
command = "bash -cl '/home/jovyan/envs/py36/bin/python -c \"%s \"'" % (python_cmd)
status = self.execute_command(bucket_name,command,detach=False)

self.update_bucket_statuses()

# now verify it is dead
pid = self.get_jupyter_pid(bucket['docker']['container'])
if not pid is None:
print("ERROR: Failed to stop jupyter lab.")
return False

self.buckets[ind]['docker']['jupyter']['token'] = None
self.buckets[ind]['docker']['jupyter']['port'] = None
self.save_config()

return True

def get_jupyter_pid(self,container):

result = self.dockerhelper.execute_command(container,'ps -ef',detach=False)
if result == False:
return None

output = result[1].decode('utf-8').split('\n')

pid = None
for line in output:
if ('jupyter-lab' in line or 'jupyter lab' in line) and '--no-browser --ip 0.0.0.0' in line:
parsed_line = [x for x in line.split(' ') if x != '']
pid = parsed_line[1]
break

return pid

def update_bucket_statuses(self):
for i,bucket in enumerate(self.buckets):
container_id = bucket['docker']['container']
Expand Down
Loading