From: Mathias Klette Date: Tue, 30 Apr 2013 14:08:08 +0000 (+0200) Subject: move common variables from debian_build into common_code X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=eec0e7c75e6013264298a9612177190c47763303;p=profitbricks%2Fjenkins-build-scripts.git move common variables from debian_build into common_code --- diff --git a/common_code.py b/common_code.py index aa7cfc3..9e5c26d 100755 --- a/common_code.py +++ b/common_code.py @@ -1,19 +1,33 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import sys import logging +import os +import platform +import sys from logging import Formatter -def logger_init(): - log_format = '%(asctime)s %(name)s[%(process)d] %(levelname)s: %(message)s' - formatter = Formatter(log_format) +def logger_init( + log_name = __file__, + log_format = '%(asctime)s %(name)s[%(process)d] %(levelname)s: %(message)s', + log_level = None, + ): + + if log_level == None: + try: + if ENV['GIT_BRANCH_NAME'] == 'master': + log_level = logging.INFO + else: + log_level = logging.DEBUG + except KeyError as e: + log_level = logging.NOTSET + logging.basicConfig( - format=log_format, - level=logging.INFO - ) - logger = logging.getLogger(__file__) - logger.setLevel(logging.INFO) + format = log_format, + level = log_level, + ) + logger = logging.getLogger(log_name) + logger.setLevel(log_level) return logger def exit_ok(): @@ -24,4 +38,30 @@ def exit_error(): sys.exitfunc = lambda: None sys.exit(1) +ENV = os.environ +ENV.update({'ARGV': ' '.join(sys.argv)}) + +BIN_GIT = '/usr/bin/git' +BIN_RM = '/bin/rm' +BIN_SUDO = '/usr/bin/sudo' +BIN_DPUT = '/usr/bin/dput' +BIN_MAKE_KPKG = '/usr/bin/make-kpkg' + +BUILD_ARCH_MAP = { + 'x86_64': 'amd64', + 'i386': '686', + } +BUILD_ARCH = BUILD_ARCH_MAP.get(platform.machine(), '686') +BUILD_AREA = os.path.abspath(os.path.join(os.curdir,'../build-area')) +BUILD_ID = ENV['BUILD_ID'] +BUILD_JOBNAME = ENV['JOB_NAME'] +BUILD_NUMBER = ENV['BUILD_NUMBER'] +BUILD_START = datetime.datetime.now() +BUILD_URL = ENV['BUILD_URL'] + +CWD = ENV['WORKSPACE'] +GIT_REPO_PATH = ENV['GIT_REPO_PATH'] +GIT_REPO_NAME = os.path.basename(result['GIT_REPO_PATH']) +GIT_BRANCH_NAME = ENV['GIT_BRANCH_NAME'] +GIT_COMMITTER_EMAIL = ENV['GIT_COMMITTER_EMAIL'] diff --git a/debian_build.py b/debian_build.py index 94689d5..f635260 100755 --- a/debian_build.py +++ b/debian_build.py @@ -39,77 +39,50 @@ from multiprocessing import cpu_count from ConfigParser import SafeConfigParser # from common_code -logger = logger_init() - -JOB_URL = 'http://jenkins:80/job/%(flavour)s/buildWithParameters?token=BuildIt&' -JOB_DELAY = '0sec' - - -GIT = '/usr/bin/git' -BIN_RM = '/bin/rm' -BIN_SUDO = '/usr/bin/sudo' -BIN_DPUT = '/usr/bin/dput' -MAKE_KPKG = '/usr/bin/make-kpkg' -DEFAULT_PARALLEL_JOBS = cpu_count() + 1 - -BUILD_ARCH_MAP = { - 'x86_64': 'amd64', - 'i386': '686' -} - -BUILD_ARCH = BUILD_ARCH_MAP.get(platform.machine(), '686') - -CWD = os.environ.get('WORKSPACE') -BUILD_NUMBER = os.environ.get('BUILD_NUMBER') -BUILD_ID = os.environ.get('BUILD_ID') -BUILD_URL = os.environ.get('BUILD_URL') -BUILD_AREA = os.path.abspath(os.path.join(os.curdir,'../build-area')) -BUILD_JOBNAME = os.environ.get('JOB_NAME') -BUILD_START = datetime.datetime.now() - -GIT_REPO_PATH = os.environ.get('GIT_REPO_PATH') -GIT_REPO_NAME = os.path.basename(GIT_REPO_PATH) -GIT_BRANCH_NAME = os.environ.get('GIT_BRANCH_NAME') -GIT_COMMITTER_EMAIL = os.environ.get('GIT_COMMITTER_EMAIL') - -#unused: (except in in-active code) -GIT_TARGET_WORKSPACE = os.path.join( - CWD, - ('%s-build%s' %(BUILD_ID, BUILD_NUMBER)) -) -#unused: (except in in-active code) -GIT_TARGET_DIR = os.path.join( - GIT_TARGET_WORKSPACE, - os.path.basename(GIT_REPO_PATH) -) - -DPUT_OPTIONS_DEFAULT = { - 'fqdn': 'alexandria.pb.local', - 'method': 'scp', - 'login': 'reprepro', - 'incoming': '/srv/profitbricks-repository/incoming/profitbricks', - 'allow_unsigned_uploads': 1, - 'post_upload_command': 'ssh reprepro@alexandria.pb.local /srv/profitbricks-repository/bin/pb_processincoming', - } -DPUT_OPTIONS_DEV = { - 'fqdn': 'alexandria.pb.local', - 'method': 'scp', - 'login': 'reprepro', - 'incoming': '/srv/dev-repository/incoming/', - 'allow_unsigned_uploads': 1, - 'post_upload_command': 'ssh reprepro@alexandria.pb.local /srv/dev-repository/bin/pb_processincoming', - } -DPUT_CF = os.path.join(CWD, '..', 'dput.cf') - -NO_UPLOAD = os.environ.get('NO_UPLOAD') - -AUTO_CHANGELOG_JOBS = ( - 'ri-docs', - 'so-docs', - 'infrastructure-docs', - 'storage-docs', - ) - + logger = logger_init() + +# local constants + DEFAULT_PARALLEL_JOBS = cpu_count() + 1 + JOB_URL = 'http://jenkins:80/job/%(flavour)s/buildWithParameters?token=BuildIt&' + JOB_DELAY = '0sec' + + #unused: (except in in-active code) + GIT_TARGET_WORKSPACE = os.path.join( + CWD, + ('%s-build%s' %(BUILD_ID, BUILD_NUMBER)), + ) + #unused: (except in in-active code) + GIT_TARGET_DIR = os.path.join( + GIT_TARGET_WORKSPACE, + os.path.basename(GIT_REPO_PATH) + ) + + DPUT_OPTIONS_DEFAULT = { + 'fqdn': 'alexandria.pb.local', + 'method': 'scp', + 'login': 'reprepro', + 'incoming': '/srv/profitbricks-repository/incoming/profitbricks', + 'allow_unsigned_uploads': 1, + 'post_upload_command': 'ssh reprepro@alexandria.pb.local /srv/profitbricks-repository/bin/pb_processincoming', + } + DPUT_OPTIONS_DEV = { + 'fqdn': 'alexandria.pb.local', + 'method': 'scp', + 'login': 'reprepro', + 'incoming': '/srv/dev-repository/incoming/', + 'allow_unsigned_uploads': 1, + 'post_upload_command': 'ssh reprepro@alexandria.pb.local /srv/dev-repository/bin/pb_processincoming', + } + DPUT_CF = os.path.join(CWD, '..', 'dput.cf') + + NO_UPLOAD = ENV['NO_UPLOAD'] + + AUTO_CHANGELOG_JOBS = ( + 'ri-docs', + 'so-docs', + 'infrastructure-docs', + 'storage-docs', + ) class HudsonUrl(urllib.FancyURLopener): pass @@ -126,21 +99,27 @@ def read_file(path): def dput_package_upload(changes_path): try: - cmd = [BIN_DPUT, '-c', '%s' %(DPUT_CF), '--no-upload-log', 'profitbricks', '%s' %(changes_path)] + cmd = [BIN_DPUT, + '-c', + '%s' %(DPUT_CF), + '--no-upload-log', + 'profitbricks', + '%s' %(changes_path), + ] + logger.debug( 'Trying to execute: "%s"' %(cmd)) cmd_obj = subprocess.Popen( - cmd, - shell=False, - stdout=sys.stdout, - stderr=subprocess.STDOUT, - close_fds=True, - cwd=os.path.dirname(DPUT_CF) - ) - - ret = cmd_obj.wait() - logger.debug('Exit status: %d' %( ret )) + cmd, + shell=False, + stdout=sys.stdout, + stderr=subprocess.STDOUT, + close_fds=True, + cwd=os.path.dirname(DPUT_CF) + ) + result = cmd_obj.wait() + logger.debug('Exit status: %d' %( result )) - if ret: + if not result: raise Exception(message) except Exception, error: @@ -166,28 +145,28 @@ def remove_git_target_workspace(): try: cmd = [BIN_SUDO, BIN_RM, '-rf', GIT_TARGET_WORKSPACE] cmdobj = subprocess.Popen( - cmd, - shell=False, - cwd='/', - close_fds=True, - stdout=sys.stdout, - stderr=sys.stderr, - env={'':''} - ) + cmd, + shell=False, + cwd='/', + close_fds=True, + stdout=sys.stdout, + stderr=sys.stderr, + env={'':''} + ) logger.debug( - 'Trying to call "%s" to delete "%s"' - %(' '.join(cmd), GIT_TARGET_WORKSPACE) - ) - - ret = cmdobj.wait() - if ret: - _str = ( - 'Cmd "%s" returned non-zero (exitcode: %s).' - %(' '.join(cmd), ret) + 'Trying to call "%s" to delete "%s"' + %(' '.join(cmd), GIT_TARGET_WORKSPACE) ) - logger.debug(_str) - raise Exception(_str) + ret = cmdobj.wait() + + if not ret: + message = ( + 'Cmd "%s" returned non-zero (exitcode: %s).' + %(' '.join(cmd), ret) + ) + logger.debug(message) + raise Exception(message) except Exception, error: logger.exception(error) raise