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

        log.info('UPLOAD artifact {}.{} in staging repository {}'.format(artifact['group'], artifact['artifact'], stagedRepositoryId))
        form = MultiPartForm()
        form.add_field('r', stagedRepositoryId)
        form.add_field('g', artifact['group'])
        form.add_field('a', artifact['artifact'])
        form.add_field('v', artifact['version'])
        form.add_field('p', artifact['package'])
        form.add_field('c', artifact['classifier'])
        form.add_field('e', artifact['extension'])
        form.add_field('desc', description)

        form.add_file('file', artifact['filename'], fileHandle=open(artifact['path'], 'rb'))
        body = str(form)

        url = '{}/{}'.format(self._restApiUrl, 'upload')

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

        response = self._request(url, headers, byteRequest=body)
        if response:
            oresponse = json.loads(response)
            if 'repositoryId' in oresponse:
                repoId = oresponse['repositoryId']
                if repoId:
                    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('artifact {}.{} uploaded in staging repository {}'.format(artifact['group'], artifact['artifact'], repoId))
                        return repoId

        log.error('fail to upload artifact {}.{} in staging repository {}'.format(artifact['group'], artifact['artifact'], stagedRepositoryId))
        return None
Ejemplo n.º 2
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
Ejemplo n.º 3
0
    def bundle_upload(self, artifactFolder, relativePath, userAgent=None):
        '''
            Handles the upload of bundle.
        '''

        log.info('UPLOAD BUNDLE in new staging repository')
        log.info('create bundle.zip')
        zf = zipfile.ZipFile(os.path.join(self._tmpFolder, 'bundle.zip'), mode='w')
        filesTocheck = []
        relativePath = relativePath.replace('\\', '/')
        relativePath = relativePath[1:] if relativePath.startswith('/') else relativePath
        relativePath = relativePath[:-1] if relativePath.endswith('/') else relativePath
        try:
            for root, dirs, files in os.walk(artifactFolder):
                for name in files:
                    filePath = os.path.join(root, name)
                    filesTocheck.append(name)
                    log.info('adding {}'.format(filePath))
                    zf.write(filePath, name)
        finally:
            log.info('closing bundle.zip')
            zf.close()

        form = MultiPartForm()
        form.add_file('file', 'bundle.zip', fileHandle=open(os.path.join(self._tmpFolder, 'bundle.zip'), 'rb'))
        body = str(form)

        url = '{}/{}'.format(self._restApiUrl, 'bundle_upload')

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

        response = self._request(url, headers, byteRequest=body)
        if response:
            oresponse = json.loads(response)
            if oresponse and 'repositoryUris' in oresponse and isinstance(oresponse['repositoryUris'], list):
                m = re.search(self._reJsonRespRepoId, oresponse['repositoryUris'][0])
                if m:
                    repoId = m.group('repoId')
                    if repoId:
                        def checkStatus():
                            content_items = self.repository_content(repoId, relativePath)
                            if content_items is None:
                                return None

                            for filename in filesTocheck:
                                for content_item in content_items:
                                    if 'text' in content_item and content_item['text'] == filename:
                                        break
                                else:
                                    return 404

                            return 200

                        def isStatusValid(status):
                            if status == 200:
                                log.info('\tthe bundle is available on Nexus')
                                return 0
                            if status is None:
                                log.error('\tcannot check if bundle is available on Nexus')
                                return 1
                            log.warning('\tthe bundle is not fully uploaded yet... Nexus still working resource respond {}'.format(status))
                            return 2

                        if self._requestManyTimes(checkStatus, isStatusValid, cbLogMessage=lambda tryNumber: log.info('\ttry {} to check if bundle is uploaded on nexus'.format(tryNumber))):
                            log.info('bundle uploaded in staging repository {}'.format(repoId))
                            return m.group('repoId')

        log.error('fail to upload bundle in new staging repository')
        return None