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 | |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 3 | from salt.serializers import yaml |
| 4 | |
Yuriy Taraday | 7612604 | 2017-08-11 15:27:41 +0400 | [diff] [blame] | 5 | |
| 6 | def failure(name, message): |
| 7 | return { |
| 8 | 'name': name, |
| 9 | 'changes': {}, |
| 10 | 'result': False, |
| 11 | 'comment': message, |
| 12 | } |
| 13 | |
| 14 | |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame^] | 15 | def present(name, chart_name, namespace, version=None, values=None, |
| 16 | tiller_namespace='kube-system', tiller_host=None): |
| 17 | tiller_args = { |
| 18 | 'tiller_namespace': tiller_namespace, |
| 19 | 'tiller_host': tiller_host, |
| 20 | } |
| 21 | exists = __salt__['helm.release_exists'](name, namespace, **tiller_args) |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 22 | if not exists: |
Yuriy Taraday | 6618fb9 | 2017-08-11 17:11:48 +0400 | [diff] [blame] | 23 | err = __salt__['helm.release_create']( |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame^] | 24 | name, chart_name, namespace, version, values, **tiller_args) |
Yuriy Taraday | 6618fb9 | 2017-08-11 17:11:48 +0400 | [diff] [blame] | 25 | if err: |
| 26 | return failure(name, err) |
Yuriy Taraday | 7612604 | 2017-08-11 15:27:41 +0400 | [diff] [blame] | 27 | return { |
| 28 | 'name': name, |
| 29 | 'changes': {name: 'CREATED'}, |
| 30 | 'result': True, |
| 31 | 'comment': 'Release "{}" was created'.format(name), |
| 32 | } |
| 33 | |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame^] | 34 | old_values = __salt__['helm.get_values'](name, **tiller_args) |
Yuriy Taraday | 6618fb9 | 2017-08-11 17:11:48 +0400 | [diff] [blame] | 35 | err = __salt__['helm.release_upgrade']( |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame^] | 36 | name, chart_name, namespace, version, values, **tiller_args) |
Yuriy Taraday | 6618fb9 | 2017-08-11 17:11:48 +0400 | [diff] [blame] | 37 | if err: |
| 38 | return failure(name, err) |
Yuriy Taraday | 7612604 | 2017-08-11 15:27:41 +0400 | [diff] [blame] | 39 | |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame^] | 40 | new_values = __salt__['helm.get_values'](name, **tiller_args) |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 41 | if new_values == old_values: |
| 42 | return { |
| 43 | 'name': name, |
| 44 | 'changes': {}, |
| 45 | 'result': True, |
| 46 | 'comment': 'Release "{}" already exists'.format(name), |
| 47 | } |
Yuriy Taraday | 7612604 | 2017-08-11 15:27:41 +0400 | [diff] [blame] | 48 | |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 49 | old_str = yaml.serialize(old_values, default_flow_style=False) |
| 50 | new_str = yaml.serialize(new_values, default_flow_style=False) |
| 51 | diff = difflib.unified_diff( |
| 52 | old_str.split('\n'), new_str.split('\n'), lineterm='') |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 53 | return { |
| 54 | 'name': name, |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 55 | 'changes': {'values': '\n'.join(diff)}, |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 56 | 'result': True, |
Yuriy Taraday | 3c3f63c | 2017-06-29 20:46:20 +0400 | [diff] [blame] | 57 | 'comment': 'Release "{}" was updated'.format(name), |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 58 | } |
Yuriy Taraday | 893b3fb | 2017-07-03 16:22:57 +0400 | [diff] [blame] | 59 | |
| 60 | |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame^] | 61 | def absent(name, namespace, tiller_namespace='kube-system', tiller_host=None): |
| 62 | tiller_args = { |
| 63 | 'tiller_namespace': tiller_namespace, |
| 64 | 'tiller_host': tiller_host, |
| 65 | } |
| 66 | exists = __salt__['helm.release_exists'](name, namespace, **tiller_args) |
Yuriy Taraday | 893b3fb | 2017-07-03 16:22:57 +0400 | [diff] [blame] | 67 | if not exists: |
| 68 | return { |
| 69 | 'name': name, |
| 70 | 'changes': {}, |
| 71 | 'result': True, |
| 72 | 'comment': 'Release "{}" doesn\'t exist'.format(name), |
| 73 | } |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame^] | 74 | err = __salt__['helm.release_delete'](name, **tiller_args) |
Yuriy Taraday | 6618fb9 | 2017-08-11 17:11:48 +0400 | [diff] [blame] | 75 | if err: |
| 76 | return failure(name, err) |
Yuriy Taraday | 893b3fb | 2017-07-03 16:22:57 +0400 | [diff] [blame] | 77 | return { |
| 78 | 'name': name, |
| 79 | 'changes': {name: 'DELETED'}, |
| 80 | 'result': True, |
| 81 | 'comment': 'Release "{}" was deleted'.format(name), |
| 82 | } |