Add deploy maven-file command to lftools 87/5187/35
authorAnil Belur <abelur@linuxfoundation.org>
Tue, 13 Jun 2017 00:24:27 +0000 (10:24 +1000)
committerAnil Belur <abelur@linuxfoundation.org>
Tue, 20 Jun 2017 21:37:14 +0000 (07:37 +1000)
The 'deploy maven-file' command uses mvn deploy-file goal
for deploying files to a nexus repository.

Credentials can be provided by settings.xml file in the following way
(order or precedence listed):

    1) User provides the settings file explicitly via -s parameter
    2) User provides the settings file via SETTINGS_FILE envvar
    3) User does not provide the file (default Maven location used)

Jira: releng-160
Change-Id: I0a3af408f70d7d2551a187ec3561b5e609e733d5
Signed-off-by: Anil Belur <abelur@linuxfoundation.org>
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
docs/commands/deploy.rst
lftools/cli/deploy.py
shell/deploy

index 697b12e..1562035 100644 (file)
@@ -20,6 +20,11 @@ logs
 
 .. program-output:: lftools deploy logs --help
 
+maven-file
+----------
+
+.. program-output:: lftools deploy maven-file --help
+
 nexus
 -----
 
index 5bf3a90..2606b87 100644 (file)
@@ -67,6 +67,80 @@ def logs(ctx, nexus_url, nexus_path, build_url):
     sys.exit(status)
 
 
+@click.command(name='maven-file')
+@click.argument('nexus-url', envvar='NEXUS_URL')
+@click.argument('repo-id', envvar='REPO_ID')
+@click.argument('file-name', envvar='FILE_NAME')
+# Maven Config
+@click.option('-b', '--maven-bin', envvar='MAVEN_BIN',
+              help='Path of maven binary.')
+@click.option('-gs', '--global-settings', envvar='GLOBAL_SETTINGS_FILE',
+              help='Global settings file.')
+@click.option('-s', '--settings', envvar='SETTINGS_FILE',
+              help='Settings file.')
+# Maven Artifact GAV
+@click.option('-a', '--artifact-id',
+              help='Maven Artifact ID.')
+@click.option('-c', '--classifier',
+              help='File classifier.')
+@click.option('-f', '--pom-file',
+              help='Pom file to extract GAV information from.')
+@click.option('-g', '--group-id',
+              help='Maven Group ID')
+@click.option('-v', '--version',
+              help='Maven artifact version.')
+@click.pass_context
+def maven_file(
+    # Maven Config
+    ctx, nexus_url, repo_id, file_name,
+    maven_bin, global_settings, settings,
+    # Maven GAV
+    artifact_id, group_id, classifier, version,
+        pom_file):
+    """Deploy a file to a Nexus maven2 repository.
+
+    As this script uses mvn to deploy. The server configuration should be
+    configured in your local settings.xml. By default the script uses the
+    mvn default "~/.m2/settings.xml" for the configuration but this can be
+    overrided in the following order:
+
+        \b
+        1. Passed through CLI option "-s" ("-gs" for global-settings)
+        2. Environment variable "$SETTINGS_FILE" ("$GLOBAL_SETTINGS_FILE" for global-settings)
+        3. Maven default "~/.m2/settings.xml".
+
+    If pom-file is passed in via the "-f" option then the Maven GAV parameters
+    are not necessary. pom-file setting overrides the Maven GAV parameters.
+    """
+    params = ['deploy', 'maven-file']
+
+    # Maven Configuration
+    if maven_bin:
+        params.extend(["-b", maven_bin])
+    if global_settings:
+        params.extend(["-l", global_settings])
+    if settings:
+        params.extend(["-s", settings])
+
+    # Maven Artifact GAV
+    if artifact_id:
+        params.extend(["-a", artifact_id])
+    if classifier:
+        params.extend(["-c", classifier])
+    if group_id:
+        params.extend(["-g", group_id])
+    if pom_file:
+        params.extend(["-f", pom_file])
+    if version:
+        params.extend(["-v", version])
+
+    # Set required variables last as getopts get's processed first.
+    params.extend([nexus_url, repo_id, file_name])
+
+    status = subprocess.call(params)
+    sys.exit(status)
+
+
 @click.command()
 @click.argument('nexus-repo-url', envvar='NEXUS_REPO_URL')
 @click.argument('deploy-dir', envvar='DEPLOY_DIR')
