summaryrefslogtreecommitdiff
path: root/ironic/drivers/modules/ansible/playbooks/library/root_hints.py
blob: eeb9b7e68739d8ccbfa98d460727e6f646235e44 (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
93
94
95
96
97
# -*- coding: utf-8 -*-
#
# 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.

GIB = 1 << 30

EXTRA_PARAMS = set(['wwn', 'serial', 'wwn_with_extension',
                    'wwn_vendor_extension'])


# NOTE: ansible calculates device size as float with 2-digits precision,
# Ironic requires size in GiB, if we will use ansible size parameter
# a bug is possible for devices > 1 TB
def size_gib(device_info):
    sectors = device_info.get('sectors')
    sectorsize = device_info.get('sectorsize')
    if sectors is None or sectorsize is None:
        return '0'

    return str((int(sectors) * int(sectorsize)) // GIB)


def merge_devices_info(devices, devices_wwn):
    merged_info = devices.copy()
    for device in merged_info:
        if device in devices_wwn:
            merged_info[device].update(devices_wwn[device])

        # replace size
        merged_info[device]['size'] = size_gib(merged_info[device])

    return merged_info


def root_hint(hints, devices):
    hint = None
    name = hints.pop('name', None)
    for device in devices:
        for key in hints:
            if hints[key] != devices[device].get(key):
                break
        else:
            # If multiple hints are specified, a device must satisfy all
            # the hints
            dev_name = '/dev/' + device
            if name is None or name == dev_name:
                hint = dev_name
                break

    return hint


def main():
    module = AnsibleModule(  # noqa This is normal for Ansible modules.
        argument_spec=dict(
            root_device_hints=dict(required=True, type='dict'),
            ansible_devices=dict(required=True, type='dict'),
            ansible_devices_wwn=dict(required=True, type='dict')
        ),
        supports_check_mode=True)

    hints = module.params['root_device_hints']
    devices = module.params['ansible_devices']
    devices_wwn = module.params['ansible_devices_wwn']

    if not devices_wwn:
        extra = set(hints) & EXTRA_PARAMS
        if extra:
            module.fail_json(msg='Extra hints (supported by additional ansible'
                             ' module) are set but this information can not be'
                             ' collected. Extra hints: %s' % ', '.join(extra))

    devices_info = merge_devices_info(devices, devices_wwn or {})
    hint = root_hint(hints, devices_info)

    if hint is None:
        module.fail_json(msg='Root device hints are set, but none of the '
                         'devices satisfy them. Collected devices info: %s'
                         % devices_info)

    ret_data = {'ansible_facts': {'ironic_root_device': hint}}
    module.exit_json(**ret_data)


from ansible.module_utils.basic import *  # noqa
if __name__ == '__main__':
    main()