Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | ''' |
| 3 | Management of reclass metadata |
| 4 | ============================== |
| 5 | |
| 6 | .. code-block:: yaml |
| 7 | |
| 8 | node01: |
| 9 | reclass.node_present: |
| 10 | - name: hostaname.domain.com |
| 11 | - path: _generated |
| 12 | - cluster: default |
| 13 | - environment: prd |
| 14 | - classes: |
| 15 | - neco.service.neco |
| 16 | - neco2.class.neco.dal |
| 17 | - parameters: |
| 18 | neco: value1 |
| 19 | neco2: value2 |
| 20 | |
| 21 | ''' |
Adam Tengler | 805666d | 2017-05-15 16:01:13 +0000 | [diff] [blame^] | 22 | import json |
| 23 | import six |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 24 | |
| 25 | |
| 26 | def __virtual__(): |
| 27 | ''' |
Adam Tengler | 805666d | 2017-05-15 16:01:13 +0000 | [diff] [blame^] | 28 | Only load if the reclass module is in __salt__ |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 29 | ''' |
Adam Tengler | 805666d | 2017-05-15 16:01:13 +0000 | [diff] [blame^] | 30 | return 'reclass' if 'reclass.node_list' in __salt__ else False |
| 31 | |
| 32 | |
| 33 | def node_present(name, path=None, cluster="default", environment="prd", classes=None, parameters=None, **kwargs): |
| 34 | ''' |
| 35 | Ensure that the reclass node exists |
| 36 | |
| 37 | :param name: new node FQDN |
| 38 | :param path: custom path in nodes for new node |
| 39 | :param classes: classes given to the new node |
| 40 | :param parameters: parameters given to the new node |
| 41 | :param environment: node's environment |
| 42 | :param cluster: node's cluster |
| 43 | |
| 44 | ''' |
| 45 | ret = {'name': name, |
| 46 | 'changes': {}, |
| 47 | 'result': True, |
| 48 | 'comment': 'Node "{0}" already exists and it is in correct state'.format(name)} |
| 49 | |
| 50 | # Check if node is already present |
| 51 | node = __salt__['reclass.node_get'](name, **kwargs) |
| 52 | |
| 53 | if 'Error' in node: |
| 54 | # Create node |
| 55 | __salt__['reclass.node_create'](name, path, cluster, environment, classes, parameters, **kwargs) |
| 56 | ret['comment'] = 'Node "{0}" has been created'.format(name) |
| 57 | ret['changes']['Node'] = 'Created' |
| 58 | return ret |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 59 | |