|
| 1 | +/* |
| 2 | + * SonarQube CLI |
| 3 | + * Copyright (C) 2026 SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | + |
| 21 | +// Self-update logic for the sonarqube-cli binary |
| 22 | + |
| 23 | +import { existsSync } from 'node:fs'; |
| 24 | +import { copyFile, chmod, rename, unlink, mkdtemp, writeFile, rm } from 'node:fs/promises'; |
| 25 | +import { join } from 'node:path'; |
| 26 | +import { tmpdir } from 'node:os'; |
| 27 | +import { spawnProcess } from './process.js'; |
| 28 | +import { VERSION } from '../version.js'; |
| 29 | +import { SONARSOURCE_BINARIES_URL, SONARQUBE_CLI_DIST_PREFIX } from './config-constants.js'; |
| 30 | +import { detectPlatform } from './platform-detector.js'; |
| 31 | +import logger from './logger.js'; |
| 32 | + |
| 33 | +const REQUEST_TIMEOUT_MS = 30000; |
| 34 | +const DOWNLOAD_TIMEOUT_MS = 120000; |
| 35 | + |
| 36 | +/** |
| 37 | + * Fetch the latest available CLI version from binaries.sonarsource.com |
| 38 | + */ |
| 39 | +export async function fetchLatestCliVersion(): Promise<string> { |
| 40 | + const url = `${SONARSOURCE_BINARIES_URL}/${SONARQUBE_CLI_DIST_PREFIX}/latest-version.txt`; |
| 41 | + |
| 42 | + const response = await fetch(url, { |
| 43 | + headers: { 'User-Agent': `sonarqube-cli/${VERSION}` }, |
| 44 | + signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), |
| 45 | + }); |
| 46 | + |
| 47 | + if (!response.ok) { |
| 48 | + throw new Error(`Failed to fetch latest version: ${response.status} ${response.statusText}`); |
| 49 | + } |
| 50 | + |
| 51 | + const version = (await response.text()).trim(); |
| 52 | + if (!version) { |
| 53 | + throw new Error('Could not determine latest version from release server'); |
| 54 | + } |
| 55 | + |
| 56 | + return version; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Compare two dot-separated version strings numerically. |
| 61 | + * Returns negative if a < b, positive if a > b, 0 if equal. |
| 62 | + */ |
| 63 | +export function compareVersions(a: string, b: string): number { |
| 64 | + return a.localeCompare(b, undefined, { numeric: true }); |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Build the download URL for a given CLI version on the current platform. |
| 69 | + * Naming convention: sonarqube-cli-<version>-<os>-<arch>.exe |
| 70 | + */ |
| 71 | +function buildCliDownloadUrl(version: string): string { |
| 72 | + const platform = detectPlatform(); |
| 73 | + const filename = `sonarqube-cli-${version}-${platform.os}-${platform.arch}.exe`; |
| 74 | + return `${SONARSOURCE_BINARIES_URL}/${SONARQUBE_CLI_DIST_PREFIX}/${filename}`; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Download the CLI binary to a temporary file and return its path. |
| 79 | + */ |
| 80 | +async function downloadToTemp(url: string): Promise<{ tmpDir: string; tmpFile: string }> { |
| 81 | + const tmpDir = await mkdtemp(join(tmpdir(), 'sonar-update-')); |
| 82 | + const tmpFile = join(tmpDir, 'sonar.download'); |
| 83 | + |
| 84 | + logger.debug(`Downloading CLI from: ${url}`); |
| 85 | + |
| 86 | + const response = await fetch(url, { |
| 87 | + headers: { 'User-Agent': `sonarqube-cli/${VERSION}` }, |
| 88 | + signal: AbortSignal.timeout(DOWNLOAD_TIMEOUT_MS), |
| 89 | + }); |
| 90 | + |
| 91 | + if (!response.ok) { |
| 92 | + throw new Error(`Download failed: ${response.status} ${response.statusText}`); |
| 93 | + } |
| 94 | + |
| 95 | + const buffer = await response.arrayBuffer(); |
| 96 | + await writeFile(tmpFile, Buffer.from(buffer)); |
| 97 | + |
| 98 | + return { tmpDir, tmpFile }; |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Verify a binary responds correctly to --version, returning the version string. |
| 103 | + */ |
| 104 | +async function verifyBinary(binaryPath: string): Promise<string> { |
| 105 | + const result = await spawnProcess(binaryPath, ['--version'], { |
| 106 | + stdout: 'pipe', |
| 107 | + stderr: 'pipe', |
| 108 | + }); |
| 109 | + |
| 110 | + if (result.exitCode !== 0) { |
| 111 | + throw new Error('Downloaded binary failed version check'); |
| 112 | + } |
| 113 | + |
| 114 | + const combined = result.stdout + ' ' + result.stderr; |
| 115 | + // eslint-disable-next-line sonarjs/regex-complexity -- bounded quantifiers prevent backtracking |
| 116 | + const match = /(\d{1,20}(?:\.\d{1,20}){1,3})/.exec(combined); |
| 117 | + if (!match) { |
| 118 | + throw new Error('Could not parse version from downloaded binary output'); |
| 119 | + } |
| 120 | + |
| 121 | + return match[1]; |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Remove macOS quarantine attribute from the file (no-op if not quarantined). |
| 126 | + */ |
| 127 | +async function removeQuarantine(filePath: string): Promise<void> { |
| 128 | + try { |
| 129 | + await spawnProcess('xattr', ['-d', 'com.apple.quarantine', filePath], { |
| 130 | + stdout: 'pipe', |
| 131 | + stderr: 'pipe', |
| 132 | + }); |
| 133 | + } catch { |
| 134 | + // Binary is not quarantined — this is expected and fine |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * Perform an in-place binary self-update with rollback on failure. |
| 140 | + * |
| 141 | + * Strategy: |
| 142 | + * 1. Download new binary to system tmpdir |
| 143 | + * 2. Copy to <binaryPath>.new (same FS → avoids cross-device rename) |
| 144 | + * 3. Rename current binary to <binaryPath>.backup (atomic) |
| 145 | + * 4. Rename .new to current path (atomic) |
| 146 | + * 5. Verify the new binary works |
| 147 | + * 6. On success: remove backup |
| 148 | + * 7. On failure: restore backup, throw |
| 149 | + */ |
| 150 | +export async function performSelfUpdate(version: string): Promise<string> { |
| 151 | + const currentBinaryPath = process.execPath; |
| 152 | + const newBinaryPath = `${currentBinaryPath}.new`; |
| 153 | + const backupPath = `${currentBinaryPath}.backup`; |
| 154 | + const downloadUrl = buildCliDownloadUrl(version); |
| 155 | + const platform = detectPlatform(); |
| 156 | + |
| 157 | + let tmpDir: string | null = null; |
| 158 | + |
| 159 | + try { |
| 160 | + // --- Download --- |
| 161 | + const { tmpDir: td, tmpFile } = await downloadToTemp(downloadUrl); |
| 162 | + tmpDir = td; |
| 163 | + |
| 164 | + // Set permissions before moving |
| 165 | + if (platform.os !== 'windows') { |
| 166 | + await chmod(tmpFile, 0o755); |
| 167 | + } |
| 168 | + |
| 169 | + if (platform.os === 'macos') { |
| 170 | + await removeQuarantine(tmpFile); |
| 171 | + } |
| 172 | + |
| 173 | + // --- Copy to binary directory (handles cross-filesystem download) --- |
| 174 | + await copyFile(tmpFile, newBinaryPath); |
| 175 | + if (platform.os !== 'windows') { |
| 176 | + await chmod(newBinaryPath, 0o755); |
| 177 | + } |
| 178 | + |
| 179 | + // --- Atomic swap --- |
| 180 | + if (existsSync(currentBinaryPath)) { |
| 181 | + await rename(currentBinaryPath, backupPath); |
| 182 | + } |
| 183 | + await rename(newBinaryPath, currentBinaryPath); |
| 184 | + |
| 185 | + // --- Verify --- |
| 186 | + try { |
| 187 | + const installedVersion = await verifyBinary(currentBinaryPath); |
| 188 | + |
| 189 | + // Success: remove backup |
| 190 | + if (existsSync(backupPath)) { |
| 191 | + await unlink(backupPath); |
| 192 | + } |
| 193 | + |
| 194 | + return installedVersion; |
| 195 | + } catch (verifyErr) { |
| 196 | + // Rollback: restore backup |
| 197 | + logger.warn(`Verification failed, rolling back: ${(verifyErr as Error).message}`); |
| 198 | + if (existsSync(backupPath)) { |
| 199 | + await rename(backupPath, currentBinaryPath); |
| 200 | + } |
| 201 | + throw verifyErr; |
| 202 | + } |
| 203 | + } finally { |
| 204 | + // Best-effort cleanup of temp directory |
| 205 | + if (tmpDir) { |
| 206 | + try { |
| 207 | + await rm(tmpDir, { recursive: true, force: true }); |
| 208 | + } catch { |
| 209 | + // Ignore cleanup errors |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + // Best-effort cleanup of .new file if it somehow survived |
| 214 | + if (existsSync(newBinaryPath)) { |
| 215 | + try { |
| 216 | + await unlink(newBinaryPath); |
| 217 | + } catch { |
| 218 | + // Ignore cleanup errors |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments