Example #1
0
    def container_action(self):
        """
        Apply container action.

        e.g, start, stop, freeze a container.
        """
        container = self.request.matchdict["container"]

        try:
            data = self.request.json_body
        except ValueError:
            self.request.response.status = 400
            return {"status": "error", "error": "Bad Request"}

        if data is None:
            self.request.response.status = 400
            return {"status": "error", "error": "Bad Request"}

        status = data["action"]
        try:
            if status == "stop":
                lxc.stop(container)
                return {"status": "ok"}
            elif status == "start":
                lxc.start(container)
                return {"status": "ok"}
            elif status == "freeze":
                lxc.freeze(container)
                return {"status": "ok"}

            self.request.response.status = 400
            return {"status": "error", "error": "Bad Request"}
        except lxc.ContainerDoesNotExist:
            self.request.response.status = 404
            return {"status": "error", "error": "Container doesn't exist"}
Example #2
0
def post_container(name):
    data = request.get_json(force=True)
    if data is None:
        return jsonify(status="error", error="Bad request"), 400

    status = data['action']
    try:
        if status == "stop":
            lxc.stop(name)
            return jsonify(status="ok"), 200
        elif status == "start":
            lxc.start(name)
            return jsonify(status="ok"), 200
        elif status == "freeze":
            lxc.freeze(name)
            return jsonify(status="ok"), 200

        return jsonify(status="error", error="Bad request"), 400
    except lxc.ContainerDoesntExists:
        return jsonify(status="error", error="Container doesn' t exists"), 409
Example #3
0
def post_container(name):
    data = request.get_json(force=True)
    if data is None:
        return jsonify(status="error", error="Bad request"), 400

    status = data['action']
    try:
        if status == "stop":
            lxc.stop(name)
            return jsonify(status="ok"), 200
        elif status == "start":
            lxc.start(name)
            return jsonify(status="ok"), 200
        elif status == "freeze":
            lxc.freeze(name)
            return jsonify(status="ok"), 200

        return jsonify(status="error", error="Bad request"), 400
    except lxc.ContainerDoesntExists:
        return jsonify(status="error", error="Container doesn' t exists"), 409
Example #4
0
    def stop_container(self):
        """
        Stop LXC container.
        """
        try:
            if lxc.stop(self.container) == 0:
                self.session.flash('Container {} stopped successfully'.format(self.container), queue='success')
            else:
                self.session.flash('Unable to stop {}'.format(self.container), queue='error')
        except lxc.ContainerNotRunning:
            self.session.flash('Container {} is already stopped'.format(self.container), queue='error')

        return self._return_from_action()
Example #5
0
 def test_05_stop(self):
     lxc.stop('test00')
Example #6
0
def action():
    """
    manage all actions related to containers
    lxc-start, lxc-stop, etc...
    """
    act = request.args['action']
    name = request.args['name']

    # TODO: refactor this method, it's horrible to read
    if act == 'start':
        try:
            if lxc.start(name) == 0:
                time.sleep(
                    1
                )  # Fix bug : "the container is randomly not displayed in overview list after a boot"
                flash(u'Container %s started successfully!' % name, 'success')
            else:
                flash(u'Unable to start %s!' % name, 'error')
        except lxc.ContainerAlreadyRunning:
            flash(u'Container %s is already running!' % name, 'error')
    elif act == 'stop':
        try:
            if lxc.stop(name) == 0:
                flash(u'Container %s stopped successfully!' % name, 'success')
            else:
                flash(u'Unable to stop %s!' % name, 'error')
        except lxc.ContainerNotRunning:
            flash(u'Container %s is already stopped!' % name, 'error')
    elif act == 'freeze':
        try:
            if lxc.freeze(name) == 0:
                flash(u'Container %s frozen successfully!' % name, 'success')
            else:
                flash(u'Unable to freeze %s!' % name, 'error')
        except lxc.ContainerNotRunning:
            flash(u'Container %s not running!' % name, 'error')
    elif act == 'unfreeze':
        try:
            if lxc.unfreeze(name) == 0:
                flash(u'Container %s unfrozen successfully!' % name, 'success')
            else:
                flash(u'Unable to unfeeze %s!' % name, 'error')
        except lxc.ContainerNotRunning:
            flash(u'Container %s not frozen!' % name, 'error')
    elif act == 'destroy':
        if session['su'] != 'Yes':
            return abort(403)
        try:
            if lxc.destroy(name) == 0:
                flash(u'Container %s destroyed successfully!' % name,
                      'success')
            else:
                flash(u'Unable to destroy %s!' % name, 'error')
        except lxc.ContainerDoesntExists:
            flash(u'The Container %s does not exists!' % name, 'error')
    elif act == 'reboot' and name == 'host':
        if session['su'] != 'Yes':
            return abort(403)
        msg = '\v*** LXC Web Panel *** \
                \nReboot from web panel'

        try:
            subprocess.check_call('/sbin/shutdown -r now \'%s\'' % msg,
                                  shell=True)
            flash(u'System will now restart!', 'success')
        except subprocess.CalledProcessError:
            flash(u'System error!', 'error')
    elif act == 'push':
        # TODO: implement push action
        pass
    try:
        if request.args['from'] == 'edit':
            return redirect('../%s/edit' % name)
        else:
            return redirect(url_for('main.home'))
    except KeyError:
        return redirect(url_for('main.home'))
