Exemplo n.º 1
0
def refresh_cpu_containers(name=None):
    if 'logged_in' in session:
        if name == 'containers':
            containers_running = lxc.running()
            containers = lwp.containers_cpu_percent_cgroup(containers_running)
            return jsonify(data=containers.values())
        elif name == 'host':
            return lwp.host_cpu_percent()
        return jsonify({
            'cpu': lwp.container_cpu_percent_cgroup(name),
        })
Exemplo n.º 2
0
def refresh_memory_containers(name=None):
    if name == 'containers':
        containers_running = lxc.running()
        containers = []
        for container in containers_running:
            container = container.replace(' (auto)', '')
            containers.append({'name': container, 'memusg': lwp.memory_usage(container), 'settings': lwp.get_container_settings(container)})
        return jsonify(data=containers)
    elif name == 'host':
        return jsonify(lwp.host_memory_usage())
    return jsonify({'memusg': lwp.memory_usage(name)})
Exemplo n.º 3
0
def refresh_memory_containers(name=None):
    if 'logged_in' in session:
        if name == 'containers':
            containers_running = lxc.running()
            containers = []
            for container in containers_running:
                container = container.replace(' (auto)', '')
                containers.append({'name': container,
                                   'memusg': lwp.memory_usage(container)})
            return jsonify(data=containers)
        elif name == 'host':
            return jsonify(lwp.host_memory_usage())
        return jsonify({'memusg': lwp.memory_usage(name)})
Exemplo n.º 4
0
def lxc_net():
    if 'logged_in' in session:
        if session['su'] != 'Yes':
            return abort(403)

        if request.method == 'POST':
            if lxc.running() == []:
                try:
                    if request.form['status'] == 'Enable':
                        lwp.push_net_value('USE_LXC_BRIDGE', 'true')
                        if lwp.net_restart() == 0:
                            flash(u'LXC Networking enabled successfully!', 'success')
                        else:
                            flash(u'Failed to restart LXC networking.', 'error')
                    elif request.form['status'] == 'Disable':
                        lwp.push_net_value('USE_LXC_BRIDGE', 'false')
                        if lwp.net_restart() == 0:
                            flash(u'LXC Networking disabled successfully!', 'success')
                        else:
                            flash(u'Failed to restart LXC networking.', 'error')
                except KeyError:
                    cfg = lwp.get_net_settings()
                    ip_regex = '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
                    if request.form['bridge'] != cfg['bridge'] and re.match('^\w+$', request.form['bridge']):
                        lwp.push_net_value('LXC_BRIDGE', request.form['bridge'])

                    if request.form['address'] != cfg['address'] and re.match('^%s$' % ip_regex, request.form['address']):
                        lwp.push_net_value('LXC_ADDR', request.form['address'])

                    if request.form['netmask'] != cfg['netmask'] and re.match('^%s$' % ip_regex, request.form['netmask']):
                        lwp.push_net_value('LXC_NETMASK', request.form['netmask'])

                    if request.form['network'] != cfg['network'] and re.match('^%s(?:/\d{1,2}|)$' % ip_regex, request.form['network']):
                        lwp.push_net_value('LXC_NETWORK', request.form['network'])

                    if request.form['range'] != cfg['range'] and re.match('^%s,%s$' % (ip_regex, ip_regex), request.form['range']):
                        lwp.push_net_value('LXC_DHCP_RANGE', request.form['range'])

                    if request.form['max'] != cfg['max'] and re.match('^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$', request.form['max']):
                        lwp.push_net_value('LXC_DHCP_MAX', request.form['max'])
                    if lwp.net_restart() == 0:
                        flash(u'LXC Network settings applied successfully!', 'success')
                    else:
                        flash(u'Failed to restart LXC networking.', 'error')
            else:
                flash(u'Stop all containers before restart lxc-net.', 'warning')
        return render_template('lxc-net.html', containers=lwp.ls(), cfg=lwp.get_net_settings(), running=lxc.running())
    return render_template('login.html')
