tmeneau | 61efbef | 2017-10-17 11:19:46 -0400 | [diff] [blame] | 1 | import re |
| 2 | |
| 3 | from salt.exceptions import CommandExecutionError |
| 4 | |
| 5 | def managed(name, present={}, absent=[], exclusive=False): |
| 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 |
| 20 | exclusive |
| 21 | A boolean flag indicating whether the state should ensure only the |
| 22 | supplied repositories are availabe to the target minion. |
| 23 | ''' |
| 24 | ret = {'name': name, |
| 25 | 'changes': {}, |
| 26 | 'result': True, |
| 27 | 'comment': ''} |
| 28 | |
| 29 | try: |
| 30 | result = __salt__['helm.manage_repos']( |
| 31 | present=present, |
| 32 | absent=absent, |
| 33 | exclusive=exclusive |
| 34 | ) |
| 35 | |
| 36 | if result['failed']: |
| 37 | ret['comment'] = 'Failed to add or remove some repositories' |
| 38 | ret['changes'] = result |
| 39 | ret['result'] = False |
| 40 | return ret |
| 41 | |
| 42 | if result['added'] or result['removed']: |
| 43 | ret['comment'] = 'Repositories were added or removed' |
| 44 | ret['changes'] = result |
| 45 | return ret |
| 46 | |
| 47 | ret['comment'] = ("Repositories were in the desired state: " |
| 48 | "%s" % [name for (name, url) in present.iteritems()]) |
| 49 | return ret |
| 50 | except CommandExecutionError as e: |
| 51 | ret['result'] = False |
| 52 | ret['comment'] = "Failed to add some repositories: %s" % e |
| 53 | return ret |
| 54 | |
| 55 | def updated(name): |
| 56 | ''' |
| 57 | Ensure the local Helm repository cache is up to date with each of the |
| 58 | helm client's configured remote chart repositories. Because the `helm repo |
| 59 | update` command doesn't indicate whether any changes were made to the local |
| 60 | cache, this will only indicate change if the Helm client failed to retrieve |
| 61 | an update from one or more of the repositories, regardless of whether an |
| 62 | update was made to the local Helm chart repository cache. |
| 63 | |
| 64 | name |
| 65 | The name of the state |
| 66 | ''' |
| 67 | ret = {'name': name, |
| 68 | 'changes': {}, |
| 69 | 'result': True, |
| 70 | 'comment': 'Successfully synced repositories: ' } |
| 71 | |
| 72 | output = None |
| 73 | try: |
| 74 | output = __salt__['helm.update_repos']() |
| 75 | except CommandExecutionError as e: |
| 76 | ret['result'] = False |
| 77 | ret['comment'] = "Failed to update repos: %s" % e |
| 78 | return ret |
| 79 | |
| 80 | success_repos = re.findall( |
| 81 | r'Successfully got an update from the \"([^\"]+)\"', output) |
| 82 | failed_repos = re.findall( |
| 83 | r'Unable to get an update from the \"([^\"]+)\"', output) |
| 84 | |
| 85 | if failed_repos and len(failed_repos) > 0: |
| 86 | ret['result'] = False |
| 87 | ret['changes']['succeeded'] = success_repos |
| 88 | ret['changes']['failed'] = failed_repos |
| 89 | ret['comment'] = 'Failed to sync against some repositories' |
| 90 | else: |
| 91 | ret['comment'] += "%s" % success_repos |
| 92 | |
| 93 | return ret |