Split Nexus staged repo create & close functions 43/11843/8
authorThanh Ha <thanh.ha@linuxfoundation.org>
Wed, 18 Jul 2018 18:05:44 +0000 (14:05 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Fri, 20 Jul 2018 01:12:04 +0000 (21:12 -0400)
Split out these functions so that they can be reused.

Issue: RELENG-1081
Change-Id: I5a5c2ee10ed98f7c0c25134298e5a10bb7b23dd1
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
lftools/cli/deploy.py
shell/deploy

index aad8eae..2425922 100644 (file)
@@ -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)
index b72ae07..2cfce19 100755 (executable)
@@ -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" <<EOF
 <promoteRequest>
   <data>
-    <description>Create staging repo.</description>
+    <stagedRepositoryId>$staging_repo_id</stagedRepositoryId>
+    <description>Close staging repository.</description>
   </data>
 </promoteRequest>
 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>\(.*\)<\/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" <<EOF
 <promoteRequest>
   <data>
-    <stagedRepositoryId>$staging_repo_id</stagedRepositoryId>
-    <description>Close staging repository.</description>
+    <description>Create staging repo.</description>
   </data>
 </promoteRequest>
 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>\(.*\)<\/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>\(.*\)<\/stagedRepositoryId>.*/\1/p' <<< $resp)"
 }
 
 deploy_nexus_zip() {