Skip to content

Commit

Permalink
add suspend and resume methods to container view and urls
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Glatthard committed Aug 11, 2015
1 parent 0df0ed8 commit cb0957a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
1 change: 0 additions & 1 deletion ipynbsrv/web/templates/web/snippets/container_grid.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
{% if ct.is_running %}
{% if ct.is_suspended %}
<div class="panel panel-warning panel-container">
}
{% else %}
<div class="panel panel-success panel-container">
}
Expand Down
2 changes: 2 additions & 0 deletions ipynbsrv/web/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
url(r'^container/restart$', 'ipynbsrv.web.views.containers.restart', name='container_restart'),
url(r'^container/start$', 'ipynbsrv.web.views.containers.start', name='container_start'),
url(r'^container/stop$', 'ipynbsrv.web.views.containers.stop', name='container_stop'),
url(r'^container/suspend$', 'ipynbsrv.web.views.containers.suspend', name='container_suspend'),
url(r'^container/resume$', 'ipynbsrv.web.views.containers.resume', name='container_resume'),

# # /images(s)/...
url(r'^images/$', 'ipynbsrv.web.views.images.index', name='images'),
Expand Down
72 changes: 69 additions & 3 deletions ipynbsrv/web/views/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def restart(request):
if container:
try:
client.containers(ct_id).restart.post()
messages.success(request, "Container is restarting.")
messages.success(request, "Container is now restarting.")
except Exception as e:
messages.error(request, api_error_message(e, ""))
else:
Expand Down Expand Up @@ -221,7 +221,7 @@ def start(request):
if container:
try:
client.containers(ct_id).start.post()
messages.success(request, "Container is starting.")
messages.success(request, "Container is now starting up.")
except Exception as e:
messages.error(request, api_error_message(e, ""))
else:
Expand Down Expand Up @@ -254,7 +254,73 @@ def stop(request):
if container:
try:
client.containers(ct_id).stop.post()
messages.success(request, "Container stopped successfully.")
messages.success(request, "Container is shutting down.")
except Exception as e:
messages.error(request, api_error_message(e, ""))
else:
messages.error(request, "Container does not exist.")

return redirect('containers')


@user_passes_test(login_allowed)
def suspend(request):
"""
Suspend the container.
"""
if request.method != "POST":
messages.error(request, "Invalid request method.")
return redirect('containers')
if 'id' not in request.POST:
messages.error(request, "Invalid POST request.")
return redirect('containers')

ct_id = int(request.POST.get('id'))

client = get_httpclient_instance(request)

try:
container = client.containers(ct_id).get()
except HttpNotFoundError:
messages.error(request, "Container does not exist.")

if container:
try:
client.containers(ct_id).suspend.post()
messages.success(request, "Container is going into suspended mode.")
except Exception as e:
messages.error(request, api_error_message(e, ""))
else:
messages.error(request, "Container does not exist.")

return redirect('containers')


@user_passes_test(login_allowed)
def resume(request):
"""
Resume the container
"""
if request.method != "POST":
messages.error(request, "Invalid request method.")
return redirect('containers')
if 'id' not in request.POST:
messages.error(request, "Invalid POST request.")
return redirect('containers')

ct_id = int(request.POST.get('id'))

client = get_httpclient_instance(request)

try:
container = client.containers(ct_id).get()
except HttpNotFoundError:
messages.error(request, "Container does not exist.")

if container:
try:
client.containers(ct_id).resume.post()
messages.success(request, "Container is resuming.")
except Exception as e:
messages.error(request, api_error_message(e, ""))
else:
Expand Down

0 comments on commit cb0957a

Please sign in to comment.