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())
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())
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)})
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())
def lxc_net(self): """ lxc-net (/etc/default/lxc) settings page and actions if form post request. """ # TODO: do this with pyramid permissions if not self.request.user.is_superuser: raise HTTPForbidden() cfg = get_net_settings() form = NetworkSetupForm(self.request.POST, **cfg) if self.request.method == 'POST': if form.validate(): if not lxc.running(): # loop over fields, save only the fields that have changed settings_changed = False for field_name, field in form._fields.items(): if field.data != cfg[field_name]: settings_changed = True push_net_value(NetworkSetupForm.LXC_NETWORK_OPTION[field_name], field.data) self.session.flash('{} updated'.format(field.label.text), queue='success') # only restart networking if a setting has changed if settings_changed: if net_restart() == 0: self.session.flash('LXC Network settings applied successfully', queue='success') else: self.session.flash('Failed to restart LXC networking', queue='error') # re-read network configuration to be coherent cfg = get_net_settings() form = NetworkSetupForm(self.request.POST, **cfg) else: self.session.flash('Stop all containers before restarting lxc-net', queue='warning') return {'form': form, 'cfg': cfg, 'running': lxc.running()}
def refresh_memory(self): name = self.request.matchdict['name'] if name == 'containers': containers_running = lxc.running() containers = [] for container in containers_running: container = container.replace(' (auto)', '') containers.append({ 'name': container, 'memory_usage': memory_usage(container), 'settings': get_container_settings(container) }) return containers elif name == 'host': return host_memory_usage() return {'memory_usage': memory_usage(name)}
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)})