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

Random mac address for cloned vm issue fixed by @honza801

This commit is contained in:
catborise 2018-07-26 15:29:56 +03:00
parent 1700ddf8f1
commit 340d93463e
3 changed files with 51 additions and 31 deletions

View file

@ -1197,15 +1197,10 @@
}
</script>
<script>
function random_mac(net) {
var hexDigits = "0123456789abcdef";
var macAddress="52:54:00:";
for (var i=0; i<3; i++) {
macAddress+=hexDigits.charAt(Math.round(Math.random()*16));
macAddress+=hexDigits.charAt(Math.round(Math.random()*16));
if (i != 2) macAddress+=":";
}
$('input[name="clone-net-mac-'+net+'"]').val(macAddress);
function random_mac(net) {
$.getJSON('/instance/random_mac_address/', function(data) {
$('input[name="clone-net-mac-'+net+'"]').val(data['mac']);
});
};
</script>
<script>
@ -1305,7 +1300,11 @@
$("#console_select_listen_address option[value='" + console_listen_address + "']").prop('selected', true);
}
});
{% if not request.user.is_superuser %}
{% if request.user.is_superuser %}
$(document).ready(function () {
random_mac(0);
});
{% else %}
$('#select_clone_name').on('change', function () {
update_clone_disk_name($(this).val());
guess_mac_address('#select_clone_name', 0);
@ -1328,7 +1327,6 @@
<script>
function network_select_enable(){
// set network button enabled
var selected = $('network_select').val();
if (selected != "to Change") {
$('button[name="change_network"]').removeAttr('disabled');

View file

@ -2,18 +2,12 @@ from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^(?P<compute_id>[0-9]+)/(?P<vname>[\w\-\.]+)/$',
views.instance, name='instance'),
url(r'^statistics/(?P<compute_id>[0-9]+)/(?P<vname>[\w\-\.]+)/$',
views.inst_graph, name='inst_graph'),
url(r'^status/(?P<compute_id>[0-9]+)/(?P<vname>[\w\-\.]+)/$',
views.inst_status, name='inst_status'),
url(r'^guess_mac_address/(?P<vname>[\w\-\.]+)/$',
views.guess_mac_address, name='guess_mac_address'),
url(r'^guess_clone_name/$',
views.guess_clone_name, name='guess_clone_name'),
url(r'^check_instance/(?P<vname>[\w\-\.]+)/$',
views.check_instance, name='check_instance'),
url(r'^sshkeys/(?P<vname>[\w\-\.]+)/$',
views.sshkeys, name='sshkeys'),
url(r'^(?P<compute_id>[0-9]+)/(?P<vname>[\w\-\.]+)/$', views.instance, name='instance'),
url(r'^statistics/(?P<compute_id>[0-9]+)/(?P<vname>[\w\-\.]+)/$', views.inst_graph, name='inst_graph'),
url(r'^status/(?P<compute_id>[0-9]+)/(?P<vname>[\w\-\.]+)/$', views.inst_status, name='inst_status'),
url(r'^guess_mac_address/(?P<vname>[\w\-\.]+)/$', views.guess_mac_address, name='guess_mac_address'),
url(r'^guess_clone_name/$', views.guess_clone_name, name='guess_clone_name'),
url(r'^random_mac_address/$', views.random_mac_address, name='random_mac_address'),
url(r'^check_instance/(?P<vname>[\w\-\.]+)/$', views.check_instance, name='check_instance'),
url(r'^sshkeys/(?P<vname>[\w\-\.]+)/$', views.sshkeys, name='sshkeys'),
]

View file

@ -5,7 +5,7 @@ import socket
import crypt
import re
import string
from random import choice
import random
from bisect import insort
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
@ -526,7 +526,7 @@ def instance(request, compute_id, vname):
cache = request.POST.get('cache', '')
target = get_new_disk_dev(disks, bus)
path = connCreate.create_volume(storage, name, size, format, meta_prealloc, extension)
path = connCreate.create_volume(storage, name, size, format, meta_prealloc)
conn.attach_disk(path, target, subdriver=format, cache=cache, targetbus=bus)
msg = _('Attach new disk')
addlogmsg(request.user.username, instance.name, msg)
@ -909,10 +909,10 @@ def inst_graph(request, compute_id, vname):
response.write(data)
return response
@login_required
def guess_mac_address(request, vname):
def _get_dhcp_mac_address(vname):
dhcp_file = '/srv/webvirtcloud/dhcpd.conf'
data = {'vname': vname, 'mac': '52:54:00:'}
mac = ''
if os.path.isfile(dhcp_file):
with open(dhcp_file, 'r') as f:
name_found = False
@ -920,10 +920,37 @@ def guess_mac_address(request, vname):
if "host %s." % vname in line:
name_found = True
if name_found and "hardware ethernet" in line:
data['mac'] = line.split(' ')[-1].strip().strip(';')
mac = line.split(' ')[-1].strip().strip(';')
break
return mac
@login_required
def guess_mac_address(request, vname):
data = { 'vname': vname }
mac = _get_dhcp_mac_address(vname)
if not mac:
mac = _get_random_mac_address()
data['mac'] = mac
return HttpResponse(json.dumps(data))
def _get_random_mac_address():
mac = '52:54:00:%02x:%02x:%02x' % (
random.randint(0x00, 0xff),
random.randint(0x00, 0xff),
random.randint(0x00, 0xff)
)
return mac
@login_required
def random_mac_address(request):
data = {}
data['mac'] = _get_random_mac_address()
return HttpResponse(json.dumps(data))
@login_required
def guess_clone_name(request):
dhcp_file = '/srv/webvirtcloud/dhcpd.conf'
@ -940,6 +967,7 @@ def guess_clone_name(request):
return HttpResponse(json.dumps({'name': hostname}))
return HttpResponse(json.dumps({}))
@login_required
def check_instance(request, vname):
check_instance = Instance.objects.filter(name=vname)