From ad6a5878a8f132e9e8d6a6bbc8517cfd7d06e568 Mon Sep 17 00:00:00 2001 From: Bengt Thuree Date: Fri, 16 Nov 2018 14:52:30 +1100 Subject: [PATCH] Refactor upload_maven_file_to_nexus to Python Issue: RELENG-1373 Change-Id: Ic896b85ee67a5138814b991d433fc27df9d5be54 Signed-off-by: Bengt Thuree --- lftools/cli/deploy.py | 22 +++---- lftools/deploy.py | 55 +++++++++++++++- tests/fixtures/deploy/zip-test-files/test.tar.xz | Bin 0 -> 580 bytes tests/test_deploy.py | 78 +++++++++++++++++++++++ 4 files changed, 140 insertions(+), 15 deletions(-) create mode 100644 tests/fixtures/deploy/zip-test-files/test.tar.xz diff --git a/lftools/cli/deploy.py b/lftools/cli/deploy.py index fa35bdee..4da3ac37 100644 --- a/lftools/cli/deploy.py +++ b/lftools/cli/deploy.py @@ -109,18 +109,16 @@ def file(ctx, the usual Maven pom.xml information so that it conforms to Maven 2 repo specs. """ - status = subprocess.call([ - 'deploy', 'file', - nexus_url, - nexus_repo_id, - group_id, - artifact_id, - version, - packaging, - file, - classifier - ]) - sys.exit(status) + try: + deploy_sys.upload_maven_file_to_nexus( + nexus_url, nexus_repo_id, + group_id, artifact_id, version, + packaging, file, classifier) + except HTTPError as e: + log.error(str(e)) + sys.exit(1) + + log.info('Upload maven file to nexus completed.') @click.command() diff --git a/lftools/deploy.py b/lftools/deploy.py index a83f6f63..5032ccd9 100644 --- a/lftools/deploy.py +++ b/lftools/deploy.py @@ -86,7 +86,7 @@ def _request_post(url, data, headers): return resp -def _request_post_file(url, file_to_upload): +def _request_post_file(url, file_to_upload, parameters=None): """Execute a request post, return the resp.""" resp = {} try: @@ -97,7 +97,10 @@ def _request_post_file(url, file_to_upload): files = {'file': upload_file} try: - resp = requests.post(url, files=files) + if parameters: + resp = requests.post(url, data=parameters, files=files) + else: + resp = requests.post(url, files=files) except requests.exceptions.MissingSchema: raise requests.HTTPError("Not valid URL: {}".format(url)) except requests.exceptions.ConnectionError: @@ -115,7 +118,7 @@ def _request_post_file(url, file_to_upload): raise requests.HTTPError("Failed to upload to Nexus with status code: {}.\n{}\n{}".format( resp.status_code, resp.text, zipfile.ZipFile(file_to_upload).infolist())) else: - raise requests.HTTPError("Failed to upload to Nexus with status code: {}.\n{}\n{}\{}".format( + raise requests.HTTPError("Failed to upload to Nexus with status code: {}.\n{}\n{}".format( resp.status_code, resp.text, file_to_upload)) return resp @@ -439,3 +442,49 @@ def nexus_stage_repo_close(nexus_url, staging_profile_id, staging_repo_id): if not resp.status_code == 201: _log_error_and_exit("Failed with status code {}".format(resp.status_code), resp.text) + + +def upload_maven_file_to_nexus(nexus_url, nexus_repo_id, + group_id, artifact_id, version, + packaging, file, classifier=None): + """Upload file to Nexus as a Maven artifact. + + This function will upload an artifact to Nexus while providing all of + the usual Maven pom.xml information so that it conforms to Maven 2 repo + specs. + + Parameters: + nexus_url: The URL to the Nexus repo. + (Ex: https://nexus.example.org) + nexus_repo_id: Repo ID of repo to push artifact to. + group_id: Maven style Group ID to upload artifact as. + artifact_id: Maven style Artifact ID to upload artifact as. + version: Maven style Version to upload artifact as. + packaging: Packaging type to upload as (Eg. tar.xz) + file: File to upload. + classifier: Maven classifier. (optional) + + Sample: + lftools deploy nexus \ + http://192.168.1.26:8081/nexus/content/repositories/releases \ + tests/fixtures/deploy/zip-test-files + """ + url = '{}/service/local/artifact/maven/content'.format(_format_url(nexus_url)) + + log.info('Uploading URL: {}'.format(url)) + params = {} + params.update({'r': (None, '{}'.format(nexus_repo_id))}) + params.update({'g': (None, '{}'.format(group_id))}) + params.update({'a': (None, '{}'.format(artifact_id))}) + params.update({'v': (None, '{}'.format(version))}) + params.update({'p': (None, '{}'.format(packaging))}) + if classifier: + params.update({'c': (None, '{}'.format(classifier))}) + + log.debug('Maven Parameters: {}'.format(params)) + + resp = _request_post_file(url, file, params) + + if re.search('nexus-error', resp.text): + error_msg = _get_node_from_xml(resp.text, 'msg') + raise requests.HTTPError("Nexus Error: {}".format(error_msg)) diff --git a/tests/fixtures/deploy/zip-test-files/test.tar.xz b/tests/fixtures/deploy/zip-test-files/test.tar.xz new file mode 100644 index 0000000000000000000000000000000000000000..7167d114d5bdeabdcc01f3d1a732d5ebfd39305c GIT binary patch literal 580 zcmV-K0=xbFH+ooF000E$*0e?f03iVu0001VFXf})C;tKgT>uvg#)pzxJExDwB@Va` zw8^FP>SB`Z-iROdSwm|i@2HM?KM&GVgk%8XGmr4kDus)#+yzVw&z|6V#_^d45zT78 zm&!LXeHMEe!1|5+025Ac?YrSQJLHR){%+`bPHJzfccE%=ir#}7XN&G=Goo0!atp@x zo9Rb6P4pONsix;+_VW4WT@!blG`-6=6`Qh6>F`V~=)s`LdKj|!e{hR1C7rTNf=Pqm zG_aS-ttSWDDr?#KJ@%xR1nnK_N8A=~2VzFL;YP+F1v{kp(+P%8%S8QfIIDSRkMjs1 zEO-ne$;7i_W%htg3tEt#*lz0^JB0iNwjDqqKm#7R%z<#=%RcTw=mW6gH3Aq2}ZkW<(?n(`P+lP4eA93U7zZWq?8qK$x&RraqFu+ z^^NBZJKkp1#1 + * + Something went wrong. + + """ + test_url='http://something.went.wrong:8081' + responses.add(responses.POST, '{}/{}'.format(test_url, common_urlpart), body=xml_other_error, status=405) + with pytest.raises(requests.HTTPError) as excinfo: + resp = deploy_sys.upload_maven_file_to_nexus(test_url, nexus_repo_id, group_id, artifact_id, version, packaging, zip_file) + assert 'Something went wrong' in str(excinfo.value) -- 2.16.6