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

adds link state option for instance networks. Enable/disable link while instance running

This commit is contained in:
catborise 2019-12-24 14:54:04 +03:00
parent 01f2290dd9
commit ecf31b0b5b
3 changed files with 50 additions and 10 deletions

View file

@ -878,6 +878,7 @@
<th>{% trans 'MAC' %}</th>
<th>{% trans 'IP Address' %}</th>
<th>{% trans 'Source' %}</th>
<th>{% trans 'LinkState' %}</th>
<th>{% trans 'Filter' %}</th>
<th>{% trans 'Qos' %}</th>
<th>{% trans 'Actions' %}</th>
@ -886,11 +887,20 @@
<tbody>
{% for network in networks %}
<tr>
<td class="col-sm-2"><label>eth{{ forloop.counter0 }}({{ network.target|default:"no target" }})</label></td>
<td><label>{{ network.mac }}</label></td>
<td><label>{{ network.ipv4|default:"unknown" }}</label></td>
<td><label>{{ network.nic }}</label></td>
<td><label>{{ network.filterref|default:"None" }}</label></td>
<td class="col-sm-1">eth{{ forloop.counter0 }}({{ network.target|default:"no target" }})</td>
<td>{{ network.mac }}</td>
<td>{{ network.ipv4|default:"unknown" }}</td>
<td>{{ network.nic }}</td>
<td>
<form method="post">{% csrf_token %}
<input name="mac" value="{{ network.mac }}" hidden/>
<input name="set_link_state" value="{{ network.state }}" hidden/>
<input type="checkbox" {% if network.state == 'up' %} checked
{% endif %} onclick='submit();' />
{% trans 'active' %}
</form>
</td>
<td>{{ network.filterref|default:"None" }}</td>
<td>
<form class="form-horizontal" method="post" name="add_qos{{ forloop.counter0 }}" role="form">{% csrf_token %}
<input type="text" name="net-mac-{{ forloop.counter0 }}" value="{{ network.mac }}" hidden/>

View file

@ -873,6 +873,15 @@ def instance(request, compute_id, vname):
addlogmsg(request.user.username, instance.name, msg)
return HttpResponseRedirect(request.get_full_path() + '#network')
if 'set_link_state' in request.POST:
mac_address = request.POST.get('mac', '')
state = request.POST.get('set_link_state')
state = 'down' if state == 'up' else 'up'
conn.set_link_state(mac_address, state)
msg = _("Set Link State: {}".format(state))
addlogmsg(request.user.username, instance.name, msg)
return HttpResponseRedirect(request.get_full_path() + '#network')
if 'set_qos' in request.POST:
qos_dir = request.POST.get('qos_direction', '')
average = request.POST.get('qos_average') or 0

View file

@ -331,7 +331,6 @@ class wvmInstance(wvmConnect):
return {}
def refresh_interface_addresses(self):
self._ip_cache = {"qemuga": {}, "arp": {}}
if not self.get_status() == 1:
@ -353,6 +352,7 @@ class wvmInstance(wvmConnect):
mac_inst = net.xpath('mac/@address')[0]
nic_inst = net.xpath('source/@network|source/@bridge|source/@dev')[0]
target_inst = '' if not net.xpath('target/@dev') else net.xpath('target/@dev')[0]
link_state = 'up' if not net.xpath('link') else net.xpath('link/@state')[0]
filterref_inst = '' if not net.xpath('filterref/@filter') else net.xpath('filterref/@filter')[0]
if net.xpath('bandwidth/inbound'):
in_attr = net.xpath('bandwidth/inbound')[0]
@ -374,6 +374,7 @@ class wvmInstance(wvmConnect):
result.append({'mac': mac_inst,
'nic': nic_inst,
'target': target_inst,
'state': link_state,
'ipv4': ipv4,
'ipv6': ipv6,
'filterref': filterref_inst,
@ -1275,6 +1276,24 @@ class wvmInstance(wvmConnect):
new_xml = ElementTree.tostring(tree)
self._defineXML(new_xml)
def set_link_state(self, mac_address, state):
tree = etree.fromstring(self._XMLDesc(0))
for interface in tree.findall('devices/interface'):
source = interface.find('mac')
if source.get('address') == mac_address:
link = interface.find('link')
if link is not None:
interface.remove(link)
link_el = etree.Element("link")
link_el.attrib["state"] = state
interface.append(link_el)
new_xml = etree.tostring(interface)
if self.get_status() == 1:
self.instance.updateDeviceFlags(new_xml, VIR_DOMAIN_AFFECT_LIVE)
self.instance.updateDeviceFlags(new_xml, VIR_DOMAIN_AFFECT_CONFIG)
if self.get_status() == 5:
self.instance.updateDeviceFlags(new_xml, VIR_DOMAIN_AFFECT_CONFIG)
def _set_options(self, tree, options):
for o in ['title', 'description']:
option = tree.find(o)
@ -1388,10 +1407,12 @@ class wvmInstance(wvmConnect):
return None
dev = util.get_xml_path(self._XMLDesc(0), func=_get_agent)
state = dev.xpath("target/@state")[0]
if dev and state == "connected":
return True
return False
if len(dev) > 0:
states = dev.xpath("target/@state")
state = states[0] if len(states) > 0 else ''
if state == "connected":
return True
return False