Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 1 | import difflib |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 2 | import logging |
| 3 | |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 4 | from salt.serializers import yaml |
| 5 | |
| 6 | def present(name, chart_name, version=None, values=None): |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 7 | exists = __salt__['helm.release_exists'](name) |
| 8 | if not exists: |
| 9 | result = __salt__['helm.release_create']( |
| 10 | name, chart_name, version, values) |
| 11 | if result: |
| 12 | return { |
| 13 | 'name': name, |
| 14 | 'changes': {name: 'CREATED'}, |
| 15 | 'result': True, |
| 16 | 'comment': 'Release "{}" was created'.format(name), |
| 17 | } |
| 18 | else: |
| 19 | return { |
| 20 | 'name': name, |
| 21 | 'changes': {}, |
| 22 | 'result': False, |
| 23 | 'comment': 'Failed to create release "{}"'.format(name), |
| 24 | } |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 25 | old_values = __salt__['helm.get_values'](name) |
| 26 | result = __salt__['helm.release_upgrade']( |
| 27 | name, chart_name, version, values) |
| 28 | if not result: |
| 29 | return { |
| 30 | 'name': name, |
| 31 | 'changes': {}, |
| 32 | 'result': False, |
| 33 | 'comment': 'Failed to create release "{}"'.format(name), |
| 34 | } |
| 35 | new_values = __salt__['helm.get_values'](name) |
| 36 | if new_values == old_values: |
| 37 | return { |
| 38 | 'name': name, |
| 39 | 'changes': {}, |
| 40 | 'result': True, |
| 41 | 'comment': 'Release "{}" already exists'.format(name), |
| 42 | } |
| 43 | old_str = yaml.serialize(old_values, default_flow_style=False) |
| 44 | new_str = yaml.serialize(new_values, default_flow_style=False) |
| 45 | diff = difflib.unified_diff( |
| 46 | old_str.split('\n'), new_str.split('\n'), lineterm='') |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 47 | return { |
| 48 | 'name': name, |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 49 | 'changes': {'values': '\n'.join(diff)}, |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 50 | 'result': True, |
Yuriy Taraday | 3c3f63c | 2017-06-29 20:46:20 +0400 | [diff] [blame^] | 51 | 'comment': 'Release "{}" was updated'.format(name), |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 52 | } |