Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 1 | import logging |
| 2 | |
| 3 | from salt.serializers import yaml |
| 4 | |
| 5 | HELM_HOME = '/srv/helm/home' |
| 6 | LOG = logging.getLogger(__name__) |
| 7 | |
Yuriy Taraday | 6618fb9 | 2017-08-11 17:11:48 +0400 | [diff] [blame] | 8 | def ok_or_output(cmd, prefix=None): |
| 9 | ret = __salt__['cmd.run_all'](**cmd) |
| 10 | if ret['retcode'] == 0: |
| 11 | return None |
| 12 | msg = "Stdout:\n{0[stdout]}\nStderr:\n{0[stderr]}".format(ret) |
| 13 | if prefix: |
| 14 | msg = prefix + ':\n' + msg |
| 15 | return msg |
| 16 | |
| 17 | |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame] | 18 | def _helm_cmd(*args, **tiller_kwargs): |
| 19 | if tiller_kwargs['tiller_host']: |
| 20 | tiller_args = ('--host', tiller_kwargs['tiller_host']) |
| 21 | else: |
| 22 | tiller_args = ('--tiller-namespace', tiller_kwargs['tiller_namespace']) |
Yuriy Taraday | f9dd012 | 2017-08-17 16:26:16 +0400 | [diff] [blame] | 23 | env = {'HELM_HOME': HELM_HOME} |
| 24 | if tiller_kwargs['kube_config']: |
| 25 | env['KUBECONFIG'] = tiller_kwargs['kube_config'] |
Yuriy Taraday | e9f982d | 2017-08-17 18:06:58 +0400 | [diff] [blame] | 26 | if tiller_kwargs['gce_service_token']: |
| 27 | env['GOOGLE_APPLICATION_CREDENTIALS'] = \ |
| 28 | tiller_kwargs['gce_service_token'] |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 29 | return { |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame] | 30 | 'cmd': ('helm',) + tiller_args + args, |
Yuriy Taraday | f9dd012 | 2017-08-17 16:26:16 +0400 | [diff] [blame] | 31 | 'env': env, |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame] | 35 | def release_exists(name, namespace='default', |
Yuriy Taraday | f9dd012 | 2017-08-17 16:26:16 +0400 | [diff] [blame] | 36 | tiller_namespace='kube-system', tiller_host=None, |
Yuriy Taraday | e9f982d | 2017-08-17 18:06:58 +0400 | [diff] [blame] | 37 | kube_config=None, gce_service_token=None): |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame] | 38 | cmd = _helm_cmd('list', '--short', '--all', '--namespace', namespace, name, |
Yuriy Taraday | f9dd012 | 2017-08-17 16:26:16 +0400 | [diff] [blame] | 39 | tiller_namespace=tiller_namespace, tiller_host=tiller_host, |
Yuriy Taraday | e9f982d | 2017-08-17 18:06:58 +0400 | [diff] [blame] | 40 | kube_config=kube_config, |
| 41 | gce_service_token=gce_service_token) |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 42 | return __salt__['cmd.run_stdout'](**cmd) == name |
| 43 | |
| 44 | |
Yuriy Taraday | f169d82 | 2017-08-14 13:40:21 +0400 | [diff] [blame] | 45 | def release_create(name, chart_name, namespace='default', |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame] | 46 | version=None, values=None, |
Yuriy Taraday | f9dd012 | 2017-08-17 16:26:16 +0400 | [diff] [blame] | 47 | tiller_namespace='kube-system', tiller_host=None, |
Yuriy Taraday | e9f982d | 2017-08-17 18:06:58 +0400 | [diff] [blame] | 48 | kube_config=None, gce_service_token=None): |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame] | 49 | tiller_args = { |
| 50 | 'tiller_namespace': tiller_namespace, |
| 51 | 'tiller_host': tiller_host, |
Yuriy Taraday | f9dd012 | 2017-08-17 16:26:16 +0400 | [diff] [blame] | 52 | 'kube_config': kube_config, |
Yuriy Taraday | e9f982d | 2017-08-17 18:06:58 +0400 | [diff] [blame] | 53 | 'gce_service_token': gce_service_token, |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame] | 54 | } |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 55 | args = [] |
| 56 | if version is not None: |
| 57 | args += ['--version', version] |
| 58 | if values is not None: |
| 59 | args += ['--values', '/dev/stdin'] |
Yuriy Taraday | 66e61df | 2017-08-11 15:14:26 +0400 | [diff] [blame] | 60 | cmd = _helm_cmd('install', '--namespace', namespace, |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame] | 61 | '--name', name, chart_name, *args, **tiller_args) |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 62 | if values is not None: |
| 63 | cmd['stdin'] = yaml.serialize(values, default_flow_style=False) |
| 64 | LOG.debug('Creating release with args: %s', cmd) |
Yuriy Taraday | 6618fb9 | 2017-08-11 17:11:48 +0400 | [diff] [blame] | 65 | return ok_or_output(cmd, 'Failed to create release "{}"'.format(name)) |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 66 | |
| 67 | |
Yuriy Taraday | f9dd012 | 2017-08-17 16:26:16 +0400 | [diff] [blame] | 68 | def release_delete(name, tiller_namespace='kube-system', tiller_host=None, |
Yuriy Taraday | e9f982d | 2017-08-17 18:06:58 +0400 | [diff] [blame] | 69 | kube_config=None, gce_service_token=None): |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame] | 70 | cmd = _helm_cmd('delete', '--purge', name, |
Yuriy Taraday | f9dd012 | 2017-08-17 16:26:16 +0400 | [diff] [blame] | 71 | tiller_namespace=tiller_namespace, tiller_host=tiller_host, |
Yuriy Taraday | e9f982d | 2017-08-17 18:06:58 +0400 | [diff] [blame] | 72 | kube_config=kube_config, |
| 73 | gce_service_token=gce_service_token) |
Yuriy Taraday | 6618fb9 | 2017-08-11 17:11:48 +0400 | [diff] [blame] | 74 | return ok_or_output(cmd, 'Failed to delete release "{}"'.format(name)) |
Yuriy Taraday | 893b3fb | 2017-07-03 16:22:57 +0400 | [diff] [blame] | 75 | |
| 76 | |
Yuriy Taraday | f169d82 | 2017-08-14 13:40:21 +0400 | [diff] [blame] | 77 | def release_upgrade(name, chart_name, namespace='default', |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame] | 78 | version=None, values=None, |
Yuriy Taraday | f9dd012 | 2017-08-17 16:26:16 +0400 | [diff] [blame] | 79 | tiller_namespace='kube-system', tiller_host=None, |
Yuriy Taraday | e9f982d | 2017-08-17 18:06:58 +0400 | [diff] [blame] | 80 | kube_config=None, gce_service_token=None): |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame] | 81 | tiller_args = { |
| 82 | 'tiller_namespace': tiller_namespace, |
| 83 | 'tiller_host': tiller_host, |
Yuriy Taraday | f9dd012 | 2017-08-17 16:26:16 +0400 | [diff] [blame] | 84 | 'kube_config': kube_config, |
Yuriy Taraday | e9f982d | 2017-08-17 18:06:58 +0400 | [diff] [blame] | 85 | 'gce_service_token': gce_service_token, |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame] | 86 | } |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 87 | args = [] |
| 88 | if version is not None: |
| 89 | args += ['--version', version] |
| 90 | if values is not None: |
| 91 | args += ['--values', '/dev/stdin'] |
Yuriy Taraday | 66e61df | 2017-08-11 15:14:26 +0400 | [diff] [blame] | 92 | cmd = _helm_cmd('upgrade', '--namespace', namespace, |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame] | 93 | name, chart_name, *args, **tiller_args) |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 94 | if values is not None: |
| 95 | cmd['stdin'] = yaml.serialize(values, default_flow_style=False) |
Yuriy Taraday | 893b3fb | 2017-07-03 16:22:57 +0400 | [diff] [blame] | 96 | LOG.debug('Upgrading release with args: %s', cmd) |
Yuriy Taraday | 6618fb9 | 2017-08-11 17:11:48 +0400 | [diff] [blame] | 97 | return ok_or_output(cmd, 'Failed to upgrade release "{}"'.format(name)) |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 98 | |
| 99 | |
Yuriy Taraday | f9dd012 | 2017-08-17 16:26:16 +0400 | [diff] [blame] | 100 | def get_values(name, tiller_namespace='kube-system', tiller_host=None, |
Yuriy Taraday | e9f982d | 2017-08-17 18:06:58 +0400 | [diff] [blame] | 101 | kube_config=None, gce_service_token=None): |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame] | 102 | cmd = _helm_cmd('get', 'values', '--all', name, |
Yuriy Taraday | f9dd012 | 2017-08-17 16:26:16 +0400 | [diff] [blame] | 103 | tiller_namespace=tiller_namespace, tiller_host=tiller_host, |
Yuriy Taraday | e9f982d | 2017-08-17 18:06:58 +0400 | [diff] [blame] | 104 | kube_config=kube_config, |
| 105 | gce_service_token=gce_service_token) |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 106 | return yaml.deserialize(__salt__['cmd.run_stdout'](**cmd)) |