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

add/remove guest agent channel function added

This commit is contained in:
catborise 2019-12-24 17:19:11 +03:00
parent ecf31b0b5b
commit 9d58e56d16
3 changed files with 76 additions and 12 deletions

View file

@ -1415,7 +1415,7 @@
<p>{% trans "To set instance vCPUs hotpluggable" %}</p> <p>{% trans "To set instance vCPUs hotpluggable" %}</p>
<form class="form-horizontal" method="post" role="form">{% csrf_token %} <form class="form-horizontal" method="post" role="form">{% csrf_token %}
<div class="form-group"> <div class="form-group">
<label for="video_model_select" class="col-sm-3 control-label">{% trans "vCPU Hot Plug" %}</label> <label for="vcpu_hotplug" class="col-sm-3 control-label">{% trans "vCPU Hot Plug" %}</label>
<div class="col-sm-6"> <div class="col-sm-6">
<div class="input-group"> <div class="input-group">
<select id="vcpu_hotplug" class="form-control" name="vcpu_hotplug"> <select id="vcpu_hotplug" class="form-control" name="vcpu_hotplug">
@ -1435,6 +1435,31 @@
</form> </form>
<div class="clearfix"></div> <div class="clearfix"></div>
</div> </div>
<div class="well">
<p>{% trans "To Enable/Disable Qemu Guest Agent. Status:" %}
{% if guest_agent_ready %}
<label class="label label-success">{% trans 'Connected' %}</label>
{% else %}
<label class="label label-danger">{% trans 'Disconnected' %}</label>
{% endif %}</p>
<form class="form-horizontal" method="post" role="form">{% csrf_token %}
<div class="form-group">
<label for="guest_agent" class="col-sm-3 control-label">{% trans "Qemu Guest Agent" %}</label>
<div class="col-sm-6">
<div class="input-group">
<select id="guest_agent" class="form-control" name="guest_agent">
<option value="True" {% if guest_agent %} selected {% endif %}>{% trans 'Enabled' %}</option>
<option value="False" {% if not guest_agent %} selected {% endif %}>{% trans 'Disabled' %}</option>
</select>
<span class="input-group-btn">
<button type="submit" class="btn btn-success" name="set_guest_agent">{% trans "Set" %}</button>
</span>
</div>
</div>
</div>
</form>
<div class="clearfix"></div>
</div>
</div> </div>
{% endif %} {% endif %}
</div> </div>

View file

@ -303,6 +303,8 @@ def instance(request, compute_id, vname):
console_port = conn.get_console_port() console_port = conn.get_console_port()
console_keymap = conn.get_console_keymap() console_keymap = conn.get_console_keymap()
console_listen_address = conn.get_console_listen_addr() console_listen_address = conn.get_console_listen_addr()
guest_agent = False if conn.get_guest_agent() is None else True
guest_agent_ready = conn.is_agent_ready()
video_model = conn.get_video_model() video_model = conn.get_video_model()
snapshots = sorted(conn.get_snapshot(), reverse=True, key=lambda k: k['date']) snapshots = sorted(conn.get_snapshot(), reverse=True, key=lambda k: k['date'])
inst_xml = conn._XMLDesc(VIR_DOMAIN_XML_SECURE) inst_xml = conn._XMLDesc(VIR_DOMAIN_XML_SECURE)
@ -810,6 +812,17 @@ def instance(request, compute_id, vname):
return HttpResponseRedirect(request.get_full_path() + '#vncsettings') return HttpResponseRedirect(request.get_full_path() + '#vncsettings')
if request.user.is_superuser: if request.user.is_superuser:
if 'set_guest_agent' in request.POST:
status = request.POST.get('guest_agent')
if status == 'True':
conn.add_guest_agent()
if status == 'False':
conn.remove_guest_agent()
msg = _("Set Quest Agent {}".format(status))
addlogmsg(request.user.username, instance.name, msg)
return HttpResponseRedirect(request.get_full_path() + '#options')
if 'set_video_model' in request.POST: if 'set_video_model' in request.POST:
video_model = request.POST.get('video_model', 'vga') video_model = request.POST.get('video_model', 'vga')
conn.set_video_model(video_model) conn.set_video_model(video_model)

View file

@ -336,7 +336,7 @@ class wvmInstance(wvmConnect):
if not self.get_status() == 1: if not self.get_status() == 1:
return return
if self.agent_ready(): if self.is_agent_ready():
self._ip_cache["qemuga"] = self._get_interface_addresses( self._ip_cache["qemuga"] = self._get_interface_addresses(
VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT) VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT)
@ -1381,19 +1381,37 @@ class wvmInstance(wvmConnect):
tree = etree.fromstring(self._XMLDesc(0)) tree = etree.fromstring(self._XMLDesc(0))
for direct in tree.xpath("/domain/devices/interface/bandwidth/{}".format(direction)): for direct in tree.xpath("/domain/devices/interface/bandwidth/{}".format(direction)):
band_el = direct.getparent() band_el = direct.getparent()
interface_el = band_el.getparent() # parent bandwidth,it parent is interface interface_el = band_el.getparent() # parent bandwidth,its parent is interface
parent_mac = interface_el.xpath('mac/@address') parent_mac = interface_el.xpath('mac/@address')
if parent_mac[0] == mac: if parent_mac[0] == mac:
band_el.remove(direct) band_el.remove(direct)
self.wvm.defineXML(etree.tostring(tree)) self.wvm.defineXML(etree.tostring(tree))
def agent_ready(self): def add_guest_agent(self):
channel_xml = """
<channel type='unix'>
<target type='virtio' name='org.qemu.guest_agent.0'/>
</channel>
""" """
Return connected state of an agent. if self.get_status() == 1:
""" self.instance.attachDeviceFlags(channel_xml, VIR_DOMAIN_AFFECT_LIVE)
# we need to get a fresh agent channel object on each call so it self.instance.attachDeviceFlags(channel_xml, VIR_DOMAIN_AFFECT_CONFIG)
# reflects the current state if self.get_status() == 5:
self.instance.attachDeviceFlags(channel_xml, VIR_DOMAIN_AFFECT_CONFIG)
def remove_guest_agent(self):
tree = etree.fromstring(self._XMLDesc(0))
for target in tree.xpath("/domain/devices/channel[@type='unix']/target[@name='org.qemu.guest_agent.0']"):
parent = target.getparent()
channel_xml = etree.tostring(parent)
if self.get_status() == 1:
self.instance.detachDeviceFlags(channel_xml, VIR_DOMAIN_AFFECT_LIVE)
self.instance.detachDeviceFlags(channel_xml, VIR_DOMAIN_AFFECT_CONFIG)
if self.get_status() == 5:
self.instance.detachDeviceFlags(channel_xml, VIR_DOMAIN_AFFECT_CONFIG)
def get_guest_agent(self):
def _get_agent(doc): def _get_agent(doc):
""" """
Return agent channel object if it is defined. Return agent channel object if it is defined.
@ -1406,8 +1424,16 @@ class wvmInstance(wvmConnect):
return channel return channel
return None return None
dev = util.get_xml_path(self._XMLDesc(0), func=_get_agent) return util.get_xml_path(self._XMLDesc(0), func=_get_agent)
if len(dev) > 0:
def is_agent_ready(self):
"""
Return connected state of an agent.
"""
# we need to get a fresh agent channel object on each call so it
# reflects the current state
dev = self.get_guest_agent()
if dev is not None:
states = dev.xpath("target/@state") states = dev.xpath("target/@state")
state = states[0] if len(states) > 0 else '' state = states[0] if len(states) > 0 else ''
if state == "connected": if state == "connected":