aboutsummaryrefslogtreecommitdiffstats
path: root/ansible/library/find_kernel.py
blob: c237baaf20eedd38002e0f7fb6cebab6ac215f7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
# Copyright (c) 2017 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


DOCUMENTATION = '''
---
module: find_kernel
short_description: Look for the system kernel on the filesystem
description:
    - We need to find the kernel on non-booted systems, disk images, chroots, etc.
    To do this we check /lib/modules and look for the kernel that matches the running
    kernle, or failing that we look for the highest-numbered kernel
options:
  kernel: starting kernel to check
  module_dir: Override kernel module dir, default /lib/modules
'''

LIB_MODULES = "/lib/modules"


def try_int(s, *args):
    """Convert to integer if possible."""
    try:
        return int(s)
    except (TypeError, ValueError):
        return args[0] if args else s


def convert_ints(fields, orig):
    return tuple((try_int(f) for f in fields)), orig


def main():
    module = AnsibleModule(
        argument_spec={
            'kernel': {'required': True, 'type': 'str'},
            'module_dir': {'required': False, 'type': 'str', 'default': LIB_MODULES},
        }
    )
    params = module.params
    kernel = params['kernel']
    module_dir = params['module_dir']

    if os.path.isdir(os.path.join(module_dir, kernel)):
        module.exit_json(changed=False, kernel=kernel)

    kernel_dirs = os.listdir(module_dir)
    kernels = sorted((convert_ints(re.split('[-.]', k), k) for k in kernel_dirs), reverse=True)
    try:
        newest_kernel = kernels[0][-1]
    except IndexError:
        module.fail_json(msg="Unable to find kernels in {}".format(module_dir))

    if os.path.isdir(os.path.join(module_dir, newest_kernel)):
        module.exit_json(changed=False, kernel=newest_kernel)
    else:
        return kernel

    module.fail_json(msg="Unable to kernel other than {}".format(kernel))


# <<INCLUDE_ANSIBLE_MODULE_COMMON>>
from ansible.module_utils.basic import *  # noqa

if __name__ == '__main__':
    main()

"""

get kernel from uname,  ansible_kernel
look for that kernel in /lib/modules
if that kernel doens't exist
sort lib/modules
use latest

parse grub



"""