Beispiel #1
0
    def run(self):
        result = yield FileUpload.run(self)
        if result != SUCCESS:
            self.addCompleteLog('stderr', 'Could not find %s' % self.yaml)
            defer.returnValue(result)

        self.setProperty('conf_path', self.masterdest, 'ReadConfFromYaml')
        raw_conf = open(self.masterdest).read()
        self.addCompleteLog(self.yaml, raw_conf)

        # Make sure all interpolation have correct syntax
        d = defaultdict(str)
        try:
            raw_conf % d
        except ValueError as error:
            if 'unsupported format character' in error.args[0]:
                msg = error.args[0]
                index = int(msg[msg.find('index') + 6:])
                fmt = raw_conf[:index + 1]
                line = fmt.count('\n') + 1
                fmt = fmt[fmt.rfind('%'):]
                self.addCompleteLog(
                    'stderr', 'Error in yaml file:\n  '
                    'Unsupported format character "%s" line '
                    '%d: "%s"' % (fmt[-1], line, fmt))
                defer.returnValue(FAILURE)
            else:
                raise
        except TypeError:
            pass

        # Make sure yaml is properly formatted
        try:
            conf = self.getEveConfig()
        except yaml.YAMLError as error:
            self.addCompleteLog('stderr', 'Error in yaml file:\n%s' % error)
            defer.returnValue(FAILURE)

        # Yaml should define a mapping
        if not isinstance(conf, dict):
            self.addCompleteLog(
                'stderr', 'Error in yaml file:\nShould define a mapping')
            defer.returnValue(FAILURE)

        # Extract Eve API version (call str() to support buggy yaml files)
        if 'version' in conf.keys():
            eve_api_version = str(conf['version'])
        else:
            eve_api_version = '0.1'

        # Expand entries with several branch patterns
        try:
            branches = conf['branches']
        except (TypeError, KeyError):
            self.addCompleteLog(
                'stderr', 'Could not find the branches field'
                'in %s' % self.yaml)
            defer.returnValue(FAILURE)

        new_branches = {}
        for branch_patterns, branch_conf in branches.items():
            for branch_pattern in branch_patterns.split(','):
                new_branches[branch_pattern.strip()] = branch_conf
        conf['branches'] = new_branches
        self.setProperty('start_time', str(time.time()))

        # Use the given stage if any (forced build)
        stage_name = self.getProperty('force_stage')
        branch = self.getProperty('branch')
        if branch is None:
            branch = 'default'
        if stage_name:
            self.logger.debug('Stage forced by user to <{stage}',
                              stage=stage_name)
        else:
            # Else find the stage name from the branch name
            for branch_pattern, branch_conf in conf['branches'].items():
                self.logger.debug('Checking if <{branch}> matches <{pattern}>',
                                  branch=branch,
                                  pattern=branch_pattern)
                if fnmatch(branch, branch_pattern):
                    stage_name = branch_conf['stage']
                    self.logger.debug('<{branch}> matched <{branch_pattern}>',
                                      branch=branch,
                                      branch_pattern=branch_pattern)
                    break
            else:
                self.logger.debug('No branch match. '
                                  'Using default branch config.')
                try:
                    stage_name = conf['branches']['default']['stage']
                except KeyError:
                    self.addCompleteLog(
                        'stderr',
                        'Branch <%s> not covered by yaml file' % branch)
                    self.build.results = SKIPPED
                    defer.returnValue(SKIPPED)

        if 'bootstrap' in conf.get('stages', {}):
            self.addCompleteLog(
                'stderr', 'Error in yaml file:\n'
                'boostrap is reserved and '
                'cannot be used as a stage name')
            defer.returnValue(FAILURE)

        if stage_name not in conf.get('stages', {}):
            self.addCompleteLog(
                'stderr', 'Error in yaml file:\n'
                'the specified stage cannot be '
                'found (%s)' % stage_name)
            defer.returnValue(FAILURE)

        self.build.addStepsAfterCurrentStep([
            GetApiVersion(eve_api_version=eve_api_version),
            steps.TriggerStages([stage_name],
                                waitForFinish=True,
                                haltOnFailure=True)
        ])

        # compute artifact_name property starting from Eve API 0.2
        if version.parse(eve_api_version) >= version.parse('0.2'):
            buildnumber = str(self.getProperty('buildnumber'))
            self.build.addStepsAfterCurrentStep([
                GetCommitShortVersion(branch=branch),
                GetCommitTimestamp(),
                steps.SetArtifactsName(buildnumber=buildnumber,
                                       stage_name=stage_name),
                steps.SetArtifactsPublicURL(),
            ])

        # Read patcher conf and populate related properties
        scheduler = self.getProperty('scheduler')
        if scheduler not in (util.env.FORCE_SCHEDULER_NAME,
                             util.env.PROLONG_SCHEDULER_NAME,
                             util.env.PROMOTE_SCHEDULER_NAME):
            self.build.addStepsAfterCurrentStep([
                steps.PatcherConfig(
                    conf_path=util.env.PATCHER_FILE_PATH,
                    stage=stage_name,
                    name='collect system-level skips for this build',
                    doStepIf=bool(util.env.PATCHER_FILE_PATH),
                    hideStepIf=util.hideStepIfSuccessOrSkipped)
            ])

        defer.returnValue(SUCCESS)