Saltstack Official Linux Formula
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

102 行
2.3KB

  1. # -*- coding: utf-8 -*-
  2. '''
  3. Support for Open vSwitch database configuration.
  4. '''
  5. from __future__ import absolute_import
  6. import logging
  7. import salt.utils.path
  8. log = logging.getLogger(__name__)
  9. def __virtual__():
  10. '''
  11. Only load the module if Open vSwitch is installed
  12. '''
  13. if salt.utils.path.which('ovs-vsctl'):
  14. return 'ovs_config'
  15. return False
  16. def _retcode_to_bool(retcode):
  17. '''
  18. Evaluates ovs-vsctl command`s retcode value.
  19. Args:
  20. retcode: Value of retcode field from response.
  21. '''
  22. return True if retcode == 0 else False
  23. def set(cfg, value, wait=True):
  24. '''
  25. Updates a specified configuration entry.
  26. Args:
  27. cfg/value: a config entry to update
  28. wait: wait or not for ovs-vswitchd to reconfigure itself before it exits.
  29. CLI Example:
  30. .. code-block:: bash
  31. salt '*' ovs_config.set other_config:dpdk-init true
  32. '''
  33. wait = '' if wait else '--no-wait '
  34. cmd = 'ovs-vsctl {0}set Open_vSwitch . {1}="{2}"'.format(wait, cfg, str(value).lower())
  35. result = __salt__['cmd.run_all'](cmd)
  36. return _retcode_to_bool(result['retcode'])
  37. def remove(cfg):
  38. '''
  39. Removes a specified configuration entry.
  40. Args:
  41. cfg: a config entry to remove
  42. CLI Example:
  43. .. code-block:: bash
  44. salt '*' ovs_config.remove other_config
  45. '''
  46. if ':' in cfg:
  47. section, key = cfg.split(':')
  48. cmd = 'ovs-vsctl remove Open_vSwitch . {} {}'.format(section, key)
  49. else:
  50. cmd = 'ovs-vsctl clear Open_vSwitch . ' + cfg
  51. result = __salt__['cmd.run_all'](cmd)
  52. return _retcode_to_bool(result['retcode'])
  53. def list():
  54. '''
  55. Return a current config of Open vSwitch
  56. CLI Example:
  57. .. code-block:: bash
  58. salt '*' ovs_config.list
  59. '''
  60. cmd = 'ovs-vsctl list Open_vSwitch .'
  61. result = __salt__['cmd.run_all'](cmd)
  62. if result['retcode'] == 0:
  63. config = {}
  64. for l in result['stdout'].splitlines():
  65. cfg, value = map((lambda x: x.strip()), l.split(' : '))
  66. if value.startswith('{') and len(value) > 2:
  67. for i in value[1:-1].replace('"', '').split(', '):
  68. _k, _v = i.split('=')
  69. config['{}:{}'.format(cfg,_k)] = _v
  70. else:
  71. config[cfg] = value
  72. return config
  73. else:
  74. return False