forked from eclipse-jkube/jkube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (jkube-kit/common) : Add CLIDownloaderUtil to download cli binar…
…ies (eclipse-jkube#2454) Add CLIDownloaderUtil to download CLI binaries from remote sources. Signed-off-by: Rohan Kumar <[email protected]>
- Loading branch information
1 parent
b3281a8
commit a75361d
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/CliDownloaderUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.kit.common.util; | ||
|
||
import io.fabric8.kubernetes.client.Config; | ||
import io.fabric8.kubernetes.client.http.HttpClient; | ||
import io.fabric8.kubernetes.client.http.HttpResponse; | ||
import io.fabric8.kubernetes.client.utils.HttpClientUtils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.apache.commons.io.FilenameUtils.removeExtension; | ||
import static org.eclipse.jkube.kit.common.archive.ArchiveDecompressor.extractArchive; | ||
|
||
public class CliDownloaderUtil { | ||
private CliDownloaderUtil() { } | ||
|
||
public static String downloadCli(String baseDownloadUrl, String cliBinaryPrefix, String cliArtifactName, File outputDirectory) throws IOException { | ||
URL downloadUrl = new URL(String.format("%s/%s", baseDownloadUrl, cliArtifactName)); | ||
File targetExtractionDir = outputDirectory.toPath().resolve(removeExtension(cliArtifactName)).toFile(); | ||
|
||
downloadAndExtractTo(downloadUrl, targetExtractionDir); | ||
|
||
return getCliBinaryPathFromExtractedDir(targetExtractionDir, cliBinaryPrefix); | ||
} | ||
|
||
private static void downloadAndExtractTo(URL downloadUrl, File target) throws IOException { | ||
try (HttpClient client = HttpClientUtils.getHttpClientFactory().newBuilder(Config.empty()).build()) { | ||
final HttpResponse<InputStream> response = client.sendAsync( | ||
client.newHttpRequestBuilder().timeout(30, TimeUnit.MINUTES).url(downloadUrl).build(), InputStream.class) | ||
.get(); | ||
if (!response.isSuccessful()) { | ||
throw new IOException("Server returned (" + response.code() + ") while downloading " + downloadUrl); | ||
} | ||
try (InputStream is = response.body()) { | ||
extractArchive(is, target); | ||
} | ||
} catch(InterruptedException ex) { | ||
Thread.currentThread().interrupt(); | ||
throw new IOException("Download interrupted", ex); | ||
} catch (IOException | ExecutionException e) { | ||
throw new IOException("Failed to download URL " + downloadUrl + " to " + target + ": " + e, e); | ||
} | ||
} | ||
|
||
private static String getCliBinaryPathFromExtractedDir(File targetExtractionDir, String binaryPrefix) { | ||
String cliPath = null; | ||
File[] cliArtifactList = targetExtractionDir.listFiles(f -> f.getName().startsWith(binaryPrefix)); | ||
if (cliArtifactList != null && cliArtifactList.length >= 1) { | ||
cliPath = cliArtifactList[0].getAbsolutePath(); | ||
} | ||
return cliPath; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/CliDownloaderUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.kit.common.util; | ||
|
||
import org.eclipse.jkube.kit.common.TestHttpStaticServer; | ||
import org.eclipse.jkube.kit.common.assertj.FileAssertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatIOException; | ||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | ||
|
||
class CliDownloaderUtilTest { | ||
@TempDir | ||
private File temporaryFolder; | ||
|
||
@Test | ||
void downloadCLI_whenUnixArtifactProvided_thenDownloadAndExtract() throws IOException { | ||
File remoteDirectory = new File(getClass().getResource("/downloadable-artifacts").getFile()); | ||
try (TestHttpStaticServer http = new TestHttpStaticServer(remoteDirectory)) { | ||
// Given | ||
String baseUrl = String.format("http://localhost:%d", http.getPort()); | ||
|
||
// When | ||
String downloadPath = CliDownloaderUtil.downloadCli(baseUrl, "foo", "foo-v0.0.1-linux.tgz", temporaryFolder); | ||
|
||
// Then | ||
assertThat(downloadPath).contains("foo-v0.0.1-linux", "foo"); | ||
FileAssertions.assertThat(temporaryFolder) | ||
.exists() | ||
.fileTree() | ||
.containsExactlyInAnyOrder("foo-v0.0.1-linux", "foo-v0.0.1-linux/foo"); | ||
} | ||
} | ||
|
||
@Test | ||
void downloadCLI_whenZipArtifactProvided_thenDownloadAndExtract() throws IOException { | ||
File remoteDirectory = new File(getClass().getResource("/downloadable-artifacts").getFile()); | ||
try (TestHttpStaticServer http = new TestHttpStaticServer(remoteDirectory)) { | ||
// Given | ||
String baseUrl = String.format("http://localhost:%d", http.getPort()); | ||
|
||
// When | ||
String downloadPath = CliDownloaderUtil.downloadCli(baseUrl, "foo", "foo-v0.0.1-windows.zip", temporaryFolder); | ||
|
||
// Then | ||
assertThat(downloadPath).contains("foo-v0.0.1-windows", "foo.exe"); | ||
FileAssertions.assertThat(temporaryFolder) | ||
.exists() | ||
.fileTree() | ||
.containsExactlyInAnyOrder("foo-v0.0.1-windows", "foo-v0.0.1-windows/foo.exe"); | ||
} | ||
} | ||
|
||
@Test | ||
void downloadCLI_whenArtifactNotAvailable_thenThrowException() throws IOException { | ||
File remoteDirectory = new File(getClass().getResource("/downloadable-artifacts").getFile()); | ||
try (TestHttpStaticServer http = new TestHttpStaticServer(remoteDirectory)) { | ||
// Given | ||
String baseUrl = String.format("http://localhost:%d", http.getPort()); | ||
|
||
// When + Then | ||
assertThatIOException() | ||
.isThrownBy(() -> CliDownloaderUtil.downloadCli(baseUrl, "idontexist", "idontexist-v0.0.1-linux.tgz", temporaryFolder)) | ||
.withMessageContaining("Failed to download"); | ||
} | ||
} | ||
} |
Binary file added
BIN
+111 Bytes
jkube-kit/common/src/test/resources/downloadable-artifacts/foo-v0.0.1-linux.tgz
Binary file not shown.
Binary file added
BIN
+164 Bytes
jkube-kit/common/src/test/resources/downloadable-artifacts/foo-v0.0.1-windows.zip
Binary file not shown.