blob: 80632d11f75d4df484b152215f0dbfda65065778 [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 Taraday6f826492017-08-16 12:40:24 +040018def _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 Taraday84a21032017-06-27 11:13:16 +040023 return {
Yuriy Taraday6f826492017-08-16 12:40:24 +040024 'cmd': ('helm',) + tiller_args + args,
Yuriy Taraday84a21032017-06-27 11:13:16 +040025 'env': {'HELM_HOME': HELM_HOME},
26 }
27
28
Yuriy Taraday6f826492017-08-16 12:40:24 +040029def 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 Taraday84a21032017-06-27 11:13:16 +040033 return __salt__['cmd.run_stdout'](**cmd) == name
34
35
Yuriy Taradayf169d822017-08-14 13:40:21 +040036def release_create(name, chart_name, namespace='default',
Yuriy Taraday6f826492017-08-16 12:40:24 +040037 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 Taraday84a21032017-06-27 11:13:16 +040043 args = []
44 if version is not None:
45 args += ['--version', version]
46 if values is not None:
47 args += ['--values', '/dev/stdin']
Yuriy Taraday66e61df2017-08-11 15:14:26 +040048 cmd = _helm_cmd('install', '--namespace', namespace,
Yuriy Taraday6f826492017-08-16 12:40:24 +040049 '--name', name, chart_name, *args, **tiller_args)
Yuriy Taraday84a21032017-06-27 11:13:16 +040050 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 Taraday6618fb92017-08-11 17:11:48 +040053 return ok_or_output(cmd, 'Failed to create release "{}"'.format(name))
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040054
55
Yuriy Taraday6f826492017-08-16 12:40:24 +040056def 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 Taraday6618fb92017-08-11 17:11:48 +040059 return ok_or_output(cmd, 'Failed to delete release "{}"'.format(name))
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040060
61
Yuriy Taradayf169d822017-08-14 13:40:21 +040062def release_upgrade(name, chart_name, namespace='default',
Yuriy Taraday6f826492017-08-16 12:40:24 +040063 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 Taradayaeeaa742017-06-28 15:54:56 +040069 args = []
70 if version is not None:
71 args += ['--version', version]
72 if values is not None:
73 args += ['--values', '/dev/stdin']
Yuriy Taraday66e61df2017-08-11 15:14:26 +040074 cmd = _helm_cmd('upgrade', '--namespace', namespace,
Yuriy Taraday6f826492017-08-16 12:40:24 +040075 name, chart_name, *args, **tiller_args)
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040076 if values is not None:
77 cmd['stdin'] = yaml.serialize(values, default_flow_style=False)
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040078 LOG.debug('Upgrading release with args: %s', cmd)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040079 return ok_or_output(cmd, 'Failed to upgrade release "{}"'.format(name))
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040080
81
Yuriy Taraday6f826492017-08-16 12:40:24 +040082def 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 Taradayaeeaa742017-06-28 15:54:56 +040085 return yaml.deserialize(__salt__['cmd.run_stdout'](**cmd))