Add cURL command to push an artifact to Nexus 44/11844/5
authorThanh Ha <thanh.ha@linuxfoundation.org>
Wed, 18 Jul 2018 18:30:36 +0000 (14:30 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Fri, 20 Jul 2018 01:12:04 +0000 (21:12 -0400)
Issue: RELENG-1081
Change-Id: If36810924d2d0f91e04bae469067495a25c0b2dc
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
lftools/cli/deploy.py
shell/deploy

index 2425922..05e1a60 100644 (file)
@@ -48,6 +48,42 @@ def archives(ctx, nexus_url, nexus_path, workspace, pattern):
     sys.exit(status)
 
 
+@click.command()
+@click.argument('nexus-url', envvar='NEXUS_URL')
+@click.argument('nexus-repo-id')
+@click.argument('group-id')
+@click.argument('artifact-id')
+@click.argument('version')
+@click.argument('packaging')
+@click.argument('file')
+@click.pass_context
+def file(ctx,
+         nexus_url,
+         nexus_repo_id,
+         group_id,
+         artifact_id,
+         version,
+         packaging,
+         file):
+    """Upload file to Nexus as a Maven artifact using cURL.
+
+    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.
+    """
+    status = subprocess.call([
+        'deploy', 'file',
+        nexus_url,
+        nexus_repo_id,
+        group_id,
+        artifact_id,
+        version,
+        packaging,
+        file
+    ])
+    sys.exit(status)
+
+
 @click.command()
 @click.argument('nexus-url', envvar='NEXUS_URL')
 @click.argument('nexus-path', envvar='NEXUS_PATH')
@@ -238,6 +274,7 @@ def nexus_zip(ctx, nexus_url, nexus_repo, nexus_path, deploy_zip):
 
 
 deploy.add_command(archives)
+deploy.add_command(file)
 deploy.add_command(logs)
 deploy.add_command(maven_file)
 deploy.add_command(nexus)
index 2cfce19..4893f46 100755 (executable)
@@ -76,6 +76,11 @@ deploy() {
             deploy_maven_file "$@"
             exit 0
             ;;
+        file )
+            echo "Deploying file..."
+            upload_maven_file_to_nexus "$@"
+            exit 0
+            ;;
         files )
             echo "Deploying files..."
             echo "ERROR: Unimplemented."
@@ -745,6 +750,54 @@ deploy_nexus_zip() {
     fi
 }
 
+upload_maven_file_to_nexus() {
+    # Upload file to Nexus as a Maven artifact using cURL.
+    #
+    # 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.
+
+    local nexus_url="${1%/}"
+    local nexus_repo_id="$2"
+    local group_id="$3"
+    local artifact_id="$4"
+    local version="$5"
+    local packaging="$6"
+    local file="$7"
+
+    local resp
+    local status
+    resp=$(curl -s -w " %{http_code}" --netrc -X POST \
+        -F "r=$nexus_repo_id" \
+        -F "g=$group_id" \
+        -F "a=$artifact_id" \
+        -F "v=$version" \
+        -F "p=$packaging" \
+        -F "file=@$file" \
+        "${nexus_url}/service/local/artifact/maven/content")
+    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"
+    elif [ "$status" != "201" ]; then
+        echo "ERROR: Failed with status code $status"
+        exit "$status"
+    fi
+}
+
 upload_to_nexus() {
     # Helper function to push a file to Nexus via cURL
     #