Exemplo n.º 5
0
def lxc_net():
    '''
    lxc-net (/etc/default/lxc) settings page and actions if form post request
    '''
    if session['su'] != 'Yes':
        return abort(403)

    if request.method == 'POST':
        if lxc.running() == []:
            cfg = lwp.get_net_settings()
            ip_regex = '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'

            form = {}
            for key in ['bridge', 'address', 'netmask', 'network', 'range', 'max']:
                form[key] = request.form.get(key, None)
            form['use'] = request.form.get('use', None)


            if form['use'] != cfg['use']:
                lwp.push_net_value('USE_LXC_BRIDGE', 'true' if form['use'] else 'false')

            if form['bridge'] and form['bridge'] != cfg['bridge'] and re.match('^[a-zA-Z0-9_-]+$', form['bridge']):
                lwp.push_net_value('LXC_BRIDGE', form['bridge'])

            if form['address'] and form['address'] != cfg['address'] and re.match('^%s$' % ip_regex, form['address']):
                lwp.push_net_value('LXC_ADDR', form['address'])

            if form['netmask'] and form['netmask'] != cfg['netmask'] and re.match('^%s$' % ip_regex, form['netmask']):
                lwp.push_net_value('LXC_NETMASK', form['netmask'])

            if form['network'] and form['network'] != cfg['network'] and re.match('^%s(?:/\d{1,2}|)$' % ip_regex, form['network']):
                lwp.push_net_value('LXC_NETWORK', form['network'])

            if form['range'] and form['range'] != cfg['range'] and re.match('^%s,%s$' % (ip_regex, ip_regex), form['range']):
                lwp.push_net_value('LXC_DHCP_RANGE', form['range'])

            if form['max'] and form['max'] != cfg['max'] and re.match('^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$', form['max']):
                lwp.push_net_value('LXC_DHCP_MAX', form['max'])


            if lwp.net_restart() == 0:
                flash(u'LXC Network settings applied successfully!', 'success')
            else:
                flash(u'Failed to restart LXC networking.', 'error')
        else:
            flash(u'Stop all containers before restart lxc-net.', 'warning')
    return render_template('lxc-net.html', containers=lxc.ls(), cfg=lwp.get_net_settings(), running=lxc.running())
Exemplo n.º 6
0
def refresh_disk_containers(name=None):
    if 'logged_in' in session:
        if name == 'containers':
            containers_running = lxc.running()
            containers = []
            for container in containers_running:
                container = container.replace(' (auto)', '')
                containers.append({
                    'name': container,
                    'diskusg': lwp.get_filesystem_usage(container),
                })
            return jsonify(data=containers)
        elif name == 'host':
            return jsonify(lwp.host_disk_usage())
        return jsonify({
            'diskusg': lwp.get_filesystem_usage(name),
        })
Exemplo n.º 7
0
def lxc_net():
    '''
    lxc-net (/etc/default/lxc) settings page and actions if form post request
    '''
    if 'logged_in' in session:
        if session['su'] != 'Yes':
            return abort(403)

        if request.method == 'POST':
            if lxc.running() == []:
                cfg = lwp.get_net_settings()
                ip_regex = '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'

                form = {}
                try:
                    form['use'] = request.form['use']
                except KeyError:
                    form['use'] = 'false'

                try:
                    form['bridge'] = request.form['bridge']
                except KeyError:
                    form['bridge'] = None

                try:
                    form['address'] = request.form['address']
                except KeyError:
                    form['address'] = None

                try:
                    form['netmask'] = request.form['netmask']
                except KeyError:
                    form['netmask'] = None

                try:
                    form['network'] = request.form['network']
                except KeyError:
                    form['network'] = None

                try:
                    form['range'] = request.form['range']
                except KeyError:
                    form['range'] = None

                try:
                    form['max'] = request.form['max']
                except KeyError:
                    form['max'] = None

                if form['use'] == 'true' and form['use'] != cfg['use']:
                    lwp.push_net_value('USE_LXC_BRIDGE', 'true')

                elif form['use'] == 'false' and form['use'] != cfg['use']:
                    lwp.push_net_value('USE_LXC_BRIDGE', 'false')

                if form['bridge'] and form[
                        'bridge'] != cfg['bridge'] and re.match(
                            '^[a-zA-Z0-9_-]+$', form['bridge']):
                    lwp.push_net_value('LXC_BRIDGE', form['bridge'])

                if form['address'] and form[
                        'address'] != cfg['address'] and re.match(
                            '^%s$' % ip_regex, form['address']):
                    lwp.push_net_value('LXC_ADDR', form['address'])

                if form['netmask'] and form[
                        'netmask'] != cfg['netmask'] and re.match(
                            '^%s$' % ip_regex, form['netmask']):
                    lwp.push_net_value('LXC_NETMASK', form['netmask'])

                if form['network'] and form[
                        'network'] != cfg['network'] and re.match(
                            '^%s(?:/\d{1,2}|)$' % ip_regex, form['network']):
                    lwp.push_net_value('LXC_NETWORK', form['network'])

                if form['range'] and form['range'] != cfg['range'] and re.match(
                        '^%s,%s$' % (ip_regex, ip_regex), form['range']):
                    lwp.push_net_value('LXC_DHCP_RANGE', form['range'])

                if form['max'] and form['max'] != cfg['max'] and re.match(
                        '^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$',
                        form['max']):
                    lwp.push_net_value('LXC_DHCP_MAX', form['max'])

                if lwp.net_restart() == 0:
                    flash(u'LXC Network settings applied successfully!',
                          'success')
                else:
                    flash(u'Failed to restart LXC networking.', 'error')
            else:
                flash(u'Stop all containers before restart lxc-net.',
                      'warning')
        return render_template('lxc-net.html',
                               containers=lxc.ls(),
                               cfg=lwp.get_net_settings(),
                               running=lxc.running())
    return render_template('login.html')
