Exemplo n.º 1
0
def action():
    '''
    manage all actions related to containers
    lxc-start, lxc-stop, etc...
    '''
    if 'logged_in' in session:
        if request.args['token'] == session.get('token') :
            action = request.args['action']
            name = request.args['name']

            if action == '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 action == '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 action == 'shutdown':
                try:
                    if lxc.shutdown(name) == 0:
                        flash(u'Container %s shutdown initiated.' % name, 'success')
                    else:
                        flash(u'Unable to shutdown %s!' % name, 'error')
                except lxc.ContainerNotRunning:
                    flash(u'Container %s is already stopped!' % name, 'error')
            elif action == '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 action == '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 action == '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 action == '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:
                    flash(u'System error!', 'error')
        try:
            if request.args['from'] == 'edit':
                return redirect('../%s/edit' % name)
            else:
                return redirect(url_for('home'))
        except:
            return redirect(url_for('home'))
    return render_template('login.html')
Exemplo n.º 2
0
def action():
    '''
    manage all actions related to containers
    lxc-start, lxc-stop, etc...
    '''
    if 'logged_in' in session:
        if request.args['token'] == session.get('token'):
            action = request.args['action']
            name = request.args['name']

            if action == '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 action == '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 action == '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 action == '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 action == '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 action == '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:
                    flash(u'System error!', 'error')
        try:
            if request.args['from'] == 'edit':
                return redirect('../%s/edit' % name)
            else:
                return redirect(url_for('home'))
        except:
            return redirect(url_for('home'))
    return render_template('login.html')