Add deploy nexus-stage command 33/5133/7
authorThanh Ha <thanh.ha@linuxfoundation.org>
Fri, 9 Jun 2017 03:12:56 +0000 (23:12 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Sun, 11 Jun 2017 17:14:43 +0000 (13:14 -0400)
Use curl to deploy a staging repository to Nexus.

Jira: RELENG-120
Change-Id: Ibd6c25f4318264e5f3e96aeede1c7fe891c473b2
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
docs/commands/deploy.rst
lftools/cli/deploy.py
shell/deploy

index 0f7a2a4..697b12e 100644 (file)
@@ -24,3 +24,8 @@ nexus
 -----
 
 .. program-output:: lftools deploy nexus --help
+
+nexus-stage
+-----
+
+.. program-output:: lftools deploy nexus-stage --help
index 67057aa..5bf3a90 100644 (file)
@@ -85,6 +85,22 @@ def nexus(ctx, nexus_repo_url, deploy_dir):
     sys.exit(status)
 
 
+@click.command(name='nexus-stage')
+@click.argument('nexus-url', envvar='NEXUS_URL')
+@click.argument('staging-profile-id', envvar='STAGING_PROFILE_ID')
+@click.argument('deploy-dir', envvar='DEPLOY_DIR')
+@click.pass_context
+def nexus_stage(ctx, nexus_url, staging_profile_id, deploy_dir):
+    """Deploy a Maven repository to a Nexus staging repository.
+
+    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.add_command(archives)
 deploy.add_command(logs)
 deploy.add_command(nexus)
+deploy.add_command(nexus_stage)
index 1b36316..20dc09b 100755 (executable)
@@ -286,6 +286,76 @@ deploy_nexus() {
     popd
 }
 
+deploy_nexus_stage() {
+    # Deploy Maven artifacts to Nexus staging repo using curl
+    #
+    # Parameters:
+    #     nexus_url:    URL to Nexus server. (Ex: https://nexus.opendaylight.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)
+
+    local nexus_url="$1"
+    local staging_profile_id="$2"
+    local deploy_dir="$3"
+
+    if [ -z "$3" ]; then
+        echo "Missing required arguments."
+        echo "Usage: deploy nexus-stage <nexus_url> <staging_profile_id> <deploy_dir>"
+        exit 1
+    fi
+
+    FILE_XML="$(mktemp)"
+
+    cat > "$FILE_XML" <<EOF
+<promoteRequest>
+  <data>
+    <description>Create staging repo.</description>
+  </data>
+</promoteRequest>
+EOF
+
+    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}')
+
+    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"
+    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
+
+    cat > "$FILE_XML" <<EOF
+<promoteRequest>
+  <data>
+    <stagedRepositoryId>$staging_repo_id</stagedRepositoryId>
+    <description>Close staging repository.</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"
+
+    echo "Completed uploading files to ${staging_repo_id}."
+
+    # cleanup
+    rm "$FILE_XML"
+}
+
 # Only run the script if it is being called directly and not sourced.
 if [[ "${BASH_SOURCE[0]}" == "${0}" ]]
 then