-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexamples.mjs
91 lines (84 loc) · 2.83 KB
/
examples.mjs
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
/**
* © Copyright IBM Corp. 2022, 2023
* SPDX-License-Identifier: Apache-2.0
*/
import { spawn } from 'child_process';
import { readFileSync } from 'fs';
import minimist from 'minimist';
const defaultCr = 'aiopsuiextension-sample';
const defaultNamespace = 'cp4waiops';
const addExamples = (finalSpec, namespace, cr) => {
let newSpec = JSON.stringify(finalSpec);
const getSpecChildProcess = spawn('oc', ['get', 'AIOpsUIExtension', cr, '-o', 'jsonpath={.spec}', '-n', namespace]);
getSpecChildProcess.on('exit', (ret) => {
const writeSpecChildProcess = !ret ?
spawn('oc', ['patch', 'AIOpsUIExtension', cr, '--type', 'merge', '-p', newSpec, '-n', namespace]) :
spawn('oc', ['apply', '-f', '-', '-n', namespace]);
if (ret) {
console.log('Creating examples...')
newSpec = JSON.stringify({
kind: 'AIOpsUIExtension',
metadata: { name: cr },
apiVersion: 'consoleui.aiops.ibm.com/v1',
...finalSpec
});
writeSpecChildProcess.stdin.setEncoding('utf8');
writeSpecChildProcess.stdin.write(newSpec + '\n');
setTimeout(() => {
writeSpecChildProcess.stdin.end();
}, 1000);
} else {
console.log('Updating examples...')
}
writeSpecChildProcess.stderr
.on('data', (err) => {
const errString = Buffer.from(err).toString();
console.log(errString);
if (errString.includes('Unauthorized')) {
console.log('Could not write example spec due to Unauthorized error. Please ensure you are logged into the Openshift CLI.');
}
});
writeSpecChildProcess.stdout
.on('data', (err) => {
console.log(Buffer.from(err).toString());
});
writeSpecChildProcess
.on('exit', (ret) => {
console.log(!ret ? 'All done! You might need to wait a minute until the examples are ready.' : `Exited with errors.`);
});
});
}
const readConfig = () => {
const routeData = readFileSync('config/routes.json');
return JSON.parse(routeData);
}
const removeExamples = (namespace, cr) => {
const deleteSpecChildProcess = spawn('oc', ['delete', 'AIOpsUIExtension', cr, '-n', namespace]);
deleteSpecChildProcess.stderr
.on('data', (err) => {
console.log(Buffer.from(err).toString());
});
deleteSpecChildProcess
.on('exit', (ret) => {
console.log(!ret ? 'All done!' : `Exited with errors.`);
});
}
/**
* Create, patch or remove example custom resource
*
* @cr {*} Custom resource name
* @namespace {*} Namespace
* @remove {*} Remove examples
*/
const { cr, namespace, remove } = minimist(process.argv.slice(2),
{
alias: { n: 'namespace', r: 'remove' },
boolean: ['remove'],
default: { namespace: defaultNamespace, cr: defaultCr }
});
const specObj = readConfig();
if (remove) {
removeExamples(namespace, cr);
} else {
addExamples(specObj, namespace, cr);
}