@@ -102,5 +176,6 @@ def nexus_stage(ctx, nexus_url, staging_profile_id, deploy_dir):
 
 deploy.add_command(archives)
 deploy.add_command(logs)
+deploy.add_command(maven_file)
 deploy.add_command(nexus)
 deploy.add_command(nexus_stage)
index ac987e0..67316cd 100755 (executable)
@@ -65,6 +65,11 @@ deploy() {
             deploy_archives "$@"
             exit 0
             ;;
+        maven-file )
+            echo "Deploying artifacts..."
+            deploy_maven_file "$@"
+            exit 0
+            ;;
         files )
             echo "Deploying files..."
             echo "ERROR: Unimplemented."
@@ -162,6 +167,233 @@ deploy_archives() {
     rm -rf "$tmpdir"
 }
 
+deploy_maven_file_usage () {
+    echo "Usage: $0 <nexus_url> <repo_id> <file>"
+    echo ""
+    echo "    nexus_url:   The URL to the Nexus Server."
+    echo "    repo_id:     Server ID as defined in settings.xml."
+    echo "    file:        File to deploy."
+    echo ""
+    echo "Options:"
+    echo "    -b /path/to/mvn"
+    echo "    -l /path/to/global-settings.xml"
+    echo "    -s /path/to/settings.xml"
+    echo "    -a <artifact_id>"
+    echo "    -c <classifier>"
+    echo "    -g <group_id>"
+    echo "    -v <version>"
+}
+
+deploy_maven_file () {
+    # Deploy a file to a Nexus maven2 repository.
+    #
+    # Usage: deploy_maven_file <nexus_url> <repo_id> <file>
+    #        (Refer to help for full listing by passing -h)
+    #
+    # As this script uses mvn to deploy. The server configuration should be
+    # configured in your local settings.xml. By default the script uses the
+    # mvn default "~/.m2/settings.xml" for the configuration but this can be
+    # overrided in the following order:
+    #
+    #     1. Passed through CLI option "-s" ("-l" for global-settings)
+    #     2. Environment variable "$SETTINGS_FILE" ("$GLOBAL_SETTINGS_FILE" for global-settings)
+    #     3. Maven default "~/.m2/settings.xml".
+    #
+    # If pom-file is passed in via the "-f" option then the Maven GAV parameters
+    # are not necessary. pom-file setting overrides the Maven GAV parameters.
+    while getopts a:b:c:f:g:hl:m:s:v: o; do
+      case "$o" in
+        h)
+            deploy_maven_file_usage
+            exit 0
+            ;;
+
+        ################
+        # Maven Config #
+        ################
+        b)
+            local mvn_bin="$OPTARG"
+            ;;
+        l)
+            local global_settings="$OPTARG"
+            ;;
+        s)
+            settings="$OPTARG"
+            ;;
+
+        #########################
+        # Maven Artitfact (GAV) #
+        #########################
+        a)
+            local artifact_id="$OPTARG"
+            ;;
+        c)
+            local classifier="$OPTARG"
+            ;;
+        f)
+            local pom_file="$OPTARG"
+            echo "pom-file: $pom_file"
+            echo "ERROR: pom_file is currently unimplemented."
+            exit 1
+            ;;
+        g)
+            local group_id="$OPTARG"
+            ;;
+        v)
+            local version="$OPTARG"
+            ;;
+
+        [?])
+            deploy_maven_file_usage
+            exit 1
+            ;;
+      esac
+    done
+    shift $((OPTIND-1))
+
+    # User input
+    local nexus_url="$1"
+    local repo_id="$2"
+    local file="$3"
+
+    if [ "$#" -ne "3" ]; then
+        echo "Missing required arguments."
+        exit 1
+    fi
+
+    # Until we implement the -f <pom-file> parameter assume that group_id must
+    # be passed.
+    if [ -z "$group_id" ]; then
+        echo "ERROR: group_id must be defined. Please pass -g <group_id>."
+        exit 1
+    fi
+
+    ################
+    # Start script #
+    ################
+
+    local mvn_bin="${mvn_bin:-mvn}"
+    local mvn_goals=("org.apache.maven.plugins:maven-deploy-plugin:deploy-file")
+
+    declare -a params
+    params+=("-DrepositoryId=\"$repo_id\"")
+    params+=("-Durl=\"$nexus_url\"")
+    params+=("-Dfile=\"$file\"")
+
+    # Precedence:
+    #   1) -g option
+    #   2) $GLOBAL_SETTINGS_FILE environment variable
+    if [ ! -z "$global_settings" ]; then
+        params+=("-gs $global_settings")
+    elif [ ! -z "$GLOBAL_SETTINGS_FILE" ] && [ -z "$global_settings" ]; then
+        params+=("-gs $GLOBAL_SETTINGS_FILE")
+    fi
+
+    # Precedence:
+    #   1) -s option
+    #   2) $SETTINGS_FILE environment variable
+    if [ ! -z "$settings" ]; then
+        params+=("-s $settings")
+    elif [ ! -z "$SETTINGS_FILE" ] && [ -z "$settings" ]; then
+        params+=("-s $SETTINGS_FILE")
+    fi
+
+    local file_type
+    if [[ "$file" == *.tar.gz ]]; then
+        file_type="tar.gz"
+    else
+        file_type="${file##*.}"
+    fi
+    params+=("-Dtype=\"$file_type\"")
+
+    case "$file_type" in
+        deb )
+            if hash dpkg 2>/dev/null; then
+                echo "dpkg command is available."
+
+                # If user does not provide artifact_id and / or version then parse
+                # information from file.
+                if [ -z "$artifact_id" ]; then
+                    artifact_id=$(dpkg -I "$file" | grep ' Package: ' | sed 's/^[ \t]*Package:[ \t]*//')
+
+                fi
+                if [ -z "$version" ]; then
+                    version=$(dpkg -I "$file" | grep ' Version: ' | sed 's/^[ \t]*Version:[ \t]*//')
+                fi
+            else
+                echo "dpkg command is not available."
+
+                basefile=$(basename -s .deb "$file")
+
+                # If user does not provide artifact_id and / or version then parse
+                # information from file.
+                if [ -z "$artifact_id" ]; then
+                    artifact_id=$(echo "$basefile" | cut -f 1 -d '_')
+                fi
+                if [ -z "$version" ]; then
+                    version=$(echo "$basefile" | cut -f 2- -d '_')
+                fi
+            fi
+            ;;
+
+        rpm )
+            if hash rpm 2>/dev/null; then
+                echo "rpm command is available."
+
+                # If user does not provide artifact_id and / or version then parse
+                # information from file.
+                if [ -z "$artifact_id" ]; then
+                    artifact_id=$(rpm -qp --queryformat="%{name}" "$file")
+                fi
+                if [ -z "$version" ]; then
+                    if grep -qE '\.s(rc\.)?rpm' <<<"$file"; then
+                        rpmrelease=$(rpm -qp --queryformat="%{release}.src" "$file")
+                    else
+                        rpmrelease=$(rpm -qp --queryformat="%{release}.%{arch}" "$file")
+                    fi
+
+                    version=$(rpm -qp --queryformat="%{version}" "$file")
+                    version+="-$rpmrelease"
+                fi
+            else
+                echo "rpm command is not available."
+
+                basefile=$(basename -s .rpm "$file")
+
+                # If user does not provide artifact_id and / or version then parse
+                # information from file.
+                if [ -z "$artifact_id" ]; then
+                    artifact_id=$(echo "$basefile" | rev | cut -f 3- -d '-' | rev)
+                fi
+                if [ -z "$version" ]; then
+                    version=$(echo "$basefile" | rev | cut -f 1 -d '-' | rev)
+                fi
+            fi
+            ;;
+
+        * )
+            echo "ERROR: Unrecognized file type \"$file_type\"." 1>&2
+            exit 1
+            ;;
+    esac
+
+    params+=("-DgroupId=\"$group_id\"")
+    params+=("-DartifactId=\"$artifact_id\"")
+    params+=("-Dversion=\"$version\"")
+
+    if [ ! -z "$classifier" ]; then
+        params+=("-Dclassifier=\"$classifier\"")
+    fi
+
+    echo "Deploying $file to $nexus_url..."
+
+    # make sure the script bombs if we fail an upload
+    if ! eval "$mvn_bin" "${mvn_goals[*]}" "${params[*]}"; then
+        echo "ERROR: There was an error with the upload"
+        exit 1
+    fi
+}
+
 deploy_logs() {
     # Deploy logs to a Nexus site repository named logs.
     #