tmeneau | 61efbef | 2017-10-17 11:19:46 -0400 | [diff] [blame] | 1 | import re |
| 2 | |
| 3 | from salt.exceptions import CommandExecutionError |
| 4 | |
tmeneau | 8cf4fce | 2017-10-17 15:05:35 -0400 | [diff] [blame] | 5 | def managed(name, present={}, absent=[], exclusive=False, helm_home=None): |
tmeneau | 61efbef | 2017-10-17 11:19:46 -0400 | [diff] [blame] | 6 | ''' |
| 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 |
tmeneau | 8cf4fce | 2017-10-17 15:05:35 -0400 | [diff] [blame] | 20 | |
tmeneau | 61efbef | 2017-10-17 11:19:46 -0400 | [diff] [blame] | 21 | exclusive |
| 22 | A boolean flag indicating whether the state should ensure only the |
| 23 | supplied repositories are availabe to the target minion. |
tmeneau | 8cf4fce | 2017-10-17 15:05:35 -0400 | [diff] [blame] | 24 | |
| 25 | helm_home |
| 26 | An optional path to the Helm home directory |
tmeneau | 61efbef | 2017-10-17 11:19:46 -0400 | [diff] [blame] | 27 | ''' |
| 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, |
tmeneau | 8cf4fce | 2017-10-17 15:05:35 -0400 | [diff] [blame] | 37 | exclusive=exclusive, |
| 38 | helm_home=helm_home |
tmeneau | 61efbef | 2017-10-17 11:19:46 -0400 | [diff] [blame] | 39 | ) |
| 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 | |
tmeneau | 8cf4fce | 2017-10-17 15:05:35 -0400 | [diff] [blame] | 60 | def updated(name, helm_home=None): |
tmeneau | 61efbef | 2017-10-17 11:19:46 -0400 | [diff] [blame] | 61 | ''' |
| 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 |
tmeneau | 8cf4fce | 2017-10-17 15:05:35 -0400 | [diff] [blame] | 71 | |
| 72 | helm_home |
| 73 | An optional path to the Helm home directory |
tmeneau | 61efbef | 2017-10-17 11:19:46 -0400 | [diff] [blame] | 74 | ''' |
| 75 | ret = {'name': name, |
| 76 | 'changes': {}, |
| 77 | 'result': True, |
| 78 | 'comment': 'Successfully synced repositories: ' } |
| 79 | |
tmeneau | b858a00 | 2017-10-27 09:12:30 -0400 | [diff] [blame] | 80 | |
tmeneau | 61efbef | 2017-10-17 11:19:46 -0400 | [diff] [blame] | 81 | try: |
tmeneau | b858a00 | 2017-10-27 09:12:30 -0400 | [diff] [blame] | 82 | result = __salt__['helm.update_repos'](helm_home=helm_home) |
| 83 | cmd_str = "\nExecuted command: %s" % result['cmd'] |
| 84 | |
| 85 | success_repos = re.findall( |
| 86 | r'Successfully got an update from the \"([^\"]+)\"', result['stdout']) |
| 87 | failed_repos = re.findall( |
| 88 | r'Unable to get an update from the \"([^\"]+)\"', result['stdout']) |
| 89 | |
| 90 | if failed_repos and len(failed_repos) > 0: |
| 91 | ret['result'] = False |
| 92 | ret['changes']['succeeded'] = success_repos |
| 93 | ret['changes']['failed'] = failed_repos |
| 94 | ret['comment'] = 'Failed to sync against some repositories' + cmd_str |
| 95 | else: |
| 96 | ret['comment'] += "%s" % success_repos + cmd_str |
| 97 | |
tmeneau | 61efbef | 2017-10-17 11:19:46 -0400 | [diff] [blame] | 98 | except CommandExecutionError as e: |
tmeneau | b858a00 | 2017-10-27 09:12:30 -0400 | [diff] [blame] | 99 | ret['name'] = e.cmd |
tmeneau | 61efbef | 2017-10-17 11:19:46 -0400 | [diff] [blame] | 100 | ret['result'] = False |
tmeneau | b858a00 | 2017-10-27 09:12:30 -0400 | [diff] [blame] | 101 | ret['comment'] = ("Failed to update repos: %s" % e.error + |
| 102 | "\nExecuted command: %s" % e.cmd) |
tmeneau | 61efbef | 2017-10-17 11:19:46 -0400 | [diff] [blame] | 103 | return ret |
| 104 | |
tmeneau | 61efbef | 2017-10-17 11:19:46 -0400 | [diff] [blame] | 105 | return ret |