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

instance/clone: move random_mac logic into instances.views.random_mac_address url ^random_mac_address/

fix instance/clone: get random mac if dhcp not found
This commit is contained in:
Ing. Jan KRCMAR 2018-07-26 11:35:37 +02:00
parent 9f7ec62226
commit 13ffaae522
3 changed files with 30 additions and 12 deletions

View file

@ -1212,14 +1212,9 @@
</script> </script>
<script> <script>
function random_mac(net) { function random_mac(net) {
var hexDigits = "0123456789abcdef"; $.getJSON('/instance/random_mac_address/', function(data) {
var macAddress="52:54:00:"; $('input[name="clone-net-mac-'+net+'"]').val(data['mac']);
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);
}; };
</script> </script>
<script> <script>
@ -1325,7 +1320,11 @@
$("#vdi_url").attr("href", data); $("#vdi_url").attr("href", data);
}); });
}); });
{% if not request.user.is_superuser %} {% if request.user.is_superuser %}
$(document).ready(function () {
random_mac(0);
});
{% else %}
$('#select_clone_name').on('change', function () { $('#select_clone_name').on('change', function () {
update_clone_disk_name($(this).val()); update_clone_disk_name($(this).val());
guess_mac_address('#select_clone_name', 0); guess_mac_address('#select_clone_name', 0);

View file

@ -8,6 +8,8 @@ urlpatterns = [
views.inst_graph, name='inst_graph'), views.inst_graph, name='inst_graph'),
url(r'^status/(?P<compute_id>[0-9]+)/(?P<vname>[\w\-\.]+)/$', url(r'^status/(?P<compute_id>[0-9]+)/(?P<vname>[\w\-\.]+)/$',
views.inst_status, name='inst_status'), views.inst_status, name='inst_status'),
url(r'^random_mac_address/$',
views.random_mac_address, name='random_mac_address'),
url(r'^guess_mac_address/(?P<vname>[\w\-\.]+)/$', url(r'^guess_mac_address/(?P<vname>[\w\-\.]+)/$',
views.guess_mac_address, name='guess_mac_address'), views.guess_mac_address, name='guess_mac_address'),
url(r'^guess_clone_name/$', url(r'^guess_clone_name/$',

View file

@ -5,7 +5,7 @@ import socket
import crypt import crypt
import re import re
import string import string
from random import choice import random
from bisect import insort from bisect import insort
from django.http import HttpResponse, HttpResponseRedirect from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
@ -901,7 +901,7 @@ def inst_graph(request, compute_id, vname):
def _get_dhcp_mac_address(vname): def _get_dhcp_mac_address(vname):
dhcp_file = '/srv/webvirtcloud/dhcpd.conf' dhcp_file = '/srv/webvirtcloud/dhcpd.conf'
mac = '52:54:00:' mac = ''
if os.path.isfile(dhcp_file): if os.path.isfile(dhcp_file):
with open(dhcp_file, 'r') as f: with open(dhcp_file, 'r') as f:
name_found = False name_found = False
@ -916,7 +916,24 @@ def _get_dhcp_mac_address(vname):
@login_required @login_required
def guess_mac_address(request, vname): def guess_mac_address(request, vname):
data = { 'vname': vname } data = { 'vname': vname }
data['mac'] = _get_dhcp_mac_address(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)) return HttpResponse(json.dumps(data))
@login_required @login_required