Add deploy nexus command 55/5055/9
authorThanh Ha <thanh.ha@linuxfoundation.org>
Wed, 31 May 2017 21:01:43 +0000 (17:01 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Sun, 11 Jun 2017 17:14:43 +0000 (13:14 -0400)
Use curl to deploy a Maven repository to Nexus.

Jira: RELENG-121
Change-Id: If46e5054a2234eab77e7bd07120d8d0f11dfd293
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
docs/commands/deploy.rst [new file with mode: 0644]
lftools/cli/deploy.py
shell/deploy

diff --git a/docs/commands/deploy.rst b/docs/commands/deploy.rst
new file mode 100644 (file)
index 0000000..0f7a2a4
--- /dev/null
@@ -0,0 +1,26 @@
+******
+Deploy
+******
+
+.. program-output:: lftools deploy --help
+
+Commands
+========
+
+.. contents:: Deploy Commands
+    :local:
+
+archives
+--------
+
+.. program-output:: lftools deploy archives --help
+
+logs
+----
+
+.. program-output:: lftools deploy logs --help
+
+nexus
+-----
+
+.. program-output:: lftools deploy nexus --help
index 318b14f..67057aa 100644 (file)
@@ -67,5 +67,24 @@ def logs(ctx, nexus_url, nexus_path, build_url):
     sys.exit(status)
 
 
+@click.command()
+@click.argument('nexus-repo-url', envvar='NEXUS_REPO_URL')
+@click.argument('deploy-dir', envvar='DEPLOY_DIR')
+@click.pass_context
+def nexus(ctx, nexus_repo_url, deploy_dir):
+    """Deploy a Maven repository to a specified Nexus repository.
+
+    This script takes a local Maven repository and deploys it to a Nexus
+    repository.
+
+    Example Repository:
+
+        https://nexus.example.org/content/repositories/release
+    """
+    status = subprocess.call(['deploy', 'nexus', nexus_repo_url, deploy_dir])
+    sys.exit(status)
+
+
 deploy.add_command(archives)
 deploy.add_command(logs)
+deploy.add_command(nexus)
index fb826ea..1b36316 100755 (executable)
@@ -75,6 +75,16 @@ deploy() {
             deploy_logs "$@"
             exit 0
             ;;
+        nexus )
+            echo "Deploying Maven artifacts..."
+            deploy_nexus "$@"
+            exit 0
+            ;;
+        nexus-stage )
+            echo "Deploying Maven artifacts to staging repo..."
+            deploy_nexus_stage "$@"
+            exit 0
+            ;;
         * )
             echo "Invalid command: $subcommand" 1>&2
             exit 1
@@ -231,6 +241,51 @@ deploy_logs() {
     rm -rf "$tmpdir"
 }
 
+deploy_nexus() {
+    # Deploy Maven artifacts to Nexus using curl
+    #
+    # Parameters:
+    #
+    #     nexus_repo_url: The URL to the Nexus repo.
+    #         (Ex:  https://nexus.example.org/content/repositories/release)
+    #     deploy_dir:     The path to a directory to deploy. (Ex: /tmp/m2repo)
+    #
+    # One purpose of this is so that we can get around the problematic
+    # deploy-at-end configuration with upstream Maven.
+    # https://issues.apache.org/jira/browse/MDEPLOY-193
+
+    local nexus_repo_url="$1"
+    local deploy_dir="$2"
+
+    if [ -z "$2" ]; then
+        echo "Missing required arguments."
+        echo "Usage: deploy nexus <nexus_repo_url> <deploy_dir>"
+        exit 1
+    fi
+
+    pushd "$deploy_dir"
+    file_list=($(find . -type f \
+                ! -name "maven-metadata*" \
+                ! -name _remote.repositories \
+                ! -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
+    popd
+}
+
 # Only run the script if it is being called directly and not sourced.
 if [[ "${BASH_SOURCE[0]}" == "${0}" ]]
 then