diff --git a/Dockerfile b/Dockerfile index 686a828b..37d4dfbd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,13 @@ RUN chmod a+x ./docker/scripts/build_agent.sh RUN ./docker/scripts/build_agent.sh # Build docker image -FROM katalonstudio/katalon:8.6.8 +# Install and inherit java version 17 from katalonstudio/katalon:9.2.0 +FROM katalonstudio/katalon:9.2.0 + +# Install java version 8 +RUN apt-get update && \ + apt-get -y install openjdk-8-jdk --no-install-recommends && \ + apt-get clean # Agent arguement ARG AGENT_VERSION @@ -45,6 +51,7 @@ ENV X11_DISPLAY='' ENV KEEP_FILES='' ENV NO_KEEP_FILES='' ENV AUTO_UPGRADE_ENVIRONMENT=false +ENV IS_DOCKER_AGENT=true # PATH Environment ENV PATH "$GRADLE_BIN:$PATH:$KATALON_AGENT_DIR" diff --git a/package-lock.json b/package-lock.json index 7ca76157..39e93c68 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "katalon-agent", - "version": "v1.7.6", + "version": "v2.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -2953,6 +2953,11 @@ "resolved": "https://registry.npmjs.org/commander/-/commander-7.0.0.tgz", "integrity": "sha512-ovx/7NkTrnPuIV8sqk/GjUIIM1+iUQeqA3ye2VNpq9sVoiZsooObWlQy+OPWGI17GDaEoybuAGJm6U8yC077BA==" }, + "compare-versions": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/compare-versions/-/compare-versions-6.1.0.tgz", + "integrity": "sha512-LNZQXhqUvqUTotpZ00qLSaify3b4VFD588aRr8MKFw4CMUr98ytzCW5wDH5qx/DEY5kCDXcbcRuCqL0szEf2tg==" + }, "component-emitter": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.2.1.tgz", diff --git a/package.json b/package.json index e2a30a94..255550a4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "katalon-agent", - "version": "v2.0.3", + "version": "v2.1.0", "description": "", "main": "cli.js", "scripts": { @@ -31,6 +31,7 @@ "axios": "^0.27.2", "commander": "^7.0.0", "decompress": "^4.2.1", + "compare-versions": "^6.1.0", "find": "^0.3.0", "fs-extra": "^9.1.0", "glob": "^7.2.3", diff --git a/src/core/api/constants.js b/src/core/api/constants.js index 9aeb8582..af12736f 100644 --- a/src/core/api/constants.js +++ b/src/core/api/constants.js @@ -42,6 +42,8 @@ const KS_OLD_RELEASES_URL = const KS_RELEASES_URL = 'https://download.katalon.com/katalon-studio/releases.json'; +const KRE_LATEST_OPTION_VALUE = 'latest'; + module.exports = { TESTOPS_BASE_URL, PATHS, @@ -51,4 +53,5 @@ module.exports = { FILTERED_ERROR_CODE, KS_OLD_RELEASES_URL, KS_RELEASES_URL, + KRE_LATEST_OPTION_VALUE, }; diff --git a/src/core/utils.js b/src/core/utils.js index 12953378..569afa1d 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -2,6 +2,10 @@ const moment = require('moment'); const path = require('path'); const tmp = require('tmp'); const fs = require('fs'); +const childProcess = require('child_process'); +const { compare } = require('compare-versions'); +const { KRE_LATEST_OPTION_VALUE } = require('./api/constants'); + const packageJson = require('../../package.json'); module.exports = { @@ -97,4 +101,36 @@ module.exports = { }); return arStr.join(' '); }, + + runCommand(command, args, options = {}) { + const result = childProcess.spawnSync(command, args, options); + if (result.status !== 0) { + throw new Error(result.stderr.toString()); + } + return result; + }, + + switchJavaVersion(ksVersionNumber) { + let javaPath; + const java17Path = '/usr/lib/jvm/java-17-openjdk-amd64/bin/java'; + const java8Path = '/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java'; + + if (ksVersionNumber === KRE_LATEST_OPTION_VALUE || compare(ksVersionNumber, '9.0.0', '>=')) { + javaPath = java17Path; + } else { + javaPath = java8Path; + } + + this.runCommand( + 'update-alternatives', + [ + '--set', + 'java', + javaPath, + ], + { + stdio: 'inherit', + }, + ); + }, }; diff --git a/src/service/katalon-studio.js b/src/service/katalon-studio.js index 38cc20b7..1aba04a9 100644 --- a/src/service/katalon-studio.js +++ b/src/service/katalon-studio.js @@ -1,12 +1,15 @@ +/* eslint-disable no-param-reassign */ const _ = require('lodash'); const fs = require('fs'); const path = require('path'); +const { filter, maxBy } = require('lodash'); const api = require('../core/api'); const defaultLogger = require('../config/logger'); const os = require('../core/os'); const { KatalonStudioDownloader } = require('./remote-downloader'); const utils = require('../core/utils'); +const { KRE_LATEST_OPTION_VALUE } = require('../core/api/constants'); function find(startPath, filter, callback) { if (!fs.existsSync(startPath)) { @@ -43,9 +46,15 @@ function getKsLocation(ksVersionNumber, ksLocation) { return api.getKSReleases().then(({ body }) => { const osVersion = os.getVersion(); - const ksVersion = body.find( - (item) => item.version === ksVersionNumber && item.os === osVersion, - ); + let ksVersion; + + if (ksVersionNumber === KRE_LATEST_OPTION_VALUE) { + const kreOsVersions = filter(body, ({ os }) => os === osVersion); + ksVersion = maxBy(kreOsVersions, 'version'); + ksVersionNumber = ksVersion.version; + } else { + ksVersion = body.find((item) => item.version === ksVersionNumber && item.os === osVersion); + } const userhome = os.getUserHome(); const ksLocationParentDir = path.join(userhome, '.katalon', ksVersionNumber); @@ -82,6 +91,11 @@ module.exports = { return getKsLocation(ksVersionNumber, ksLocation).then(({ ksLocationParentDir }) => { logger.info(`Katalon Folder: ${ksLocationParentDir}`); + if (process.env.IS_DOCKER_AGENT) { + logger.info(`Check and switch java version for Docker mode to compitable KRE version: ${ksVersionNumber}`); + utils.switchJavaVersion(ksVersionNumber); + } + let ksExecutable = find(ksLocationParentDir, /katalonc$|katalonc\.exe$/) || find(ksLocationParentDir, /katalon$|katalon\.exe$/);