+++ /dev/null
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
-import os
-import sys
-import git
-import urllib
-import logging
-import shutil
-import subprocess
-import socket
-
-from logging import Formatter
-
-JOB_URL = 'http://jenkins:80/job/%(job_name)s/buildWithParameters?token=BuildIt&'
-JOB_DELAY = '0sec'
-
-LOG_FORMAT = '%(asctime)s %(name)s[%(process)d] %(levelname)s: %(message)s'
-#logging.basicConfig(format=LOG_FORMAT, level=logging.DEBUG)
-logger = logging.getLogger(__file__)
-logger.setLevel(logging.INFO)
-stream_handler = logging.StreamHandler(strm=sys.stdout)
-stream_handler.setLevel(logging.INFO)
-formatter = Formatter('%(asctime)s %(name)s[%(process)d] %(levelname)s: %(message)s')
-stream_handler.setFormatter(formatter)
-logger.addHandler(stream_handler)
-
-
-
-class HudsonUrl(urllib.FancyURLopener):
- pass
-
-
-def checkout_local(old_id, new_id):
- git_work_dir = os.path.join('/tmp', '%s' %(new_id))
- os.mkdir(git_work_dir, 0755)
- cmd = ['/usr/bin/git', 'checkout', '-f', '%s' %(new_id), '%s' %(git_work_dir)]
- cmdobj = subprocess.Popen(
- cmd,
- shell=False,
- env={'GIT_WORK_TREE': git_work_dir},
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- close_fds=True
- )
-
- ret = cmdobj.wait()
-
- if ret:
- logger.error('Error was: %s' %(cmdobj.stderr.readlines()))
- raise Exception(
- 'cmd %s returned with %s' %(' '.join(cmd), ret)
- )
- sys.exit(1)
-
- return git_work_dir
-
-def has_config(git_id):
- cmd = ['/usr/bin/git', 'ls-tree', '--name-only', str(git_id)]
- cmdobj = subprocess.Popen(
- cmd,
- shell=False,
- env={'': ''},
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- close_fds=True
- )
-
- ret = cmdobj.wait()
-
- if ret:
- logger.debug(
- 'cmd %s returned with %s' %(' '.join(cmd), ret)
- )
- logger.error('Error was: %s' %(cmdobj.stderr.readlines()))
- sys.exit(1)
-
- files = map(lambda x: x.rstrip(), cmdobj.stdout.readlines())
- return '.config' in files
-
-def delete_local_checkout(directory):
- try:
- shutil.rmtree(directory)
- except Exception, error:
- logger.error('Some error happend while deleting %s' %(directory))
- logger.exception(error)
- return False
- else:
- logger.info('%s successfully deleted' %(directory))
- return True
-
-
-if __name__ == '__main__':
- all_values = sys.stdin.read()
- args = filter(None, all_values.split('\n'))
- for entry in args:
- try:
- git_old_id, git_new_id, git_ref_path = entry.split()
- except Exception, error:
- logger.error(
- 'Error while splitting all values from stdin. stdin was: %s' %(all_values)
- )
- logger.exception(error)
- sys.exit(1)
-
- git_branch_name = os.path.relpath(git_ref_path, 'refs/heads')
- repo = git.repo.Repo()
-
- job_prefix=''
-
- # choose distribution based on branch
- if git_branch_name == 'master' or git_branch_name.startswith('hotfix/'):
- distribution='production-proposed-updates'
- elif git_branch_name == 'experimental' or git_branch_name.startswith('feature/') or git_branch_name.startswith('poc/') or git_branch_name.startswith('bugfix/') :
- #distribution='dev_'+ git_branch_name.replace("/","_")
- distribution = 'experimental'
- job_prefix='dev_'
- else:
- distribution='pre-staging'
-
- job_name = job_prefix + os.path.splitext(os.path.basename(repo.path))[0]
- urlencode_expansion_dict = {
- 'job_name': job_name
- }
-
- logger.info('')
- logger.info('active branch: %s'% repo.active_branch)
- for branch in repo.branches:
- if git_branch_name == branch.name:
- logger.info('branch is %s' % branch)
- logger.info('branch.name is %s' % branch.name)
- logger.info('git_branch_name is %s' % git_branch_name)
- logger.info('committer_email is %s' % branch.commit.committer.email)
- committer_email = branch.commit.committer.email
- break
- else:
- logger.info('push was a branch delete - not calling jenkins')
- sys.exit(0)
-
- #
- # only trigger builds for some branches
- #
- if git_branch_name == 'develop' or git_branch_name == 'master' or git_branch_name.startswith('hotfix/') or git_branch_name.startswith('release/') or git_branch_name == 'experimental' or git_branch_name.startswith('feature/') or git_branch_name.startswith('poc/') or git_branch_name.startswith('bugfix/'):
-
- url = HudsonUrl()
- data = urllib.urlencode(
- (
-# ('token', 'BUILD'),
- ('GIT_REPO_PATH', repo.path),
- ('GIT_OLD_ID', git_old_id),
- ('GIT_NEW_ID', git_new_id),
- ('GIT_COMMITTER_EMAIL', committer_email),
- ('GIT_UPSTREAM_BRANCH', git_branch_name),
- ('GIT_BRANCH_NAME', git_branch_name),
- ('DISTRIBUTION', distribution),
- ('delay', JOB_DELAY)
- )
- )
- # FIXME: OLD_ID, NEW_ID, COMMITER_EMAIL + UPSTREAM_BRANCH can go away soon
- ret = url.open(JOB_URL %(urlencode_expansion_dict) + '%s' %(data))
-
- if ret.code == 200:
- logger.info('jenkins url called successfully.')
- elif ret.code == 404:
- logger.info('jenkins has no such job configured yet.')
- else:
- logger.info('URL was %s' % JOB_URL %(urlencode_expansion_dict) + '%s' %(data))
- logger.info('jenkins sends the following: %s' %(ret.read())
- )
- sys.exit(1)
-
- sys.exit(0)
-
-# vim: autoindent smartindent tabstop=4 expandtab shiftwidth=4 softtabstop=4 nu enc=utf-8 cinwords=if,elif,else,for,while,try,except,finally,def,class