blob: ee1d55695b247d822b1de374f85e427e0f12df61 [file] [log] [blame]
tmeneau61efbef2017-10-17 11:19:46 -04001import re
2
3from salt.exceptions import CommandExecutionError
4
tmeneau8cf4fce2017-10-17 15:05:35 -04005def managed(name, present={}, absent=[], exclusive=False, helm_home=None):
tmeneau61efbef2017-10-17 11:19:46 -04006 '''
7 Ensure the supplied repositories are available to the helm client. If the
8 `exclusive` flag is set to a truthy value, any extra repositories in the
9 helm client will be removed.
10
11 name
12 The name of the state
13
14 present
15 A dict of repository names to urls to ensure are registered with the
16 Helm client
17
18 absent
19 A list of repository names to ensure are unregistered from the Helm client
tmeneau8cf4fce2017-10-17 15:05:35 -040020
tmeneau61efbef2017-10-17 11:19:46 -040021 exclusive
22 A boolean flag indicating whether the state should ensure only the
23 supplied repositories are availabe to the target minion.
tmeneau8cf4fce2017-10-17 15:05:35 -040024
25 helm_home
26 An optional path to the Helm home directory
tmeneau61efbef2017-10-17 11:19:46 -040027 '''
28 ret = {'name': name,
29 'changes': {},
30 'result': True,
31 'comment': ''}
32
33 try:
34 result = __salt__['helm.manage_repos'](
35 present=present,
36 absent=absent,
tmeneau8cf4fce2017-10-17 15:05:35 -040037 exclusive=exclusive,
38 helm_home=helm_home
tmeneau61efbef2017-10-17 11:19:46 -040039 )
40
41 if result['failed']:
42 ret['comment'] = 'Failed to add or remove some repositories'
43 ret['changes'] = result
44 ret['result'] = False
45 return ret
46
47 if result['added'] or result['removed']:
48 ret['comment'] = 'Repositories were added or removed'
49 ret['changes'] = result
50 return ret
51
52 ret['comment'] = ("Repositories were in the desired state: "
53 "%s" % [name for (name, url) in present.iteritems()])
54 return ret
55 except CommandExecutionError as e:
56 ret['result'] = False
57 ret['comment'] = "Failed to add some repositories: %s" % e
58 return ret
59
tmeneau8cf4fce2017-10-17 15:05:35 -040060def updated(name, helm_home=None):
tmeneau61efbef2017-10-17 11:19:46 -040061 '''
62 Ensure the local Helm repository cache is up to date with each of the
63 helm client's configured remote chart repositories. Because the `helm repo
64 update` command doesn't indicate whether any changes were made to the local
65 cache, this will only indicate change if the Helm client failed to retrieve
66 an update from one or more of the repositories, regardless of whether an
67 update was made to the local Helm chart repository cache.
68
69 name
70 The name of the state
tmeneau8cf4fce2017-10-17 15:05:35 -040071
72 helm_home
73 An optional path to the Helm home directory
tmeneau61efbef2017-10-17 11:19:46 -040074 '''
75 ret = {'name': name,
76 'changes': {},
77 'result': True,
78 'comment': 'Successfully synced repositories: ' }
79
80 output = None
81 try:
tmeneau8cf4fce2017-10-17 15:05:35 -040082 output = __salt__['helm.update_repos'](helm_home=helm_home)
tmeneau61efbef2017-10-17 11:19:46 -040083 except CommandExecutionError as e:
84 ret['result'] = False
85 ret['comment'] = "Failed to update repos: %s" % e
86 return ret
87
88 success_repos = re.findall(
89 r'Successfully got an update from the \"([^\"]+)\"', output)
90 failed_repos = re.findall(
91 r'Unable to get an update from the \"([^\"]+)\"', output)
92
93 if failed_repos and len(failed_repos) > 0:
94 ret['result'] = False
95 ret['changes']['succeeded'] = success_repos
96 ret['changes']['failed'] = failed_repos
97 ret['comment'] = 'Failed to sync against some repositories'
98 else:
99 ret['comment'] += "%s" % success_repos
100
101 return ret