From: Bengt Thuree Date: Tue, 20 Nov 2018 13:02:20 +0000 (+1100) Subject: Refactor deploy_nexus_stage to Python X-Git-Tag: v0.19.0~7 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=d2aca2e11395c596080e6a63ad59acb15abfc61d;p=releng%2Flftools.git Refactor deploy_nexus_stage to Python This is part of the work to convert the existing SHELL scripts to Python scripts. * deploy nexus stage Issue: RELENG-1380 Change-Id: I63e5546fb7455253d14effd638e65d45c36487cb Signed-off-by: Bengt Thuree --- diff --git a/lftools/cli/deploy.py b/lftools/cli/deploy.py index e9b93a45..59625b1e 100644 --- a/lftools/cli/deploy.py +++ b/lftools/cli/deploy.py @@ -259,8 +259,9 @@ def nexus_stage(ctx, nexus_url, staging_profile_id, deploy_dir): This script takes a local Maven repository and deploys it to a Nexus staging repository as defined by the staging-profile-id. """ - status = subprocess.call(['deploy', 'nexus-stage', nexus_url, staging_profile_id, deploy_dir]) - sys.exit(status) + deploy_sys.deploy_nexus_stage(nexus_url, + staging_profile_id, + deploy_dir) @click.command(name='nexus-stage-repo-close') diff --git a/lftools/deploy.py b/lftools/deploy.py index 59404b93..0861d155 100644 --- a/lftools/deploy.py +++ b/lftools/deploy.py @@ -535,3 +535,36 @@ def deploy_nexus(nexus_repo_url, deploy_dir, snapshot=False): pool.join() os.chdir(previous_dir) + + +def deploy_nexus_stage(nexus_url, staging_profile_id, deploy_dir): + """Deploy Maven artifacts to Nexus staging repo. + + Parameters: + nexus_url: URL to Nexus server. (Ex: https://nexus.example.org) + staging_profile_id: The staging profile id as defined in Nexus for the + staging repo. + deploy_dir: The directory to deploy. (Ex: /tmp/m2repo) + + Sample: + lftools deploy nexus-stage http://192.168.1.26:8081/nexus 4e6f95cd2344 /tmp/slask + Deploying Maven artifacts to staging repo... + Staging repository aaf-1005 created. + /tmp/slask ~/LF/work/lftools-dev/lftools/shell + Uploading fstab + Uploading passwd + ~/LF/work/lftools-dev/lftools/shell + Completed uploading files to aaf-1005. + """ + staging_repo_id = nexus_stage_repo_create(nexus_url, staging_profile_id) + log.info("Staging repository {} created.".format(staging_repo_id)) + + deploy_nexus_url = '{0}/service/local/staging/deployByRepositoryId/{1}'.format( + _format_url(nexus_url), + staging_repo_id) + + log.debug("Nexus URL = {}".format(_format_url(deploy_nexus_url))) + deploy_nexus(deploy_nexus_url, deploy_dir) + + nexus_stage_repo_close(nexus_url, staging_profile_id, staging_repo_id) + log.info("Completed uploading files to {}".format(staging_repo_id)) diff --git a/releasenotes/notes/deploy_nexus_stage-e5f6f3e068f88ca4.yaml b/releasenotes/notes/deploy_nexus_stage-e5f6f3e068f88ca4.yaml new file mode 100644 index 00000000..ce03b525 --- /dev/null +++ b/releasenotes/notes/deploy_nexus_stage-e5f6f3e068f88ca4.yaml @@ -0,0 +1,14 @@ +--- +features: + - | + Refactored deploy_nexus_stage function + from shell/deploy to pure Python to be more portable with Windows systems. + Also added a number of unit tests to cover all executable branches of the + code. + +deprecations: + - | + shell/deploy script's deploy_nexus_stage + function is now deprecated and will be removed in a future release. + + diff --git a/tests/test_deploy.py b/tests/test_deploy.py index a0f2e34a..5bef101e 100644 --- a/tests/test_deploy.py +++ b/tests/test_deploy.py @@ -599,3 +599,38 @@ def test_deploy_nexus_nosnapshot(datafiles, responses): responses.add(responses.POST, success_upload_url, status=201) deploy_sys.deploy_nexus(nexus_url, deploy_dir) + + +@pytest.mark.datafiles( + os.path.join(FIXTURE_DIR, 'deploy'), + ) +def test_nexus_deploy_stage(datafiles, responses): + """Test nexus_deploy_stage.""" + url='http://valid.deploy.stage' + url_repo = 'service/local/staging/profiles' + staging_profile_id='93fb68073c18' + repo_id='test1-1030' + + #Setup for nexus_stage_repo_create + xml_created = "{}".format(repo_id) + responses.add(responses.POST, '{}/{}/{}/start'.format(url, url_repo, staging_profile_id), + body=xml_created, status=201) + + #Setup for deploy_nexus with no snapshot + os.chdir(str(datafiles)) + nexus_deploy_url = '{}/service/local/staging/deployByRepositoryId/{}'.format(url, repo_id) + deploy_dir = 'm2repo' + test_files = ['4.0.3-SNAPSHOT/odlparent-lite-4.0.3-20181120.113136-1.pom', + '4.0.3-SNAPSHOT/odlparent-lite-4.0.3-20181120.113136-1.pom.sha1', + '4.0.3-SNAPSHOT/odlparent-lite-4.0.3-20181120.113136-1.pom.md5'] + for file in test_files: + success_upload_url = '{}/{}'.format(nexus_deploy_url, file) + responses.add(responses.POST, success_upload_url, + status=201) + + #Setup for nexus_stage_repo_close + responses.add(responses.POST, '{}/{}/{}/finish'.format(url, url_repo, staging_profile_id), + body=None, status=201) + + #Execute test, should not return anything for successful run. + deploy_sys.deploy_nexus_stage (url, staging_profile_id, deploy_dir)