Ilya Kharin | 9be4068 | 2017-05-02 16:43:51 +0400 | [diff] [blame^] | 1 | import logging |
| 2 | |
| 3 | LOG = logging.getLogger(__name__) |
| 4 | |
| 5 | |
| 6 | def __virtual__(): |
| 7 | if 'rundeck.get_project' not in __salt__: |
| 8 | return ( |
| 9 | False, |
| 10 | 'The rundeck_scm state module cannot be loaded: rundeck is ' |
| 11 | 'unavailable', |
| 12 | ) |
| 13 | return True |
| 14 | |
| 15 | |
| 16 | def present_import(name, project_name, **params): |
| 17 | result = { |
| 18 | 'name': name, |
| 19 | 'changes': {}, |
| 20 | 'result': False, |
| 21 | 'comment': '', |
| 22 | 'pchanges': {}, |
| 23 | } |
| 24 | if __opts__['test'] == True: |
| 25 | result['comment'] = 'There is nothing to change in the test mode.' |
| 26 | result['result'] = None |
| 27 | ok, plugin = __salt__['rundeck.get_plugin'](project_name, 'import') |
| 28 | if ok: |
| 29 | if plugin: |
| 30 | config = __salt__['rundeck.create_scm_import_config']( |
| 31 | project_name, params, config=plugin['config']) |
| 32 | LOG.debug("SCM Import for the %s project: %s/%s", |
| 33 | project_name, plugin["config"], config) |
| 34 | if plugin['config'] != config: |
| 35 | ok, plugin = __salt__['rundeck.update_scm_import_config']( |
| 36 | project_name, plugin, config) |
| 37 | result['comment'] = ( |
| 38 | "SCM Import plugin for the {} project was updated." |
| 39 | .format(project_name)) |
| 40 | result['changes'][name] = 'UPDATED' |
| 41 | else: |
| 42 | result['comment'] = ( |
| 43 | "SCM Import plugin for the {} project is already up to " |
| 44 | "date.".format(project_name)) |
| 45 | result['result'] = True |
| 46 | else: |
| 47 | ok, plugin = __salt__['rundeck.setup_scm_import']( |
| 48 | project_name, params) |
| 49 | if ok: |
| 50 | result['changes'][name] = 'CREATED' |
| 51 | result['comment'] = ( |
| 52 | "SCM Import was configured for the {} project." |
| 53 | .format(project_name)) |
| 54 | result['result'] = True |
| 55 | else: |
| 56 | result['comment'] = plugin |
| 57 | else: |
| 58 | result['comment'] = plugin |
| 59 | return result |
| 60 | |
| 61 | |
| 62 | def sync_import(name, project_name, **params): |
| 63 | result = { |
| 64 | 'name': name, |
| 65 | 'changes': {}, |
| 66 | 'result': True, |
| 67 | 'comment': '', |
| 68 | 'pchanges': {}, |
| 69 | } |
| 70 | |
| 71 | if __opts__['test'] == True: |
| 72 | result['comment'] = 'There is nothing to change in the test mode.' |
| 73 | result['result'] = None |
| 74 | return result |
| 75 | |
| 76 | ok, plugin = __salt__['rundeck.get_plugin'](project_name, 'import') |
| 77 | if not ok: |
| 78 | result['comment'] = plugin |
| 79 | return result |
| 80 | |
| 81 | ok, state = __salt__['rundeck.get_plugin_state'](project_name, 'import') |
| 82 | if not ok: |
| 83 | result['comment'] = state |
| 84 | return result |
| 85 | |
| 86 | history = [] |
| 87 | |
| 88 | for action_name, action in [ |
| 89 | ('initialize-tracking', 'rundeck.perform_scm_import_tracking'), |
| 90 | ('remote-pull', 'rundeck.perform_scm_import_pull'), |
| 91 | ('import-all', 'rundeck.perform_scm_import'), |
| 92 | ]: |
| 93 | if action_name in state['actions']: |
| 94 | ok, msg = __salt__[action]( |
| 95 | project_name, plugin, params) |
| 96 | if not ok: |
| 97 | result['comment'] = msg |
| 98 | result['result'] = False |
| 99 | return result |
| 100 | else: |
| 101 | history.append(msg['message']) |
| 102 | |
| 103 | ok, state = __salt__['rundeck.get_plugin_state']( |
| 104 | project_name, 'import') |
| 105 | if not ok: |
| 106 | result['comment'] = state |
| 107 | result['result'] = False |
| 108 | return result |
| 109 | |
| 110 | if history: |
| 111 | result['changes'][name] = '\n'.join(history) |
| 112 | return result |
| 113 | |
| 114 | |
| 115 | def disabled_import(name, project_name): |
| 116 | result = { |
| 117 | 'name': name, |
| 118 | 'changes': {}, |
| 119 | 'result': False, |
| 120 | 'comment': '', |
| 121 | 'pchanges': {}, |
| 122 | } |
| 123 | if __opts__['test'] == True: |
| 124 | result['comment'] = 'There is nothing to change in the test mode.' |
| 125 | result['result'] = None |
| 126 | ok, status = __salt__['rundeck.get_plugin_status'](project_name, 'import') |
| 127 | if ok: |
| 128 | if status['enabled']: |
| 129 | ok, msg = __salt__['rundeck.disable_plugin'](project_name, 'import') |
| 130 | result['comment'] = msg |
| 131 | if ok: |
| 132 | result['changes'][name] = 'DISABLED' |
| 133 | result['result'] = True |
| 134 | else: |
| 135 | result['result'] = True |
| 136 | else: |
| 137 | result['comment'] = status |
| 138 | return result |
| 139 | |
| 140 | |
| 141 | def enabled_import(name, project_name): |
| 142 | result = { |
| 143 | 'name': name, |
| 144 | 'changes': {}, |
| 145 | 'result': False, |
| 146 | 'comment': '', |
| 147 | 'pchanges': {}, |
| 148 | } |
| 149 | if __opts__['test'] == True: |
| 150 | result['comment'] = 'There is nothing to change in the test mode.' |
| 151 | result['result'] = None |
| 152 | ok, status = __salt__['rundeck.get_plugin_status'](project_name, 'import') |
| 153 | if ok: |
| 154 | if status['configured'] and not status['enabled']: |
| 155 | ok, msg = __salt__['rundeck.enable_plugin'](project_name, 'import') |
| 156 | result['comment'] = msg |
| 157 | if ok: |
| 158 | result['changes'][name] = 'ENABLED' |
| 159 | result['result'] = True |
| 160 | elif not status['configured']: |
| 161 | result['comment'] = "Could not enable not configured SCM plugin." |
| 162 | else: |
| 163 | result['result'] = True |
| 164 | else: |
| 165 | result['comment'] = status |
| 166 | return result |