feat: display helm cmd in state output
Fixes salt-formulas/salt-formula-helm#5
diff --git a/_states/helm_release.py b/_states/helm_release.py
index 7a66b10..dae657a 100644
--- a/_states/helm_release.py
+++ b/_states/helm_release.py
@@ -68,23 +68,28 @@
kwargs['tiller_namespace'] = tiller_namespace
old_release = __salt__['helm.get_release'](name, **kwargs)
if not old_release:
- err = __salt__['helm.release_create'](
+ try:
+ result = __salt__['helm.release_create'](
name, chart_name, namespace, version, values_file, **kwargs
)
- if err:
- return _failure(name, err)
return {
+ 'name': name,
+ 'changes': {
'name': name,
- 'changes': {
- 'name': name,
- 'chart_name': chart_name,
- 'namespace': namespace,
- 'version': version,
- 'values': _get_values_from_file(values_file)
- },
- 'result': True,
- 'comment': 'Release "{}" was created'.format(name),
+ 'chart_name': chart_name,
+ 'namespace': namespace,
+ 'version': version,
+ 'values': _get_values_from_file(values_file),
+ 'stdout': result.get('stdout')
+ },
+ 'result': True,
+ 'comment': ('Release "%s" was created' % name +
+ '\nExecuted command: %s' % result['cmd'])
}
+ except CommandExecutionError as e:
+ msg = (("Failed to create new release: %s" % e.error) +
+ "\nExecuted command: %s" % e.cmd)
+ return _failure(name, msg)
changes = {}
warnings = []
@@ -120,29 +125,35 @@
module_fn = 'helm.release_upgrade'
if changes.get("namespace"):
LOG.debug("purging old release (%s) due to namespace change" % name)
- err = __salt__['helm.release_delete'](name, **kwargs)
- if err:
- return _failure(name, err, changes)
+ try:
+ result = __salt__['helm.release_delete'](name, **kwargs)
+ except CommandExecutionError as e:
+ msg = ("Failed to delete release for namespace change: %s" % e.error +
+ "\nExecuted command: %s" % e.cmd)
+ return _failure(name, msg, changes)
+
module_fn = 'helm.release_create'
warnings.append('Release (%s) was replaced due to namespace change' % name)
- err = __salt__[module_fn](
+ try:
+ result = __salt__[module_fn](
name, chart_name, namespace, version, values_file, **kwargs
- )
- if err:
- return _failure(name, err, changes)
-
- ret = {
+ )
+ changes.update({ 'stdout': result.get('stdout') })
+ ret = {
'name': name,
'changes': changes,
'result': True,
- 'comment': 'Release "{}" was updated'.format(name),
- }
+ 'comment': 'Release "%s" was updated\nExecuted command: %s' % (name, result['cmd'])
+ }
+ if warnings:
+ ret['warnings'] = warnings
- if warnings:
- ret['warnings'] = warnings
-
- return ret
+ return ret
+ except CommandExecutionError as e:
+ msg = ("Failed to delete release for namespace change: %s" % e.error +
+ "\nExecuted command: %s" % e.cmd)
+ return _failure(name, msg, changes)
def absent(name, tiller_namespace='kube-system', **kwargs):
@@ -160,14 +171,16 @@
'name': name,
'changes': {},
'result': True,
- 'comment': 'Release "{}" doesn\'t exist'.format(name),
+ 'comment': 'Release "%s" doesn\'t exist' % name
}
- err = __salt__['helm.release_delete'](name, **kwargs)
- if err:
- return _failure(name, err)
- return {
+ try:
+ result = __salt__['helm.release_delete'](name, **kwargs)
+ return {
'name': name,
- 'changes': {name: 'DELETED'},
+ 'changes': { name: 'DELETED', 'stdout': result['stdout'] },
'result': True,
- 'comment': 'Release "{}" was deleted'.format(name),
- }
+ 'comment': 'Release "%s" was deleted\nExecuted command: %s' % (name, result['cmd'])
+ }
+ except CommandExecutionError as e:
+ return _failure(e.cmd, e.error)
+
diff --git a/_states/helm_repos.py b/_states/helm_repos.py
index ee1d556..5c41c0b 100644
--- a/_states/helm_repos.py
+++ b/_states/helm_repos.py
@@ -77,25 +77,29 @@
'result': True,
'comment': 'Successfully synced repositories: ' }
- output = None
+
try:
- output = __salt__['helm.update_repos'](helm_home=helm_home)
+ result = __salt__['helm.update_repos'](helm_home=helm_home)
+ cmd_str = "\nExecuted command: %s" % result['cmd']
+
+ success_repos = re.findall(
+ r'Successfully got an update from the \"([^\"]+)\"', result['stdout'])
+ failed_repos = re.findall(
+ r'Unable to get an update from the \"([^\"]+)\"', result['stdout'])
+
+ if failed_repos and len(failed_repos) > 0:
+ ret['result'] = False
+ ret['changes']['succeeded'] = success_repos
+ ret['changes']['failed'] = failed_repos
+ ret['comment'] = 'Failed to sync against some repositories' + cmd_str
+ else:
+ ret['comment'] += "%s" % success_repos + cmd_str
+
except CommandExecutionError as e:
+ ret['name'] = e.cmd
ret['result'] = False
- ret['comment'] = "Failed to update repos: %s" % e
+ ret['comment'] = ("Failed to update repos: %s" % e.error +
+ "\nExecuted command: %s" % e.cmd)
return ret
- success_repos = re.findall(
- r'Successfully got an update from the \"([^\"]+)\"', output)
- failed_repos = re.findall(
- r'Unable to get an update from the \"([^\"]+)\"', output)
-
- if failed_repos and len(failed_repos) > 0:
- ret['result'] = False
- ret['changes']['succeeded'] = success_repos
- ret['changes']['failed'] = failed_repos
- ret['comment'] = 'Failed to sync against some repositories'
- else:
- ret['comment'] += "%s" % success_repos
-
return ret
\ No newline at end of file