|
6 | 6 | # to you under the Apache License, Version 2.0 (the |
7 | 7 | # "License"); you may not use this file except in compliance |
8 | 8 | # with the License. You may obtain a copy of the License at |
9 | | -# |
| 9 | +# |
10 | 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
11 | | -# |
| 11 | +# |
12 | 12 | # Unless required by applicable law or agreed to in writing, |
13 | 13 | # software distributed under the License is distributed on an |
14 | 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | 15 | # KIND, either express or implied. See the License for the |
16 | 16 | # specific language governing permissions and limitations |
17 | 17 | # under the License. |
18 | | -import sys |
| 18 | + |
| 19 | +import logging |
19 | 20 | import re |
| 21 | +import sys |
| 22 | +import os |
| 23 | +import subprocess |
| 24 | +from threading import Timer |
20 | 25 | from xml.dom.minidom import parse |
21 | 26 | from cloudutils.configFileOps import configFileOps |
22 | 27 | from cloudutils.networkConfig import networkConfig |
| 28 | + |
| 29 | +logging.basicConfig(filename='/var/log/libvirt/qemu-hook.log', |
| 30 | + filemode='a', |
| 31 | + format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s', |
| 32 | + datefmt='%H:%M:%S', |
| 33 | + level=logging.INFO) |
| 34 | +logger = logging.getLogger('qemu-hook') |
| 35 | + |
| 36 | +customDir = "/etc/libvirt/hooks/custom" |
| 37 | +customDirPermissions = 0744 |
| 38 | +timeoutSeconds = 10 * 60 |
| 39 | +validQemuActions = ['prepare', 'start', 'started', 'stopped', 'release', 'migrate', 'restore', 'reconnect', 'attach'] |
| 40 | + |
23 | 41 | def isOldStyleBridge(brName): |
24 | 42 | if brName.find("cloudVirBr") == 0: |
25 | | - return True |
| 43 | + return True |
26 | 44 | else: |
27 | | - return False |
| 45 | + return False |
| 46 | + |
28 | 47 | def isNewStyleBridge(brName): |
29 | 48 | if brName.startswith('brvx-'): |
30 | 49 | return False |
31 | 50 | if re.match(r"br(\w+)-(\d+)", brName) == None: |
32 | | - return False |
| 51 | + return False |
33 | 52 | else: |
34 | | - return True |
| 53 | + return True |
| 54 | + |
35 | 55 | def getGuestNetworkDevice(): |
36 | | - netlib = networkConfig() |
| 56 | + netlib = networkConfig() |
37 | 57 | cfo = configFileOps("/etc/cloudstack/agent/agent.properties") |
38 | 58 | guestDev = cfo.getEntry("guest.network.device") |
39 | 59 | enslavedDev = netlib.getEnslavedDev(guestDev, 1) |
40 | 60 | return enslavedDev.split(".")[0] |
| 61 | + |
41 | 62 | def handleMigrateBegin(): |
42 | 63 | try: |
43 | 64 | domain = parse(sys.stdin) |
44 | 65 | for interface in domain.getElementsByTagName("interface"): |
45 | 66 | source = interface.getElementsByTagName("source")[0] |
46 | 67 | bridge = source.getAttribute("bridge") |
47 | 68 | if isOldStyleBridge(bridge): |
48 | | - vlanId = bridge.replace("cloudVirBr","") |
| 69 | + vlanId = bridge.replace("cloudVirBr", "") |
49 | 70 | elif isNewStyleBridge(bridge): |
50 | | - vlanId = re.sub(r"br(\w+)-","",bridge) |
| 71 | + vlanId = re.sub(r"br(\w+)-", "", bridge) |
51 | 72 | else: |
52 | 73 | continue |
53 | 74 | phyDev = getGuestNetworkDevice() |
54 | | - newBrName="br" + phyDev + "-" + vlanId |
| 75 | + newBrName = "br" + phyDev + "-" + vlanId |
55 | 76 | source.setAttribute("bridge", newBrName) |
56 | 77 | print(domain.toxml()) |
57 | 78 | except: |
58 | 79 | pass |
| 80 | + |
| 81 | + |
| 82 | +def executeCustomScripts(sysArgs): |
| 83 | + createDirectoryIfNotExists(customDir, customDirPermissions) |
| 84 | + scripts = getCustomScriptsFromDirectory() |
| 85 | + |
| 86 | + for scriptName in scripts: |
| 87 | + executeScript(scriptName, sysArgs) |
| 88 | + |
| 89 | + |
| 90 | +def executeScript(scriptName, sysArgs): |
| 91 | + logger.info('Executing custom script: %s, parameters: %s' % (scriptName, ' '.join(map(str, sysArgs)))) |
| 92 | + path = customDir + os.path.sep + scriptName |
| 93 | + |
| 94 | + if not os.access(path, os.X_OK): |
| 95 | + logger.warning('Custom script: %s is not executable; skipping execution.' % scriptName) |
| 96 | + return |
| 97 | + |
| 98 | + try: |
| 99 | + process = subprocess.Popen([path] + sysArgs, stdout=subprocess.PIPE, |
| 100 | + stderr=subprocess.PIPE, shell=False) |
| 101 | + try: |
| 102 | + timer = Timer(timeoutSeconds, terminateProcess, [process, scriptName]) |
| 103 | + timer.start() |
| 104 | + output, error = process.communicate() |
| 105 | + |
| 106 | + if process.returncode == -15: |
| 107 | + logger.error('Custom script: %s terminated after timeout of %s second[s].' |
| 108 | + % (scriptName, timeoutSeconds)) |
| 109 | + return |
| 110 | + if process.returncode != 0: |
| 111 | + logger.info('return code: %s' % str(process.returncode)) |
| 112 | + raise Exception(error) |
| 113 | + logger.info('Custom script: %s finished successfully; output: \n%s' % |
| 114 | + (scriptName, str(output))) |
| 115 | + finally: |
| 116 | + timer.cancel() |
| 117 | + except (OSError, Exception) as e: |
| 118 | + logger.exception("Custom script: %s finished with error: \n%s" % (scriptName, e)) |
| 119 | + |
| 120 | + |
| 121 | +def terminateProcess(process, scriptName): |
| 122 | + logger.warning('Custom script: %s taking longer than %s second[s]; terminating..' % (scriptName, str(timeoutSeconds))) |
| 123 | + process.terminate() |
| 124 | + |
| 125 | + |
| 126 | +def getCustomScriptsFromDirectory(): |
| 127 | + return sorted(filter(lambda fileName: (fileName is not None) & (fileName != "") & ('_' in fileName) & |
| 128 | + (fileName.startswith((action + '_')) | fileName.startswith(('all' + '_'))), |
| 129 | + os.listdir(customDir)), key=lambda fileName: substringAfter(fileName, '_')) |
| 130 | + |
| 131 | + |
| 132 | +def createDirectoryIfNotExists(dir, permissions): |
| 133 | + if not os.path.exists(dir): |
| 134 | + logger.info('Directory %s does not exist; creating it.' % dir) |
| 135 | + os.makedirs(dir, permissions) |
| 136 | + |
| 137 | + |
| 138 | +def substringAfter(s, delimiter): |
| 139 | + return s.partition(delimiter)[2] |
| 140 | + |
| 141 | + |
59 | 142 | if __name__ == '__main__': |
60 | 143 | if len(sys.argv) != 5: |
61 | 144 | sys.exit(0) |
62 | 145 |
|
63 | | - if sys.argv[2] == "migrate" and sys.argv[3] == "begin": |
64 | | - handleMigrateBegin() |
| 146 | + # For docs refer https://libvirt.org/hooks.html#qemu |
| 147 | + logger.debug("Executing qemu hook with args: %s" % sys.argv) |
| 148 | + action, status = sys.argv[2:4] |
| 149 | + |
| 150 | + if action not in validQemuActions: |
| 151 | + logger.error('The given action: %s, is not a valid libvirt qemu operation.' % action) |
| 152 | + sys.exit(0) |
| 153 | + |
| 154 | + if action == "migrate" and status == "begin": |
| 155 | + handleMigrateBegin() |
| 156 | + |
| 157 | + executeCustomScripts(sys.argv[1:]) |
0 commit comments