fix: add repo update state and improve repo change detection
Also rename the repos_synchronized SLS state to repos_managed
Fixes salt-formulas/salt-formula-helm#4
diff --git a/_states/helm_repos.py b/_states/helm_repos.py
new file mode 100644
index 0000000..88f0894
--- /dev/null
+++ b/_states/helm_repos.py
@@ -0,0 +1,93 @@
+import re
+
+from salt.exceptions import CommandExecutionError
+
+def managed(name, present={}, absent=[], exclusive=False):
+ '''
+ Ensure the supplied repositories are available to the helm client. If the
+ `exclusive` flag is set to a truthy value, any extra repositories in the
+ helm client will be removed.
+
+ name
+ The name of the state
+
+ present
+ A dict of repository names to urls to ensure are registered with the
+ Helm client
+
+ absent
+ A list of repository names to ensure are unregistered from the Helm client
+ exclusive
+ A boolean flag indicating whether the state should ensure only the
+ supplied repositories are availabe to the target minion.
+ '''
+ ret = {'name': name,
+ 'changes': {},
+ 'result': True,
+ 'comment': ''}
+
+ try:
+ result = __salt__['helm.manage_repos'](
+ present=present,
+ absent=absent,
+ exclusive=exclusive
+ )
+
+ if result['failed']:
+ ret['comment'] = 'Failed to add or remove some repositories'
+ ret['changes'] = result
+ ret['result'] = False
+ return ret
+
+ if result['added'] or result['removed']:
+ ret['comment'] = 'Repositories were added or removed'
+ ret['changes'] = result
+ return ret
+
+ ret['comment'] = ("Repositories were in the desired state: "
+ "%s" % [name for (name, url) in present.iteritems()])
+ return ret
+ except CommandExecutionError as e:
+ ret['result'] = False
+ ret['comment'] = "Failed to add some repositories: %s" % e
+ return ret
+
+def updated(name):
+ '''
+ Ensure the local Helm repository cache is up to date with each of the
+ helm client's configured remote chart repositories. Because the `helm repo
+ update` command doesn't indicate whether any changes were made to the local
+ cache, this will only indicate change if the Helm client failed to retrieve
+ an update from one or more of the repositories, regardless of whether an
+ update was made to the local Helm chart repository cache.
+
+ name
+ The name of the state
+ '''
+ ret = {'name': name,
+ 'changes': {},
+ 'result': True,
+ 'comment': 'Successfully synced repositories: ' }
+
+ output = None
+ try:
+ output = __salt__['helm.update_repos']()
+ except CommandExecutionError as e:
+ ret['result'] = False
+ ret['comment'] = "Failed to update repos: %s" % e
+ 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