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 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_container(name): return jsonify(lxc.info(name))
def edit_container(self): """ edit containers page and actions if form post request """ container = self.request.matchdict['container'] cfg = get_container_settings(container) form = EditContainerForm(self.request.POST, container=container, **strip_empty_fields(cfg)) if self.request.method == 'POST': if form.validate(): # when memory sliders are at maximum, unset them if form.memlimit.data == TOTAL_HOST_MEMORY: form.memlimit.data = '' if form.swlimit.data == TOTAL_HOST_MEMORY * 2: form.swlimit.data = '' for field_name, field in form._fields.items(): # nothing changed, one is None, the other an empty string # this happens with IntegerField + Optional() validator if field.data is None and cfg[field_name] == '': continue # a field has changed, something needs to be saved if field.data != cfg[field_name]: if field_name == 'bucket': machine = Machine.objects.get(name=container) if machine: # update bucket token machine.bucket_token = field.data machine.save() else: # create new record in machine table Machine.objects.create(name=container, bucket_token=field.data) self.session.flash('Bucket config for {} saved'.format(container), queue='success') else: # convert to lxc config values if field_name == 'flags': value = 'up' if field.data else 'down' elif field_name == 'start_auto': value = '1' if form.start_auto.data else '0' else: value = field.data push_config_value(CGROUP_EXT[field_name][0], value, container=container) self.session.flash(CGROUP_EXT[field_name][1], queue='success') # re-read container configuration now to be coherent cfg = get_container_settings(container) form = EditContainerForm(container=container, **strip_empty_fields(cfg)) info = lxc.info(container) return { 'form': form, 'container': container, 'infos': { 'status': info['state'], 'pid': info['pid'], 'memory_usage': memory_usage(container) }, 'settings': cfg, 'host_memory': TOTAL_HOST_MEMORY, 'storage_repos': self.settings['storage_repos'] }
def container_detail(self): """ Returns the info for a given container. """ return lxc.info(self.request.matchdict["container"])