From 9c37dcc2dcf7fec80dcd333779f1dc1711a47b31 Mon Sep 17 00:00:00 2001 From: "Ing. Jan KRCMAR" Date: Tue, 28 Aug 2018 10:55:27 +0200 Subject: [PATCH 1/3] #addInstanceNetwork modal box --- .../templates/add_instance_network_block.html | 42 +++++++++++++++++++ instances/templates/instance.html | 14 ++++--- instances/views.py | 11 +++++ vrtManager/instance.py | 17 ++++++++ 4 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 instances/templates/add_instance_network_block.html diff --git a/instances/templates/add_instance_network_block.html b/instances/templates/add_instance_network_block.html new file mode 100644 index 0000000..b990eec --- /dev/null +++ b/instances/templates/add_instance_network_block.html @@ -0,0 +1,42 @@ +{% load i18n %} +{% if request.user.is_superuser and status == 5 %} + + + + + + +{% endif %} diff --git a/instances/templates/instance.html b/instances/templates/instance.html index 04e67da..f569297 100644 --- a/instances/templates/instance.html +++ b/instances/templates/instance.html @@ -827,7 +827,10 @@ {% endif %} {% if request.user.is_superuser %}
-

{% trans "Assign network device to bridge" %}

+

+ {% trans "Assign network device to bridge" %} + {% include 'add_instance_network_block.html' %} +

{% csrf_token %}

{% trans "Network devices" %}

