From cb263aceee9e200e62a1c59996bae813590ea049 Mon Sep 17 00:00:00 2001 From: Ed Bartosh Date: Sun, 1 Feb 2015 12:01:55 +0200 Subject: [PATCH] Implement interface to Jenkins Implemented trigger_build function using 3rd party jenkinsapi. This function will be used for rebuild, remove and lock/unlock functionality. Change-Id: Idf55f4ed3c8dee51bdb51719fa338d68867b7b25 Signed-off-by: Ed Bartosh --- debian/control | 1 + packaging/repa.spec | 1 + repa.1 | 11 +++++++++- repa/jenkins.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ repa/main.py | 3 ++- 5 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 repa/jenkins.py diff --git a/debian/control b/debian/control index 79738f9..1563322 100644 --- a/debian/control +++ b/debian/control @@ -14,6 +14,7 @@ Depends: python (>=2.6), osc, gbs-api, python-setuptools, + python-jenkinsapi, ${misc:Depends}, ${python:Depends} Description: tool to help release engineers to maintain code submissions diff --git a/packaging/repa.spec b/packaging/repa.spec index 75d62a9..67fa065 100644 --- a/packaging/repa.spec +++ b/packaging/repa.spec @@ -18,6 +18,7 @@ Requires: gbs-api Requires: osc Requires: python >= 2.6 Requires: python-setuptools +Requires: python-jenkinsapi %description This tool is to assist release engineers to operate with submissions diff --git a/repa.1 b/repa.1 index 8ae3320..099d2a0 100644 --- a/repa.1 +++ b/repa.1 @@ -382,6 +382,15 @@ apiuser = your_user_name apipasswd = your_password .RE .RS 2 +jenkins_url = https://build.tizen.org/robot +.RE +.RS 2 +jenkins_user = your_jenkins_user_name +.RE +.RS 2 +jenkins_passwd = your_jenkins_password +.RE +.RS 2 processes = 20 .RE .RS 2 @@ -405,7 +414,7 @@ noaggregate = mic-bootstrap-x86-arm.rpm|mic-bootstrap.rpm|mic-bootstrap-debugsou .RS 2 -Mandatory options: apiurl, apiuser, apipasswd and project +Mandatory options: apiurl, apiuser, apipasswd, jenkins_url, jenkins_user, jenkins_passwd and project .RE .RS 2 diff --git a/repa/jenkins.py b/repa/jenkins.py new file mode 100644 index 0000000..29759e8 --- /dev/null +++ b/repa/jenkins.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# +# This file is part of REPA: Release Engineering Process Assistant. +# +# Copyright (C) 2015 Intel Corporation +# +# REPA is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# version 2 as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. + +""" +REPA: Release Engineering Process Assistant. + +Copyright (C) Intel Corporation 2015 +Licence: GPL version 2 +Author: Ed Bartosh + +Jenkins module. Triggering Jenkins builds. +""" + +from jenkinsapi.jenkins import Jenkins +from requests import HTTPError, ConnectionError + +class JenkinsError(Exception): + """Local exception.""" + pass + +def trigger_build(job, parameters, cred, block=True): + """ + Trigger Jenkins build. + + :param job: job name + :type job: string + :param parameters: job parameters + :type parameters: dictionary + :param cred: credentials + :type cred: named tuple cred.url, cred.username, cred.password + :param block: block the call until build is finished + :type block: bool + :returns: build number, build status, console output + """ + try: + jenkins = Jenkins(cred.url, cred.username, cred.password) + except (HTTPError, ConnectionError) as error: + raise JenkinsError("Can't connect to jenkins: %s" % str(error)) + + if job not in jenkins: + raise JenkinsError("Job %s doesn't exist" % job) + + qitem = jenkins[job].invoke(block=block, build_params=parameters) + build = qitem.get_build() + return build.get_number(), build.get_status(), build.get_console() diff --git a/repa/main.py b/repa/main.py index 44680cc..39f42ec 100755 --- a/repa/main.py +++ b/repa/main.py @@ -83,7 +83,8 @@ def parse_args(argv): def read_config(paths=('/etc/repa.conf', expanduser('~/.repa.conf')), section='general', - mandatory=('apiurl', 'apiuser', 'apipasswd', 'project')): + mandatory=('apiurl', 'apiuser', 'apipasswd', 'project', + 'jenkins_url', 'jenkins_user', 'jenkins_passwd')): """ Read repa config. Configuration is read from the set of files provided. -- 2.7.4