From: Ed Bartosh Date: Tue, 12 Nov 2013 11:53:44 +0000 (+0200) Subject: Moved ref sync from job_sync_git to job_submitobs X-Git-Tag: 0.15~49 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1533ede47bceeebdcb95b8542c79ef92d7b2f52c;p=services%2Fjenkins-scripts.git Moved ref sync from job_sync_git to job_submitobs This is done to avoid submitting to obs on sync instance if sync fails. It's impossible to do when jobs are running asynchronously. This change also reduces amount of code as it removes duplication code used in both jobs. Fixes: #1437 Change-Id: Iba10ec0126963b65296ba516cc7215a86212db36 Signed-off-by: Ed Bartosh --- diff --git a/job_submitobs.py b/job_submitobs.py index 7133e7d..a974e7d 100755 --- a/job_submitobs.py +++ b/job_submitobs.py @@ -18,7 +18,7 @@ from common import utils from common.git import Git, clone_gitproject from common import obspkg from common import mapping -from common.gerrit import Gerrit, get_gerrit_event +from common.gerrit import Gerrit, get_gerrit_event, GerritError from common.send_mail import prepare_mail from common.buildservice import BuildService @@ -276,9 +276,41 @@ def check_obs_project(apiurl, apiuser, apipasswd, obs_prjs): return [prj for prj in set(obs_prjs) if not bs.exists(prj)] +def sync_ref(event, git, user, host, port): + """ + Push ref to gerrit. Create project if necessary + Return: True if sync was successful, False otherwise. + """ + + try: + mygerrit = Gerrit(host, user, port) + mygerrit.create_project(event['project'], '-p', + os.getenv('GERRIT_PARENT_PROJECT')) + except GerritError, err: + print >> sys.stderr, "Error creating gerrit project %s: %s" % \ + (event['project'], str(err)) + + repo = 'ssh://%s@%s:%s/%s' % (user, host, port, event['project']) + refname = event['refname'] + try: + if event['newrev'] == '0000000000000000000000000000000000000000': + git.push(repo=repo, src=':%s' % event['refname']) + elif event['refname'].startswith("refs/tags/"): + git.push(repo=repo, src=refname, tags=True, force=True) + else: + git.push(repo=repo, src='origin/%s' % refname, + dst='refs/heads/%s' % refname, tags=True, force=True) + except GitRepositoryError, err: + print >> sys.stderr, "Error pushing %s to %s: %s" % (refname, repo, err) + return False + + return True + def main(): """script entry point""" + print '---[JOB STARTED]----------------------------------------' + # prepare related global variables workspace = os.getenv('WORKSPACE') gitcache = os.getenv('GIT_CACHE_DIR') @@ -288,20 +320,14 @@ def main(): apipasswd = os.getenv('OBS_API_PASSWD') event = get_gerrit_event() - # prepare separate temp directory for each build - tmpdir = tempfile.mkdtemp(prefix=workspace+'/') - prjdir = os.path.join(tmpdir, event['project']) - print '---[JOB STARTED]----------------------------------------' - # check whether tag name is start with 'submit/' - if event['event_type'] == "ref-updated": - if not event['refname'].startswith('refs/tags/submit/'): - print '\nREFNAME "%s" isn\'t start with refs/tags/submit, exit '\ - 'now' % event['refname'] - return 0 - elif is_tag_deleted(event['oldrev'], event['newrev']): - print '\nREFNAME "%s" is deleted, exit now' % event['refname'] - return 0 + if event['event_type'] != "ref-updated": + # This is just a sanity check as ref-updated is the only event we + # react on and it's configured in the job configuraion + print >> sys.stderr, "Configuration error: This job can't process"\ + "event %s! Only ref-updated events are allowed" % \ + event['event_type'] + return 1 if os.getenv('SYNC_GERRIT_PROJECT_PATTERN'): # check whether need to sync this project to inside obs @@ -310,6 +336,37 @@ def main(): print 'Do not need to sync project %s, exit now' % event['project'] return 0 + if event['refname'].startswith('refs/changes/'): + print 'Do not need to process refname %s, exit now' % event['refname'] + return 0 + + # prepare separate temp directory for each build + tmpdir = tempfile.mkdtemp(prefix=workspace+'/') + prjdir = os.path.join(tmpdir, event['project']) + + # clone gerrit project to local dir + if not clone_gitproject(event['project'], prjdir): + print >> sys.stderr, 'Error cloning %s' % event['project'] + return 1 + mygit = Git(prjdir) + + # sync refs if it's sync instance + sync_user = os.getenv('SYNC_GERRIT_USERNAME') + sync_host = os.getenv('SYNC_GERRIT_HOSTNAME') + sync_port = os.getenv('SYNC_GERRIT_SSHPORT') + if sync_user and sync_host: + if not sync_ref(event, mygit, sync_user, sync_host, sync_port): + return 1 + + # check whether tag name is start with 'submit/' + if not event['refname'].startswith('refs/tags/submit/'): + print '\nREFNAME "%s" isn\'t start with refs/tags/submit, exit now'\ + % event['refname'] + return 0 + elif is_tag_deleted(event['oldrev'], event['newrev']): + print '\nREFNAME "%s" is deleted, exit now' % event['refname'] + return 0 + obs_mapping = '%s/%s/git-obs-mapping.xml' % (gitcache, mapping_prj) # check whether git-obs-mapping.xml exist in local if not os.path.isfile(obs_mapping): @@ -324,11 +381,6 @@ def main(): print '\nThis project do not map to any OBS project, exit now' return 0 - # clone gerrit project to local dir - if not clone_gitproject(event['project'], prjdir): - return 1 - - mygit = Git(prjdir) mygerrit = Gerrit(event['hostname'], event['username'], \ event['sshport'], int(os.getenv('GERRIT_SILENT_MODE'))) diff --git a/job_sync_git.py b/job_sync_git.py deleted file mode 100755 index 9e8b764..0000000 --- a/job_sync_git.py +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/env python -# vim: ai ts=4 sts=4 et sw=4 - -"""This script will sync a merged change in gerrit to OBS corresponding project. -""" - -import os -import sys -import tempfile -import shutil -import re - -# internal module -from common.git import Git, clone_gitproject -from common.gerrit import get_gerrit_event -from common import gerrit - -NULL_COMMIT = '0000000000000000000000000000000000000000' - -def end(rc = 0, tmpdir = None): - # cleanup workspace - if tmpdir and os.path.isdir(tmpdir): - shutil.rmtree(tmpdir) - return rc - -def main(): - - print '---[JOB STARTED]----------------------------------------' - - event = get_gerrit_event() - - # check whether need to sync this project to inside gerrit - r = re.compile(os.getenv('SYNC_GERRIT_PROJECT_PATTERN')) - if not r.match(event['project']): - print 'Do not need to sync project %s, exit now' % event['project'] - return end(0) - - if event['refname'].startswith('refs/changes/'): - print 'Do not need to sync REFNAME %s, exit now' % event['refname'] - return end(0) - - workspace = os.getenv('WORKSPACE') - tmpdir = tempfile.mkdtemp(prefix=workspace+'/') - prjdir = os.path.join(tmpdir, event['project']) - - tz_git_url = 'ssh://%s@%s:%s' % (os.getenv('SYNC_GERRIT_USERNAME'), - os.getenv('SYNC_GERRIT_HOSTNAME'), - os.getenv('SYNC_GERRIT_SSHPORT')) - - # clone gerrit project to local dir - if not clone_gitproject(event['project'], prjdir): - return end(1, tmpdir) - - mygit = Git(prjdir) - - try: - mygerrit = gerrit.Gerrit(os.getenv('SYNC_GERRIT_HOSTNAME'), - os.getenv('SYNC_GERRIT_USERNAME'), - os.getenv('SYNC_GERRIT_SSHPORT')) - - mygerrit.create_project(event['project'], '-p', - os.getenv('GERRIT_PARENT_PROJECT')) - except gerrit.GerritError, err: - print >> sys.stderr, "Error creating gerrit project %s: %s" % \ - (event['project'], str(err)) - - if event['newrev'] == NULL_COMMIT: - mygit.push(repo='%s/%s' % (tz_git_url, event['project']), src=':%s' % event['refname']) - else: - if event['refname'].startswith("refs/tags/"): - mygit.push(repo='%s/%s' % (tz_git_url, event['project']), - src=event['refname'], tags=True, force=True) - else: - mygit.push(repo='%s/%s' % (tz_git_url, event['project']), - src='origin/%s' % event['refname'], dst='refs/heads/%s' % - event['refname'], tags=True, force=True) - - return end(0, tmpdir) - -if __name__ == '__main__': - sys.exit(main()) diff --git a/packaging/jenkins-scripts.spec b/packaging/jenkins-scripts.spec index b50b41c..8be1de3 100644 --- a/packaging/jenkins-scripts.spec +++ b/packaging/jenkins-scripts.spec @@ -39,15 +39,6 @@ Requires: gbs-api %description common Common part of jenkins scripts -%package sync -Summary: Scripts for tizendev/tizen.org Jenkins instance -Group: Development/Tools/Building -Requires: %{name}-common = %{version}-%{release} -Requires: %{name}-submitobs = %{version}-%{release} - -%description sync -Jenkins scripts for tizendev/tizen.org Jenkins instance - %package tzs Summary: Scripts for tzs customer appliance Group: Development/Tools/Building @@ -129,10 +120,6 @@ fi %{destdir}/job_load_repos.yaml.py %{destdir}/job_update_local_git.py -%files sync -%defattr(-,jenkins,jenkins) -%{destdir}/job_sync_git.py - %files tzs %defattr(-,jenkins,jenkins) %{destdir}/job_submit.py @@ -141,5 +128,4 @@ fi %defattr(-,jenkins,jenkins) %{destdir}/job_submitobs.py - %changelog