1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-25 07:35:22 +00:00

user can now clone instances, admin can specify user quotas (instances,cpus,memory)

user can only select predefined instance names, mac and disk names are selected automatically
This commit is contained in:
Jan Krcmar 2016-03-23 09:00:42 +01:00
parent de5cb19913
commit 317c2a85ae
9 changed files with 221 additions and 54 deletions

View file

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
from django.conf import settings
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('accounts', '0003_usersshkey'),
]
operations = [
migrations.CreateModel(
name='UserAttributes',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('max_instances', models.IntegerField(default=0)),
('max_cpus', models.IntegerField(default=0)),
('max_memory', models.IntegerField(default=0)),
('user', models.OneToOneField(to=settings.AUTH_USER_MODEL)),
],
),
]

View file

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('accounts', '0004_userattributes'),
]
operations = [
migrations.AddField(
model_name='userattributes',
name='can_clone_instances',
field=models.BooleanField(default=False),
),
]

View file

@ -20,3 +20,13 @@ class UserSSHKey(models.Model):
def __unicode__(self): def __unicode__(self):
return self.keyname return self.keyname
class UserAttributes(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
can_clone_instances = models.BooleanField(default=False)
max_instances = models.IntegerField(default=0)
max_cpus = models.IntegerField(default=0)
max_memory = models.IntegerField(default=0)
def __unicode__(self):
return self.user.username

View file

@ -83,6 +83,30 @@
<input type="checkbox" name="user_is_superuser" {% if user.is_superuser %}checked{% endif %}> <input type="checkbox" name="user_is_superuser" {% if user.is_superuser %}checked{% endif %}>
</div> </div>
</div> </div>
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Can clone instances" %}</label>
<div class="col-sm-2">
<input type="checkbox" name="userattributes_can_clone_instances" {% if user.userattributes.can_clone_instances %}checked{% endif %}>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Max instances" %}</label>
<div class="col-sm-6">
<input type="text" name="userattributes_max_instances" class="form-control" value="{{ user.userattributes.max_instances }}">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Max cpus" %}</label>
<div class="col-sm-6">
<input type="text" name="userattributes_max_cpus" class="form-control" value="{{ user.userattributes.max_cpus }}">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Max memory" %}</label>
<div class="col-sm-6">
<input type="text" name="userattributes_max_memory" class="form-control" value="{{ user.userattributes.max_memory }}">
</div>
</div>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="submit" class="pull-left btn btn-danger" name="delete"> <button type="submit" class="pull-left btn btn-danger" name="delete">

View file

@ -4,7 +4,7 @@ from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from accounts.models import UserInstance, UserSSHKey from accounts.models import *
from instances.models import Instance from instances.models import Instance
from accounts.forms import UserAddForm from accounts.forms import UserAddForm
from django.conf import settings from django.conf import settings
@ -92,13 +92,23 @@ def accounts(request):
if 'edit' in request.POST: if 'edit' in request.POST:
user_id = request.POST.get('user_id', '') user_id = request.POST.get('user_id', '')
user_pass = request.POST.get('user_pass', '') user_pass = request.POST.get('user_pass', '')
user_is_staff = request.POST.get('user_is_staff', False)
user_is_superuser = request.POST.get('user_is_superuser', False)
user_edit = User.objects.get(id=user_id) user_edit = User.objects.get(id=user_id)
user_edit.set_password(user_pass) user_edit.set_password(user_pass)
user_edit.is_staff = user_is_staff user_edit.is_staff = request.POST.get('user_is_staff', False)
user_edit.is_superuser = user_is_superuser user_edit.is_superuser = request.POST.get('user_is_superuser', False)
user_edit.save() user_edit.save()
try:
userattributes = user_edit.userattributes
except UserAttributes.DoesNotExist:
userattributes = UserAttributes(user=user_edit)
userattributes.can_clone_instances = request.POST.get('userattributes_can_clone_instances', False)
userattributes_max_instances = request.POST.get('userattributes_max_instances', 0)
userattributes_max_cpus = request.POST.get('userattributes_max_cpus', 0)
userattributes_max_memory = request.POST.get('userattributes_max_memory', 0)
userattributes.max_instances = userattributes_max_instances if userattributes_max_instances else 0
userattributes.max_cpus = userattributes_max_cpus if userattributes_max_cpus else 0
userattributes.max_memory = userattributes_max_memory if userattributes_max_memory else 0
userattributes.save()
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
if 'block' in request.POST: if 'block' in request.POST:
user_id = request.POST.get('user_id', '') user_id = request.POST.get('user_id', '')

View file

@ -501,11 +501,15 @@
{% trans "Network" %} {% trans "Network" %}
</a> </a>
</li> </li>
{% endif %}
{% if request.user.is_superuser or request.user.userattributes.can_clone_instances %}
<li role="presentation"> <li role="presentation">
<a href="#clone" aria-controls="clone" role="tab" data-toggle="tab"> <a href="#clone" aria-controls="clone" role="tab" data-toggle="tab">
{% trans "Clone" %} {% trans "Clone" %}
</a> </a>
</li> </li>
{% endif %}
{% if request.user.is_superuser %}
<li role="presentation"> <li role="presentation">
<a href="#migrate" aria-controls="migrate" role="tab" data-toggle="tab"> <a href="#migrate" aria-controls="migrate" role="tab" data-toggle="tab">
{% trans "Migrate" %} {% trans "Migrate" %}
@ -690,45 +694,67 @@
</form> </form>
<div class="clearfix"></div> <div class="clearfix"></div>
</div> </div>
{% endif %}
{% if request.user.is_superuser or request.user.userattributes.can_clone_instances %}
<div role="tabpanel" class="tab-pane tab-pane-bordered" id="clone"> <div role="tabpanel" class="tab-pane tab-pane-bordered" id="clone">
<p style="font-weight:bold;">{% trans "Create a clone" %}</p> <p style="font-weight:bold;">{% trans "Create a clone" %}</p>
<form class="form-horizontal" action="" method="post" role="form">{% csrf_token %} <form class="form-horizontal" action="" method="post" role="form">{% csrf_token %}
<div class="form-group"> <div class="form-group">
<label class="col-sm-3 control-label" style="font-weight:normal;">{% trans "Clone Name" %}</label> <label class="col-sm-3 control-label" style="font-weight:normal;">{% trans "Clone Name" %}</label>
<div class="col-sm-4"> <div class="col-sm-4">
{% if request.user.is_superuser %}
<input id="clone_name" type="text" class="form-control" name="name" value="{{ vname }}-clone"/> <input id="clone_name" type="text" class="form-control" name="name" value="{{ vname }}-clone"/>
{% else %}
<select id="select_clone_name" class="form-control" name="name" size="1"/>
{% for name in clone_free_names %}
<option value="{{ name }}">{{ name }}</option>
{% endfor %}
</select>
{% endif %}
</div> </div>
</div> </div>
<p style="font-weight:bold;">{% trans "Network devices" %}</p> {% if request.user.is_superuser %}
{% for network in networks %} <p style="font-weight:bold;">{% trans "Network devices" %}</p>
<div class="form-group"> {% for network in networks %}
<label class="col-sm-3 control-label" style="font-weight:normal;">eth{{ forloop.counter0 }} ({{ network.nic }})</label> <div class="form-group">
<div class="col-sm-4"> <label class="col-sm-3 control-label" style="font-weight:normal;">eth{{ forloop.counter0 }} ({{ network.nic }})</label>
<input type="text" class="form-control" name="clone-net-mac-{{ forloop.counter0 }}" value="{{ network.mac }}"/> <div class="col-sm-4">
</div> <input type="text" class="form-control" name="clone-net-mac-{{ forloop.counter0 }}" value="{{ network.mac }}"/>
<div class="col-sm-4">
<button type="button" class="btn btn-sm btn-success pull-left" name="random-mac-{{ forloop.counter0 }}"
onclick="random_mac({{ forloop.counter0 }})" style="margin-top: 2px;">{% trans "Random" %}</button>
<button type="button" class="btn btn-sm btn-success pull-left" name="guess-mac-{{ forloop.counter0 }}"
onclick="guess_mac_address({{ forloop.counter0 }})" style="margin-top: 2px;">{% trans "Guess" %}</button>
</div>
</div>
{% endfor %}
<p style="font-weight:bold;">{% trans "Storage devices" %}</p>
{% for disk in clone_disks %}
<div class="form-group">
<label class="col-sm-3 control-label" style="font-weight:normal;">{{ disk.dev }} ({{ disk.storage }})</label>
<div class="col-sm-4">
<input id="disk_name-{{ disk.dev }}" type="text" class="form-control" name="disk-{{ disk.dev }}" value="{{ disk.image }}"/>
</div>
{% ifequal disk.format 'qcow2' %}
<label class="col-sm-2 control-label" style="font-weight:normal;margin-left:-35px;">Metadata</label>
<div class="col-sm-1">
<input type="checkbox" name="meta-{{ disk.dev }}" value="true" style="margin-top: 10px;">
</div> </div>
{% endifequal %} <div class="col-sm-4">
</div> <button type="button" class="btn btn-sm btn-success pull-left" name="random-mac-{{ forloop.counter0 }}"
{% endfor %} onclick="random_mac({{ forloop.counter0 }})" style="margin-top: 2px;">{% trans "Random" %}</button>
<button type="button" class="btn btn-sm btn-success pull-left" name="guess-mac-{{ forloop.counter0 }}"
onclick="guess_mac_address('#clone_name', {{ forloop.counter0 }})" style="margin-top: 2px;">{% trans "Guess" %}</button>
</div>
</div>
{% endfor %}
{% else %}
{% for network in networks %}
<input type="hidden" class="form-control" name="clone-net-mac-{{ forloop.counter0 }}" value="{{ network.mac }}"/>
{% endfor %}
{% endif %}
{% if request.user.is_superuser %}
<p style="font-weight:bold;">{% trans "Storage devices" %}</p>
{% for disk in clone_disks %}
<div class="form-group">
<label class="col-sm-3 control-label" style="font-weight:normal;">{{ disk.dev }} ({{ disk.storage }})</label>
<div class="col-sm-4">
<input id="disk_name-{{ disk.dev }}" type="text" class="form-control" name="disk-{{ disk.dev }}" value="{{ disk.image }}"/>
</div>
{% ifequal disk.format 'qcow2' %}
<label class="col-sm-2 control-label" style="font-weight:normal;margin-left:-35px;">Metadata</label>
<div class="col-sm-1">
<input type="checkbox" name="meta-{{ disk.dev }}" value="true" style="margin-top: 10px;">
</div>
{% endifequal %}
</div>
{% endfor %}
{% else %}
{% for disk in clone_disks %}
<input id="disk_name-{{ disk.dev }}" type="hidden" class="form-control" name="disk-{{ disk.dev }}" value="{{ disk.image }}"/>
{% endfor %}
{% endif %}
<div class="form-group"> <div class="form-group">
<label class="col-sm-3 control-label">{% trans "Title" %}</label> <label class="col-sm-3 control-label">{% trans "Title" %}</label>
<div class="col-sm-6"> <div class="col-sm-6">
@ -749,6 +775,8 @@
</form> </form>
<div class="clearfix"></div> <div class="clearfix"></div>
</div> </div>
{% endif %}
{% if request.user.is_superuser %}
<div role="tabpanel" class="tab-pane tab-pane-bordered" id="migrate"> <div role="tabpanel" class="tab-pane tab-pane-bordered" id="migrate">
<p>{% trans "For migration both host servers must have equal settings and OS type" %}</p> <p>{% trans "For migration both host servers must have equal settings and OS type" %}</p>
<form class="form-horizontal" method="post" role="form">{% csrf_token %} <form class="form-horizontal" method="post" role="form">{% csrf_token %}
@ -986,8 +1014,8 @@
} }
</script> </script>
<script> <script>
function guess_mac_address(net) { function guess_mac_address(src_elem, net) {
new_vname = $('#clone_name').val(); new_vname = $(src_elem).val();
$.getJSON('/instance/guess_mac_address/' + new_vname + '/', function(data) { $.getJSON('/instance/guess_mac_address/' + new_vname + '/', function(data) {
$('input[name="clone-net-mac-'+net+'"]').val(data['mac']); $('input[name="clone-net-mac-'+net+'"]').val(data['mac']);
}); });
@ -1056,6 +1084,16 @@
$("#console_select_type option[value='" + console_type + "']").prop('selected', true); $("#console_select_type option[value='" + console_type + "']").prop('selected', true);
} }
}); });
{% if not request.user.is_superuser %}
$('#select_clone_name').on('change', function () {
update_clone_disk_name($(this).val());
guess_mac_address('#select_clone_name', 0);
});
$(document).ready(function () {
update_clone_disk_name($('#select_clone_name').val());
guess_mac_address('#select_clone_name', 0);
});
{% endif %}
</script> </script>
<script> <script>
$(function () { $(function () {

View file

@ -20,6 +20,7 @@ from vrtManager.connection import connection_manager
from libvirt import libvirtError, VIR_DOMAIN_XML_SECURE from libvirt import libvirtError, VIR_DOMAIN_XML_SECURE
from webvirtcloud.settings import QEMU_KEYMAPS, QEMU_CONSOLE_TYPES from webvirtcloud.settings import QEMU_KEYMAPS, QEMU_CONSOLE_TYPES
from logs.views import addlogmsg from logs.views import addlogmsg
from django.conf import settings
@login_required @login_required
@ -204,6 +205,31 @@ def instance(request, compute_id, vname):
else: else:
return long(float(size_str)) return long(float(size_str))
def get_clone_free_names(size=10):
prefix = settings.CLONE_INSTANCE_DEFAULT_PREFIX
free_names = []
existing_names = [i.name for i in Instance.objects.filter(name__startswith=prefix)]
index = 1
while len(free_names) < size:
new_name = prefix + str(index)
if new_name not in existing_names:
free_names.append(new_name)
index += 1
return free_names
def check_user_quota():
userinstances = UserInstance.objects.filter(user__id=request.user.id)
instances_count = len(userinstances)
cpus_count = instances_count
memory_count = instances_count * 2048
if request.user.userattributes.max_instances > 0 and instances_count > request.user.userattributes.max_instances:
return "instance"
if request.user.userattributes.max_cpus > 0 and cpus_count > request.user.userattributes.max_cpus:
return "cpu"
if request.user.userattributes.max_memory > 0 and memory_count > request.user.userattributes.max_memory:
return "memory"
return ""
try: try:
conn = wvmInstance(compute.hostname, conn = wvmInstance(compute.hostname,
compute.login, compute.login,
@ -241,6 +267,7 @@ def instance(request, compute_id, vname):
has_managed_save_image = conn.get_managed_save_image() has_managed_save_image = conn.get_managed_save_image()
clone_disks = show_clone_disk(disks, vname) clone_disks = show_clone_disk(disks, vname)
console_passwd = conn.get_console_passwd() console_passwd = conn.get_console_passwd()
clone_free_names = get_clone_free_names()
try: try:
instance = Instance.objects.get(compute_id=compute_id, name=vname) instance = Instance.objects.get(compute_id=compute_id, name=vname)
@ -496,23 +523,6 @@ def instance(request, compute_id, vname):
addlogmsg(request.user.username, instance.name, msg) addlogmsg(request.user.username, instance.name, msg)
return HttpResponseRedirect(reverse('instance', args=[compute_id, vname])) return HttpResponseRedirect(reverse('instance', args=[compute_id, vname]))
if 'clone' in request.POST:
clone_data = {}
clone_data['name'] = request.POST.get('name', '')
check_instance = Instance.objects.filter(name=clone_data['name'])
if check_instance:
msg = _("Instance '%s' already exists!" % clone_data['name'])
error_messages.append(msg)
else:
for post in request.POST:
clone_data[post] = request.POST.get(post, '')
conn.clone_instance(clone_data)
msg = _("Clone")
addlogmsg(request.user.username, instance.name, msg)
return HttpResponseRedirect(reverse('instance', args=[compute_id, clone_data['name']]))
if 'change_network' in request.POST: if 'change_network' in request.POST:
network_data = {} network_data = {}
@ -539,6 +549,34 @@ def instance(request, compute_id, vname):
addlogmsg(request.user.username, instance.name, msg) addlogmsg(request.user.username, instance.name, msg)
return HttpResponseRedirect(request.get_full_path() + '#options') return HttpResponseRedirect(request.get_full_path() + '#options')
if request.user.is_superuser or request.user.userattributes.can_clone_instances:
if 'clone' in request.POST:
clone_data = {}
clone_data['name'] = request.POST.get('name', '')
quota_msg = check_user_quota()
if quota_msg:
msg = _("User %s quota reached, cannot create '%s'!" % (quota_msg, clone_data['name']))
error_messages.append(msg)
check_instance = Instance.objects.filter(name=clone_data['name'])
if check_instance:
msg = _("Instance '%s' already exists!" % clone_data['name'])
error_messages.append(msg)
else:
for post in request.POST:
clone_data[post] = request.POST.get(post, '')
new_uuid = conn.clone_instance(clone_data)
new_instance = Instance(compute_id=compute_id, name=clone_data['name'], uuid=new_uuid)
new_instance.save()
userinstance = UserInstance(instance_id=new_instance.id, user_id=request.user.id)
userinstance.save()
msg = _("Clone of '%s'" % instance.name)
addlogmsg(request.user.username, new_instance.name, msg)
return HttpResponseRedirect(reverse('instance', args=[compute_id, clone_data['name']]))
conn.close() conn.close()
except libvirtError as lib_err: except libvirtError as lib_err:

View file

@ -697,9 +697,10 @@ class wvmInstance(wvmConnect):
'description': clone_data.get('clone-description', ''), 'description': clone_data.get('clone-description', ''),
} }
self._set_options(tree, options) self._set_options(tree, options)
self._defineXML(ElementTree.tostring(tree)) self._defineXML(ElementTree.tostring(tree))
return self.get_instance(clone_data['name']).UUIDString()
def change_network(self, network_data): def change_network(self, network_data):
xml = self._XMLDesc(VIR_DOMAIN_XML_SECURE) xml = self._XMLDesc(VIR_DOMAIN_XML_SECURE)
tree = ElementTree.fromstring(xml) tree = ElementTree.fromstring(xml)

View file

@ -113,3 +113,4 @@ LIBVIRT_KEEPALIVE_INTERVAL = 5
LIBVIRT_KEEPALIVE_COUNT = 5 LIBVIRT_KEEPALIVE_COUNT = 5
ALLOW_INSTANCE_MULTIPLE_OWNER = True ALLOW_INSTANCE_MULTIPLE_OWNER = True
CLONE_INSTANCE_DEFAULT_PREFIX = 'ourea'