Skip to content

Commit

Permalink
feat (jkube-kit/common) : Add utility class for downloading CLI from …
Browse files Browse the repository at this point in the history
…GitHub (eclipse-jkube#2454)

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Nov 27, 2023
1 parent 2116a3b commit f7ad618
Show file tree
Hide file tree
Showing 21 changed files with 911 additions and 2 deletions.
72 changes: 72 additions & 0 deletions jkube-kit/build/service/buildpacks/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.eclipse.jkube</groupId>
<artifactId>jkube-kit-parent</artifactId>
<version>1.16-SNAPSHOT</version>
<relativePath>../../../parent/pom.xml</relativePath>
</parent>

<artifactId>jkube-kit-build-service-buildpacks</artifactId>

<name>JKube Kit :: Build :: Service :: Buildpacks</name>

<dependencies>
<dependency>
<groupId>org.eclipse.jkube</groupId>
<artifactId>jkube-kit-build-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
</dependencies>

<build>
<resources>
<!-- Copy over default pack properties defined above -->
<resource>
<directory>src/main/resources-filtered</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* 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.service.buildpacks;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.util.FileUtil;
import org.eclipse.jkube.kit.common.util.PropertiesUtil;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import static org.eclipse.jkube.kit.common.util.GitHubCliDownloaderUtil.downloadCLIFromGitHub;
import static org.eclipse.jkube.kit.common.util.GitHubCliDownloaderUtil.getCLIDownloadPlatformApplicableBinary;
import static org.eclipse.jkube.kit.common.util.GitHubCliDownloaderUtil.getCLIDownloadPlatform;
import static org.eclipse.jkube.kit.common.util.GitHubCliDownloaderUtil.isCLIDownloadPlatformWindows;
import static org.eclipse.jkube.kit.common.util.GitHubCliDownloaderUtil.isCLIDownloadPlatformProcessorArchitectureARM;

public class BuildPackCliDownloader {
private static final String DEFAULT_CLI_PROPERTIES = "/META-INF/jkube/default-cli.properties";
private static final String PACK_DEFAULT_CLI_VERSION_PROPERTY = "pack-cli.upstream.version";
private static final String PACK_CLI_DOWNLOADS_LINK_PROPERTY = "pack-cli.upstream.download-link";
private static final String PACK_CLI_BINARY_PREFIX_PROPERTY = "pack-cli.upstream.binary-prefix";
private static final String PACK_CLI_UNIX_ARCHIVE_SUFFIX_PROPERTY = "pack-cli.upstream.unix.archive-suffix";
private static final String PACK_CLI_WINDOWS_ARCHIVE_SUFFIX_PROPERTY = "pack-cli.upstream.windows.archive-suffix";

private final KitLogger kitLogger;
private final String packCliVersion;
private final String packCliDownloadLink;
private final String packCliBinaryPrefix;
private final String packCliUnixDownloadArchiveSuffix;
private final String packCliWindowsDownloadArchiveSuffix;

public BuildPackCliDownloader(KitLogger kitLogger) {
this.kitLogger = kitLogger;
Properties properties = PropertiesUtil.getPropertiesFromResource(getClass().getResource(DEFAULT_CLI_PROPERTIES));
packCliVersion = (String) properties.get(PACK_DEFAULT_CLI_VERSION_PROPERTY);
packCliDownloadLink = (String) properties.get(PACK_CLI_DOWNLOADS_LINK_PROPERTY);
packCliBinaryPrefix = (String) properties.get(PACK_CLI_BINARY_PREFIX_PROPERTY);
packCliUnixDownloadArchiveSuffix = (String) properties.get(PACK_CLI_UNIX_ARCHIVE_SUFFIX_PROPERTY);
packCliWindowsDownloadArchiveSuffix = (String) properties.get(PACK_CLI_WINDOWS_ARCHIVE_SUFFIX_PROPERTY);
}

public String getPackCLIIfPresentOrDownload(File baseDir, File outputDirectory) {
try {
String packCliDownloadBaseUrl = packCliDownloadLink + "/" + packCliVersion;
String packCliArtifactSuffix = String.format(".%s", isCLIDownloadPlatformWindows() ? packCliWindowsDownloadArchiveSuffix : packCliUnixDownloadArchiveSuffix);
String downloadedPackCliPath = findPreviouslyDownloadedPackCLIPath(outputDirectory);

if (StringUtils.isBlank(downloadedPackCliPath)) {
kitLogger.info("Downloading pack CLI %s", packCliVersion);
String downloadArtifactName = createApplicableDownloadArtifactName(packCliBinaryPrefix, getCLIDownloadPlatform(), packCliVersion, packCliArtifactSuffix);
downloadedPackCliPath = downloadCLIFromGitHub(kitLogger, packCliDownloadBaseUrl, packCliBinaryPrefix, downloadArtifactName, outputDirectory);
}
return FileUtil.getRelativeFilePath(baseDir.getAbsolutePath(), downloadedPackCliPath);
} catch (IOException ioException) {
kitLogger.info("Not able to download pack CLI : " + ioException.getMessage());
return getLocalPackCLI();
}
}

public String createApplicableDownloadArtifactName(String artifactNamePrefix, String platform, String version, String suffix) {
StringBuilder artifactNameBuilder = new StringBuilder();
artifactNameBuilder.append(artifactNamePrefix).append("-").append(version).append("-").append(platform);
if (isCLIDownloadPlatformProcessorArchitectureARM()) {
artifactNameBuilder.append("-arm64");
}
artifactNameBuilder.append(suffix);
return artifactNameBuilder.toString();
}

private String getLocalPackCLI() {
kitLogger.info("Checking for local pack CLI");
String packCliFound = checkPackCLIPresentOnMachine();
if (StringUtils.isBlank(packCliFound)) {
throw new IllegalStateException("No local pack binary found");
}
return packCliFound;
}

private String findPreviouslyDownloadedPackCLIPath(File outputDirectory) {
if (outputDirectory != null && outputDirectory.exists()) {
File extractedPackDir = new File(outputDirectory, String.format("pack-%s-%s", packCliVersion, getCLIDownloadPlatform()));
if (extractedPackDir.exists()) {
File[] foundPackCliList = extractedPackDir.listFiles(f -> f.getName().startsWith("pack") && f.canExecute());
if (foundPackCliList != null && foundPackCliList.length > 0 &&
isPackCLIValid(foundPackCliList[0].getAbsolutePath())) {
return foundPackCliList[0].getAbsolutePath();
}
}
}
return null;
}

private String checkPackCLIPresentOnMachine() {
String localPackCliPath = getCLIDownloadPlatformApplicableBinary(packCliBinaryPrefix);
if (isPackCLIValid(localPackCliPath)) {
return getCLIDownloadPlatformApplicableBinary(packCliBinaryPrefix);
}
return null;
}

private boolean isPackCLIValid(String packCLIPath) {
BuildPackVersionCommand versionCommand = new BuildPackVersionCommand(kitLogger, packCLIPath);
try {
versionCommand.execute();
return true;
} catch (IOException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.service.buildpacks;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jkube.kit.common.ExternalCommand;
import org.eclipse.jkube.kit.common.KitLogger;

import java.util.ArrayList;
import java.util.List;

import static org.eclipse.jkube.kit.common.util.GitHubCliDownloaderUtil.getCLIDownloadPlatformApplicableBinary;

@Getter
public class BuildPackVersionCommand extends ExternalCommand {
private String version;
private final String packCliPath;
private static final String PACK_CLI_ARTIFACT_PREFIX = "pack";

protected BuildPackVersionCommand(KitLogger log, String packCliPath) {
super(log);
this.packCliPath = packCliPath;
}

@Override
protected String[] getArgs() {
List<String> args = new ArrayList<>();
if (StringUtils.isNotBlank(packCliPath)) {
args.add(packCliPath);
} else {
args.add(getCLIDownloadPlatformApplicableBinary(PACK_CLI_ARTIFACT_PREFIX));
}
args.add("--version");
return args.toArray(new String[0]);
}

@Override
protected void processLine(String line) {
version = line;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# 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
#

# Properties for specifying the default images version to use
# The replacement values are defined in the parent pom.xml as properties

# Default Pack CLI version
pack-cli.upstream.version=${version.cli.pack.upstream.github}
pack-cli.upstream.download-link=${download-link.cli.pack.upstream.github}
pack-cli.upstream.binary-prefix=${binary-prefix.cli.pack.upstream.github}
pack-cli.upstream.unix.archive-suffix=${archive-suffix.unix.cli.pack.upstream.github}
pack-cli.upstream.windows.archive-suffix=${archive-suffix.windows.cli.pack.upstream.github}
Loading

0 comments on commit f7ad618

Please sign in to comment.