-
Notifications
You must be signed in to change notification settings - Fork 267
/
self-update.ts
553 lines (479 loc) · 19.1 KB
/
self-update.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
* Copyright (C) 2018-2023 Garden Technologies, Inc. <info@garden.io>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import tar from "tar"
import { Command, CommandParams, CommandResult } from "./base"
import { printHeader } from "../logger/util"
import { BooleanParameter, ChoicesParameter, GlobalOptions, ParameterValues, StringParameter } from "../cli/params"
import { dedent, deline } from "../util/string"
import { basename, dirname, join, resolve } from "path"
import chalk from "chalk"
import { getPackageVersion, getPlatform } from "../util/util"
import { RuntimeError } from "../exceptions"
import { makeTempDir } from "../util/fs"
import { createReadStream, createWriteStream } from "fs"
import { copy, mkdirp, move, readdir, remove } from "fs-extra"
import { got } from "../util/http"
import { promisify } from "node:util"
import { gardenEnv } from "../constants"
import semver from "semver"
import stream from "stream"
import { LogEntry } from "../logger/log-entry"
const selfUpdateArgs = {
version: new StringParameter({
help: `Specify which version to switch/update to. It can be either a stable release, a pre-release, or an edge release version.`,
}),
}
const selfUpdateOpts = {
"force": new BooleanParameter({
help: `Install the Garden CLI even if the specified or detected latest version is the same as the current version.`,
}),
"install-dir": new StringParameter({
help: `Specify an installation directory, instead of using the directory of the Garden CLI being used. Implies --force.`,
}),
"platform": new ChoicesParameter({
choices: ["macos", "linux", "windows"],
help: `Override the platform, instead of detecting it automatically.`,
}),
"major": new BooleanParameter({
defaultValue: false,
// TODO Core 1.0 major release: add these notes:
// "Takes precedence over --minor flag if both are defined."
// "The latest patch version will be installed if neither --major nor --minor flags are specified."
help: dedent`
Install the latest major version of Garden. Falls back to the current version if the greater major version does not exist.
Note! If you use a non-stable version (i.e. pre-release, or draft, or edge), then the latest possible major version will be installed.`,
}),
}
export type SelfUpdateArgs = typeof selfUpdateArgs
export type SelfUpdateOpts = typeof selfUpdateOpts
const versionScopes = ["major", "minor", "patch"] as const
export type VersionScope = typeof versionScopes[number]
function getVersionScope(opts: ParameterValues<GlobalOptions & SelfUpdateOpts>): VersionScope {
if (opts["major"]) {
return "major"
}
// TODO Core 1.0 major release: uncomment this:
// if (opts["minor"]) {
// return "minor"
// }
return "patch"
}
export function isEdgeVersion(version: string): boolean {
return version === "edge" || version.startsWith("edge-")
}
export function isPreReleaseVersion(semVersion: semver.SemVer | null): boolean {
return (semVersion?.prerelease.length || 0) > 0
}
interface SelfUpdateResult {
currentVersion: string
latestVersion: string
desiredVersion: string
installationDirectory: string
installedBuild?: string
installedVersion?: string
abortReason?: string
}
/**
* Utilities and wrappers on top of GitHub REST API.
*/
export type Pagination = { pageNumber: number; pageSize: number }
export async function fetchReleases({ pageNumber, pageSize }: Pagination) {
const results: any[] = await got(
`${gardenEnv.GARDEN_RELEASES_ENDPOINT}?page=${pageNumber}&per_page=${[pageSize]}`
).json()
return results
}
/**
* Traverse the Garden releases on GitHub and get the first one matching the given predicate.
*
* @param primaryPredicate the primary predicate to identify the wanted release
* @param fallbackPredicates the list of fallback predicates to be used if the primary one returns no result
* @param fetcher the optional function to override the default release fetching machinery
*/
export async function findRelease({
primaryPredicate,
fallbackPredicates = [],
fetcher = fetchReleases,
}: {
primaryPredicate: (any: any) => boolean
fallbackPredicates?: ((any: any) => boolean)[]
fetcher?: (pagination: Pagination) => Promise<any[]>
}) {
const pageSize = 100
let pageNumber = 1
let fetchedReleases: any[]
/*
Stores already fetched releases. This will be used with the fallback predicates.
It is a memory consumer, but also a trade-off to avoid GitHub API rate limit errors.
This will not eat gigs of RAM.
*/
let allReleases: any[] = []
do {
/*
This returns the releases ordered by 'published_at' field.
It means that there are 2 ordered subsequences of 0.12.x and 0.13.x releases in the result list,
but the list itself is not properly ordered.
*/
fetchedReleases = await fetcher({ pageNumber, pageSize })
for (const release of fetchedReleases) {
if (primaryPredicate(release)) {
return release
}
}
allReleases.push(...fetchedReleases)
pageNumber++
} while (fetchedReleases.length > 0)
for (const fallbackPredicate of fallbackPredicates) {
for (const release of allReleases) {
if (fallbackPredicate(release)) {
return release
}
}
}
return undefined
}
/**
* @return the latest version tag
* @throws {RuntimeError} if the latest version cannot be detected
*/
export async function getLatestVersion(log: LogEntry): Promise<string> {
let latestVersion: string | undefined = undefined
const endpoint = `${gardenEnv.GARDEN_RELEASES_ENDPOINT}/latest`
try {
const latestVersionRes: any = await got(endpoint).json()
latestVersion = latestVersionRes.tag_name
} catch (err) {
log.debug(`Retrieving the latest Garden version from ${endpoint} failed with error ${err}.`)
}
if (!latestVersion) {
throw new RuntimeError(
deline`Unable to retrieve the latest Garden release version.
This could be a temporary service error, please try again later.`,
{}
)
}
return latestVersion
}
export async function getLatestVersions(numOfStableVersions: number, log: LogEntry) {
let releasesResponse: any | undefined = undefined
const endpoint = `${gardenEnv.GARDEN_RELEASES_ENDPOINT}`
try {
releasesResponse = await got(`${endpoint}?per_page=100`).json()
} catch (err) {
log.debug(`Retrieving the latest Garden releases from ${endpoint} failed with error ${err}.`)
}
if (!releasesResponse) {
throw new RuntimeError(
deline`Unable to retrieve the list of Garden releases.
This could be a temporary service error, please try again later.`,
{}
)
}
return [
chalk.cyan("edge-acorn"),
chalk.cyan("edge-bonsai"),
...releasesResponse
.filter((r: any) => !r.prerelease && !r.draft)
.map((r: any) => chalk.cyan(r.name))
.slice(0, numOfStableVersions),
]
}
export class SelfUpdateCommand extends Command<SelfUpdateArgs, SelfUpdateOpts> {
name = "self-update"
help = "Update the Garden CLI."
cliOnly = true
noProject = true
// TODO Core 1.0 major release: add this example (after --major example):
// garden self-update --minor # install the latest minor version (if it exists) greater than the current one
description = dedent`
Updates your Garden CLI in-place.
Defaults to the latest minor release version, but you can also request a specific release version as an argument.
Examples:
garden self-update # update to the latest minor Garden CLI version
garden self-update edge-acorn # switch to the latest edge build of garden 0.12 (which is created anytime a PR is merged to the 0.12 branch)
garden self-update edge-bonsai # switch to the latest edge build of garden Bonsai (0.13) (which is created anytime a PR is merged to main)
garden self-update 0.12.24 # switch to the exact version 0.12.24 of the CLI
garden self-update --major # install the latest version, even if it's a major bump
garden self-update --force # re-install even if the same version is detected
garden self-update --install-dir ~/garden # install to ~/garden instead of detecting the directory
`
arguments = selfUpdateArgs
options = selfUpdateOpts
_basePreReleasesUrl = "https://github.com/garden-io/garden/releases/download/"
// Overridden during testing
_baseReleasesUrl = "https://download.garden.io/core/"
printHeader({ headerLog }) {
printHeader(headerLog, "Update Garden", "rolled_up_newspaper")
}
async action({
log,
args,
opts,
}: CommandParams<SelfUpdateArgs, SelfUpdateOpts>): Promise<CommandResult<SelfUpdateResult>> {
const currentVersion = getPackageVersion()
let desiredVersion = args.version
if (desiredVersion && desiredVersion[0] === "v") {
desiredVersion = desiredVersion.slice(1)
}
let installationDirectory = opts["install-dir"]
let platform = opts.platform
if (!installationDirectory) {
installationDirectory = dirname(process.execPath)
log.info(
chalk.white(
"No installation directory specified via --install-dir option. Garden will be re-installed to the current installation directory: "
) + chalk.cyan(installationDirectory)
)
} else {
log.info(chalk.white("Installation directory: ") + chalk.cyan(installationDirectory))
}
installationDirectory = resolve(installationDirectory)
log.info(chalk.white("Checking for target and latest versions..."))
const latestVersion = await getLatestVersion(log)
if (!desiredVersion) {
const versionScope = getVersionScope(opts)
desiredVersion = await this.findTargetVersion(currentVersion, versionScope, latestVersion)
}
log.info(chalk.white("Current Garden version: ") + chalk.cyan(currentVersion))
log.info(chalk.white("Target Garden version to be installed: ") + chalk.cyan(desiredVersion))
log.info(chalk.white("Latest release version: ") + chalk.cyan(latestVersion))
if (!opts.force && !opts["install-dir"] && desiredVersion === currentVersion) {
log.warn("")
log.warn(
chalk.yellow(
"The desired version and the current version are the same. Nothing to do. Specify --force if you'd like to re-install the same version."
)
)
return {
result: {
currentVersion,
installationDirectory,
latestVersion,
desiredVersion,
abortReason: "Version already installed",
},
}
}
// Find the executable location
// -> Make sure it's an actual executable, not a script (e.g. from a local dev build)
const expectedExecutableName = process.platform === "win32" ? "garden.exe" : "garden"
if (!opts["install-dir"] && basename(process.execPath) !== expectedExecutableName) {
log.error(
chalk.redBright(
`The executable path ${process.execPath} doesn't indicate this is a normal binary installation for your platform. Perhaps you're running a local development build?`
)
)
return {
result: {
currentVersion,
installationDirectory,
latestVersion,
desiredVersion,
abortReason: "Not running from binary installation",
},
}
}
const tempDir = await makeTempDir()
try {
// Fetch the desired version and extract it to a temp directory
const { build, filename, extension, url } = this.getReleaseArtifactDetails(platform, desiredVersion)
if (!platform) {
platform = getPlatform() === "darwin" ? "macos" : getPlatform()
}
log.info("")
log.info(chalk.white(`Downloading version ${chalk.cyan(desiredVersion)} from ${chalk.underline(url)}...`))
const tempPath = join(tempDir.path, filename)
try {
// See https://github.com/sindresorhus/got/blob/main/documentation/3-streams.md
const pipeline = promisify(stream.pipeline)
await pipeline(got.stream(url), createWriteStream(tempPath))
} catch (err) {
if (err.code === "ERR_NON_2XX_3XX_RESPONSE" && err.response?.statusCode === 404) {
log.info("")
log.error(chalk.redBright(`Could not find version ${desiredVersion} for ${build}.`))
// Print the latest available stable versions
try {
const latestVersions = await getLatestVersions(10, log)
log.info(
chalk.white.bold(`Here are the latest available versions: `) + latestVersions.join(chalk.white(", "))
)
} catch (err) {
log.debug(`Could not retrieve the latest available versions, ${err}`)
}
return {
result: {
currentVersion,
latestVersion,
desiredVersion,
installationDirectory,
abortReason: "Version not found",
},
}
} else {
throw err
}
}
// Move the current release to a backup directory
// -> Name the backup directory by the current version
const backupRoot = join(installationDirectory, ".backup")
const backupPath = join(backupRoot, currentVersion)
await remove(backupPath)
await mkdirp(backupPath)
log.info(chalk.white(`Backing up prior installation to ${chalk.gray(backupPath)}...`))
for (const path of await readdir(installationDirectory)) {
if (path === ".backup") {
continue
}
const absPath = join(installationDirectory, path)
await move(absPath, join(backupPath, path))
}
// Move the extracted files to the install directory
log.info(chalk.white(`Extracting to installation directory ${chalk.cyan(installationDirectory)}...`))
if (extension === "zip") {
// Note: lazy-loading for startup performance
const { Extract } = require("unzipper")
await new Promise((_resolve, reject) => {
const extractor = Extract({ path: tempDir.path })
extractor.on("error", reject)
extractor.on("finish", _resolve)
const reader = createReadStream(tempPath)
reader.pipe(extractor)
})
} else {
await tar.x({
file: tempPath,
cwd: tempDir.path,
})
}
await mkdirp(installationDirectory)
await copy(join(tempDir.path, build), installationDirectory)
log.info("")
log.info(chalk.green("Done!"))
return {
result: {
currentVersion,
installedVersion: desiredVersion,
installedBuild: build,
latestVersion,
desiredVersion,
installationDirectory,
},
}
} finally {
await tempDir.cleanup()
}
}
private getReleaseArtifactDetails(platform: string, desiredVersion: string) {
if (!platform) {
platform = getPlatform() === "darwin" ? "macos" : getPlatform()
}
const architecture = "amd64" // getArchitecture()
const extension = platform === "windows" ? "zip" : "tar.gz"
const build = `${platform}-${architecture}`
const desiredSemVer = semver.parse(desiredVersion)
let filename: string
let url: string
if (desiredSemVer && isPreReleaseVersion(desiredSemVer)) {
const desiredVersionWithoutPreRelease = `${desiredSemVer.major}.${desiredSemVer.minor}.${desiredSemVer.patch}`
filename = `garden-${desiredVersionWithoutPreRelease}-${build}.${extension}`
url = `${this._basePreReleasesUrl}${desiredVersion}/${filename}`
} else {
filename = `garden-${desiredVersion}-${build}.${extension}`
url = `${this._baseReleasesUrl}${desiredVersion}/${filename}`
}
return { build, filename, extension, url }
}
/**
* Returns either the latest patch, or minor, or major version greater than {@code currentVersion}
* depending on the {@code versionScope}.
* If the {@code currentVersion} is not a stable version (i.e. it's an edge or a pre-release),
* then the latest possible version tag will be returned.
*
* @param currentVersion the current version of Garden Core
* @param versionScope the SemVer version scope
*
* @return the matching version tag
* @throws {RuntimeError} if the desired version cannot be detected,
* or if the current version cannot be recognized as a valid release version
*/
private async findTargetVersion(
currentVersion: string,
versionScope: VersionScope,
latestVersion: string
): Promise<string> {
if (isEdgeVersion(currentVersion)) {
return latestVersion
}
const currentSemVer = semver.parse(currentVersion)
if (isPreReleaseVersion(currentSemVer)) {
return latestVersion
}
// The current version is necessary, it's not possible to proceed without its value
if (!currentSemVer) {
throw new RuntimeError(
`Unexpected current version: ${currentVersion}. ` +
`Please make sure it is either a valid (semver) release version.`,
{}
)
}
const targetVersionPredicate = this.getTargetVersionPredicate(currentSemVer, versionScope)
const fallbackVersionPredicate = this.getTargetVersionPredicate(currentSemVer, "patch")
// Currently we support only semver minor and patch versions, so we use patch as a fallback predicate.
// TODO Core 1.0 implement proper fallback predicates for all semver version parts.
const targetRelease = await findRelease({
primaryPredicate: targetVersionPredicate,
fallbackPredicates: [fallbackVersionPredicate],
})
if (!targetRelease) {
throw new RuntimeError(
`Unable to find the latest Garden version greater or equal than ${currentVersion} for the scope: ${versionScope}`,
{}
)
}
return targetRelease.tag_name
}
getTargetVersionPredicate(currentSemVer: semver.SemVer, versionScope: VersionScope) {
return function _latestVersionInScope(release: any) {
const tagName = release.tag_name
// skip pre-release, draft and edge tags
if (isEdgeVersion(tagName) || release.prerelease || release.draft) {
return false
}
const tagSemVer = semver.parse(tagName)
// skip any kind of unexpected tag versions, only stable releases should be processed here
if (!tagSemVer) {
return false
}
switch (versionScope) {
// TODO Core 1.0: review these semantics and make tjhe necessary corrections
case "major": {
if (tagSemVer.major === currentSemVer.major) {
return tagSemVer.minor > currentSemVer.minor
}
return tagSemVer.major >= currentSemVer.major
}
case "minor":
return tagSemVer.major === currentSemVer.major && tagSemVer.minor > currentSemVer.minor
case "patch":
return (
tagSemVer.major === currentSemVer.major &&
tagSemVer.minor === currentSemVer.minor &&
/*
On the patch level of the same major.minor version the version history is linear and properly sorted.
So, we can use the >= condition here.
*/
tagSemVer.patch >= currentSemVer.patch
)
default: {
const _exhaustiveCheck: never = versionScope
return _exhaustiveCheck
}
}
}
}
}