blob: 6493aa53016eb97d724eef435049413262f8f55d [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 Taradayf9dd0122017-08-17 16:26:16 +040023 env = {'HELM_HOME': HELM_HOME}
24 if tiller_kwargs['kube_config']:
25 env['KUBECONFIG'] = tiller_kwargs['kube_config']
Yuriy Taradaye9f982d2017-08-17 18:06:58 +040026 if tiller_kwargs['gce_service_token']:
27 env['GOOGLE_APPLICATION_CREDENTIALS'] = \
28 tiller_kwargs['gce_service_token']
Yuriy Taraday84a21032017-06-27 11:13:16 +040029 return {
Yuriy Taraday6f826492017-08-16 12:40:24 +040030 'cmd': ('helm',) + tiller_args + args,
Yuriy Taradayf9dd0122017-08-17 16:26:16 +040031 'env': env,
Yuriy Taraday84a21032017-06-27 11:13:16 +040032 }
33
34
Yuriy Taraday6f826492017-08-16 12:40:24 +040035def release_exists(name, namespace='default',
Yuriy Taradayf9dd0122017-08-17 16:26:16 +040036 tiller_namespace='kube-system', tiller_host=None,
Yuriy Taradaye9f982d2017-08-17 18:06:58 +040037 kube_config=None, gce_service_token=None):
Yuriy Taraday6f826492017-08-16 12:40:24 +040038 cmd = _helm_cmd('list', '--short', '--all', '--namespace', namespace, name,
Yuriy Taradayf9dd0122017-08-17 16:26:16 +040039 tiller_namespace=tiller_namespace, tiller_host=tiller_host,
Yuriy Taradaye9f982d2017-08-17 18:06:58 +040040 kube_config=kube_config,
41 gce_service_token=gce_service_token)
Yuriy Taraday84a21032017-06-27 11:13:16 +040042 return __salt__['cmd.run_stdout'](**cmd) == name
43
44
Yuriy Taradayf169d822017-08-14 13:40:21 +040045def release_create(name, chart_name, namespace='default',
Yuriy Taraday6f826492017-08-16 12:40:24 +040046 version=None, values=None,
Yuriy Taradayf9dd0122017-08-17 16:26:16 +040047 tiller_namespace='kube-system', tiller_host=None,
Yuriy Taradaye9f982d2017-08-17 18:06:58 +040048 kube_config=None, gce_service_token=None):
Yuriy Taraday6f826492017-08-16 12:40:24 +040049 tiller_args = {
50 'tiller_namespace': tiller_namespace,
51 'tiller_host': tiller_host,
Yuriy Taradayf9dd0122017-08-17 16:26:16 +040052 'kube_config': kube_config,
Yuriy Taradaye9f982d2017-08-17 18:06:58 +040053 'gce_service_token': gce_service_token,
Yuriy Taraday6f826492017-08-16 12:40:24 +040054 }
Yuriy Taraday84a21032017-06-27 11:13:16 +040055 args = []
56 if version is not None:
57 args += ['--version', version]
58 if values is not None:
59 args += ['--values', '/dev/stdin']
Yuriy Taraday66e61df2017-08-11 15:14:26 +040060 cmd = _helm_cmd('install', '--namespace', namespace,
Yuriy Taraday6f826492017-08-16 12:40:24 +040061 '--name', name, chart_name, *args, **tiller_args)
Yuriy Taraday84a21032017-06-27 11:13:16 +040062 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 Taraday6618fb92017-08-11 17:11:48 +040065 return ok_or_output(cmd, 'Failed to create release "{}"'.format(name))
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040066
67
Yuriy Taradayf9dd0122017-08-17 16:26:16 +040068def release_delete(name, tiller_namespace='kube-system', tiller_host=None,
Yuriy Taradaye9f982d2017-08-17 18:06:58 +040069 kube_config=None, gce_service_token=None):
Yuriy Taraday6f826492017-08-16 12:40:24 +040070 cmd = _helm_cmd('delete', '--purge', name,
Yuriy Taradayf9dd0122017-08-17 16:26:16 +040071 tiller_namespace=tiller_namespace, tiller_host=tiller_host,
Yuriy Taradaye9f982d2017-08-17 18:06:58 +040072 kube_config=kube_config,
73 gce_service_token=gce_service_token)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040074 return ok_or_output(cmd, 'Failed to delete release "{}"'.format(name))
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040075
76
Yuriy Taradayf169d822017-08-14 13:40:21 +040077def release_upgrade(name, chart_name, namespace='default',
Yuriy Taraday6f826492017-08-16 12:40:24 +040078 version=None, values=None,
Yuriy Taradayf9dd0122017-08-17 16:26:16 +040079 tiller_namespace='kube-system', tiller_host=None,
Yuriy Taradaye9f982d2017-08-17 18:06:58 +040080 kube_config=None, gce_service_token=None):
Yuriy Taraday6f826492017-08-16 12:40:24 +040081 tiller_args = {
82 'tiller_namespace': tiller_namespace,
83 'tiller_host': tiller_host,
Yuriy Taradayf9dd0122017-08-17 16:26:16 +040084 'kube_config': kube_config,
Yuriy Taradaye9f982d2017-08-17 18:06:58 +040085 'gce_service_token': gce_service_token,
Yuriy Taraday6f826492017-08-16 12:40:24 +040086 }
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040087 args = []
88 if version is not None:
89 args += ['--version', version]
90 if values is not None:
91 args += ['--values', '/dev/stdin']
Yuriy Taraday66e61df2017-08-11 15:14:26 +040092 cmd = _helm_cmd('upgrade', '--namespace', namespace,
Yuriy Taraday6f826492017-08-16 12:40:24 +040093 name, chart_name, *args, **tiller_args)
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040094 if values is not None:
95 cmd['stdin'] = yaml.serialize(values, default_flow_style=False)
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040096 LOG.debug('Upgrading release with args: %s', cmd)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040097 return ok_or_output(cmd, 'Failed to upgrade release "{}"'.format(name))
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040098
99
Yuriy Taradayf9dd0122017-08-17 16:26:16 +0400100def get_values(name, tiller_namespace='kube-system', tiller_host=None,
Yuriy Taradaye9f982d2017-08-17 18:06:58 +0400101 kube_config=None, gce_service_token=None):
Yuriy Taraday6f826492017-08-16 12:40:24 +0400102 cmd = _helm_cmd('get', 'values', '--all', name,
Yuriy Taradayf9dd0122017-08-17 16:26:16 +0400103 tiller_namespace=tiller_namespace, tiller_host=tiller_host,
Yuriy Taradaye9f982d2017-08-17 18:06:58 +0400104 kube_config=kube_config,
105 gce_service_token=gce_service_token)
Yuriy Taradayaeeaa742017-06-28 15:54:56 +0400106 return yaml.deserialize(__salt__['cmd.run_stdout'](**cmd))