Skip to content

Commit 156dbf4

Browse files
yadvrDaanHoogland
authored andcommitted
agent: Add logging to libvirt qemu hook (#2554)
This allows logging to the default libvirt qemu hook Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent b69c378 commit 156dbf4

1 file changed

Lines changed: 28 additions & 9 deletions

File tree

agent/bindir/libvirtqemuhook.in

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,78 @@
66
# to you under the Apache License, Version 2.0 (the
77
# "License"); you may not use this file except in compliance
88
# with the License. You may obtain a copy of the License at
9-
#
9+
#
1010
# http://www.apache.org/licenses/LICENSE-2.0
11-
#
11+
#
1212
# Unless required by applicable law or agreed to in writing,
1313
# software distributed under the License is distributed on an
1414
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1515
# KIND, either express or implied. See the License for the
1616
# specific language governing permissions and limitations
1717
# under the License.
18-
import sys
18+
19+
import logging
1920
import re
21+
import sys
2022
from xml.dom.minidom import parse
2123
from cloudutils.configFileOps import configFileOps
2224
from cloudutils.networkConfig import networkConfig
25+
26+
logging.basicConfig(filename='/var/log/libvirt/qemu-hook.log',
27+
filemode='a',
28+
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
29+
datefmt='%H:%M:%S',
30+
level=logging.INFO)
31+
logger = logging.getLogger('qemu-hook')
32+
2333
def isOldStyleBridge(brName):
2434
if brName.find("cloudVirBr") == 0:
2535
return True
2636
else:
2737
return False
38+
2839
def isNewStyleBridge(brName):
2940
if brName.startswith('brvx-'):
3041
return False
3142
if re.match(r"br(\w+)-(\d+)", brName) == None:
3243
return False
3344
else:
3445
return True
46+
3547
def getGuestNetworkDevice():
36-
netlib = networkConfig()
48+
netlib = networkConfig()
3749
cfo = configFileOps("/etc/cloudstack/agent/agent.properties")
3850
guestDev = cfo.getEntry("guest.network.device")
3951
enslavedDev = netlib.getEnslavedDev(guestDev, 1)
4052
return enslavedDev.split(".")[0]
53+
4154
def handleMigrateBegin():
4255
try:
4356
domain = parse(sys.stdin)
4457
for interface in domain.getElementsByTagName("interface"):
4558
source = interface.getElementsByTagName("source")[0]
4659
bridge = source.getAttribute("bridge")
4760
if isOldStyleBridge(bridge):
48-
vlanId = bridge.replace("cloudVirBr","")
61+
vlanId = bridge.replace("cloudVirBr", "")
4962
elif isNewStyleBridge(bridge):
50-
vlanId = re.sub(r"br(\w+)-","",bridge)
63+
vlanId = re.sub(r"br(\w+)-", "", bridge)
5164
else:
5265
continue
5366
phyDev = getGuestNetworkDevice()
54-
newBrName="br" + phyDev + "-" + vlanId
67+
newBrName = "br" + phyDev + "-" + vlanId
5568
source.setAttribute("bridge", newBrName)
5669
print(domain.toxml())
5770
except:
5871
pass
72+
73+
5974
if __name__ == '__main__':
6075
if len(sys.argv) != 5:
6176
sys.exit(0)
6277

63-
if sys.argv[2] == "migrate" and sys.argv[3] == "begin":
64-
handleMigrateBegin()
78+
# For docs refer https://libvirt.org/hooks.html#qemu
79+
logger.debug("Executing qemu hook with args: %s" % sys.argv)
80+
action, status = sys.argv[2:4]
81+
82+
if action == "migrate" and status == "begin":
83+
handleMigrateBegin()

0 commit comments

Comments
 (0)