def refresh_memory_containers(name=None): if 'logged_in' in session: if name == 'containers': listx = lxc.listx() containers = [] for container in listx['RUNNING']: container = container.replace(' (auto)', '') containers.append({ 'name': container, 'memusg': lwp.memory_usage_cgroup(container), 'max_memusg': lwp.max_memory_usage_cgroup(container) }) for container in listx['FROZEN']: container = container.replace(' (auto)', '') containers.append({ 'name': container, 'memusg': lwp.memory_usage_cgroup(container), 'max_memusg': lwp.max_memory_usage_cgroup(container) }) return jsonify(data=containers) elif name == 'host': return jsonify(lwp.host_memory_usage()) return jsonify({ 'memusg': lwp.memory_usage_cgroup(name), 'max_memusg': lwp.max_memory_usage_cgroup(name) })
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) if request.method == 'POST': form = request.form.copy() if form['bucket'] != get_bucket_token(container): 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') # convert boolean in correct value for lxc, if checkbox is inset value is not submitted inside POST form['flags'] = 'up' if 'flags' in form else 'down' form['start_auto'] = '1' if 'start_auto' in form else '0' # if memlimits/memswlimit is at max values unset form values if int(form['memlimit']) == host_memory['total']: form['memlimit'] = '' if int(form['swlimit']) == host_memory['total'] * 2: form['swlimit'] = '' for option in form.keys(): # if the key is supported AND is different if option in cfg.keys() and form[option] != cfg[option]: # validate value with regex if re.match(cgroup_ext[option][1], form[option]): lwp.push_config_value(cgroup_ext[option][0], form[option], container=container) flash(cgroup_ext[option][2], 'success') else: flash( 'Cannot validate value for option {}. Unsaved!'.format( option), 'error') # we should re-read container configuration now to be coherent with the newly saved values cfg = lwp.get_container_settings(container) info = lxc.info(container) infos = { 'status': info['state'], 'pid': info['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)
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) if request.method == "POST": form = request.form.copy() if form["bucket"] != get_bucket_token(container): 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") # convert boolean in correct value for lxc, if checkbox is inset value is not submitted inside POST form["flags"] = "up" if "flags" in form else "down" form["start_auto"] = "1" if "start_auto" in form else "0" # if memlimits/memswlimit is at max values unset form values if int(form["memlimit"]) == host_memory["total"]: form["memlimit"] = "" if int(form["swlimit"]) == host_memory["total"] * 2: form["swlimit"] = "" for option in form.keys(): # if the key is supported AND is different if option in cfg.keys() and form[option] != cfg[option]: # validate value with regex if re.match(cgroup_ext[option][1], form[option]): lwp.push_config_value(cgroup_ext[option][0], form[option], container=container) flash(cgroup_ext[option][2], "success") else: flash("Cannot validate value for option {}. Unsaved!".format(option), "error") # we should re-read container configuration now to be coherent with the newly saved values cfg = lwp.get_container_settings(container) info = lxc.info(container) infos = {"status": info["state"], "pid": info["pid"], "memusg": lwp.memory_usage(container)} # prepare a regex dict from cgroups_ext definition regex = {} for k, v in cgroup_ext.items(): regex[k] = v[1] return render_template( "edit.html", containers=lxc.ls(), container=container, infos=infos, settings=cfg, host_memory=host_memory, storage_repos=storage_repos, regex=regex, clonable_containers=lxc.listx()["STOPPED"], )
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 refresh_memory_containers(name=None): if 'logged_in' in session: if name == 'containers': containers_running = lxc.list_containers(active=True, defined=False, as_object=True) containers = [] for container in containers_running: name = container.name.replace(' (auto)', '') containers.append({'name': name, 'memusg': lwp.memory_usage(container)}) return jsonify(data=containers) elif name == 'host': return jsonify(lwp.host_memory_usage()) return jsonify({'memusg': lwp.memory_usage(lxc.Container(name))})
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) if request.method == 'POST': form = request.form.copy() if form['bucket'] != get_bucket_token(container): 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') # convert boolean in correct value for lxc, if checkbox is inset value is not submitted inside POST form['flags'] = 'up' if 'flags' in form else 'down' form['start_auto'] = '1' if 'start_auto' in form else '0' # if memlimits/memswlimit is at max values unset form values if int(form['memlimit']) == host_memory['total']: form['memlimit'] = '' if int(form['swlimit']) == host_memory['total'] * 2: form['swlimit'] = '' for option in form.keys(): # if the key is supported AND is different if option in cfg.keys() and form[option] != cfg[option]: # validate value with regex if re.match(cgroup_ext[option][1], form[option]): lwp.push_config_value(cgroup_ext[option][0], form[option], container=container) flash(cgroup_ext[option][2], 'success') else: flash('Cannot validate value for option {}. Unsaved!'.format(option), 'error') # we should re-read container configuration now to be coherent with the newly saved values cfg = lwp.get_container_settings(container) info = lxc.info(container) infos = {'status': info['state'], 'pid': info['pid'], 'memusg': lwp.memory_usage(container)} # prepare a regex dict from cgroups_ext definition regex = {} for k, v in cgroup_ext.items(): regex[k] = v[1] return render_template('edit.html', containers=lxc.ls(), container=container, infos=infos, settings=cfg, host_memory=host_memory, storage_repos=storage_repos, regex=regex, clonable_containers=lxc.listx()['STOPPED'])
def get_host_info(): """ Returns lxc containers on the current machine and brief status information. """ info = { 'hostname': socket.gethostname(), 'distribution': lwp.name_distro(), 'version': lwp.check_version(), 'network': lwp.get_net_settings(), 'memory': lwp.host_memory_usage(), 'cpu': lwp.host_cpu_percent(), 'disk': lwp.host_disk_usage(), 'uptime': lwp.host_uptime(), } return jsonify(info)
def get(self): os = platform.dist() os_str = ' '.join(os) host_cpu_infos = lwp.host_cpu_infos() return dict(uptime=lwp.host_uptime(), hostname=socket.gethostname(), dist=os_str, disk_usage=lwp.host_disk_usage(), cpu=dict( usage=lwp.host_cpu_percent(), model=host_cpu_infos['name'], cores=host_cpu_infos['cores'] ), memory=lwp.host_memory_usage(), kernel=lwp.host_kernel_verion())
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 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')
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')
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)
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 edit(name=None): ''' edit containers page and actions if form post request ''' if 'logged_in' in session: host_memory = lwp.host_memory_usage() container = lxc.Container(name) 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]))?' def get_int(data, key): value = data.get(key) if value and value.isdigit(): return int(value) return 0 form = {} def parse_network(): networks = list() for key, value in request.form.items(): if key.startswith('network'): _, order, key = key.split('.') order = int(order) if order + 1 > len(networks): network = {'flags': 'down'} networks.append(network) else: network = networks[order] network[key] = value return networks networks = parse_network() form['rootfs'] = request.form['rootfs'] form['utsname'] = request.form['hostname'] form['memlimit'] = get_int(request.form, 'memlimit') form['swlimit'] = get_int(request.form, 'swlimit') form['cpus'] = get_int(request.form, 'cpus') form['shares'] = get_int(request.form, 'cpushares') try: form['autostart'] = request.form['autostart'] except KeyError: form['autostart'] = False for idx, network in enumerate(networks): if network['flags'] == 'down': container.network.remove(idx) continue for prop in ('ipv4', 'type', 'link'): cfgkey = 'lxc.network.%s.%s' % (idx, prop) try: value = container.get_config_item(cfgkey) except KeyError: value = '' if network[prop] != value: if network[prop]: container.set_config_item(cfgkey, network[prop]) else: container.clear_config_item(cfgkey) 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']): container.set_config_item('lxc.utsname', form['utsname']) flash(u'Hostname updated for %s!' % container.name, 'success') if form['memlimit'] != cfg['memlimit'] and \ form['memlimit'] <= int(host_memory['total']): configkey = lwp.cgroup['memlimit'] if form['memlimit'] >= int(host_memory['total'] - 10): form['memlimit'] = 0 if form['memlimit'] != cfg['memlimit']: if form['memlimit'] == 0: container.clear_config_item(configkey) else: container.set_config_item(configkey, str(form['memlimit'])) if container.state != 'STOPPED': container.set_cgroup_item(configkey, str(form['memlimit'])) flash(u'Memory limit updated for %s!' % container.name, 'success') if form['swlimit'] != cfg['swlimit'] and \ form['swlimit'] <= int(host_memory['total'] * 2): configkey = lwp.cgroup['swlimit'] if form['swlimit'] == int(host_memory['total'] * 2): form['swlimit'] = 0 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']: container.set_config_item(configkey, str(form['swlimit'])) if container.state != 'STOPPED': container.set_cgroup_item(configkey, str(form['swlimit'])) flash(u'Swap limit updated for %s!' % container.name, 'success') if (not form['cpus'] and form['cpus'] != cfg['cpus']) or \ (form['cpus'] != cfg['cpus'] and re.match('^[0-9,-]+$', form['cpus'])): container.set_config_item('lxxc.cgroup.cpuset.cpus', str(form['cpus'])) if container.state != 'STOPPED': container.set_cgroup_item('lxc.cgroup.cpuset.cpus', str(form['cpus'])) flash(u'CPUs updated for %s!' % container.name, 'success') if (not form['shares'] and form['shares'] != cfg['shares']) or \ (form['shares'] != cfg['shares'] and re.match('^[0-9]+$', form['shares'])): container.set_config_item('lxc.cgroup.cpu.shares', str(form['shares'])) if container.state != 'STOPPED': container.set_cgroup_item('lxc.cgroup.cpu.shares', str(form['shares'])) flash(u'CPU shares updated for %s!' % container.name, 'success') if form['rootfs'] != cfg['rootfs'] and \ re.match('^[a-zA-Z0-9_/\-\.]+', form['rootfs']): container.set_config_item('lxc.rootfs', form['rootfs']) flash(u'Rootfs updated!' % container.name, 'success') container.save_config() 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') status = container.state pid = container.init_pid infos = {'status': status, 'pid': pid, 'memusg': lwp.memory_usage(container)} settings = lwp.get_container_settings(container) netstatus = 'up' in [ net['flags'] for net in settings['networks'] ] return render_template('edit.html', containers=lxc.list_containers(), container=name, infos=infos, settings=settings, netstatus=netstatus, host_memory=host_memory) return render_template('login.html')
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')