Skip to content

Commit

Permalink
Chicken or egg (elastic#30)
Browse files Browse the repository at this point in the history
* Revert "Add cloud-sdk-java as dependency (elastic#22)"

This reverts commit 6a823b7.

* Implement circularly dependent gradle task as bash script, add dependency to buildSrc

* update readme
  • Loading branch information
imkarrer committed Aug 21, 2018
1 parent f58f1d8 commit 40ce6ca
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 69 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -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

Expand Down
67 changes: 0 additions & 67 deletions ci/cloud/build.gradle
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions 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"
}
21 changes: 21 additions & 0 deletions 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})

0 comments on commit 40ce6ca

Please sign in to comment.