Example #7
0
 def test_05_stop(self):
     lxc.stop('test00')
Example #8
0
def action():
    """
    manage all actions related to containers
    lxc-start, lxc-stop, etc...
    """
    act = request.args['action']
    name = request.args['name']

    # TODO: refactor this method, it's horrible to read
    if act == 'start':
        try:
            if lxc.start(name) == 0:
                time.sleep(1)  # Fix bug : "the container is randomly not displayed in overview list after a boot"
                flash(u'Container %s started successfully!' % name, 'success')
            else:
                flash(u'Unable to start %s!' % name, 'error')
        except lxc.ContainerAlreadyRunning:
            flash(u'Container %s is already running!' % name, 'error')
    elif act == 'stop':
        try:
            if lxc.stop(name) == 0:
                flash(u'Container %s stopped successfully!' % name, 'success')
            else:
                flash(u'Unable to stop %s!' % name, 'error')
        except lxc.ContainerNotRunning:
            flash(u'Container %s is already stopped!' % name, 'error')
    elif act == 'freeze':
        try:
            if lxc.freeze(name) == 0:
                flash(u'Container %s frozen successfully!' % name, 'success')
            else:
                flash(u'Unable to freeze %s!' % name, 'error')
        except lxc.ContainerNotRunning:
            flash(u'Container %s not running!' % name, 'error')
    elif act == 'unfreeze':
        try:
            if lxc.unfreeze(name) == 0:
                flash(u'Container %s unfrozen successfully!' % name, 'success')
            else:
                flash(u'Unable to unfeeze %s!' % name, 'error')
        except lxc.ContainerNotRunning:
            flash(u'Container %s not frozen!' % name, 'error')
    elif act == 'destroy':
        if session['su'] != 'Yes':
            return abort(403)
        try:
            if lxc.destroy(name) == 0:
                flash(u'Container %s destroyed successfully!' % name, 'success')
            else:
                flash(u'Unable to destroy %s!' % name, 'error')
        except lxc.ContainerDoesntExists:
            flash(u'The Container %s does not exists!' % name, 'error')
    elif act == 'reboot' and name == 'host':
        if session['su'] != 'Yes':
            return abort(403)
        msg = '\v*** LXC Web Panel *** \
                \nReboot from web panel'
        try:
            subprocess.check_call('/sbin/shutdown -r now \'%s\'' % msg, shell=True)
            flash(u'System will now restart!', 'success')
        except subprocess.CalledProcessError:
            flash(u'System error!', 'error')
    elif act == 'push':
        # TODO: implement push action
        pass
    try:
        if request.args['from'] == 'edit':
            return redirect(url_for('main.edit', container=name))
        else:
            return redirect(url_for('main.home'))
    except KeyError:
        return redirect(url_for('main.home'))
Example #9
0
def action():
    """
    manage all actions related to containers
    lxc-start, lxc-stop, etc...
    """
    act = request.args["action"]
    name = request.args["name"]

    # TODO: refactor this method, it's horrible to read
    if act == "start":
        try:
            if lxc.start(name) == 0:
                time.sleep(1)  # Fix bug : "the container is randomly not displayed in overview list after a boot"
                flash(u"Container %s started successfully!" % name, "success")
            else:
                flash(u"Unable to start %s!" % name, "error")
        except lxc.ContainerAlreadyRunning:
            flash(u"Container %s is already running!" % name, "error")
    elif act == "stop":
        try:
            if lxc.stop(name) == 0:
                flash(u"Container %s stopped successfully!" % name, "success")
            else:
                flash(u"Unable to stop %s!" % name, "error")
        except lxc.ContainerNotRunning:
            flash(u"Container %s is already stopped!" % name, "error")
    elif act == "freeze":
        try:
            if lxc.freeze(name) == 0:
                flash(u"Container %s frozen successfully!" % name, "success")
            else:
                flash(u"Unable to freeze %s!" % name, "error")
        except lxc.ContainerNotRunning:
            flash(u"Container %s not running!" % name, "error")
    elif act == "unfreeze":
        try:
            if lxc.unfreeze(name) == 0:
                flash(u"Container %s unfrozen successfully!" % name, "success")
            else:
                flash(u"Unable to unfeeze %s!" % name, "error")
        except lxc.ContainerNotRunning:
            flash(u"Container %s not frozen!" % name, "error")
    elif act == "destroy":
        if session["su"] != "Yes":
            return abort(403)
        try:
            if lxc.destroy(name) == 0:
                flash(u"Container %s destroyed successfully!" % name, "success")
            else:
                flash(u"Unable to destroy %s!" % name, "error")
        except lxc.ContainerDoesntExists:
            flash(u"The Container %s does not exists!" % name, "error")
    elif act == "reboot" and name == "host":
        if session["su"] != "Yes":
            return abort(403)
        msg = "\v*** LXC Web Panel *** \
                \nReboot from web panel"
        try:
            subprocess.check_call("/sbin/shutdown -r now '%s'" % msg, shell=True)
            flash(u"System will now restart!", "success")
        except subprocess.CalledProcessError:
            flash(u"System error!", "error")
    elif act == "push":
        # TODO: implement push action
        pass
    try:
        if request.args["from"] == "edit":
            return redirect(url_for("main.edit", container=name))
        else:
            return redirect(url_for("main.home"))
    except KeyError:
        return redirect(url_for("main.home"))