Exemplo n.º 1
0
    def deployByRepositoryId(self, stagedRepositoryId, artifact, userAgent=None):
        '''
            Handles the upload of file.
        '''

        url = '{}/deployByRepositoryId/{}/{}'.format(self._restApiUrl, stagedRepositoryId, artifact['relpath'].replace('\\', '/'))
        log.info('DEPLOY local file {} in staging repository {} to {}'.format(artifact['filename'], stagedRepositoryId, url))
        form = MultiPartForm()
        f = open(artifact['path'], 'rb')
        form.add_file_handle('file', artifact['filename'], fileHandle=f)
        fileUploadHandle = form.file()
        f.close()

        headers = {}
        headers['Content-Type'] = form.get_content_type()
        if userAgent:
            headers['User-Agent'] = userAgent

        def uploadRequest():
            return self._formbased_post_file(url, headers, fileUploadHandle, returnOnlyHttpCode=True)

        def uploadStatus(status):
            if status >= 200 and status < 400:
                log.info('\tthe deploy request was received by Nexus http {}'.format(status))
                return 0

            if status == 400:
                log.info('\tthe deploy request failed. Nexus returned http {}'.format(status))
                return 2

            if status > 400:
                log.info('\tthe deploy request failed. Nexus returned http {}'.format(status))
                return 1

            log.error('\tcannot check if upload is correctly executed on Nexus')
            return 2

        def uploadRetryLog(tryNumber):
            log.info('\ttry {} to deploy on nexus'.format(tryNumber))

        if self._requestManyTimes(uploadRequest, uploadStatus, nbTry=tuning.DEPLOY_FILE_REQ_ATTEMPTS, waitSecond=tuning.DEPLOY_FILE_DELAY_BETWEEN_REQ_ATTEMPTS, cbLogMessage=uploadRetryLog):
            def checkStatus():
                return self.repository_content(stagedRepositoryId, artifact['relpath'].replace(artifact['filename'], ''))

            def isStatusValid(content_items):
                if content_items is None:
                    log.error('\tcannot check if file is available on Nexus')
                    return 1

                for content_item in content_items:
                    if 'text' in content_item and content_item['text'] == artifact['filename']:
                        log.info('\tthe file is available on Nexus')
                        return 0

                log.warning('\tthe file is not fully uploaded yet... Nexus still working')
                return 2

            if self._requestManyTimes(checkStatus, isStatusValid, cbLogMessage=lambda tryNumber: log.info('\ttry {} to check if file is uploaded on nexus'.format(tryNumber))):
                log.info('file {} deployed in staging repository {}'.format(artifact['filename'], stagedRepositoryId))
                fileUploadHandle.close()
                return True

#         if response:
#             log.error(response)

        log.error('fail to deploy file {} in staging repository {}'.format(artifact['filename'], stagedRepositoryId))
        fileUploadHandle.close()
        return False