blob: 76f2bb11f8e55fdae81260b9b5c73c9452612032 [file] [log] [blame]
Yuriy Taraday84a21032017-06-27 11:13:16 +04001import logging
2
3from salt.serializers import yaml
4
5HELM_HOME = '/srv/helm/home'
6LOG = logging.getLogger(__name__)
7
Yuriy Taraday6618fb92017-08-11 17:11:48 +04008def 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 Taraday84a21032017-06-27 11:13:16 +040018def _helm_cmd(*args):
19 return {
20 'cmd': ('helm',) + args,
21 'env': {'HELM_HOME': HELM_HOME},
22 }
23
24
Yuriy Taradayf169d822017-08-14 13:40:21 +040025def release_exists(name, namespace='default'):
Yuriy Taraday66e61df2017-08-11 15:14:26 +040026 cmd = _helm_cmd('list', '--short', '--all', '--namespace', namespace, name)
Yuriy Taraday84a21032017-06-27 11:13:16 +040027 return __salt__['cmd.run_stdout'](**cmd) == name
28
29
Yuriy Taradayf169d822017-08-14 13:40:21 +040030def release_create(name, chart_name, namespace='default',
31 version=None, values=None):
Yuriy Taraday84a21032017-06-27 11:13:16 +040032 args = []
33 if version is not None:
34 args += ['--version', version]
35 if values is not None:
36 args += ['--values', '/dev/stdin']
Yuriy Taraday66e61df2017-08-11 15:14:26 +040037 cmd = _helm_cmd('install', '--namespace', namespace,
38 '--name', name, chart_name, *args)
Yuriy Taraday84a21032017-06-27 11:13:16 +040039 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 Taraday6618fb92017-08-11 17:11:48 +040042 return ok_or_output(cmd, 'Failed to create release "{}"'.format(name))
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040043
44
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040045def release_delete(name):
46 cmd = _helm_cmd('delete', '--purge', name)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040047 return ok_or_output(cmd, 'Failed to delete release "{}"'.format(name))
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040048
49
Yuriy Taradayf169d822017-08-14 13:40:21 +040050def release_upgrade(name, chart_name, namespace='default',
51 version=None, values=None):
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040052 args = []
53 if version is not None:
54 args += ['--version', version]
55 if values is not None:
56 args += ['--values', '/dev/stdin']
Yuriy Taraday66e61df2017-08-11 15:14:26 +040057 cmd = _helm_cmd('upgrade', '--namespace', namespace,
58 name, chart_name, *args)
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040059 if values is not None:
60 cmd['stdin'] = yaml.serialize(values, default_flow_style=False)
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040061 LOG.debug('Upgrading release with args: %s', cmd)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040062 return ok_or_output(cmd, 'Failed to upgrade release "{}"'.format(name))
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040063
64
65def get_values(name):
66 cmd = _helm_cmd('get', 'values', '--all', name)
67 return yaml.deserialize(__salt__['cmd.run_stdout'](**cmd))