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

instance/clone disk names revisited

This commit is contained in:
Ing. Jan KRCMAR 2018-09-11 15:11:13 +02:00
parent fb03f3c554
commit aba4ee8623
2 changed files with 39 additions and 39 deletions

View file

@ -914,7 +914,7 @@
{% endif %}
{% if request.user.is_superuser %}
<p style="font-weight:bold;">{% trans "Storage devices" %}</p>
{% for disk in clone_disks %}
{% for disk in 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">
@ -929,7 +929,7 @@
</div>
{% endfor %}
{% else %}
{% for disk in clone_disks %}
{% for disk in disks %}
<input id="disk_name-{{ disk.dev }}" type="hidden" class="form-control" name="disk-{{ disk.dev }}" value="{{ disk.image }}"/>
{% endfor %}
{% endif %}
@ -1266,17 +1266,13 @@
</script>
<script>
function update_clone_disk_name(new_vname) {
vname = '{{ vname }}-clone';
{% for disk in clone_disks %}
vname = '{{ vname }}';
{% for disk in disks %}
disk_name = '{{ disk.image }}';
disk_minus = disk_name.split('-');
disk_minus_suffix = disk_minus[disk_minus.length-1];
disk_minus.pop();
disk_minus_name = disk_minus.join('-');
disk_dot = disk_name.split('.')
disk_dot_suffix = disk_dot[disk_dot.length-1];
if (disk_name.lastIndexOf('-') > -1 && disk_minus_name == vname) {
image = new_vname + "-" + disk_minus_suffix;
if (disk_name.startsWith(vname)) {
image = disk_name.replace(vname, new_vname);
} else if (disk_name.lastIndexOf('.') > -1 && disk_dot_suffix.length <= 7) {
image = new_vname + "." + disk_dot_suffix
} else if (new_vname != disk_name) {
@ -1346,6 +1342,7 @@
$(document).ready(function () {
random_mac('clone-net-mac-0');
random_mac('add-net-mac');
update_clone_disk_name($('#clone_name').val());
});
{% else %}
$('#select_clone_name').on('change', function () {

View file

@ -226,24 +226,6 @@ def instance(request, compute_id, vname):
if not userinstance:
return HttpResponseRedirect(reverse('index'))
def show_clone_disk(disks, vname=''):
clone_disk = []
for disk in disks:
if disk['image'] is None:
continue
if disk['image'].count("-") and disk['image'].rsplit("-", 1)[0] == vname:
name, suffix = disk['image'].rsplit("-", 1)
image = name + "-clone" + "-" + suffix
elif disk['image'].count(".") and len(disk['image'].rsplit(".", 1)[1]) <= 7:
name, suffix = disk['image'].rsplit(".", 1)
image = name + "-clone" + "." + suffix
else:
image = disk['image'] + "-clone"
clone_disk.append(
{'dev': disk['dev'], 'storage': disk['storage'],
'image': image, 'format': disk['format']})
return clone_disk
def filesizefstr(size_str):
if size_str == '':
return 0
@ -373,7 +355,6 @@ def instance(request, compute_id, vname):
snapshots = sorted(conn.get_snapshot(), reverse=True, key=lambda k:k['date'])
inst_xml = conn._XMLDesc(VIR_DOMAIN_XML_SECURE)
has_managed_save_image = conn.get_managed_save_image()
clone_disks = show_clone_disk(disks, vname)
console_passwd = conn.get_console_passwd()
clone_free_names = get_clone_free_names()
user_quota_msg = check_user_quota(0, 0, 0, 0)
@ -766,16 +747,10 @@ def instance(request, compute_id, vname):
auto_vname = clone_free_names[0]
clone_data['name'] = auto_vname
clone_data['clone-net-mac-0'] = _get_dhcp_mac_address(auto_vname)
for post in clone_data.keys():
if post.startswith('disk-'):
disk_name = clone_data[post]
if "-" in disk_name:
suffix = disk_name.split("-")[-1]
disk_name = '-'.join((auto_vname, suffix))
else:
suffix = disk_name.split(".")[-1]
disk_name = '.'.join((auto_vname, suffix))
clone_data[post] = disk_name
for disk in disks:
disk_dev = "disk-{}".format(disk['dev'])
disk_name = get_clone_disk_name(disk, vname, auto_vname)
clone_data[disk_dev] = disk_name
if not request.user.is_superuser and quota_msg:
msg = _("User %s quota reached, cannot create '%s'!" % (quota_msg, clone_data['name']))
@ -1023,6 +998,34 @@ def check_instance(request, vname):
return HttpResponse(json.dumps(data))
def get_clone_disk_name(disk, prefix, clone_name=''):
if not disk['image']:
return None
if disk['image'].startswith(prefix) and clone_name:
suffix = disk['image'][len(prefix):]
image = "{}{}".format(clone_name, suffix)
elif "." in disk['image'] and len(disk['image'].rsplit(".", 1)[1]) <= 7:
name, suffix = disk['image'].rsplit(".", 1)
image = "{}-clone.{}".format(name, suffix)
else:
image = "{}-clone".format(disk['image'])
return image
def _get_clone_disks(disks, vname=''):
clone_disks = []
for disk in disks:
new_image = get_clone_disk_name(disk, vname)
if not new_image:
continue
new_disk = {
'dev': disk['dev'],
'storage': disk['storage'],
'image': new_image,
'format': disk['format']
}
clone_disks.append(new_disk)
return clone_disks
def sshkeys(request, vname):
"""
:param request: