From 66d8562377e8a4488b91b477027de4ac8a358415 Mon Sep 17 00:00:00 2001 From: Thanh Ha Date: Wed, 18 Jul 2018 14:05:44 -0400 Subject: [PATCH] Split Nexus staged repo create & close functions Split out these functions so that they can be reused. Issue: RELENG-1081 Change-Id: I5a5c2ee10ed98f7c0c25134298e5a10bb7b23dd1 Signed-off-by: Thanh Ha --- lftools/cli/deploy.py | 32 ++++++++++++++++++++ shell/deploy | 83 ++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 97 insertions(+), 18 deletions(-) diff --git a/lftools/cli/deploy.py b/lftools/cli/deploy.py index aad8eaef..24259221 100644 --- a/lftools/cli/deploy.py +++ b/lftools/cli/deploy.py @@ -188,6 +188,36 @@ def nexus_stage(ctx, nexus_url, staging_profile_id, deploy_dir): sys.exit(status) +@click.command(name='nexus-stage-repo-close') +@click.argument('nexus-url', envvar='NEXUS_URL') +@click.argument('staging-profile-id', envvar='STAGING_PROFILE_ID') +@click.argument('staging-repo-id') +@click.pass_context +def nexus_stage_repo_close(ctx, nexus_url, staging_profile_id, staging_repo_id): + """Close a Nexus staging repo.""" + status = subprocess.call([ + 'deploy', 'nexus-stage-repo-close', + nexus_url, + staging_profile_id, + staging_repo_id + ]) + sys.exit(status) + + +@click.command(name='nexus-stage-repo-create') +@click.argument('nexus-url', envvar='NEXUS_URL') +@click.argument('staging-profile-id', envvar='STAGING_PROFILE_ID') +@click.pass_context +def nexus_stage_repo_create(ctx, nexus_url, staging_profile_id): + """Create a Nexus staging repo.""" + status = subprocess.call([ + 'deploy', 'nexus-stage-repo-create', + nexus_url, + staging_profile_id + ]) + sys.exit(status) + + @click.command(name='nexus-zip') @click.argument('nexus-url', envvar='NEXUS_URL') @click.argument('nexus-repo', envvar='NEXUS_REPO') @@ -212,4 +242,6 @@ deploy.add_command(logs) deploy.add_command(maven_file) deploy.add_command(nexus) deploy.add_command(nexus_stage) +deploy.add_command(nexus_stage_repo_close) +deploy.add_command(nexus_stage_repo_create) deploy.add_command(nexus_zip) diff --git a/shell/deploy b/shell/deploy index b72ae07e..2cfce192 100755 --- a/shell/deploy +++ b/shell/deploy @@ -96,6 +96,14 @@ deploy() { deploy_nexus_stage "$@" exit 0 ;; + nexus-stage-repo-close ) + nexus_stage_repo_close "$@" + exit 0 + ;; + nexus-stage-repo-create ) + nexus_stage_repo_create "$@" + exit 0 + ;; nexus-zip ) echo "Deploying nexus-zip..." deploy_nexus_zip "$@" @@ -602,12 +610,34 @@ deploy_nexus_stage() { exit 1 fi - FILE_XML="$(mktemp)" + local staging_repo_id + staging_repo_id=$(nexus_stage_repo_create "${nexus_url}" "${staging_profile_id}") + echo "Staging repository $staging_repo_id created." + deploy_nexus "${nexus_url}/service/local/staging/deployByRepositoryId/${staging_repo_id}" "${deploy_dir}" + + nexus_stage_repo_close "${nexus_url}" "${staging_profile_id}" "${staging_repo_id}" + echo "Completed uploading files to ${staging_repo_id}." +} + +nexus_stage_repo_close() { + # Closes a 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. + # staging_repo_id: The ID of the repo to close. + local nexus_url="${1%/}" + local staging_profile_id="$2" + local staging_repo_id="$3" + + FILE_XML="$(mktemp)" cat > "$FILE_XML" < - Create staging repo. + $staging_repo_id + Close staging repository. EOF @@ -616,8 +646,9 @@ EOF local status resp=$(curl -s -w " %{http_code}" --netrc -X POST -d "@$FILE_XML" \ -H "Content-Type:application/xml" \ - "${nexus_url}/service/local/staging/profiles/${staging_profile_id}/start") + "${nexus_url}/service/local/staging/profiles/${staging_profile_id}/finish") status=$(echo "$resp" | awk 'END {print $NF}') + rm -f "$FILE_XML" # Cleanup if echo "$resp" | grep -q nexus-error; then local msg @@ -628,32 +659,48 @@ EOF echo "ERROR: Failed with status code $status" exit "$status" fi +} - local staging_repo_id - staging_repo_id=$(sed -n -e 's/.*\(.*\)<\/stagedRepositoryId>.*/\1/p' <<< $resp) - echo "Staging repository $staging_repo_id created." - - deploy_nexus "${nexus_url}/service/local/staging/deployByRepositoryId/${staging_repo_id}" "${deploy_dir}" - - # Close the repo +nexus_stage_repo_create() { + # Create a 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. + # + # Returns: staging_repo_id + local nexus_url="${1%/}" + local staging_profile_id="$2" + FILE_XML="$(mktemp)" cat > "$FILE_XML" < - $staging_repo_id - Close staging repository. + Create staging repo. EOF - curl -s --netrc -X POST \ - -H "Content-Type:application/xml" -d "@$FILE_XML" \ - "${nexus_url}/service/local/staging/profiles/${staging_profile_id}/finish" + local resp + local status + resp=$(curl -s -w " %{http_code}" --netrc -X POST -d "@$FILE_XML" \ + -H "Content-Type:application/xml" \ + "${nexus_url}/service/local/staging/profiles/${staging_profile_id}/start") + status=$(echo "$resp" | awk 'END {print $NF}') + rm -f "$FILE_XML" # Cleanup - echo "Completed uploading files to ${staging_repo_id}." + if echo "$resp" | grep -q nexus-error; then + local msg + msg=$(sed -n -e 's/.*\(.*\)<\/msg>.*/\1/p' <<< $resp) + echo "ERROR: $msg" + exit "$status" + elif [ "$status" != "201" ]; then + echo "ERROR: Failed with status code $status" + exit "$status" + fi - # cleanup - rm "$FILE_XML" + echo "$(sed -n -e 's/.*\(.*\)<\/stagedRepositoryId>.*/\1/p' <<< $resp)" } deploy_nexus_zip() { -- 2.16.6