diff --git a/hub-detect/artifactory-properties.gradle b/hub-detect/artifactory-properties.gradle new file mode 100644 index 000000000..9ef868960 --- /dev/null +++ b/hub-detect/artifactory-properties.gradle @@ -0,0 +1,87 @@ +task updateArtifactoryProperties() { + dependsOn artifactoryPublish + doLast { + if (project.isSnapshot || "true" == project.findProperty('qa.build')) { + println 'For a snapshot or qa build, artifactory properties will not be updated.' + } else { + try { + println 'For a release build, an update of artifactory properties will be attempted.' + def latestPropertyKey = "DETECT_LATEST" + + def majorVersion = project.version.split("\\.")[0] + def majorVersionPropertyKey = "${latestPropertyKey}_${majorVersion}" + + def artifactoryCredentials = "${project.ext.artifactoryDeployerUsername}:${project.ext.artifactoryDeployerPassword}" + def defaultCurlArgs = defaultCurlArgs(artifactoryCredentials) + + def currentArtifact = findCurrentArtifact(defaultCurlArgs) + if (currentArtifact) { + setArtifactoryProperty(defaultCurlArgs, latestPropertyKey, currentArtifact['downloadUri']) + setArtifactoryProperty(defaultCurlArgs, majorVersionPropertyKey, currentArtifact['downloadUri']) + } else { + println "Artifactory properties won't be updated since ${project.version} was not found." + } + } catch (Exception e) { + println "Manual corrections to the properties for ${project.name}-${project.version} may be necessary." + println "Error correcting the artifactory properties: ${e.message}" + } + } + } +} + +def findCurrentArtifact(def defaultCurlArgs) { + try { + def url = "${project.ext.artifactoryUrl}/api/search/artifact?name=${project.name}-${project.version}.jar&repos=${project.ext.artifactoryRepo}" + def artifactoryItems = getArtifactoryItems(url, defaultCurlArgs) + + if (artifactoryItems != null && artifactoryItems.size() == 1) { + return artifactoryItems[0] + } + } catch (Exception e) { + println "Could not find the current artifact: ${e.message}" + } + + return null +} + +def getArtifactoryItems(String url, def defaultCurlArgs) { + def artifactoryItems = [] + + def curlArgs = defaultCurlArgs + ['--header', 'X-Result-Detail: info', url] + def response = curlResponse(curlArgs) + + def jsonResponse = new groovy.json.JsonSlurper().parseText(response) + def results = jsonResponse['results'] + if (results != null && results.size() > 0) { + results.each { + def downloadUri = it['downloadUri'] + if (downloadUri) { + artifactoryItems.add([downloadUri:downloadUri]) + } + } + } + + return artifactoryItems +} + +def setArtifactoryProperty(def defaultCurlArgs, String propertyKey, String propertyValue) { + println "Setting ${propertyKey} to ${propertyValue}." + def curlArgs = defaultCurlArgs + ['--request', 'PATCH', '--data', "{\"props\":{\"${propertyKey}\":\"${propertyValue}\"}}", "${project.ext.artifactoryUrl}/api/metadata/${project.ext.artifactoryRepo}/com/blackducksoftware/integration/hub-detect"] + + curlResponse(curlArgs) +} + +def defaultCurlArgs(String artifactoryCredentials) { + return ['--silent', '--insecure', '--user', artifactoryCredentials, '--header', 'Content-Type: application/json'] +} + +def curlResponse(def curlArgs) { + new ByteArrayOutputStream().withStream { os -> + exec { + executable = 'curl' + args = curlArgs + standardOutput = os + } + return os.toString() + } +} diff --git a/hub-detect/hub-detect.gradle b/hub-detect/hub-detect.gradle index f5bbb3df8..cbdbaf4fb 100644 --- a/hub-detect/hub-detect.gradle +++ b/hub-detect/hub-detect.gradle @@ -1,12 +1,7 @@ apply plugin: 'org.springframework.boot' -// the override.snapshot property should be set to true for a CI build so -// that the version is omitted from a snapshot build artifact -if (project.isSnapshot && "true" == project.findProperty('override.snapshot')) { - version = 'latest-SNAPSHOT' -} - apply from: 'airgap.gradle' +apply from: 'artifactory-properties.gradle' build { doLast { diff --git a/hub-detect/src/main/resources/hub-detect-4-sh b/hub-detect/src/main/resources/hub-detect-4-sh new file mode 100755 index 000000000..266134d1c --- /dev/null +++ b/hub-detect/src/main/resources/hub-detect-4-sh @@ -0,0 +1,128 @@ +#!/bin/bash + +# DETECT_LATEST_RELEASE_VERSION should be set in your +# environment if you wish to use a version different +# from LATEST. +DETECT_RELEASE_VERSION=${DETECT_LATEST_RELEASE_VERSION} + +# To override the default version key, specify a +* different DETECT_VERSION_KEY in your environment and +# *that* key will be used to get the download url from +# artifactory. These DETECT_VERSION_KEY values are +# properties in Artifactory that resolve to download +# urls for the detect jar file.As of 2018-10-10, the +# available DETECT_VERSION_KEY values are: +# (DETECT_LATEST, DETECT_LATEST_4, DETECT_LATEST_3) +# and we plan to soon add a DETECT_LATEST_5. Every new +# major version of detect will have its own +# DETECT_LATEST_X key. +DETECT_VERSION_KEY=${DETECT_VERSION_KEY:-DETECT_LATEST_4} + +# You can specify your own download url from +# artifactory which can bypass using the property keys +# (this is mainly for QA purposes only) +DETECT_SOURCE=${DETECT_SOURCE:-} + +# To override the default location of /tmp, specify +# your own DETECT_JAR_PATH in your environment and +# *that* location will be used. +# *NOTE* We currently do not support spaces in the +# DETECT_JAR_PATH. +DETECT_JAR_PATH=${DETECT_JAR_PATH:-/tmp} + +# If you want to pass any java options to the +# invocation, specify DETECT_JAVA_OPTS in your +# environment. For example, to specify a 6 gigabyte +# heap size, you would set DETECT_JAVA_OPTS=-Xmx6G. +DETECT_JAVA_OPTS=${DETECT_JAVA_OPTS:-} + +# If you want to pass any additional options to +# curl, specify DETECT_CURL_OPTS in your environment. +# For example, to specify a proxy, you would set +# DETECT_CURL_OPTS=--proxy http://myproxy:3128 +DETECT_CURL_OPTS=${DETECT_CURL_OPTS:-} + +SCRIPT_ARGS="$@" +LOGGABLE_SCRIPT_ARGS="" + +for i in $*; do + if [[ $i == --blackduck.hub.password=* ]]; then + LOGGABLE_SCRIPT_ARGS="$LOGGABLE_SCRIPT_ARGS --blackduck.hub.password=" + elif [[ $i == --blackduck.hub.proxy.password=* ]]; then + LOGGABLE_SCRIPT_ARGS="$LOGGABLE_SCRIPT_ARGS --blackduck.hub.proxy.password=" + elif [[ $i == --blackduck.hub.api.token=* ]]; then + LOGGABLE_SCRIPT_ARGS="$LOGGABLE_SCRIPT_ARGS --blackduck.hub.api.token=" + elif [[ $i == --blackduck.password=* ]]; then + LOGGABLE_SCRIPT_ARGS="$LOGGABLE_SCRIPT_ARGS --blackduck.password=" + elif [[ $i == --blackduck.proxy.password=* ]]; then + LOGGABLE_SCRIPT_ARGS="$LOGGABLE_SCRIPT_ARGS --blackduck.proxy.password=" + elif [[ $i == --blackduck.api.token=* ]]; then + LOGGABLE_SCRIPT_ARGS="$LOGGABLE_SCRIPT_ARGS --blackduck.api.token=" + else + LOGGABLE_SCRIPT_ARGS="$LOGGABLE_SCRIPT_ARGS $i" + fi +done + +run() { + get_detect + run_detect +} + +get_detect() { + if [ -z "${DETECT_SOURCE}" ]; then + if [ -z "${DETECT_RELEASE_VERSION}" ]; then + VERSION_CURL_CMD="curl ${DETECT_CURL_OPTS} --silent --header \"X-Result-Detail: info\" 'https://test-repo.blackducksoftware.com/artifactory/api/storage/bds-integrations-release/com/blackducksoftware/integration/hub-detect?properties=${DETECT_VERSION_KEY}' | grep \"${DETECT_VERSION_KEY}\" | sed 's/[^[]*[^\"]*\"\([^\"]*\).*/\1/'" + DETECT_SOURCE=$(eval $VERSION_CURL_CMD) + else + DETECT_SOURCE="https://test-repo.blackducksoftware.com/artifactory/bds-integrations-release/com/blackducksoftware/integration/hub-detect/${DETECT_RELEASE_VERSION}/hub-detect-${DETECT_RELEASE_VERSION}.jar" + fi + fi + + if [ -z "${DETECT_SOURCE}" ]; then + echo "DETECT_SOURCE was not set or computed correctly, please check your configuration and environment." + exit -1 + fi + + echo "will look for : ${DETECT_SOURCE}" + + DETECT_FILENAME=${DETECT_FILENAME:-$(awk -F "/" '{print $NF}' <<< $DETECT_SOURCE)} + DETECT_DESTINATION="${DETECT_JAR_PATH}/${DETECT_FILENAME}" + + USE_REMOTE=1 + if [ ! -f $DETECT_DESTINATION ]; then + echo "You don't have the current file, so it will be downloaded." + else + echo "You have already downloaded the latest file, so the local file will be used." + USE_REMOTE=0 + fi + + if [ $USE_REMOTE -eq 1 ]; then + echo "getting ${DETECT_SOURCE} from remote" + curlReturn=$(curl $DETECT_CURL_OPTS --silent -w "%{http_code}" -L -o $DETECT_DESTINATION "${DETECT_SOURCE}") + if [ 200 -eq $curlReturn ]; then + echo "saved ${DETECT_SOURCE} to ${DETECT_DESTINATION}" + else + echo "The curl response was ${curlReturn}, which is not successful - please check your configuration and environment." + exit -1 + fi + fi +} + +run_detect() { + JAVACMD="java ${DETECT_JAVA_OPTS} -jar ${DETECT_DESTINATION}" + echo "running detect: ${JAVACMD} ${LOGGABLE_SCRIPT_ARGS}" + + # first, silently delete (-f ignores missing + # files) any existing shell script, then create + # the one we will run + rm -f $DETECT_JAR_PATH/hub-detect-java.sh + echo "#!/bin/sh" > $DETECT_JAR_PATH/hub-detect-java.sh + echo "" >> $DETECT_JAR_PATH/hub-detect-java.sh + echo $JAVACMD $SCRIPT_ARGS >> $DETECT_JAR_PATH/hub-detect-java.sh + source $DETECT_JAR_PATH/hub-detect-java.sh + RESULT=$? + echo "Result code of ${RESULT}, exiting" + exit $RESULT +} + +run diff --git a/hub-detect/src/main/resources/hub-detect-sh b/hub-detect/src/main/resources/hub-detect-sh old mode 100644 new mode 100755 index e6b2f5c65..64511aa4b --- a/hub-detect/src/main/resources/hub-detect-sh +++ b/hub-detect/src/main/resources/hub-detect-sh @@ -5,17 +5,29 @@ # from LATEST. DETECT_RELEASE_VERSION=${DETECT_LATEST_RELEASE_VERSION} -# If you would like to enable the shell script to use -# the latest snapshot instead of the latest release, -# specify DETECT_USE_SNAPSHOT=1 in your environment. -# The default is to NOT use snapshots. If you enable -# snapshots, the jar file will be downloaded whenever -# a new commit is added to the master branch. -DETECT_USE_SNAPSHOT=${DETECT_USE_SNAPSHOT:-0} +# To override the default version key, specify a +* different DETECT_VERSION_KEY in your environment and +# *that* key will be used to get the download url from +# artifactory. These DETECT_VERSION_KEY values are +# properties in Artifactory that resolve to download +# urls for the detect jar file.As of 2018-10-10, the +# available DETECT_VERSION_KEY values are: +# (DETECT_LATEST, DETECT_LATEST_4, DETECT_LATEST_3) +# and we plan to soon add a DETECT_LATEST_5. Every new +# major version of detect will have its own +# DETECT_LATEST_X key. +DETECT_VERSION_KEY=${DETECT_VERSION_KEY:-DETECT_LATEST} + +# You can specify your own download url from +# artifactory which can bypass using the property keys +# (this is mainly for QA purposes only) +DETECT_SOURCE=${DETECT_SOURCE:-} # To override the default location of /tmp, specify # your own DETECT_JAR_PATH in your environment and # *that* location will be used. +# *NOTE* We currently do not support spaces in the +# DETECT_JAR_PATH. DETECT_JAR_PATH=${DETECT_JAR_PATH:-/tmp} # If you want to pass any java options to the @@ -57,40 +69,27 @@ run() { } get_detect() { - VERSION_FILE_DESTINATION="${DETECT_JAR_PATH}/hub-detect-latest-commit-id.txt" - CURRENT_VERSION="" - if [ -f $VERSION_FILE_DESTINATION ]; then - CURRENT_VERSION=$( <$VERSION_FILE_DESTINATION ) - fi - - curl $DETECT_CURL_OPTS -o $VERSION_FILE_DESTINATION https://blackducksoftware.github.io/hub-detect/latest-commit-id.txt - LATEST_VERSION=$( <$VERSION_FILE_DESTINATION ) - - if [ $DETECT_USE_SNAPSHOT -eq 1 ]; then - if [ -z "${DETECT_RELEASE_VERSION}" ]; then - echo "will look for snapshot: hub-detect-latest-SNAPSHOT.jar" - DETECT_SOURCE="https://test-repo.blackducksoftware.com/artifactory/bds-integrations-snapshot/com/blackducksoftware/integration/hub-detect/latest-SNAPSHOT/hub-detect-latest-SNAPSHOT.jar" - DETECT_DESTINATION="${DETECT_JAR_PATH}/hub-detect-latest-SNAPSHOT.jar" - else - DETECT_SOURCE="https://test-repo.blackducksoftware.com/artifactory/bds-integrations-snapshot/com/blackducksoftware/integration/hub-detect/${DETECT_RELEASE_VERSION}/hub-detect-${DETECT_RELEASE_VERSION}.jar" - DETECT_DESTINATION="${DETECT_JAR_PATH}/hub-detect-${DETECT_RELEASE_VERSION}.jar" - fi - else + if [ -z "${DETECT_SOURCE}" ]; then if [ -z "${DETECT_RELEASE_VERSION}" ]; then - DETECT_RELEASE_VERSION=$(curl $DETECT_CURL_OPTS 'https://test-repo.blackducksoftware.com/artifactory/api/search/latestVersion?g=com.blackducksoftware.integration&a=hub-detect&repos=bds-integrations-release') - DETECT_SOURCE="https://test-repo.blackducksoftware.com/artifactory/bds-integrations-release/com/blackducksoftware/integration/hub-detect/${DETECT_RELEASE_VERSION}/hub-detect-${DETECT_RELEASE_VERSION}.jar" - DETECT_DESTINATION="${DETECT_JAR_PATH}/hub-detect-${DETECT_RELEASE_VERSION}.jar" + VERSION_CURL_CMD="curl ${DETECT_CURL_OPTS} --silent --header \"X-Result-Detail: info\" 'https://test-repo.blackducksoftware.com/artifactory/api/storage/bds-integrations-release/com/blackducksoftware/integration/hub-detect?properties=${DETECT_VERSION_KEY}' | grep \"${DETECT_VERSION_KEY}\" | sed 's/[^[]*[^\"]*\"\([^\"]*\).*/\1/'" + DETECT_SOURCE=$(eval $VERSION_CURL_CMD) else DETECT_SOURCE="https://test-repo.blackducksoftware.com/artifactory/bds-integrations-release/com/blackducksoftware/integration/hub-detect/${DETECT_RELEASE_VERSION}/hub-detect-${DETECT_RELEASE_VERSION}.jar" - DETECT_DESTINATION="${DETECT_JAR_PATH}/hub-detect-${DETECT_RELEASE_VERSION}.jar" fi - echo "will look for : ${DETECT_SOURCE}" fi + if [ -z "${DETECT_SOURCE}" ]; then + echo "DETECT_SOURCE was not set or computed correctly, please check your configuration and environment." + exit -1 + fi + + echo "will look for : ${DETECT_SOURCE}" + + DETECT_FILENAME=${DETECT_FILENAME:-$(awk -F "/" '{print $NF}' <<< $DETECT_SOURCE)} + DETECT_DESTINATION="${DETECT_JAR_PATH}/${DETECT_FILENAME}" + USE_REMOTE=1 - if [ "$CURRENT_VERSION" != "$LATEST_VERSION" ] && [ $DETECT_USE_SNAPSHOT -eq 1 ]; then - echo "You don't have the latest snapshot, so the new snapshot will be downloaded." - elif [ ! -f $DETECT_DESTINATION ]; then + if [ ! -f $DETECT_DESTINATION ]; then echo "You don't have the current file, so it will be downloaded." else echo "You have already downloaded the latest file, so the local file will be used." @@ -99,8 +98,13 @@ get_detect() { if [ $USE_REMOTE -eq 1 ]; then echo "getting ${DETECT_SOURCE} from remote" - curl $DETECT_CURL_OPTS -L -o $DETECT_DESTINATION "${DETECT_SOURCE}" - echo "saved ${DETECT_SOURCE} to ${DETECT_DESTINATION}" + curlReturn=$(curl $DETECT_CURL_OPTS --silent -w "%{http_code}" -L -o $DETECT_DESTINATION "${DETECT_SOURCE}") + if [ 200 -eq $curlReturn ]; then + echo "saved ${DETECT_SOURCE} to ${DETECT_DESTINATION}" + else + echo "The curl response was ${curlReturn}, which is not successful - please check your configuration and environment." + exit -1 + fi fi } @@ -118,7 +122,6 @@ run_detect() { source $DETECT_JAR_PATH/hub-detect-java.sh RESULT=$? echo "Result code of ${RESULT}, exiting" - rm -f $DETECT_JAR_PATH/hub-detect-java.sh exit $RESULT }