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