Added distro parameter and make use of disco api

This commit is contained in:
Gerrit Grunwald 2021-03-10 10:15:23 +01:00
parent fc62ccaa4f
commit efa74dc471
7 changed files with 6561 additions and 186 deletions

View file

@ -7,6 +7,8 @@ import * as fs from 'fs';
import * as path from 'path';
import * as semver from 'semver';
import * as util from './util';
import * as constants from './constants';
import {DISCO_URL, DISTROS, PACKAGES_PATH} from './constants';
const tempDirectory = util.getTempDir();
const IS_WINDOWS = util.isWindows();
@ -15,7 +17,8 @@ export async function getJava(
version: string,
arch: string,
jdkFile: string,
javaPackage: string
javaPackage: string,
distro: string = 'zulu'
): Promise<void> {
let toolPath = tc.find(javaPackage, version);
@ -45,7 +48,13 @@ export async function getJava(
const contents = await response.readBody();
const refs = contents.match(/<a href.*\">/gi) || [];
const downloadInfo = getDownloadInfo(refs, version, arch, javaPackage);
const downloadInfo = await getDownloadInfo(
refs,
version,
arch,
javaPackage,
distro
);
jdkFile = await tc.downloadTool(downloadInfo.url);
version = downloadInfo.version;
compressedFileExtension = IS_WINDOWS ? '.zip' : '.tar.gz';
@ -178,121 +187,155 @@ async function unzipJavaDownload(
}
}
function getDownloadInfo(
async function getDownloadInfo(
refs: string[],
version: string,
arch: string,
javaPackage: string
): {version: string; url: string} {
version = normalizeVersion(version);
const archExtension = arch === 'x86' ? 'i686' : 'x64';
let extension = '';
if (IS_WINDOWS) {
extension = `-win_${archExtension}.zip`;
} else {
if (process.platform === 'darwin') {
extension = `-macosx_${archExtension}.tar.gz`;
} else {
extension = `-linux_${archExtension}.tar.gz`;
}
}
core.debug(`Searching for files with extension: ${extension}`);
let pkgRegexp = new RegExp('');
let pkgTypeLength = 0;
javaPackage: string,
distro: string
): Promise<{version: string; url: string}> {
const architecture = arch === 'x86' ? 'i686' : 'x64';
let operatingSystem = '';
let packageType = '';
if (javaPackage === 'jdk') {
pkgRegexp = /jdk.*-/gi;
pkgTypeLength = 'jdk'.length;
} else if (javaPackage == 'jre') {
pkgRegexp = /jre.*-/gi;
pkgTypeLength = 'jre'.length;
} else if (javaPackage == 'jdk+fx') {
pkgRegexp = /fx-jdk.*-/gi;
pkgTypeLength = 'fx-jdk'.length;
packageType = 'jdk';
} else if (javaPackage === 'jre') {
packageType = 'jre';
} else if (javaPackage === 'jdk+fx') {
packageType = 'jdk+fx';
} else {
throw new Error(
`package argument ${javaPackage} is not in [jdk | jre | jdk+fx]`
);
}
// Maps version to url
let versionMap = new Map();
// Filter by platform
refs.forEach(ref => {
if (!ref.endsWith(extension + '">')) {
return;
}
// If we haven't returned, means we're looking at the correct platform
let versions = ref.match(pkgRegexp) || [];
if (versions.length > 1) {
let distribution = '';
if (distro) {
if (distro === '') {
distribution = 'zulu';
} else if (DISTROS.indexOf(distro.toLowerCase()) > -1) {
distribution = distro.toLowerCase();
} else {
throw new Error(
`Invalid ref received from https://static.azul.com/zulu/bin/: ${ref}`
`distro argument '${distro}' is not in [aoj | aoj_openj9 | corretto | dragonwell | liberica | ojdk_build | oracle_open_jdk | sap_machine | zulu]`
);
}
if (versions.length == 0) {
return;
} else {
distribution = 'zulu';
}
let archiveType;
if (IS_WINDOWS) {
operatingSystem = 'windows';
archiveType = 'zip';
} else {
if (process.platform === 'darwin') {
operatingSystem = 'macos';
archiveType = distribution === 'liberica' ? 'zip' : 'tar.gz';
} else {
operatingSystem = 'linux';
archiveType = distribution === 'ojdk_build' ? 'zip' : 'tar.gz';
}
const refVersion = versions[0].slice(pkgTypeLength, versions[0].length - 1);
}
if (semver.satisfies(refVersion, version)) {
versionMap.set(
refVersion,
'https://static.azul.com/zulu/bin/' +
ref.slice('<a href="'.length, ref.length - '">'.length)
);
let latest = 'explicit';
if (version.endsWith('x')) {
if (version.endsWith('.x')) {
version = version.slice(0, -2);
latest = 'per_version';
} else {
version = version.slice(0, -1);
latest = 'overall';
}
}
if (
version.endsWith('0') ||
version.length === 1 ||
version.length === 2 ||
version.includes('ea')
) {
latest = 'overall';
}
let url = DISCO_URL + PACKAGES_PATH;
url += '?distro=' + distribution;
if (version.length != 0) {
url += '&version=' + version;
}
if (javaPackage === 'jdk+fx') {
url += '&package_type=jdk';
url += '&javafx_bundled=true';
} else {
url += '&package_type=' + packageType;
}
url += '&release_status=ea';
url += '&release_status=ga';
url += '&architecture=' + architecture;
url += '&operating_system=' + operatingSystem;
url += '&archive_type=' + archiveType;
url += '&latest=' + latest;
const http = new httpm.HttpClient('bundles', undefined, {
allowRetries: true,
maxRetries: 3
});
let json: any = '';
const response = await http.get(url);
const statusCode = response.message.statusCode || 0;
if (statusCode == 200) {
let body = '';
try {
body = await response.readBody();
json = JSON.parse(body);
} catch (err) {
core.debug(`Unable to read body: ${err.message}`);
}
} else {
const message =
'Unexpected HTTP status code ' +
response.message.statusCode +
' when retrieving versions from ' +
url;
throw new Error(message);
}
// Choose the most recent satisfying version
let curVersion = '0.0.0';
let curUrl = '';
for (const entry of versionMap.entries()) {
const entryVersion = entry[0];
const entryUrl = entry[1];
if (semver.gt(entryVersion, curVersion)) {
curUrl = entryUrl;
curVersion = entryVersion;
}
if (json.length > 0) {
curVersion = json[0].java_version;
curUrl = await getPackageFileUrl(json[0].ephemeral_id);
}
if (curUrl == '') {
throw new Error(
`No valid download found for version ${version} and package ${javaPackage}. Check https://static.azul.com/zulu/bin/ for a list of valid versions or download your own jdk file and add the jdkFile argument`
`No valid download found for ${distribution} with version ${version} and package ${packageType}. Please download your own jdk file and add the jdkFile argument`
);
}
return {version: curVersion, url: curUrl};
}
function normalizeVersion(version: string): string {
if (version.slice(0, 2) === '1.') {
// Trim leading 1. for versions like 1.8
version = version.slice(2);
if (!version) {
throw new Error('1. is not a valid version');
}
}
async function getPackageFileUrl(ephemeralId: string) {
let url: string =
constants.DISCO_URL + constants.EPHEMERAL_IDS_PATH + '/' + ephemeralId;
const http = new httpm.HttpClient('bundle-info', undefined, {
allowRetries: true,
maxRetries: 3
});
if (version.endsWith('-ea')) {
// convert e.g. 14-ea to 14.0.0-ea
if (version.indexOf('.') == -1) {
version = version.slice(0, version.length - 3) + '.0.0-ea';
}
// match anything in -ea.X (semver won't do .x matching on pre-release versions)
if (version[0] >= '0' && version[0] <= '9') {
version = '>=' + version;
}
} else if (version.split('.').length < 3) {
// For non-ea versions, add trailing .x if it is missing
if (version[version.length - 1] != 'x') {
version = version + '.x';
const response = await http.get(url);
const statusCode = response.message.statusCode || 0;
if (statusCode == 200) {
let body = '';
try {
body = await response.readBody();
let json = JSON.parse(body);
return json.direct_download_uri;
} catch (err) {
core.debug(`Unable to read body: ${err.message}`);
}
const message = `Unexpected HTTP status code '${response.message.statusCode}' when retrieving versions from '${url}'. ${body}`.trim();
throw new Error(message);
}
return version;
return '';
}