diff --git a/lwp.conf b/lwp.conf index d0a99d2..75f58e6 100644 --- a/lwp.conf +++ b/lwp.conf @@ -11,3 +11,7 @@ time = 10 [overview] partition = / + +[attach] +enable=True +options=--cgi=35000-35009 --localhost-only -t diff --git a/lwp.py b/lwp.py index eee5ff7..a578475 100644 --- a/lwp.py +++ b/lwp.py @@ -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 @@ -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__) @@ -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') @@ -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: diff --git a/lxclite/__init__.py b/lxclite/__init__.py index 2dc6cc0..9a16ad7 100644 --- a/lxclite/__init__.py +++ b/lxclite/__init__.py @@ -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 diff --git a/templates/index.html b/templates/index.html index bbc1c86..f2d7633 100644 --- a/templates/index.html +++ b/templates/index.html @@ -61,6 +61,7 @@

{{ dist }} (host)

{% set start_action = {'stopped':'start','frozen':'unfreeze'} %} Start Stop + {% if attach == True %} Attach{% endif %} Freeze {% if session.su == 'Yes' %}{% endif %}