~jelmer/byoci/setuptools

« back to all changes in this revision

Viewing changes to byoci/monitor/commands.py

  • Committer: Vincent Ladeuil
  • Date: 2018-11-23 18:01:40 UTC
  • mto: This revision was merged to the branch mainline in revision 213.
  • Revision ID: v.ladeuil+lp@free.fr-20181123180140-vbs1sc0v2zjyy681
Test and implemebt 'byo-ci-monitor run-job'.

Replacing the she standalone testing/byo-ci-run which has no tests.

Rand into a weird test error caused by a lack of 'ldap.{user,password}'
options.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
 
18
18
import logging
 
19
import re
19
20
import sys
20
21
import time
21
22
 
197
198
command_registry.register(UpdateSlave)
198
199
 
199
200
 
 
201
def get_job_default_values(name, info):
 
202
    params = None
 
203
    for action in info['actions']:
 
204
        if 'parameterDefinitions' in action:
 
205
            params = dict()
 
206
            for param_def in action['parameterDefinitions']:
 
207
                name = param_def['name']
 
208
                val = param_def['defaultParameterValue']['value']
 
209
                params[name] = val
 
210
    return params
 
211
 
 
212
 
200
213
class RunJob(Command):
201
214
 
202
215
    name = 'run-job'
203
216
    description = 'Run job[s].'
204
217
    arg_parser_class = ArgParser
205
218
 
 
219
    def __init__(self, **kwargs):
 
220
        super().__init__(**kwargs)
 
221
        self.parser.add_argument(
 
222
            'jobre', metavar='JOB_NAME_REGEXP',
 
223
            help='A regular expression for job names.')
 
224
 
 
225
    def run(self):
 
226
        self.jk = Jenkins(self.conf.get('byoci.api'),
 
227
                          self.conf.get('byoci.user'),
 
228
                          self.conf.get('byoci.token'))
 
229
        jobs = [j for j in self.jk.get_all_jobs()
 
230
                if re.search(self.options.jobre, j['fullname'])]
 
231
        if not jobs:
 
232
            logger.error('No job matches {}'.format(self.options.jobre))
 
233
            return 1
 
234
        for job in jobs:
 
235
            name = job['fullname']
 
236
            logger.info('Running {}'.format(name))
 
237
            info = self.jk.get_job_info(name)
 
238
            # Get the param values as the jenkins module doesn't support just
 
239
            # calling the job without any parameter :-/
 
240
            params = get_job_default_values(name, info)
 
241
            self.jk.build_job(name, params)
 
242
        return 0
 
243
 
206
244
 
207
245
command_registry.register(RunJob)
208
246