Alexander Evseev | cd836a1 | 2018-07-27 13:02:45 +0200 | [diff] [blame] | 1 | import json |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 2 | import logging |
Alexander Evseev | cd836a1 | 2018-07-27 13:02:45 +0200 | [diff] [blame] | 3 | import time |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 4 | |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 5 | |
Alexander Evseev | cd836a1 | 2018-07-27 13:02:45 +0200 | [diff] [blame] | 6 | log = logging.getLogger(__name__) |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 7 | |
| 8 | |
Ilya Kharin | 3d8bffe | 2017-06-22 17:40:31 +0400 | [diff] [blame] | 9 | def __virtual__(): |
| 10 | ''' |
| 11 | Only load if jenkins_common module exist. |
| 12 | ''' |
| 13 | if 'jenkins_common.call_groovy_script' not in __salt__: |
| 14 | return ( |
| 15 | False, |
Alexander Evseev | cd836a1 | 2018-07-27 13:02:45 +0200 | [diff] [blame] | 16 | 'The jenkins_node state module cannot be loaded: ' |
Ilya Kharin | 3d8bffe | 2017-06-22 17:40:31 +0400 | [diff] [blame] | 17 | 'jenkins_common not found') |
| 18 | return True |
| 19 | |
Alexander Evseev | cd836a1 | 2018-07-27 13:02:45 +0200 | [diff] [blame] | 20 | def managed(name, plugins, remove_unwanted=False, force_remove=False): |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 21 | """ |
Alexander Evseev | cd836a1 | 2018-07-27 13:02:45 +0200 | [diff] [blame] | 22 | Manage jenkins plugins |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 23 | |
Alexander Evseev | cd836a1 | 2018-07-27 13:02:45 +0200 | [diff] [blame] | 24 | :param name: salt resource name (usually 'jenkins_plugin_manage') |
| 25 | :param plugins: map containing plugin names and parameters |
| 26 | :param remove_unwanted: whether to remove not listed plugins |
| 27 | :param force_remove: force removing plugins recursively with all dependent plugins |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 28 | :returns: salt-specified state dict |
| 29 | """ |
Alexander Evseev | cd836a1 | 2018-07-27 13:02:45 +0200 | [diff] [blame] | 30 | log.info('Managing jenkins plugins') |
| 31 | template = __salt__['jenkins_common.load_template']( |
| 32 | 'salt://jenkins/files/groovy/plugin.template', |
| 33 | __env__) |
| 34 | result = __salt__['jenkins_common.api_call'](name, template, |
| 35 | [ 'UPDATED', 'NO CHANGES' ], |
| 36 | { |
| 37 | 'plugin_list': json.dumps(plugins), |
| 38 | 'clean_unwanted': remove_unwanted, |
| 39 | 'force_remove': force_remove |
| 40 | }, |
| 41 | 'Manage Jenkins plugins') |
| 42 | log.debug('Got result: ' + json.dumps(result)) |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 43 | |
Alexander Evseev | cd836a1 | 2018-07-27 13:02:45 +0200 | [diff] [blame] | 44 | log.info('Checking if restart is required...') |
| 45 | # While next code is successful, we should wait for jenkins shutdown |
| 46 | # either: |
| 47 | # - false returned by isQuietingDown() |
| 48 | # - any error meaning that jenkins is unavailable (restarting) |
| 49 | wait = { 'result': True } |
| 50 | while (wait['result']): |
| 51 | wait = __salt__['jenkins_common.api_call']('jenkins_restart_wait', |
| 52 | 'println Jenkins.instance.isQuietingDown()', [ 'true' ], {}, |
| 53 | 'Wait for jenkins restart') |
| 54 | if (wait['result']): |
| 55 | log.debug('Jenkins restart is required. Waiting...') |
| 56 | time.sleep(5) |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 57 | |
Alexander Evseev | cd836a1 | 2018-07-27 13:02:45 +0200 | [diff] [blame] | 58 | return result |