Fix ironic problem
[ta/infra-ansible.git] / roles / baremetal_provision / library / os_ironic.py
1 #!/usr/bin/python
2 # coding: utf-8 -*-
3
4 # (c) 2014, Hewlett-Packard Development Company, L.P.
5 #
6 # This module is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This software is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this software.  If not, see <http://www.gnu.org/licenses/>.
18
19 ANSIBLE_METADATA = {'metadata_version': '1.1',
20                     'status': ['preview'],
21                     'supported_by': 'community'}
22
23
24 DOCUMENTATION = '''
25 ---
26 module: os_ironic
27 short_description: Create/Delete Bare Metal Resources from OpenStack
28 extends_documentation_fragment: openstack
29 author: "Monty Taylor (@emonty)"
30 version_added: "2.0"
31 description:
32     - Create or Remove Ironic nodes from OpenStack.
33 options:
34     state:
35       description:
36         - Indicates desired state of the resource
37       choices: ['present', 'absent']
38       default: present
39     uuid:
40       description:
41         - globally unique identifier (UUID) to be given to the resource. Will
42           be auto-generated if not specified, and name is specified.
43         - Definition of a UUID will always take precedence to a name value.
44       required: false
45       default: None
46     name:
47       description:
48         - unique name identifier to be given to the resource.
49       required: false
50       default: None
51     driver:
52       description:
53         - The name of the Ironic Driver to use with this node.
54       required: true
55       default: None
56     chassis_uuid:
57       description:
58         - Associate the node with a pre-defined chassis.
59       required: false
60       default: None
61     ironic_url:
62       description:
63         - If noauth mode is utilized, this is required to be set to the
64           endpoint URL for the Ironic API.  Use with "auth" and "auth_type"
65           settings set to None.
66       required: false
67       default: None
68     driver_info:
69       description:
70         - Information for this server's driver. Will vary based on which
71           driver is in use. Any sub-field which is populated will be validated
72           during creation.
73       suboptions:
74         power:
75             description:
76                 - Information necessary to turn this server on / off.
77                   This often includes such things as IPMI username, password, and IP address.
78             required: true
79         deploy:
80             description:
81                 - Information necessary to deploy this server directly, without using Nova. THIS IS NOT RECOMMENDED.
82         console:
83             description:
84                 - Information necessary to connect to this server's serial console.  Not all drivers support this.
85         management:
86             description:
87                 - Information necessary to interact with this server's management interface. May be shared by power_info in some cases.
88             required: true
89     nics:
90       description:
91         - 'A list of network interface cards, eg, " - mac: aa:bb:cc:aa:bb:cc"'
92       required: true
93     properties:
94       description:
95         - Definition of the physical characteristics of this server, used for scheduling purposes
96       suboptions:
97         cpu_arch:
98           description:
99             - CPU architecture (x86_64, i686, ...)
100           default: x86_64
101         cpus:
102           description:
103             - Number of CPU cores this machine has
104           default: 1
105         ram:
106           description:
107             - amount of RAM this machine has, in MB
108           default: 1
109         disk_size:
110           description:
111             - size of first storage device in this machine (typically /dev/sda), in GB
112           default: 1
113     skip_update_of_driver_password:
114       description:
115         - Allows the code that would assert changes to nodes to skip the
116           update if the change is a single line consisting of the password
117           field.  As of Kilo, by default, passwords are always masked to API
118           requests, which means the logic as a result always attempts to
119           re-assert the password field.
120       required: false
121       default: false
122     availability_zone:
123       description:
124         - Ignored. Present for backwards compatibility
125       required: false
126
127 requirements: ["shade", "jsonpatch"]
128 '''
129
130 EXAMPLES = '''
131 # Enroll a node with some basic properties and driver info
132 - os_ironic:
133     cloud: "devstack"
134     driver: "pxe_ipmitool"
135     uuid: "00000000-0000-0000-0000-000000000002"
136     properties:
137       cpus: 2
138       cpu_arch: "x86_64"
139       ram: 8192
140       disk_size: 64
141     nics:
142       - mac: "aa:bb:cc:aa:bb:cc"
143       - mac: "dd:ee:ff:dd:ee:ff"
144     driver_info:
145       power:
146         ipmi_address: "1.2.3.4"
147         ipmi_username: "admin"
148         ipmi_password: "adminpass"
149     chassis_uuid: "00000000-0000-0000-0000-000000000001"
150
151 '''
152
153 try:
154     import shade
155     HAS_SHADE = True
156 except ImportError:
157     HAS_SHADE = False
158
159 try:
160     import jsonpatch
161     HAS_JSONPATCH = True
162 except ImportError:
163     HAS_JSONPATCH = False
164
165
166 def _parse_properties(module):
167     p = module.params['properties']
168     props = dict(
169         cpu_arch=p.get('cpu_arch') if p.get('cpu_arch') else 'x86_64',
170         cpus=p.get('cpus') if p.get('cpus') else 1,
171         memory_mb=p.get('ram') if p.get('ram') else 1,
172         local_gb=p.get('disk_size') if p.get('disk_size') else 1,
173         capabilities=p.get('capabilities') if p.get('capabilities') else '',
174         root_device=p.get('root_device') if p.get('root_device') else '',
175     )
176     return props
177
178
179 def _parse_driver_info(module):
180     p = module.params['driver_info']
181     info = p.get('power')
182     if not info:
183         raise shade.OpenStackCloudException(
184             "driver_info['power'] is required")
185     if p.get('console'):
186         info.update(p.get('console'))
187     if p.get('management'):
188         info.update(p.get('management'))
189     if p.get('deploy'):
190         info.update(p.get('deploy'))
191     return info
192
193
194 def _choose_id_value(module):
195     if module.params['uuid']:
196         return module.params['uuid']
197     if module.params['name']:
198         return module.params['name']
199     return None
200
201
202
203
204 def _choose_if_password_only(module, patch):
205     if len(patch) is 1:
206         if 'password' in patch[0]['path'] and module.params['skip_update_of_masked_password']:
207             # Return false to abort update as the password appears
208             # to be the only element in the patch.
209             return False
210     return True
211
212
213 def _exit_node_not_updated(module, server):
214     module.exit_json(
215         changed=False,
216         result="Node not updated",
217         uuid=server['uuid'],
218         provision_state=server['provision_state']
219     )
220
221
222 def main():
223     argument_spec = openstack_full_argument_spec(
224         uuid=dict(required=False),
225         name=dict(required=False),
226         driver=dict(required=False),
227         driver_info=dict(type='dict', required=True),
228         nics=dict(type='list', required=True),
229         properties=dict(type='dict', default={}),
230         ironic_url=dict(required=False),
231         chassis_uuid=dict(required=False),
232         skip_update_of_masked_password=dict(required=False, type='bool'),
233         state=dict(required=False, default='present')
234     )
235     module_kwargs = openstack_module_kwargs()
236     module = AnsibleModule(argument_spec, **module_kwargs)
237
238     if not HAS_SHADE:
239         module.fail_json(msg='shade is required for this module')
240     if not HAS_JSONPATCH:
241         module.fail_json(msg='jsonpatch is required for this module')
242     if (module.params['auth_type'] in [None, 'None'] and
243             module.params['ironic_url'] is None):
244         module.fail_json(msg="Authentication appears to be disabled, "
245                              "Please define an ironic_url parameter")
246
247     if (module.params['ironic_url'] and
248             module.params['auth_type'] in [None, 'None']):
249         module.params['auth'] = dict(
250             endpoint=module.params['ironic_url']
251         )
252
253     node_id = _choose_id_value(module)
254
255     try:
256         cloud = shade.operator_cloud(**module.params)
257         server = cloud.get_machine(node_id)
258         if module.params['state'] == 'present':
259             if module.params['driver'] is None:
260                 module.fail_json(msg="A driver must be defined in order "
261                                      "to set a node to present.")
262
263             properties = _parse_properties(module)
264             driver_info = _parse_driver_info(module)
265             kwargs = dict(
266                 driver=module.params['driver'],
267                 properties=properties,
268                 driver_info=driver_info,
269                 name=module.params['name'],
270             )
271
272             if module.params['chassis_uuid']:
273                 kwargs['chassis_uuid'] = module.params['chassis_uuid']
274
275             if server is None:
276                 # Note(TheJulia): Add a specific UUID to the request if
277                 # present in order to be able to re-use kwargs for if
278                 # the node already exists logic, since uuid cannot be
279                 # updated.
280                 if module.params['uuid']:
281                     kwargs['uuid'] = module.params['uuid']
282
283                 server = cloud.register_machine(module.params['nics'],
284                                                 **kwargs)
285                 module.exit_json(changed=True, uuid=server['uuid'],
286                                  provision_state=server['provision_state'])
287             else:
288                 # TODO(TheJulia): Presently this does not support updating
289                 # nics.  Support needs to be added.
290                 #
291                 # Note(TheJulia): This message should never get logged
292                 # however we cannot realistically proceed if neither a
293                 # name or uuid was supplied to begin with.
294                 if not node_id:
295                     module.fail_json(msg="A uuid or name value "
296                                          "must be defined")
297
298                 # Note(TheJulia): Constructing the configuration to compare
299                 # against.  The items listed in the server_config block can
300                 # be updated via the API.
301
302                 server_config = dict(
303                     driver=server['driver'],
304                     properties=server['properties'],
305                     driver_info=server['driver_info'],
306                     name=server['name'],
307                 )
308
309                 # Add the pre-existing chassis_uuid only if
310                 # it is present in the server configuration.
311                 if hasattr(server, 'chassis_uuid'):
312                     server_config['chassis_uuid'] = server['chassis_uuid']
313
314                 # Note(TheJulia): If a password is defined and concealed, a
315                 # patch will always be generated and re-asserted.
316                 patch = jsonpatch.JsonPatch.from_diff(server_config, kwargs)
317
318                 if not patch:
319                     _exit_node_not_updated(module, server)
320                 elif _choose_if_password_only(module, list(patch)):
321                     # Note(TheJulia): Normally we would allow the general
322                     # exception catch below, however this allows a specific
323                     # message.
324                     try:
325                         server = cloud.patch_machine(
326                             server['uuid'],
327                             list(patch))
328                     except Exception as e:
329                         module.fail_json(msg="Failed to update node, "
330                                          "Error: %s" % e.message)
331
332                     # Enumerate out a list of changed paths.
333                     change_list = []
334                     for change in list(patch):
335                         change_list.append(change['path'])
336                     module.exit_json(changed=True,
337                                      result="Node Updated",
338                                      changes=change_list,
339                                      uuid=server['uuid'],
340                                      provision_state=server['provision_state'])
341
342             # Return not updated by default as the conditions were not met
343             # to update.
344             _exit_node_not_updated(module, server)
345
346         if module.params['state'] == 'absent':
347             if not node_id:
348                 module.fail_json(msg="A uuid or name value must be defined "
349                                      "in order to remove a node.")
350
351             if server is not None:
352                 cloud.unregister_machine(module.params['nics'],
353                                          server['uuid'])
354                 module.exit_json(changed=True, result="deleted")
355             else:
356                 module.exit_json(changed=False, result="Server not found")
357
358     except shade.OpenStackCloudException as e:
359         module.fail_json(msg=str(e))
360
361
362 # this is magic, see lib/ansible/module_common.py
363 from ansible.module_utils.basic import *
364 from ansible.module_utils.openstack import *
365
366 if __name__ == "__main__":
367     main()