summaryrefslogtreecommitdiffstats
path: root/recipes-extended/libvirt/libvirt
diff options
context:
space:
mode:
Diffstat (limited to 'recipes-extended/libvirt/libvirt')
-rwxr-xr-xrecipes-extended/libvirt/libvirt/hook_support.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/recipes-extended/libvirt/libvirt/hook_support.py b/recipes-extended/libvirt/libvirt/hook_support.py
new file mode 100755
index 00000000..c3eb8b3a
--- /dev/null
+++ b/recipes-extended/libvirt/libvirt/hook_support.py
@@ -0,0 +1,55 @@
1#!/usr/bin/env python
2#
3# Copyright (C) 2014 Wind River Systems, Inc.
4#
5# Description: Calls other scripts in order, so that there can be multiple
6# scripts for a particular hook tied to libvirt.
7#
8# For example: If this script is called "qemu" and is in the
9# "/etc/libvirt/hooks/" directory. This script will be called by libvirt
10# when certain actions are performed on a qemu guest. This script then
11# will in turn call any executable file in the same directory matching
12# "qemu-" followed by at least one alpha-numeric character. The scripts
13# are called in order (based on the python sorted function), and once any
14# sub-script returns a non-zero exit code no futher scripts are called.
15# This script passes any arguments it retrieves on the command line and a
16# copy of stdin to the sub-scripts it calls.
17
18import os
19import re
20import subprocess
21import sys
22
23def main():
24 return_value = 0
25 hook_name = os.path.basename( __file__ )
26 try:
27 hook_dir = os.path.dirname( __file__ )
28 hook_args = sys.argv
29 del hook_args[ 0 ] # Remove executable from argument list
30
31 # Save stdin, so we can pass it to each sub-script.
32 if sys.stdin.isatty():
33 stdin_save = [ "" ]
34 else:
35 stdin_save = sys.stdin.readlines()
36 # Match the name name of the hook + a dash + atleast
37 # one alpha-numeric character.
38 matcher = re.compile( "%s-\w+" % hook_name )
39 for file_name in sorted( os.listdir( hook_dir ) ):
40 file_path = os.path.join( hook_dir, file_name )
41 if matcher.match( file_name ) \
42 and os.access( file_path, os.X_OK ) \
43 and os.path.isfile( file_path ) \
44 and return_value == 0:
45 cmd = [ file_path ] + hook_args
46 p = subprocess.Popen( cmd, stdin=subprocess.PIPE )
47 p.communicate( input = ''.join( stdin_save ) )[0]
48 return_value = p.wait()
49 except Exception as e:
50 sys.stderr.write( "%s hook error: %s\n" % ( hook_name, str( e ) ) )
51 return_value = 1
52 return return_value
53
54if __name__ == '__main__':
55 sys.exit( main() )