Michael Polenchuk | 75ea11e | 2018-08-22 14:34:11 +0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | ''' |
| 3 | Management of Open vSwitch configuration |
| 4 | ======================================== |
| 5 | |
| 6 | The OVS config can be managed with the ovs_config state module: |
| 7 | |
| 8 | .. code-block:: yaml |
| 9 | |
| 10 | other_config:dpdk-init: |
| 11 | ovs_config.present: |
| 12 | - value: True |
| 13 | |
| 14 | other_config:dpdk-extra: |
| 15 | ovs_config.present: |
| 16 | - value: -n 12 --vhost-owner libvirt-qemu:kvm --vhost-perm 0664 |
| 17 | |
| 18 | external_ids: |
| 19 | ovs_config.absent |
| 20 | ''' |
| 21 | |
| 22 | |
| 23 | def __virtual__(): |
| 24 | ''' |
| 25 | Only make these states available if Open vSwitch is installed. |
| 26 | ''' |
| 27 | return 'ovs_config.list' in __salt__ |
| 28 | |
| 29 | |
| 30 | def present(name, value, wait=True): |
| 31 | ''' |
| 32 | Ensures that the named config exists, eventually creates it. |
| 33 | |
| 34 | Args: |
| 35 | name/value: The name/value of the config entry. |
| 36 | wait: Whether wait for ovs-vswitchd to reconfigure itself according to the modified database. |
| 37 | ''' |
| 38 | ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''} |
| 39 | ovs_config = __salt__['ovs_config.list']() |
| 40 | |
| 41 | if name in ovs_config and ovs_config[name] == str(value).lower(): |
| 42 | ret['result'] = True |
| 43 | ret['comment'] = '{0} is already set to {1}.'.format(name, value) |
| 44 | else: |
| 45 | config_updated = __salt__['ovs_config.set'](name, value, wait) |
| 46 | if config_updated: |
| 47 | ret['result'] = True |
| 48 | ret['comment'] = '{0} is updated.'.format(name) |
| 49 | ret['changes'] = { name: 'Updated to {0}'.format(value) } |
| 50 | else: |
| 51 | ret['result'] = False |
| 52 | ret['comment'] = 'Unable to update config of {0}.'.format(name) |
| 53 | |
| 54 | return ret |
| 55 | |
| 56 | |
| 57 | def absent(name): |
| 58 | ''' |
| 59 | Ensures that the named config does not exist, eventually deletes it. |
| 60 | |
| 61 | Args: |
| 62 | name: The name of the config entry. |
| 63 | |
| 64 | ''' |
| 65 | ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''} |
| 66 | ovs_config = __salt__['ovs_config.list']() |
| 67 | |
| 68 | if ':' in name and name not in ovs_config: |
| 69 | ret['result'] = True |
| 70 | ret['comment'] = '{0} does not exist.'.format(name) |
| 71 | else: |
| 72 | config_removed = __salt__['ovs_config.remove'](name) |
| 73 | if config_removed: |
| 74 | ret['result'] = True |
| 75 | ret['comment'] = '{0} is removed.'.format(name) |
| 76 | ret['changes'] = { name: '{0} removed'.format(name) } |
| 77 | else: |
| 78 | ret['result'] = False |
| 79 | ret['comment'] = 'Unable to delete config of {0}.'.format(name) |
| 80 | |
| 81 | return ret |