Deploy to Nexus using parallel 21/5321/1
authorThanh Ha <thanh.ha@linuxfoundation.org>
Tue, 27 Jun 2017 03:38:29 +0000 (23:38 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Tue, 27 Jun 2017 03:43:15 +0000 (23:43 -0400)
Speed up the script by using GNU parallel to push when it is available.

Change-Id: I8ddff8b8cf50cd821aae21958ab8b5c7ecc33174
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
shell/deploy

index b25d439..e330a95 100755 (executable)
@@ -508,19 +508,14 @@ deploy_nexus() {
                 ! -name resolver-status.properties \
                 | cut -c 3-))
 
-    for file in "${file_list[@]}"; do
-        echo "Uploading ${file}"
-
-        local resp
-        resp=$(curl -s -w "%{http_code}" --netrc --upload-file "$file" "$nexus_repo_url/$file")
-        status=$(echo "$resp" | awk 'END {print $NF}')
-
-        if [ "$status" != "201" ]; then
-            echo "ERROR: Failed to upload to Nexus. Aborting..."
-            echo "$resp"
-            exit "$status"
-        fi
-    done
+    if hash parallel 2>/dev/null; then
+        export -f upload_to_nexus
+        parallel --jobs 200% --halt now,fail=1 "upload_to_nexus $nexus_repo_url {}" ::: ${file_list[*]}
+    else
+        for file in "${file_list[@]}"; do
+            upload_to_nexus "$nexus_repo_url" "$file"
+        done
+    fi
     popd
 }
 
@@ -631,6 +626,30 @@ deploy_nexus_zip() {
         "${nexus_url}/service/local/repositories/${nexus_repo}/content-compressed/${nexus_path}"
 }
 
+upload_to_nexus() {
+    # Helper function to push a file to Nexus via cURL
+    #
+    # Parameters:
+    #
+    #     nexus_repo_url: The URL to the Nexus repo.
+    #         (Ex:  https://nexus.example.org/content/repositories/release)
+    #     file:     File that is to be pushed to Nexus.
+    local nexus_repo_url="$1"
+    local file="$2"
+
+    echo "Uploading ${file}"
+
+    local resp
+    resp=$(curl -s -w "%{http_code}" --netrc --upload-file "$file" "$nexus_repo_url/$file")
+    status=$(echo "$resp" | awk 'END {print $NF}')
+
+    if [ "$status" != "201" ]; then
+        echo "ERROR: Failed to upload to Nexus. Aborting..."
+        echo "$resp"
+        exit "$status"
+    fi
+}
+
 # Only run the script if it is being called directly and not sourced.
 if [[ "${BASH_SOURCE[0]}" == "${0}" ]]
 then