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 | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 23 | return { |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame^] | 24 | 'cmd': ('helm',) + tiller_args + args, |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 25 | 'env': {'HELM_HOME': HELM_HOME}, |
| 26 | } |
| 27 | |
| 28 | |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame^] | 29 | def release_exists(name, namespace='default', |
| 30 | tiller_namespace='kube-system', tiller_host=None): |
| 31 | cmd = _helm_cmd('list', '--short', '--all', '--namespace', namespace, name, |
| 32 | tiller_namespace=tiller_namespace, tiller_host=tiller_host) |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 33 | return __salt__['cmd.run_stdout'](**cmd) == name |
| 34 | |
| 35 | |
Yuriy Taraday | f169d82 | 2017-08-14 13:40:21 +0400 | [diff] [blame] | 36 | def release_create(name, chart_name, namespace='default', |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame^] | 37 | version=None, values=None, |
| 38 | tiller_namespace='kube-system', tiller_host=None): |
| 39 | tiller_args = { |
| 40 | 'tiller_namespace': tiller_namespace, |
| 41 | 'tiller_host': tiller_host, |
| 42 | } |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 43 | args = [] |
| 44 | if version is not None: |
| 45 | args += ['--version', version] |
| 46 | if values is not None: |
| 47 | args += ['--values', '/dev/stdin'] |
Yuriy Taraday | 66e61df | 2017-08-11 15:14:26 +0400 | [diff] [blame] | 48 | cmd = _helm_cmd('install', '--namespace', namespace, |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame^] | 49 | '--name', name, chart_name, *args, **tiller_args) |
Yuriy Taraday | 84a2103 | 2017-06-27 11:13:16 +0400 | [diff] [blame] | 50 | if values is not None: |
| 51 | cmd['stdin'] = yaml.serialize(values, default_flow_style=False) |
| 52 | LOG.debug('Creating release with args: %s', cmd) |
Yuriy Taraday | 6618fb9 | 2017-08-11 17:11:48 +0400 | [diff] [blame] | 53 | return ok_or_output(cmd, 'Failed to create release "{}"'.format(name)) |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 54 | |
| 55 | |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame^] | 56 | def release_delete(name, tiller_namespace='kube-system', tiller_host=None): |
| 57 | cmd = _helm_cmd('delete', '--purge', name, |
| 58 | tiller_namespace=tiller_namespace, tiller_host=tiller_host) |
Yuriy Taraday | 6618fb9 | 2017-08-11 17:11:48 +0400 | [diff] [blame] | 59 | return ok_or_output(cmd, 'Failed to delete release "{}"'.format(name)) |
Yuriy Taraday | 893b3fb | 2017-07-03 16:22:57 +0400 | [diff] [blame] | 60 | |
| 61 | |
Yuriy Taraday | f169d82 | 2017-08-14 13:40:21 +0400 | [diff] [blame] | 62 | def release_upgrade(name, chart_name, namespace='default', |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame^] | 63 | version=None, values=None, |
| 64 | tiller_namespace='kube-system', tiller_host=None): |
| 65 | tiller_args = { |
| 66 | 'tiller_namespace': tiller_namespace, |
| 67 | 'tiller_host': tiller_host, |
| 68 | } |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 69 | args = [] |
| 70 | if version is not None: |
| 71 | args += ['--version', version] |
| 72 | if values is not None: |
| 73 | args += ['--values', '/dev/stdin'] |
Yuriy Taraday | 66e61df | 2017-08-11 15:14:26 +0400 | [diff] [blame] | 74 | cmd = _helm_cmd('upgrade', '--namespace', namespace, |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame^] | 75 | name, chart_name, *args, **tiller_args) |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 76 | if values is not None: |
| 77 | cmd['stdin'] = yaml.serialize(values, default_flow_style=False) |
Yuriy Taraday | 893b3fb | 2017-07-03 16:22:57 +0400 | [diff] [blame] | 78 | LOG.debug('Upgrading release with args: %s', cmd) |
Yuriy Taraday | 6618fb9 | 2017-08-11 17:11:48 +0400 | [diff] [blame] | 79 | return ok_or_output(cmd, 'Failed to upgrade release "{}"'.format(name)) |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 80 | |
| 81 | |
Yuriy Taraday | 6f82649 | 2017-08-16 12:40:24 +0400 | [diff] [blame^] | 82 | def get_values(name, tiller_namespace='kube-system', tiller_host=None): |
| 83 | cmd = _helm_cmd('get', 'values', '--all', name, |
| 84 | tiller_namespace=tiller_namespace, tiller_host=tiller_host) |
Yuriy Taraday | aeeaa74 | 2017-06-28 15:54:56 +0400 | [diff] [blame] | 85 | return yaml.deserialize(__salt__['cmd.run_stdout'](**cmd)) |