ansible: update dpdk, trex and samplevnf install
[yardstick.git] / ansible / library / find_kernel.py
1 #!/usr/bin/env python
2 # Copyright (c) 2017 Intel Corporation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import os
17
18 DOCUMENTATION = '''
19 ---
20 module: find_kernel
21 short_description: Look for the system kernel on the filesystem
22 description:
23     - We need to find the kernel on non-booted systems, disk images, chroots, etc.
24     To do this we check /lib/modules and look for the kernel that matches the running
25     kernle, or failing that we look for the highest-numbered kernel
26 options:
27   kernel: starting kernel to check
28   module_dir: Override kernel module dir, default /lib/modules
29 '''
30
31 LIB_MODULES = "/lib/modules"
32
33
34 def try_int(s, *args):
35     """Convert to integer if possible."""
36     try:
37         return int(s)
38     except (TypeError, ValueError):
39         return args[0] if args else s
40
41
42 def convert_ints(fields, orig):
43     return tuple((try_int(f) for f in fields)), orig
44
45
46 def main():
47     module = AnsibleModule(
48         argument_spec={
49             'kernel': {'required': True, 'type': 'str'},
50             'module_dir': {'required': False, 'type': 'str', 'default': LIB_MODULES},
51         }
52     )
53     params = module.params
54     kernel = params['kernel']
55     module_dir = params['module_dir']
56
57     if os.path.isdir(os.path.join(module_dir, kernel)):
58         module.exit_json(changed=False, kernel=kernel)
59
60     kernel_dirs = os.listdir(module_dir)
61     kernels = sorted((convert_ints(re.split('[-.]', k), k) for k in kernel_dirs), reverse=True)
62     try:
63         newest_kernel = kernels[0][-1]
64     except IndexError:
65         module.fail_json(msg="Unable to find kernels in {}".format(module_dir))
66
67     if os.path.isdir(os.path.join(module_dir, newest_kernel)):
68         module.exit_json(changed=False, kernel=newest_kernel)
69     else:
70         return kernel
71
72     module.fail_json(msg="Unable to kernel other than {}".format(kernel))
73
74
75 # <<INCLUDE_ANSIBLE_MODULE_COMMON>>
76 from ansible.module_utils.basic import *  # noqa
77
78 if __name__ == '__main__':
79     main()
80
81 """
82
83 get kernel from uname,  ansible_kernel
84 look for that kernel in /lib/modules
85 if that kernel doens't exist
86 sort lib/modules
87 use latest
88
89 parse grub
90
91
92
93 """