From 6a823b74756741ce6d4121f3ecda891266ddae7d Mon Sep 17 00:00:00 2001 From: Isaac Karrer Date: Thu, 21 Jun 2018 09:03:01 -0500 Subject: [PATCH] Add cloud-sdk-java as dependency (#22) * Add cloud-sdk-java as dependency * Include instructions for building ci/cloud --- .gitignore | 1 + README.md | 11 ++++++++ ci/cloud/build.gradle | 65 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/.gitignore b/.gitignore index 3209717487ba21..2e72ba7574e35e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__/ .cache/ .mypy_cache/ /ait_workspace/ +*.jar diff --git a/README.md b/README.md index 22ee08e88bceca..3771aea26445d2 100644 --- a/README.md +++ b/README.md @@ -79,3 +79,14 @@ For more options see file: `CONTRIBUTING.md` ## License Apache License 2.0 + +## Cloud Environment + + 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`. + + `GH_OWNER` should be set to `elastic` but can be overridden to your fork if necessary. + + `GH_OWNER=elastic GH_TOKEN=mytoken ./gradlew build` diff --git a/ci/cloud/build.gradle b/ci/cloud/build.gradle index af1d761e6d3e11..4f7f3d91e1ff9d 100755 --- a/ci/cloud/build.gradle +++ b/ci/cloud/build.gradle @@ -6,19 +6,84 @@ */ 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 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' }