Example #1
0
def home():
    if 'logged_in' in session:
        containers_all = []
        listx = lxc.listx()
        sorted_list = listx['RUNNING'] + listx['FROZEN'] + listx['STOPPED']
        for container in sorted_list:
            status = lxc.info(container)['state']
            containers_all.append({'status':status, 'name':container, 'memusg':lwp.memory_usage(container), 'settings':lwp.get_container_settings(container)})
        return render_template('index.html', containers=lwp.ls(), containers_all=containers_all, dist=lwp.check_ubuntu(), templates=lwp.get_templates_list())
    return render_template('login.html')
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()
        
        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]))?'
            info = lxc.info(container)
            
            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']
            try:
                form['gateway'] = request.form['gatewayaddress']
            except KeyError:
                form['gateway'] = ''
            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

            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('^[a-zA-Z0-9_-]+$', 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 ( not form['gateway'] and form['gateway'] != cfg['gateway'] ) or ( form['gateway'] != cfg['gateway'] and re.match('^%s$' % ip_regex, form['gateway']) ):
                if IPAddress(form['gateway']) in IPNetwork(form['ipv4']):
                    lwp.push_config_value('lxc.network.ipv4.gateway', form['gateway'], container=container)
                    flash(u'Gateway address updated for %s!' % container, 'success')
                else:
                    flash(u'Gateway address not in same network of ip address','error')


            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', form['memlimit'])
                    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', form['swlimit'])
                    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 ('%s.conf' % container) in auto:
                try:
                    os.symlink('/var/lib/lxc/%s/config' % container, '/etc/lxc/auto/%s.conf' % container)
                    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 ('%s.conf' % container) in auto:
                try:
                    os.remove('/etc/lxc/auto/%s.conf' % container)
                    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)}
        return render_template('edit.html', containers=lxc.ls(), container=container, infos=infos, settings=lwp.get_container_settings(container), host_memory=host_memory)
    return render_template('login.html')
Example #3
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()

        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]))?'
            info = lxc.info(container)

            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

            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(
                    '^[a-zA-Z0-9_-]+$', 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',
                                   form['memlimit'])
                    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',
                                   form['swlimit'])
                    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 ('%s.conf' %
                                                    container) in auto:
                try:
                    os.symlink(
                        '/var/lib/docker/containers/%s/config' % container,
                        '/etc/lxc/auto/%s.conf' % container)
                    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 ('%s.conf' % container) in auto:
                try:
                    os.remove('/etc/lxc/auto/%s.conf' % container)
                    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)
        }
        return render_template('edit.html',
                               containers=lxc.ls(),
                               container=container,
                               infos=infos,
                               settings=lwp.get_container_settings(container),
                               host_memory=host_memory)
    return render_template('login.html')
Example #4
0
def get_container(name):
    return jsonify(lxc.info(name))
Example #5
0
def edit(container=None):
    '''
    edit containers page and actions if form post request
    '''
    host_memory = lwp.host_memory_usage()
    cfg = lwp.get_container_settings(container)
    # read config also from databases
    cfg['bucket'] = get_bucket_token(container)

    if request.method == 'POST':
        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]))?'
        info = lxc.info(container)

        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']
        form['bucket'] = request.form['bucket']
        try:
            form['autostart'] = request.form['autostart']
        except KeyError:
            form['autostart'] = False

        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('^[a-zA-Z0-9_-]+$', 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', form['memlimit'])
                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', form['swlimit'])
                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')

        if bool(form['autostart']) != bool(cfg['auto']):
            lwp.push_config_value('lxc.start.auto', 1 if form['autostart'] else 0, container=container)
            flash(u'Autostart saved for %s' % container, 'success')

        if form['bucket'] != cfg['bucket']:
            g.db.execute("INSERT INTO machine(machine_name, bucket_token) VALUES (?, ?)", [container, form['bucket']])
            g.db.commit()
            flash(u'Bucket config for %s saved!' % container, 'success')

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

    infos = {'status': status, 'pid': pid, 'memusg': lwp.memory_usage(container)}
    return render_template('edit.html', containers=lxc.ls(), container=container, infos=infos, settings=cfg, host_memory=host_memory, storage_repos=storage_repos)
Example #6
0
def edit(container=None):
    if 'logged_in' in session:
        host_memory = lwp.host_memory_usage()
        
        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]?)'

            form = {}
            form['type'] = request.form['type']
            form['link'] = request.form['link']
            form['flags'] = request.form['flags']
            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']

            if form['utsname'] != cfg['utsname'] and re.match('^(?!^containers$)([a-z0-9-]{1,63})|([a-z0-9-\.]{1,63}\.[a-z0-9-\.]{1,63}\.[a-z0-9\.]{1,6})$', 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+$', 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 re.match('^[0-9]+$', form['memlimit']) 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)
                    flash(u'Memory limit updated for %s!' % container, 'success')

            if form['swlimit'] != cfg['swlimit'] and re.match('^[0-9]+$', form['swlimit']) and int(form['swlimit']) <= int(host_memory['total'] * 2):
                if int(form['swlimit']) == int(host_memory['total'] * 2):
                    form['swlimit'] = ''
                if form['swlimit'] != cfg['swlimit']:
                    lwp.push_config_value('lxc.cgroup.memory.memsw.limit_in_bytes', form['swlimit'], container=container)
                    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)
                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)
                flash(u'CPU shares updated for %s!' % container, 'success')

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

        infos = {'status':status, 'pid':pid, 'memusg':lwp.memory_usage(container)}
        return render_template('edit.html', containers=lwp.ls(), container=container, infos=infos, settings=lwp.get_container_settings(container), host_memory=host_memory)
    return render_template('login.html')
Example #7
0
def get_ipv4_dhcp(name):
    if info(name)['state'] == 'RUNNING':
        return os.popen("/usr/bin/lxc-attach -n %s /sbin/ifconfig" % name).read().split()[6][5:]
    else:
        return ''
Example #8
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')