rafaelweingartner commented on a change in pull request #2554: agent: Add logging to libvirt qemu hook and cleanup URL: https://github.com/apache/cloudstack/pull/2554#discussion_r181040333
########## File path: agent/bindir/libvirtqemuhook.in ########## @@ -6,59 +6,78 @@ # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -import sys + +import logging import re +import sys from xml.dom.minidom import parse from cloudutils.configFileOps import configFileOps from cloudutils.networkConfig import networkConfig + +logging.basicConfig(filename='/var/log/libvirt/qemu-hook.log', + filemode='a', + format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s', + datefmt='%H:%M:%S', + level=logging.INFO) +logger = logging.getLogger('qemu-hook') + def isOldStyleBridge(brName): if brName.find("cloudVirBr") == 0: return True else: return False + def isNewStyleBridge(brName): if brName.startswith('brvx-'): return False if re.match(r"br(\w+)-(\d+)", brName) == None: return False else: return True + def getGuestNetworkDevice(): - netlib = networkConfig() + netlib = networkConfig() cfo = configFileOps("/etc/cloudstack/agent/agent.properties") guestDev = cfo.getEntry("guest.network.device") enslavedDev = netlib.getEnslavedDev(guestDev, 1) return enslavedDev.split(".")[0] + def handleMigrateBegin(): try: domain = parse(sys.stdin) for interface in domain.getElementsByTagName("interface"): source = interface.getElementsByTagName("source")[0] bridge = source.getAttribute("bridge") if isOldStyleBridge(bridge): - vlanId = bridge.replace("cloudVirBr","") + vlanId = bridge.replace("cloudVirBr", "") elif isNewStyleBridge(bridge): - vlanId = re.sub(r"br(\w+)-","",bridge) + vlanId = re.sub(r"br(\w+)-", "", bridge) else: continue phyDev = getGuestNetworkDevice() - newBrName="br" + phyDev + "-" + vlanId + newBrName = "br" + phyDev + "-" + vlanId source.setAttribute("bridge", newBrName) print(domain.toxml()) except: pass + + if __name__ == '__main__': if len(sys.argv) != 5: sys.exit(0) - if sys.argv[2] == "migrate" and sys.argv[3] == "begin": - handleMigrateBegin() + # For docs refer https://libvirt.org/hooks.html#qemu + logger.debug("Executing qemu hook with args: %s" % sys.argv) + action, status = sys.argv[2:4] Review comment: Just to make sure I understood this notation here. I was reading in the Internet about the notation to deal with arrays, and it seems that it is not inclusive (with respect of the first index used to define the range). I mean, if I want everything after the first parameter, which is the script name I can do the following: `user_args = sys.argv[1:]` Therefore, the code that we have here would assign parameter 3 to variable `action`, and parameter 4 to variable `status`. Is this true? I was under the impression that the variable `action` should be parameter 2, and `status` was expected to be parameter 3. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
