Add ansible scripts to deploy Kubernetes
[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
17 DOCUMENTATION = '''
18 ---
19 module: find_kernel
20 short_description: Look for the system kernel on the filesystem
21 description:
22     - We need to find the kernel on non-booted systems, disk images, chroots, etc.
23     To do this we check /lib/modules and look for the kernel that matches the running
24     kernle, or failing that we look for the highest-numbered kernel
25 options:
26   kernel: starting kernel to check
27   module_dir: Override kernel module dir, default /lib/modules
28 '''
29
30 LIB_MODULES = "/lib/modules"
31
32
33 def try_int(s, *args):
34     """Convert to integer if possible."""
35     try:
36         return int(s)
37     except (TypeError, ValueError):
38         return args[0] if args else s
39
40
41 def convert_ints(fields, orig):
42     return tuple((try_int(f) for f in fields)), orig
43
44
45 def main():
46     module = AnsibleModule(
47         argument_spec={
48             'kernel': {'required': True, 'type': 'str'},
49             'module_dir': {'required': False, 'type': 'str', 'default': LIB_MODULES},
50         }
51     )
52     params = module.params
53     kernel = params['kernel']
54     module_dir = params['module_dir']
55
56     if os.path.isdir(os.path.join(module_dir, kernel)):
57         module.exit_json(changed=False, kernel=kernel)
58
59     kernel_dirs = os.listdir(module_dir)
60     kernels = sorted((convert_ints(re.split('[-.]', k), k) for k in kernel_dirs), reverse=True)
61     try:
62         newest_kernel = kernels[0][-1]
63     except IndexError:
64         module.fail_json(msg="Unable to find kernels in {}".format(module_dir))
65
66     if os.path.isdir(os.path.join(module_dir, newest_kernel)):
67         module.exit_json(changed=False, kernel=newest_kernel)
68     else:
69         return kernel
70
71     module.fail_json(msg="Unable to kernel other than {}".format(kernel))
72
73
74 # <<INCLUDE_ANSIBLE_MODULE_COMMON>>
75 from ansible.module_utils.basic import *  # noqa
76
77 if __name__ == '__main__':
78     main()
79
80 """
81
82 get kernel from uname,  ansible_kernel
83 look for that kernel in /lib/modules
84 if that kernel doens't exist
85 sort lib/modules
86 use latest
87
88 parse grub
89
90
91
92 """