|
| 1 | +import * as core from '@actions/core' |
| 2 | +import * as exec from '@actions/exec' |
| 3 | +import fs from 'fs' |
| 4 | +import { tmpdir } from 'os' |
| 5 | +import { join } from 'path' |
| 6 | +import { downloadFile, calculateSha256, getVersionMetadata } from './install-util.js' |
| 7 | + |
| 8 | +function detectLinuxPlatform() { |
| 9 | + const arch = process.arch |
| 10 | + const platform = process.platform |
| 11 | + |
| 12 | + if (platform !== 'linux') { |
| 13 | + throw new Error(`Unsupported platform: ${platform}`) |
| 14 | + } |
| 15 | + |
| 16 | + // Map Node.js arch to IBM Cloud CLI platform identifiers |
| 17 | + const archMap = { |
| 18 | + 'x64': 'linux64', |
| 19 | + 'ia32': 'linux32', |
| 20 | + 'ppc64': 'ppc64le', |
| 21 | + 's390x': 's390x', |
| 22 | + 'arm64': 'linux-arm64' |
| 23 | + } |
| 24 | + |
| 25 | + const mappedArch = archMap[arch] |
| 26 | + if (!mappedArch) { |
| 27 | + throw new Error(`Unsupported architecture: ${arch}`) |
| 28 | + } |
| 29 | + |
| 30 | + return mappedArch |
| 31 | +} |
| 32 | + |
| 33 | +async function installLinux() { |
| 34 | + const platform = detectLinuxPlatform() |
| 35 | + core.info(`Detected platform: ${platform}`) |
| 36 | + |
| 37 | + core.info('Fetching version metadata...') |
| 38 | + const { version, installer, sha256 } = await getVersionMetadata(platform) |
| 39 | + core.info(`Latest version: ${version}`) |
| 40 | + core.info(`Download URL: ${installer}`) |
| 41 | + |
| 42 | + const tmpDir = tmpdir() |
| 43 | + const tarballPath = join(tmpDir, 'IBM_Cloud_CLI.tar.gz') |
| 44 | + core.info('Downloading IBM Cloud CLI...') |
| 45 | + await downloadFile(installer, tarballPath) |
| 46 | + |
| 47 | + core.info('Verifying checksum...') |
| 48 | + const actualSha256 = await calculateSha256(tarballPath) |
| 49 | + if (actualSha256 !== sha256) { |
| 50 | + fs.unlinkSync(tarballPath) |
| 51 | + throw new Error(`Checksum mismatch! Expected: ${sha256}, Got: ${actualSha256}`) |
| 52 | + } |
| 53 | + core.info('Checksum verified successfully') |
| 54 | + |
| 55 | + const extractDir = join(tmpDir, 'Bluemix_CLI') |
| 56 | + core.info('Extracting installer...') |
| 57 | + await exec.exec('mkdir', ['-p', extractDir]) |
| 58 | + await exec.exec('tar', ['-xzf', tarballPath, '-C', extractDir, '--strip-components=1']) |
| 59 | + |
| 60 | + core.info('Running installer...') |
| 61 | + const installScript = join(extractDir, 'install') |
| 62 | + await exec.exec('chmod', ['755', installScript]) |
| 63 | + await exec.exec(installScript, ['-q']) |
| 64 | + |
| 65 | + core.info('Cleaning up temporary files...') |
| 66 | + fs.unlinkSync(tarballPath) |
| 67 | + await exec.exec('rm', ['-rf', extractDir]) |
| 68 | + |
| 69 | + core.info('IBM Cloud CLI installed successfully') |
| 70 | +} |
| 71 | + |
| 72 | +export { installLinux } |
0 commit comments