Exemplo n.º 8
0
def lxc_net():
    '''
    lxc-net (/etc/default/lxc) settings page and actions if form post request
    '''
    if 'logged_in' in session:
        if session['su'] != 'Yes':
            return abort(403)
        try:
            cfg = lwp.get_net_settings()
        except lwp.LxcConfigFileNotComplete:
            cfg = []

        if request.method == 'POST': #By default the request method is GET

            if lxc.running() == []:
                ip_regex = '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'

                form = {}
                try: form['use'] = request.form['use']
                except KeyError: form['use'] = 'false'

                try: form['bridge'] = request.form['bridge']
                except KeyError: form['bridge'] = None

                try: form['address'] = request.form['address']
                except KeyError: form['address'] = None

                try: form['netmask'] = request.form['netmask']
                except KeyError: form['netmask'] = None

                try: form['network'] = request.form['network']
                except KeyError: form['network'] = None

                try: form['range'] = request.form['range']
                except KeyError: form['range'] = None

                try: form['max'] = request.form['max']
                except KeyError: form['max'] = None


                if form['use'] == 'true' and form['use'] != cfg['use']:
                    lwp.push_net_value('USE_LXC_BRIDGE', 'true')

                elif form['use'] == 'false' and form['use'] != cfg['use']:
                    lwp.push_net_value('USE_LXC_BRIDGE', 'false')

                if form['bridge'] and form['bridge'] != cfg['bridge'] and re.match('^[a-zA-Z0-9_-]+$', form['bridge']):
                    lwp.push_net_value('LXC_BRIDGE', form['bridge'])

                if form['address'] and form['address'] != cfg['address'] and re.match('^%s$' % ip_regex, form['address']):
                    lwp.push_net_value('LXC_ADDR', form['address'])

                if form['netmask'] and form['netmask'] != cfg['netmask'] and re.match('^%s$' % ip_regex, form['netmask']):
                    lwp.push_net_value('LXC_NETMASK', form['netmask'])

                if form['network'] and form['network'] != cfg['network'] and re.match('^%s(?:/\d{1,2}|)$' % ip_regex, form['network']):
                    lwp.push_net_value('LXC_NETWORK', form['network'])

                if form['range'] and form['range'] != cfg['range'] and re.match('^%s,%s$' % (ip_regex, ip_regex), form['range']):
                    lwp.push_net_value('LXC_DHCP_RANGE', form['range'])

                if form['max'] and form['max'] != cfg['max'] and re.match('^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$', form['max']):
                    lwp.push_net_value('LXC_DHCP_MAX', form['max'])


                if lwp.net_restart() == 0:
                    flash(u'LXC Network settings applied successfully!', 'success')
                else:
                    flash(u'Failed to restart LXC networking.', 'error')
            else:
                flash(u'Stop all containers before restart lxc-net.', 'warning')
        if cfg == []:
            flash(u'This is not a Ubuntu distro ! Check if all config params are set in /etc/default/lxc','warning')
            return redirect(url_for('home'))
        return render_template('lxc-net.html', containers=lxc.ls(), cfg=cfg, running=lxc.running())
    return render_template('login.html')