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