forked from REMnux/remnux-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremnux-cli.js
784 lines (655 loc) · 24.5 KB
/
remnux-cli.js
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
const cfg = require('./config.json')
const bluebird = require('bluebird')
const os = require('os')
const fs = bluebird.promisifyAll(require('fs'))
const child_process = bluebird.promisifyAll(require('child_process'))
const crypto = require('crypto')
const spawn = require('child_process').spawn
const docopt = require('docopt').docopt
const { Octokit } = require('@octokit/rest')
const mkdirp = require('mkdirp')
const request = require('request')
const openpgp = require('openpgp')
const username = require('username')
const readline = require('readline')
const split = require('split')
const semver = require('semver')
/**
* Setup Custom YAML Parsing
*/
const yaml = require('js-yaml')
const PythonUnicodeType = new yaml.Type('tag:yaml.org,2002:python/unicode', {
kind: 'scalar',
construct: (data) => { return data !== null ? data : ''; }
})
const PYTHON_SCHEMA = new yaml.Schema({
include: [yaml.DEFAULT_SAFE_SCHEMA],
explicit: [PythonUnicodeType]
})
const currentUser = process.env.SUDO_USER || username.sync()
const doc = `
Usage:
remnux [options] list-upgrades [--pre-release]
remnux [options] install [--pre-release] [--version=<version>] [--mode=<mode>] [--user=<user>]
remnux [options] update
remnux [options] upgrade [--pre-release] [--mode=<mode>] [--user=<user>]
remnux [options] version
remnux [options] debug
remnux -h | --help | -v
Options:
--dev Developer Mode (do not use, dangerous, bypasses checks)
--version=<version> Specific version install [default: latest]
--mode=<mode> REMnux installation mode (dedicated, addon, or cloud)
--user=<user> User used for REMnux configuration [default: ${currentUser}]
--no-cache Ignore the cache, always download the release files
--verbose Display verbose logging
`
const saltstackVersion = '3004'
const pubKey = `
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG
mQINBFUA8IMBEADHAQ5rxvmgF/RnApUGCMMG7SzLy3XOO+qByHwnqOfCkaBJXgn4
FDh6dyQZ1hK64lArCscgdlCUdSG+Dx/+xrnhTm1CKD2bjSoZcK15Qt2TlDZih+D1
PyyzeLjxXqS8NLhWP6RQdFE/Zx8Aac7977MYqiCckWa3vdkerw/E+3BhQ2CAvxVq
WlVgjqeue7oAYx3MIeMKRqkzCo7lHL26CER8ueFWTyP2RAse+9VBHlGymLnVwJ8D
wJ6iAZxCCrSQH3UUt7gUeMJKYausbwrFBrI1EIeraPF9fCGVH4rx40uF/xo232zl
1SYiHju0/3+yvfWcLAK5jv6EnCJW77FDkWsLDoR5rDjsu56/63BhZuWXwyngDlrj
HE075kjGn2uYzHmBJhns9oNJVLR6EO8cSZkhW4tasgaEY7boLBVjQF2LY5CGyz03
DG5GbFfYmFfC1lAMfW1q4D+TbNzUoyVf6/bsuwIEkdXcQLOEjRPwjnomqrPhvxWt
oN8xiYXMaIT5R2DttNO3N4z7JyudEWLZnctFu60usA6yQYYVz/re23jQazldVRyk
xEc3EdlC9kISId8T5Cvruv2U/6IboMUUTQdksiMoVUuO0i3tqn0SuS8YcQRMLYlc
3he1TRCCPt18KUFI8gSZ1heKwKfAPyhv3/mkSsF2mNHRFHy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qaFVWun4AJ2Fkldpsugt48w4TLm
59Ak5hv4VEhy7ntKEz3WYtBzeZeY27EgaQ4DoHBtvwr3tWGPhVpcTqnqwIbftywz
sIwJ26wDEPsWwWgUMCvxZfab9op+Tlk4vHuIgU3yKf2aDbikjOmxJ5blEZLgb0KX
fSjkH+LA6A==
=16sr
-----END PGP PUBLIC KEY BLOCK-----
`
const help = `
Sometimes problems occur due to network or server issues when
downloading packages, in which case retrying your operation
a bit later might lead to good results.
To determine the nature of the issue, please review the
saltstack.log file under /var/cache/remnux/cli in the
subdirectory that matches the REMnux version you're installing.
Search for the log file for "result: false" messages and look at
the surrounding lines to diagnose the issue.
For assistance go to https://github.com/REMnux/remnux-cli/issues
`
let osVersion = null
let osCodename = null
let cachePath = '/var/cache/remnux/cli'
let versionFile = '/etc/remnux-version'
let configFile = '/etc/remnux-config'
let releaseFile = '/etc/os-release'
let remnuxConfiguration = {}
const validModes = ['dedicated', 'addon','cloud']
let isModeSpecified = false
const cli = docopt(doc)
const github = new Octokit({
version: '3.0.0',
validateCache: true,
})
const error = (err) => {
console.log('')
console.log(err.message)
console.log(err.stack)
console.log(help)
process.exit(1)
}
const setup = async () => {
if (cli['--dev'] === true) {
cachePath = '/tmp/var/cache/remnux'
versionFile = '/tmp/remnux-version'
configFile = '/tmp/remnux-config'
}
await mkdirp(cachePath)
}
const validOS = async () => {
try {
const contents = fs.readFileSync(releaseFile, 'utf8')
if (contents.indexOf('UBUNTU_CODENAME=bionic') !== -1) {
osVersion = '18.04'
osCodename = 'bionic'
return true
}
if (contents.indexOf('UBUNTU_CODENAME=focal') !== -1) {
osVersion = '20.04'
osCodename = 'focal'
return true
}
throw new Error('Invalid OS or unable to determine Ubuntu version')
} catch (err) {
if (err && err.code === 'ENOENT') {
throw new Error('invalid OS, missing ${releaseFile}')
}
throw err
}
}
const checkOptions = () => {
if (cli['--mode'] != null) {
if (validModes.indexOf(cli['--mode']) === -1) {
throw new Error(`${cli['--mode']} is not a valid install mode. Valid modes are: ${validModes.join(', ')}`)
}
else {
isModeSpecified = true
}
}
}
const fileExists = (path) => {
return new Promise((resolve, reject) => {
fs.stat(path, (err, stats) => {
if (err && err.code === 'ENOENT') {
return resolve(false)
}
if (err) {
return reject(err)
}
return resolve(true)
})
})
}
const saltCheckVersion = (path, value) => {
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (err, contents) => {
if (err && err.code === 'ENOENT') {
return resolve(false);
}
if (err) {
return reject(err);
}
if (contents.indexOf(value) === 0) {
return resolve(true);
}
return resolve(false);
})
})
}
const setupSalt = async () => {
if (cli['--dev'] === false) {
const baseUrl = 'https://repo.saltproject.io/py3/ubuntu'
const aptSourceList = '/etc/apt/sources.list.d/saltstack.list'
const aptDebString = `deb [arch=amd64] ${baseUrl}/${osVersion}/amd64/${saltstackVersion} ${osCodename} main`
const aptExists = await fileExists(aptSourceList)
const saltExists = await fileExists('/usr/bin/salt-call')
const saltVersionOk = await saltCheckVersion(aptSourceList, aptDebString)
if (aptExists === true && saltVersionOk === false) {
console.log('NOTICE: Fixing incorrect Saltstack version configuration.')
console.log('Installing and configuring Saltstack properly ...')
await child_process.execAsync('apt-get remove -y --allow-change-held-packages salt-common salt-minion')
await fs.writeFileAsync(aptSourceList, `deb [arch=amd64] ${baseUrl}/${osVersion}/amd64/${saltstackVersion} ${osCodename} main`)
await child_process.execAsync(`wget -O - ${baseUrl}/${osVersion}/amd64/${saltstackVersion}/salt-archive-keyring.gpg | apt-key add -`)
await child_process.execAsync('apt-get update')
await child_process.execAsync('apt-get install -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y --allow-change-held-packages salt-common', {
env: {
...process.env,
DEBIAN_FRONTEND: 'noninteractive',
},
})
} else if (aptExists === false || saltExists === false) {
console.log('Installing and configuring SaltStack properly ...')
await fs.writeFileAsync(aptSourceList, `deb [arch=amd64] ${baseUrl}/${osVersion}/amd64/${saltstackVersion} ${osCodename} main`)
await child_process.execAsync(`wget -O - ${baseUrl}/${osVersion}/amd64/${saltstackVersion}/salt-archive-keyring.gpg | apt-key add -`)
await child_process.execAsync('apt-get update')
await child_process.execAsync('apt-get install -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y --allow-change-held-packages salt-common', {
env: {
...process.env,
DEBIAN_FRONTEND: 'noninteractive',
},
})
}
} else {
return new Promise((resolve, reject) => {
resolve()
})
}
}
const getCurrentVersion = () => {
return fs.readFileAsync(versionFile)
.catch((err) => {
if (err.code === 'ENOENT') return 'notinstalled'
if (err) throw err
})
.then(contents => contents.toString().replace(/\n/g, ''))
}
const listReleases = () => {
return github.repos.listReleases({
owner: 'REMnux',
repo: 'salt-states'
})
}
const getValidReleases = async () => {
const currentRelease = await getCurrentVersion()
let releases = await listReleases()
const realReleases = releases.data.filter(release => !Boolean(release.prerelease)).map(release => release.tag_name)
const allReleases = releases.data.map(release => release.tag_name)
if (currentRelease === 'notinstalled') {
if (cli['--pre-release'] === true) {
return allReleases
}
return realReleases
}
let curIndex = allReleases.indexOf(currentRelease)
if (curIndex === 0) {
return [allReleases[0]]
}
if (cli['--pre-release'] === true) {
return allReleases.slice(0, curIndex)
}
return allReleases.slice(0, curIndex).filter((release) => {
return realReleases.indexOf(release) !== -1
})
}
const getLatestRelease = () => {
return getValidReleases().then(releases => releases[0])
}
const isValidRelease = (version) => {
return getValidReleases().then((releases) => {
return new Promise((resolve, reject) => {
if (releases.indexOf(version) === -1) {
return resolve(false)
}
resolve(true)
})
})
}
const validateVersion = (version) => {
return getValidReleases().then((releases) => {
if (typeof releases.indexOf(version) === -1) {
throw new Error('The version you are wanting to install/upgrade to is not valid.')
}
return new Promise((resolve) => { resolve() })
})
}
const downloadReleaseFile = (version, filename) => {
console.log(`>> downloading ${filename}`)
const filepath = `${cachePath}/${version}/${filename}`
if (fs.existsSync(filepath) && cli['--no-cache'] === false) {
return new Promise((resolve) => { resolve() })
}
return new Promise((resolve, reject) => {
const output = fs.createWriteStream(filepath)
const req = request.get(`https://github.com/REMnux/salt-states/releases/download/${version}/${filename}`)
req.on('error', (err) => {
reject(err)
})
req
.on('response', (res) => {
if (res.statusCode !== 200) {
throw new Error(res.body)
}
})
.pipe(output)
.on('error', (err) => {
reject(err)
})
.on('close', resolve)
})
}
const downloadRelease = (version) => {
console.log(`>> downloading remnux-salt-states-${version}.tar.gz`)
const filepath = `${cachePath}/${version}/remnux-salt-states-${version}.tar.gz`
if (fs.existsSync(filepath) && cli['--no-cache'] === false) {
return new Promise((resolve, reject) => { resolve() })
}
return new Promise((resolve, reject) => {
const output = fs.createWriteStream(filepath)
const req = request.get(`https://github.com/REMnux/salt-states/archive/${version}.tar.gz`)
req.on('error', (err) => {
reject(err)
})
req
.pipe(output)
.on('error', (err) => {
reject(err)
})
.on('close', resolve)
})
}
const validateFile = async (version, filename) => {
console.log(`> validating file ${filename}`)
const expected = await fs.readFileAsync(`${cachePath}/${version}/${filename}.sha256`)
const actual = await new Promise((resolve, reject) => {
const shasum = crypto.createHash('sha256')
fs.createReadStream(`${cachePath}/${version}/${filename}`)
.on('error', (err) => {
reject(err)
})
.on('data', (data) => {
shasum.update(data)
})
.on('close', () => {
resolve(`${shasum.digest('hex')} /tmp/${filename}\n`)
})
})
if (expected.toString() !== actual) {
throw new Error(`Hashes for ${filename} do not match. Expected: ${expected}. Actual: ${actual}.`)
}
}
const validateSignature = async (version, filename) => {
console.log(`> validating signature for ${filename}`)
const filepath = `${cachePath}/${version}/${filename}`
const ctMessage = await fs.readFileAsync(`${filepath}`, 'utf8')
const ctSignature = await fs.readFileAsync(`${filepath}.asc`, 'utf8')
const ctPubKey = pubKey
const options = {
message: await openpgp.cleartext.readArmored(ctSignature),
publicKeys: (await openpgp.key.readArmored(ctPubKey)).keys
}
const valid = await openpgp.verify(options)
if (typeof valid.signatures === 'undefined' && typeof valid.signatures[0] === 'undefined') {
throw new Error('Invalid Signature')
}
if (valid.signatures[0].valid === false) {
throw new Error('PGP Signature is not valid')
}
}
const extractUpdate = (version, filename) => {
const filepath = `${cachePath}/${version}/${filename}`
return new Promise((resolve, reject) => {
console.log(`> extracting update ${filename}`)
let stdout = ''
let stderr = ''
const extract = spawn('tar', ['-z', '-x', '-f', filepath, '-C', `${cachePath}/${version}`])
extract.stdout.on('data', (data) => {
stdout = `${stdout}${data}`
console.log(data.toString())
})
extract.stderr.on('data', (data) => {
stderr = `${stderr}${data}`
console.log(data.toString())
})
extract.on('error', (err) => {
reject(err)
})
extract.on('close', (code) => {
if (code !== 0) {
return reject(new Error('Extraction returned exit code not zero'))
}
resolve()
})
})
}
const downloadUpdate = async (version) => {
console.log(`> downloading ${version}`)
await mkdirp(`${cachePath}/${version}`)
await downloadReleaseFile(version, `remnux-salt-states-${version}.tar.gz.asc`)
await downloadReleaseFile(version, `remnux-salt-states-${version}.tar.gz.sha256`)
await downloadReleaseFile(version, `remnux-salt-states-${version}.tar.gz.sha256.asc`)
await downloadRelease(version)
await validateFile(version, `remnux-salt-states-${version}.tar.gz`)
await validateSignature(version, `remnux-salt-states-${version}.tar.gz.sha256`)
await extractUpdate(version, `remnux-salt-states-${version}.tar.gz`)
}
const performUpdate = (version) => {
const filepath = `${cachePath}/${version}/salt-states-${version.replace('v', '')}`
const outputFilepath = `${cachePath}/${version}/results.yml`
const logFilepath = `${cachePath}/${version}/saltstack.log`
const begRegex = /Running state \[(.*)\] at time (.*)/g
const endRegex = /Completed state \[(.*)\] at time (.*) duration_in_ms=(.*)/g
const stateApplyMap = {
'dedicated': 'remnux.dedicated',
'addon': 'remnux.addon',
'cloud': 'remnux.cloud'
}
if (!isModeSpecified) {
let savedMode = remnuxConfiguration['mode']
if (validModes.indexOf(savedMode) != -1) {
cli['--mode'] = savedMode
console.log(`> using previous mode: ${cli['--mode']}`)
} else {
console.log(`> no previous REMnux version found; performing a new 'dedicated' installation.`)
cli['--mode'] = "dedicated"
}
}
return new Promise((resolve, reject) => {
console.log(`> upgrading/updating to ${version}`)
console.log(`>> Log file: ${logFilepath}`)
if (os.platform() !== 'linux') {
console.log(`>>> Platform is not Linux`)
return process.exit(0)
}
let stdout = ''
let stderr = ''
const logFile = fs.createWriteStream(logFilepath)
const updateArgs = [
'-l', 'debug', '--local',
'--file-root', filepath,
'--state-output=terse',
'--out=yaml',
'state.apply', stateApplyMap[cli['--mode']],
`pillar={remnux_user: "${remnuxConfiguration['user']}"}`
]
const update = spawn('salt-call', updateArgs)
update.stdout.pipe(fs.createWriteStream(outputFilepath))
update.stdout.pipe(logFile)
update.stderr.pipe(logFile)
update.stderr
.pipe(split())
.on('data', (data) => {
stderr = `${stderr}${data}`
const begMatch = begRegex.exec(data)
const endMatch = endRegex.exec(data)
if (begMatch !== null) {
process.stdout.write(`\n>> Running: ${begMatch[1]}\r`)
} else if (endMatch !== null) {
let message = `>> Completed: ${endMatch[1]} (Took: ${endMatch[3]} ms)`
if (process.stdout.isTTY === true) {
readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0)
}
process.stdout.write(`${message}`)
}
})
update.on('error', (err) => {
console.log(arguments)
reject(err)
})
update.on('close', (code) => {
if (code !== 0) {
return reject(new Error('Update returned exit code not zero'))
}
process.nextTick(resolve)
})
})
}
const summarizeResults = async (version) => {
const outputFilepath = `${cachePath}/${version}/results.yml`
const rawContents = await fs.readFileAsync(outputFilepath)
let results = {}
try {
results = yaml.safeLoad(rawContents, { schema: PYTHON_SCHEMA })
} catch (err) {
// TODO handle?
}
let success = 0
let failure = 0
let failures = [];
Object.keys(results['local']).forEach((key) => {
if (results['local'][key]['result'] === true) {
success++
} else {
failure++
failures.push(results['local'][key])
}
})
if (failure > 0) {
console.log(`\n\n>> Incomplete due to Failures -- Success: ${success}, Failure: ${failure}`)
console.log(`\n>>>> List of Failures (first 10 only)`)
console.log(`\n NOTE: First failure is generally the root cause.`)
console.log(`\n IMPORTANT: If seeking assistance, include this information.\n`)
failures.sort((a, b) => {
return a['__run_num__'] - b['__run_num__']
}).slice(0, 10).forEach((key) => {
console.log(` - ID: ${key['__id__']}`)
console.log(` SLS: ${key['__sls__']}`)
console.log(` Run#: ${key['__run_num__']}`)
console.log(` Comment: ${key['comment']}`)
})
return new Promise((resolve, reject) => { return resolve() })
}
console.log(`\n\n>> COMPLETED SUCCESSFULLY! Success: ${success}, Failure: ${failure}`)
console.log(`\n\n>> Please reboot to make sure all settings go into effect.`)
}
const saveConfiguration = (version) => {
const config = {
version: version,
mode: cli['--mode'],
user: cli['--user']
}
return fs.writeFileAsync(configFile, yaml.safeDump(config))
}
const loadConfiguration = async () => {
try {
return await fs.readFileAsync(configFile).then((c) => yaml.safeLoad(c))
} catch (err) {
if (err.code === 'ENOENT') {
return {
mode: 'unknown',
user: cli['--user']
}
}
throw err
}
}
const run = async () => {
if (cli['-v'] === true) {
console.log(`Version: ${cfg.version}`)
return process.exit(0)
}
console.log(`> remnux-cli@${cfg.version}`)
if (cli['debug'] === true) {
const config = await loadConfiguration()
const debug = `
Version: ${cfg.version}
User: ${currentUser}
Config:
${yaml.safeDump(config)}
`
console.log(debug)
return process.exit(0)
}
if (currentUser === 'root') {
console.log('Warning: You are running as root.')
if (currentUser === cli['--user']) {
console.log('Error: You cannot install as root without specifying the --user option.')
console.log()
console.log('The install user specified with --user must not be the root user.')
return process.exit(5)
}
}
checkOptions()
await validOS()
await setup()
remnuxConfiguration = await loadConfiguration()
const version = await getCurrentVersion()
console.log(`> remnux-version: ${version}\n`)
if (isModeSpecified) {
console.log(`> mode: ${cli['--mode']}`)
}
if (cli['version'] === true) {
return process.exit(0)
}
if (cli['list-upgrades'] === true) {
const releases = await getValidReleases()
const current = await getCurrentVersion()
if (releases.length === 0 || releases[0] === current) {
console.log('No upgrades available.')
return process.exit(0)
}
console.log('> List of available releases')
releases.forEach(release => console.log(` - ${release}`))
return process.exit(0)
}
if (!process.env.SUDO_USER && cli['--dev'] === false) {
console.log('> Error! You must be root to execute this.')
return process.exit(1)
}
await setupSalt()
if (cli['update'] === true) {
if (version === 'notinstalled') {
throw new Error('REMnux is not installed, unable to update.')
}
await downloadUpdate(version)
await performUpdate(version)
await summarizeResults(version)
}
if (cli['install'] === true) {
const currentVersion = await getCurrentVersion(versionFile)
if (currentVersion !== 'notinstalled') {
console.log('REMnux is already installed, please use the \"update\" or \"upgrade\" command.')
return process.exit(0)
}
let versionToInstall = null
if (cli['--version'] === 'latest') {
versionToInstall = await getLatestRelease()
} else {
const validRelease = await isValidRelease(cli['--version'])
if (validRelease === false) {
console.log(`${cli['--version']} is not a REMnux valid release.`)
return process.exit(5)
}
versionToInstall = cli['--version']
}
if (versionToInstall === null) {
throw new Error('versionToInstall was null, this should never happen.')
}
await validateVersion(versionToInstall)
await downloadUpdate(versionToInstall)
await performUpdate(versionToInstall)
await summarizeResults(versionToInstall)
await saveConfiguration(versionToInstall)
}
if (cli['upgrade'] === true) {
const release = await getLatestRelease()
const current = await getCurrentVersion()
if (release === current || typeof release === 'undefined') {
console.log('No upgrades available')
process.exit(0)
}
await downloadUpdate(release)
await performUpdate(release)
await summarizeResults(release)
}
}
const main = async () => {
try {
await run()
} catch (err) {
error(err)
}
}
main()