blob: a96afeddbac9b0dfec667d2d75be8bc1221cd01e [file] [log] [blame]
Alexander Evseevcd836a12018-07-27 13:02:45 +02001import json
Jakub Josef10b4e102017-01-05 18:18:41 +01002import logging
Alexander Evseevcd836a12018-07-27 13:02:45 +02003import time
Adam Tengler70763e02017-08-21 16:50:32 +00004
Jakub Josef10b4e102017-01-05 18:18:41 +01005
Alexander Evseevcd836a12018-07-27 13:02:45 +02006log = logging.getLogger(__name__)
Jakub Josef10b4e102017-01-05 18:18:41 +01007
8
Ilya Kharin3d8bffe2017-06-22 17:40:31 +04009def __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 Evseevcd836a12018-07-27 13:02:45 +020016 'The jenkins_node state module cannot be loaded: '
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040017 'jenkins_common not found')
18 return True
19
Alexander Evseevcd836a12018-07-27 13:02:45 +020020def managed(name, plugins, remove_unwanted=False, force_remove=False):
Jakub Josef10b4e102017-01-05 18:18:41 +010021 """
Alexander Evseevcd836a12018-07-27 13:02:45 +020022 Manage jenkins plugins
Jakub Josef10b4e102017-01-05 18:18:41 +010023
Alexander Evseevcd836a12018-07-27 13:02:45 +020024 :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 Josef10b4e102017-01-05 18:18:41 +010028 :returns: salt-specified state dict
29 """
Alexander Evseevcd836a12018-07-27 13:02:45 +020030 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 Josef10b4e102017-01-05 18:18:41 +010043
Alexander Evseevcd836a12018-07-27 13:02:45 +020044 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 Josef10b4e102017-01-05 18:18:41 +010057
Alexander Evseevcd836a12018-07-27 13:02:45 +020058 return result