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