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')
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')
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')
def about(): if 'logged_in' in session: return render_template('about.html', containers=lwp.ls(), version=lwp.check_version()) return render_template('login.html')
def checkconfig(): if 'logged_in' in session: if session['su'] != 'Yes': return abort(403) return render_template('checkconfig.html', containers=lwp.ls(), cfg=lxc.checkconfig()) return render_template('login.html')
def lwp_users(): if 'logged_in' in session: if session['su'] != 'Yes': return abort(403) try: trash = request.args.get('trash') except KeyError: trash = 0 if request.args.get('token') == session.get('token') and trash == '1' and request.args.get('userid') and request.args.get('username'): g.db.execute("DELETE FROM users WHERE id=? AND username=?", [request.args.get('userid'), request.args.get('username')]) g.db.commit() flash('Deleted %s' % request.args.get('username'), 'success') return redirect(url_for('lwp_users')) if request.method == 'POST': users = query_db('SELECT id, name, username, su FROM users ORDER BY id ASC') if request.form['newUser'] == 'True': if not request.form['username'] in [user['username'] for user in users]: if re.match('^\w+$', request.form['username']) and request.form['password1']: if request.form['password1'] == request.form['password2']: if request.form['name']: if re.match('[a-z A-Z0-9]{3,32}', request.form['name']): g.db.execute("INSERT INTO users (name, username, password) VALUES (?, ?, ?)", [request.form['name'], request.form['username'], hash_passwd(request.form['password1'])]) g.db.commit() else: flash('Invalid name!', 'error') else: g.db.execute("INSERT INTO users (username, password) VALUES (?, ?)", [request.form['username'], hash_passwd(request.form['password1'])]) g.db.commit() flash('Created %s' % request.form['username'], 'success') else: flash('No password match', 'error') else: flash('Invalid username or password!', 'error') else: flash('Username already exist!', 'error') elif request.form['newUser'] == 'False': if request.form['password1'] == request.form['password2']: if re.match('[a-z A-Z0-9]{3,32}', request.form['name']): try: su = request.form['su'] except KeyError: su = 'No' if not request.form['name']: g.db.execute("UPDATE users SET name='', su=? WHERE username=?", [su, request.form['username']]) g.db.commit() elif request.form['name'] and not request.form['password1'] and not request.form['password2']: g.db.execute("UPDATE users SET name=?, su=? WHERE username=?", [request.form['name'], su, request.form['username']]) g.db.commit() elif request.form['name'] and request.form['password1'] and request.form['password2']: g.db.execute("UPDATE users SET name=?, password=?, su=? WHERE username=?", [request.form['name'], hash_passwd(request.form['password1']), su, request.form['username']]) g.db.commit() elif request.form['password1'] and request.form['password2']: g.db.execute("UPDATE users SET password=?, su=? WHERE username=?", [hash_passwd(request.form['password1']), su, request.form['username']]) g.db.commit() flash('Updated', 'success') else: flash('Invalid name!', 'error') else: flash('No password match', 'error') else: flash('Unknown error!', 'error') users = query_db('SELECT id, name, username, su FROM users ORDER BY id ASC') return render_template('users.html', containers=lwp.ls(), users=users) return render_template('login.html')