{% for network in networks %} @@ -854,7 +857,7 @@ {% ifequal status 5 %} {% else %} - + {% endifequal %}
@@ -898,7 +901,7 @@
+ onclick="random_mac('clone-net-mac-{{ forloop.counter0 }}')" style="margin-top: 2px;">{% trans "Random" %}
@@ -1230,7 +1233,7 @@ @@ -1339,7 +1342,8 @@ }); {% if request.user.is_superuser %} $(document).ready(function () { - random_mac(0); + random_mac('clone-net-mac-0'); + random_mac('add-net-mac'); }); {% else %} $('#select_clone_name').on('change', function () { diff --git a/instances/views.py b/instances/views.py index d479bfe..4159c7d 100644 --- a/instances/views.py +++ b/instances/views.py @@ -696,6 +696,17 @@ def instance(request, compute_id, vname): messages.success(request, msg) return HttpResponseRedirect(request.get_full_path() + '#network') + if 'add_network' in request.POST: + mac = request.POST.get('add-net-mac') + network = request.POST.get('add-net-network') + + conn.add_network(mac, network) + msg = _("Edit network") + addlogmsg(request.user.username, instance.name, msg) + msg = _("Network Devices are changed. Please reboot instance to activate.") + messages.success(request, msg) + return HttpResponseRedirect(request.get_full_path() + '#network') + if 'add_owner' in request.POST: user_id = int(request.POST.get('user_id', '')) diff --git a/vrtManager/instance.py b/vrtManager/instance.py index 5cb9390..42bf19f 100644 --- a/vrtManager/instance.py +++ b/vrtManager/instance.py @@ -742,6 +742,23 @@ class wvmInstance(wvmConnect): return self.get_instance(clone_data['name']).UUIDString() + def add_network(self, mac_address, network, interface_type='bridge', model='virtio'): + tree = ElementTree.fromstring(self._XMLDesc(0)) + net = self.get_network(network) + xml_interface = """ + + + + + + """ % (interface_type, mac_address, net.bridgeName(), model) + if self.get_status() == 5: + devices = tree.find('devices') + elm_interface = ElementTree.fromstring(xml_interface) + devices.append(elm_interface) + xmldom = ElementTree.tostring(tree) + self._defineXML(xmldom) + def change_network(self, network_data): xml = self._XMLDesc(VIR_DOMAIN_XML_SECURE) tree = ElementTree.fromstring(xml) From b4da65564405c09c4c9a5e96fbfb607b12edec2d Mon Sep 17 00:00:00 2001 From: "Ing. Jan KRCMAR" Date: Tue, 28 Aug 2018 12:18:35 +0200 Subject: [PATCH 2/3] instance/network: allow to select compute interface, previously only compute network --- .../templates/add_instance_network_block.html | 5 +++- instances/templates/instance.html | 7 ++++-- instances/views.py | 18 ++++++++++--- vrtManager/instance.py | 25 +++++++++++++------ 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/instances/templates/add_instance_network_block.html b/instances/templates/add_instance_network_block.html index b990eec..990c524 100644 --- a/instances/templates/add_instance_network_block.html +++ b/instances/templates/add_instance_network_block.html @@ -25,7 +25,10 @@
diff --git a/instances/templates/instance.html b/instances/templates/instance.html index f569297..f103115 100644 --- a/instances/templates/instance.html +++ b/instances/templates/instance.html @@ -846,9 +846,12 @@ diff --git a/instances/views.py b/instances/views.py index 4159c7d..ac188ed 100644 --- a/instances/views.py +++ b/instances/views.py @@ -321,6 +321,13 @@ def instance(request, compute_id, vname): if dev not in existing_devs: return dev raise Exception(_('None available device name')) + + def get_network_tuple(network_source_str): + network_source_pack = network_source_str.split(":", 1) + if len(network_source_pack) > 1: + return (network_source_pack[1], network_source_pack[0]) + else: + return (network_source_pack[0], 'net') try: conn = wvmInstance(compute.hostname, @@ -329,6 +336,7 @@ def instance(request, compute_id, vname): compute.type, vname) compute_networks = sorted(conn.get_networks()) + compute_interfaces = sorted(conn.get_ifaces()) status = conn.get_status() autostart = conn.get_autostart() vcpu = conn.get_vcpu() @@ -686,7 +694,11 @@ def instance(request, compute_id, vname): network_data = {} for post in request.POST: - if post.startswith('net-'): + if post.startswith('net-source-'): + (source, source_type) = get_network_tuple(request.POST.get(post)) + network_data[post] = source + network_data[post + '-type'] = source_type + elif post.startswith('net-'): network_data[post] = request.POST.get(post, '') conn.change_network(network_data) @@ -698,9 +710,9 @@ def instance(request, compute_id, vname): if 'add_network' in request.POST: mac = request.POST.get('add-net-mac') - network = request.POST.get('add-net-network') + (source, source_type) = get_network_tuple(request.POST.get('add-net-network')) - conn.add_network(mac, network) + conn.add_network(mac, source, source_type) msg = _("Edit network") addlogmsg(request.user.username, instance.name, msg) msg = _("Network Devices are changed. Please reboot instance to activate.") diff --git a/vrtManager/instance.py b/vrtManager/instance.py index 42bf19f..ae04b4a 100644 --- a/vrtManager/instance.py +++ b/vrtManager/instance.py @@ -742,16 +742,25 @@ class wvmInstance(wvmConnect): return self.get_instance(clone_data['name']).UUIDString() - def add_network(self, mac_address, network, interface_type='bridge', model='virtio'): + def get_bridge_name(self, source, source_type='net'): + if source_type == 'iface': + iface = self.get_iface(source) + bridge_name = iface.name() + else: + net = self.get_network(source) + bridge_name = net.bridgeName() + return bridge_name + + def add_network(self, mac_address, source, source_type='net', interface_type='bridge', model='virtio'): tree = ElementTree.fromstring(self._XMLDesc(0)) - net = self.get_network(network) + bridge_name = self.get_bridge_name(source, source_type) xml_interface = """ - """ % (interface_type, mac_address, net.bridgeName(), model) + """ % (interface_type, mac_address, bridge_name, model) if self.get_status() == 5: devices = tree.find('devices') elm_interface = ElementTree.fromstring(xml_interface) @@ -764,13 +773,15 @@ class wvmInstance(wvmConnect): tree = ElementTree.fromstring(xml) for num, interface in enumerate(tree.findall('devices/interface')): - net = self.get_network(network_data['net-source-' + str(num)]) + net_source = network_data['net-source-' + str(num)] + net_source_type = network_data['net-source-' + str(num) + '-type'] + net_mac = network_data['net-mac-' + str(num)] + bridge_name = self.get_bridge_name(net_source, net_source_type) if interface.get('type') == 'bridge': source = interface.find('mac') - source.set('address', network_data['net-mac-' + str(num)]) + source.set('address', net_mac) source = interface.find('source') - source.set('bridge', net.bridgeName()) - source.set('network', net.name()) + source.set('bridge', bridge_name) new_xml = ElementTree.tostring(tree) self._defineXML(new_xml) From d5d56a2b503b32a34bc2808b0e7180f0f560491c Mon Sep 17 00:00:00 2001 From: "Ing. Jan KRCMAR" Date: Tue, 28 Aug 2018 12:35:40 +0200 Subject: [PATCH 3/3] instance/network: select actually configured net-source --- instances/templates/instance.html | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/instances/templates/instance.html b/instances/templates/instance.html index f103115..f42c050 100644 --- a/instances/templates/instance.html +++ b/instances/templates/instance.html @@ -845,22 +845,19 @@
{% endfor %} {% ifequal status 5 %} - + {% else %} - + {% endifequal %}
@@ -1371,11 +1368,7 @@