Example #1
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),
        })
Example #2
0
def edit(container=None):
    '''
    edit containers page and actions if form post request
    '''
    if 'logged_in' in session:
        host_memory = lwp.host_memory_usage()
        
        info = lxc.info(container)

        if request.method == 'POST':
            cfg = lwp.get_container_settings(container)
            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]?)(/(3[0-2]|[12]?[0-9]))?'

            form = {}
            form['type'] = request.form['type']
            form['link'] = request.form['link']
            try:
                form['flags'] = request.form['flags']
            except KeyError:
                form['flags'] = 'down'
            form['hwaddr'] = request.form['hwaddress']
            form['rootfs'] = request.form['rootfs']
            form['utsname'] = request.form['hostname']
            form['ipv4'] = request.form['ipaddress']
            form['memlimit'] = request.form['memlimit']
            form['swlimit'] = request.form['swlimit']
            form['cpus'] = request.form['cpus']
            form['shares'] = request.form['cpushares']
            try:
                form['autostart'] = request.form['autostart']
            except KeyError:
                form['autostart'] = False


            form['priority'] = request.form['priority']
            if form['priority'].isdigit():
                form['priority'] = int(form['priority'])
            else:
                form['priority'] = ''


            if form['utsname'] != cfg['utsname'] and re.match('(?!^containers$)|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$', form['utsname']):
                lwp.push_config_value('lxc.utsname', form['utsname'], container=container)
                flash(u'Hostname updated for %s!' % container, 'success')

            if form['flags'] != cfg['flags'] and re.match('^(up|down)$', form['flags']):
                lwp.push_config_value('lxc.network.flags', form['flags'], container=container)
                flash(u'Network flag updated for %s!' % container, 'success')

            if form['type'] != cfg['type'] and re.match('^\w+$', form['type']):
                lwp.push_config_value('lxc.network.type', form['type'], container=container)
                flash(u'Link type updated for %s!' % container, 'success')

            if form['link'] != cfg['link'] and re.match('^[\w\d]+$', form['link']):
                lwp.push_config_value('lxc.network.link', form['link'], container=container)
                flash(u'Link name updated for %s!' % container, 'success')

            if form['hwaddr'] != cfg['hwaddr'] and re.match('^([a-fA-F0-9]{2}[:|\-]?){6}$', form['hwaddr']):
                lwp.push_config_value('lxc.network.hwaddr', form['hwaddr'], container=container)
                flash(u'Hardware address updated for %s!' % container, 'success')

            if ( not form['ipv4'] and form['ipv4'] != cfg['ipv4'] ) or ( form['ipv4'] != cfg['ipv4'] and re.match('^%s$' % ip_regex, form['ipv4']) ):
                lwp.push_config_value('lxc.network.ipv4', form['ipv4'], container=container)
                flash(u'IP address updated for %s!' % container, 'success')

            if form['memlimit'] != cfg['memlimit'] and form['memlimit'].isdigit() and int(form['memlimit']) <= int(host_memory['total']):
                if int(form['memlimit']) == int(host_memory['total']):
                    form['memlimit'] = ''

                if form['memlimit'] != cfg['memlimit']:
                    lwp.push_config_value('lxc.cgroup.memory.limit_in_bytes', form['memlimit'], container=container)
                    if info["state"].lower() != "stopped":
                        lxc.cgroup(container, 'lxc.cgroup.memory.limit_in_bytes', int(form['memlimit'])*1024*1024)
                    flash(u'Memory limit updated for %s!' % container, 'success')

            if form['swlimit'] != cfg['swlimit'] and form['swlimit'].isdigit() and int(form['swlimit']) <= int(host_memory['total'] * 2):
                if int(form['swlimit']) == int(host_memory['total'] * 2):
                    form['swlimit'] = ''

                if form['swlimit'].isdigit(): form['swlimit'] = int(form['swlimit'])
                if form['memlimit'].isdigit(): form['memlimit'] = int(form['memlimit'])

                if ( form['memlimit'] == '' and form['swlimit'] != '' ) or ( form['memlimit'] > form['swlimit'] and form['swlimit'] != '' ):
                    flash(u'Can\'t assign swap memory lower than the memory limit', 'warning')

                elif form['swlimit'] != cfg['swlimit'] and form['memlimit'] <= form['swlimit']:
                    lwp.push_config_value('lxc.cgroup.memory.memsw.limit_in_bytes', form['swlimit'], container=container)
                    if info["state"].lower() != "stopped":
                        lxc.cgroup(container, 'lxc.cgroup.memory.memsw.limit_in_bytes', int(form['swlimit'])*1024*1024)
                    flash(u'Swap limit updated for %s!' % container, 'success')

            if ( not form['cpus'] and form['cpus'] != cfg['cpus'] ) or ( form['cpus'] != cfg['cpus'] and re.match('^[0-9,-]+$', form['cpus']) ):
                lwp.push_config_value('lxc.cgroup.cpuset.cpus', form['cpus'], container=container)
                if info["state"].lower() != "stopped":
                    lxc.cgroup(container, 'lxc.cgroup.cpuset.cpus', form['cpus'])
                flash(u'CPUs updated for %s!' % container, 'success')

            if ( not form['shares'] and form['shares'] != cfg['shares'] ) or ( form['shares'] != cfg['shares'] and re.match('^[0-9]+$', form['shares']) ):
                lwp.push_config_value('lxc.cgroup.cpu.shares', form['shares'], container=container)
                if info["state"].lower() != "stopped":
                    lxc.cgroup(container, 'lxc.cgroup.cpu.shares', form['shares'])
                flash(u'CPU shares updated for %s!' % container, 'success')

            if form['rootfs'] != cfg['rootfs'] and re.match('^[a-zA-Z0-9_/\-]+', form['rootfs']):
                lwp.push_config_value('lxc.rootfs', form['rootfs'], container=container)
                flash(u'Rootfs updated!' % container, 'success')

            auto = lwp.ls_auto()
            if form['autostart'] == 'True' and (not container in auto or auto[container] != form['priority']):
                try:
                    if container in auto and auto[container]:
                        old_conf = '/etc/lxc/auto/%06d-%s.conf' % (auto[container], container,)
                        if os.path.exists(old_conf):
                            os.remove(old_conf)
                            flash(u'Autostart for %s: old priority dropped' % container, 'success')
                    else:
                        old_conf = '/etc/lxc/auto/%s.conf' % (container,)
                        if os.path.exists(old_conf):
                            os.remove(old_conf)
                            flash(u'Autostart for %s: default priority dropped' % container, 'success')

                    if form['priority']:
                        new_conf = '/etc/lxc/auto/%06d-%s.conf' % (form['priority'], container, )
                        flash(u'Autostart for %s: set new priority' % container, 'success')
                    else:
                        new_conf = '/etc/lxc/auto/%s.conf' % (container,)

                    os.symlink('/var/lib/lxc/%s/config' % container, new_conf)
                    flash(u'Autostart enabled for %s' % container, 'success')
                except OSError:
                    flash(u'Unable to create symlink \'/etc/lxc/auto/%s.conf\'' % container, 'error')
            elif not form['autostart'] and container in auto:
                try:
                    if auto[container]:
                        old_conf = '/etc/lxc/auto/%06d-%s.conf' % (auto[container], container, )
                    else:
                        old_conf = '/etc/lxc/auto/%s.conf' % (container, )
                    os.remove(old_conf)
                    flash(u'Autostart disabled for %s' % container, 'success')
                except OSError:
                    flash(u'Unable to remove symlink', 'error')


        info = lxc.info(container)
        status = info['state']
        pid = info['pid']

        infos = {
            'status': status,
            'pid': pid,
            'memusg': lwp.memory_usage(container),
            'cpu': lwp.container_cpu_percent(container),
            'max_memusg': lwp.max_memory_usage(container),
            'diskusg': lwp.get_filesystem_usage(container)
        }

        settings = lwp.get_container_settings(container)
        settings["ipv4_hint"] = 'Undefined'
        if settings["ipv4_real"]:
            settings["ipv4_hint"] = settings["ipv4"]
            settings["ipv4"] = ''

        # Align value to slider step
        host_memory["total"] -= host_memory["total"] % 2

        return render_template('edit.html',
                               containers=lxc.ls(),
                               container=container,
                               infos=infos,
                               settings=settings,
                               host_memory=host_memory)
    return render_template('login.html')