diff --git a/README.md b/README.md index ccee721fb1e2f..2328e252e3225 100644 --- a/README.md +++ b/README.md @@ -71,11 +71,13 @@ For more options see file: `CONTRIBUTING.md` Building the `ci/cloud` project requires a [github API token](https://blog.github.com/2013-05-16-personal-api-tokens/). The API key will need repo access (repo checkbox). - Once a github API token has been acquired two environment variables must be set: `GH_OWNER` and `GH_TOKEN`. + Once a github API token has been acquired three environment variables must be set: `GH_OWNER`, `GH_TOKEN`, and `SDK_VERSION`. `GH_OWNER` should be set to `elastic` but can be overridden to your fork if necessary. - `GH_OWNER=elastic GH_TOKEN=mytoken ./gradlew build` + `chmod +x getJavaSdk.sh` + + `GH_OWNER=elastic GH_TOKEN=mytoken SDK_VERSION=1.2.0-SNAPSHOT ./getJavaSdk.sh` ## Contributing diff --git a/ci/cloud/build.gradle b/ci/cloud/build.gradle index aac308e78ff67..ecd1a8fa3f4d6 100755 --- a/ci/cloud/build.gradle +++ b/ci/cloud/build.gradle @@ -6,8 +6,6 @@ */ import com.sun.org.apache.xalan.internal.xslt.Process -import groovy.json.JsonSlurper -import groovy.json.JsonException import org.estf.gradle.SetupCloudCluster import org.estf.gradle.DeleteCloudCluster import org.estf.gradle.ShellCommand @@ -19,82 +17,17 @@ plugins { group 'org.estf.cloud' version '1.0' -project.ext.sdkVersion = '1.2.0-SNAPSHOT' apply plugin: 'groovy' repositories { mavenCentral() - flatDir { - dirs 'libs' - } -} - -/** - * This task download the cloud-java-sdk to libs/ - * Github makes it difficult to download artifacts from the releases page. - * First, we must locate the asset for the release by tag. - * Next, we must find the asset's URL in the response, and make a request to download it following redirects. - * - * THIS IS TEMPORARY UNTIL WE HAVE A PRIVATE COMMON MAVEN FOR ALL TEAMS TO SHARE - */ - /* -task getJavaSdk { - def libsFolder = new File('libs') - if (!libsFolder.exists()) { - libsFolder.mkdirs() - } - def ghOwner = System.env['GH_OWNER'] - def ghToken = System.env['GH_TOKEN'] - if (ghOwner == null || ghToken == null) { - throw new GradleException('GH_OWNER and GH_TOKEN must be set.') - } - - def getReleaseByTagTemplate = 'https://api.github.com/repos/%s/cloud-sdk-java/releases/tags/v%s?access_token=%s' - def getReleaseByTagUrl = String.format(getReleaseByTagTemplate, ghOwner, project.sdkVersion, ghToken) - def getReleaseByTagCommand = ['curl', getReleaseByTagUrl] - def getReleaseByTagResponse = getReleaseByTagCommand.execute() - handleCurlExitCode(getReleaseByTagResponse, getReleaseByTagCommand) - def json = new JsonSlurper().parseText(getReleaseByTagResponse.text) - handleErrorMessage(json, getReleaseByTagUrl) - - def assetUrl = json.assets[0].url + "?access_token=${ghToken}" - def filename = "libs/${json.assets[0].name}" - def getAssetCommand = - ['curl', '-L', '-H', 'Accept: application/octet-stream', assetUrl, '-o', filename] - def getAssetResponse = getAssetCommand.execute() - handleCurlExitCode(getAssetResponse, getAssetCommand) - // If the download fails, the error message will be written as the contents of the .jar. - // If the .jar is a parseable json body we can assume something went wrong with the download. - try { - json = new JsonSlurper().parseText(new File(filename).text) - handleErrorMessage(json, assetUrl) - } catch (JsonException jsonException) { - // NOP - } -} - -def handleErrorMessage(json, url) { - if (json.message) { - throw new GradleException("An error message was returned when attempting to find the release by tag\n" + - "URL: ${url}\n" + - "ERROR: ${json.message}") - } -} - -def handleCurlExitCode(response, command) { - if (response.waitFor() != 0) { - throw new GradleException("The following command exited with nonzero status\n" + - "Command: ${command}") - } } dependencies { - compile group: 'co.elastic.cloud', name: 'sdk-java', version: project.sdkVersion compile 'org.codehaus.groovy:groovy-all:2.3.11' testCompile group: 'junit', name: 'junit', version: '4.12' } -*/ /* General Methods diff --git a/ci/cloud/buildSrc/build.gradle b/ci/cloud/buildSrc/build.gradle new file mode 100644 index 0000000000000..d1ebef3a81638 --- /dev/null +++ b/ci/cloud/buildSrc/build.gradle @@ -0,0 +1,9 @@ +repositories { + flatDir { + dirs 'libs' + } +} + +dependencies { + compile group: 'co.elastic.cloud', name: 'sdk-java', version: "1.2.0-SNAPSHOT" +} \ No newline at end of file diff --git a/ci/cloud/getJavaSdk.sh b/ci/cloud/getJavaSdk.sh new file mode 100755 index 0000000000000..48d035434861c --- /dev/null +++ b/ci/cloud/getJavaSdk.sh @@ -0,0 +1,21 @@ +#!/bin/bash +export PYTHONIOENCODING=utf8 + +libsDir="buildSrc/libs" +ghOwner="${GH_OWNER:?GH_OWNER needs to be set!}" +ghToken="${GH_TOKEN:?GH_TOKEN needs to be set!}" +sdkVersion="${SDK_VERSION:?SDK_VERSION needs to be set!}" + +getReleaseByTagUrl="https://api.github.com/repos/${ghOwner}/cloud-sdk-java/releases/tags/v${sdkVersion}?access_token=${ghToken}" + +if [ ! -d ${libsDir} ]; then + echo "Creating libs directory..." + mkdir ${libsDir} + echo "Created!" +fi + +getReleaseByTagResponse=$(curl -s ${getReleaseByTagUrl}) +assetName=$(curl -s ${getReleaseByTagUrl} | python -c "import sys, json;print json.load(sys.stdin)['assets'][0]['name']") +assetUrl=$(curl -s ${getReleaseByTagUrl} | python -c "import sys, json; print json.load(sys.stdin)['assets'][0]['url']") +assetUrlWithAuth="${assetUrl}?access_token=${ghToken}" +downloadResponse=$(curl -L -H "Accept: application/octet-stream" ${assetUrlWithAuth} -o ${libsDir}/${assetName}) \ No newline at end of file