Initial commit
[ta/infra-ansible.git] / roles / baremetal_provision / library / os_node_provision_check.py
1 #!/usr/bin/python
2
3 # Copyright 2019 Nokia
4
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import fnmatch
18
19 try:
20     import shade
21     from shade import meta
22     HAS_SHADE = True
23 except ImportError:
24     HAS_SHADE = False
25
26 DOCUMENTATION = '''
27 ---
28 module: os_node_provision_check
29 short_description: Retrieve facts about ironic nodes list provided,
30                    Returns a list of nodes which are not in deploying and failed states
31 version_added: "2.0"
32 description:
33     - Retrieve facts about ironic nodes list provided,
34       Returns a list of nodes which are not in deploying and failed states
35 requirements:
36     - "python >= 2.6"
37     - "shade"
38 options:
39    nodes_details:
40      description:
41        - List of dicts [{id: <ironic_node_id>, name: <ironic_node_name>},....], containing ironic node ids and names.
42          This modules runs through all the ironic nodes and returns list of nodes in deploying and failed state.
43      required: True
44      default: None
45 returns:
46      provision_pending_list
47      provision_failed_list
48 extends_documentation_fragment: openstack
49 '''
50
51 EXAMPLES = '''
52 # Gather facts about all nodes named C<web*>:
53 - os_node_provision_check:
54     cloud: rax-dfw
55     nodes_details:
56       - id: <id-1>
57         name: <name-1>
58       - id: <id-2>
59         name: <name-2>
60 '''
61
62
63 def main():
64
65     argument_spec = openstack_full_argument_spec(
66         nodes_details=dict(type='list', required=True), 
67         ironic_url=dict(required=False),
68     )
69     module_kwargs = openstack_module_kwargs()
70     module = AnsibleModule(argument_spec, **module_kwargs)
71
72     if not HAS_SHADE:
73         module.fail_json(msg='shade is required for this module')
74     if (module.params['auth_type'] in [None, 'None'] and
75             module.params['ironic_url'] is None):
76         module.fail_json(msg="Authentication appears to be disabled, "
77                              "Please define an ironic_url parameter")
78
79     if (module.params['ironic_url'] and
80             module.params['auth_type'] in [None, 'None']):
81         module.params['auth'] = dict(
82             endpoint=module.params['ironic_url']
83         )
84
85     try:
86         cloud = shade.operator_cloud(**module.params)
87
88         nodes_details = module.params['nodes_details']
89         provision_pending_list = []
90         provision_failed_list = []
91
92         for node in nodes_details:
93             node_name = node['name']
94             node_details = cloud.get_machine(node_name)
95
96             if node_details['provision_state'] in ['deploying', 'wait call-back']:
97                 provision_pending_list.append(node_name)
98
99             if node_details['provision_state'] in ['deploy failed']:
100                 node_provision_state = {}
101                 node_provision_state['name'] = node_name
102                 node_provision_state['provision_state'] = node_details['provision_state']
103                 provision_failed_list.append(node_provision_state)
104
105         module.exit_json(changed=False, ansible_facts=dict(provision_pending_list=provision_pending_list, provision_failed_list=provision_failed_list))
106
107     except shade.OpenStackCloudException as e:
108         module.fail_json(msg=str(e))
109
110 # this is magic, see lib/ansible/module_common.py
111 from ansible.module_utils.basic import *
112 from ansible.module_utils.openstack import *
113 if __name__ == '__main__':
114     main()