Skip to content
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.

Attach to a container using shellinabox #104

Open
wants to merge 1 commit into
base: 0.2
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
4 changes: 4 additions & 0 deletions lwp.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ time = 10

[overview]
partition = /

[attach]
enable=True
options=--cgi=35000-35009 --localhost-only -t
24 changes: 22 additions & 2 deletions lwp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import os

from flask import Flask, request, session, g, redirect, url_for, abort, \
render_template, flash, jsonify
render_template, flash, jsonify, make_response

try:
import configparser
Expand All @@ -55,6 +55,8 @@
ADDRESS = config.get('global', 'address')
PORT = int(config.get('global', 'port'))

ATT_ENABLE = config.getboolean('attach', 'enable')
lxc.ATT_OPTS = config.get('attach', 'options')

# Flask app
app = Flask(__name__)
Expand Down Expand Up @@ -117,7 +119,8 @@ def home():
return render_template('index.html', containers=lxc.ls(),
containers_all=containers_all,
dist=lwp.check_ubuntu(),
templates=lwp.get_templates_list())
templates=lwp.get_templates_list(),
attach=ATT_ENABLE)
return render_template('login.html')


Expand Down Expand Up @@ -602,6 +605,23 @@ def action():
flash(u'Unable to start %s!' % name, 'error')
except lxc.ContainerAlreadyRunning:
flash(u'Container %s is already running!' % name, 'error')
elif action == 'attach':
try:
answer = lxc.attach(name)
answer = answer.split('\n')
header = []
for s in answer:
if s.strip() == "":
break
header.append(s.strip())
answer = answer[len(header)+1:]
resp = make_response("\n".join(answer))
for s in header:
a=s.split(':')
resp.headers[a[0].strip()]=a[1].strip()
return resp
except lxc.ContainerNotRunning:
flash(u'Container %s is not running!' % name, 'error')
elif action == 'stop':
try:
if lxc.stop(name) == 0:
Expand Down
21 changes: 21 additions & 0 deletions lxclite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,27 @@ def stop(container):
return _run('lxc-stop -n {}'.format(container))


# Default options for shellinabox call
#
ATT_OPTS = "--cgi -t"
ATT_USER = ":root:root:/:"

def attach(container):
'''
attaches a container
'''

if not exists(container):
raise ContainerDoesntExists(
'Container {} does not exists!'.format(container))

if container in stopped():
raise ContainerNotRunning(
'Container {} is not running!'.format(container))

return _run("/usr/bin/shellinaboxd {} -s '/{}/usr/bin/lxc-attach -n {}'".format(ATT_OPTS, ATT_USER, container), True)


def freeze(container):
'''
Freezes a container
Expand Down
1 change: 1 addition & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ <h3>{{ dist }} (host)</h3>
{% set start_action = {'stopped':'start','frozen':'unfreeze'} %}
<a class="btn btn-small{% if status.status == 'running' %} disabled{% endif %}"{% if status.status == 'stopped' or status.status == 'frozen' %} href="{{ url_for('action', name=container.name, action=start_action[status.status], token=session.token) }}"{% endif %}><i class="icon-play"></i> Start</a>
<a class="btn btn-small{% if status.status == 'stopped' %} disabled{% endif %}"{% if status.status == 'running' or status.status == 'frozen' %} href="{{ url_for('action', name=container.name, action='stop', token=session.token) }}"{% endif %}><i class="icon-stop"></i> Stop</a>
{% if attach == True %}<a target="_blank" class="btn btn-small{% if status.status == 'stopped' %} disabled{% endif %}"{% if status.status == 'running' %} href="{{ url_for('action', name=container.name, action='attach', token=session.token) }}"{% endif %}><i class="icon-circle-arrow-right"></i> Attach</a>{% endif %}
<a class="btn btn-small{% if status.status == 'frozen' or status.status == 'stopped' %} disabled{% endif %}"{% if status.status == 'running' %} href="{{ url_for('action', name=container.name, action='freeze', token=session.token) }}"{% endif %}><i class="icon-pause"></i> Freeze</a>
</div>
{% if session.su == 'Yes' %}<a class="pull-right close destroy" data-container-name="{{container.name}}" style="margin-top:4px;"><i class="icon-remove-sign"></i></a>{% endif %}
Expand Down