-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbase.ts
261 lines (225 loc) · 8.49 KB
/
base.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
/**
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the ""License"");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an ""AS IS"" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import paths from 'path';
import {MissingArgumentError, SoloError} from '../core/errors.js';
import {ShellRunner} from '../core/shell_runner.js';
import {type LeaseManager} from '../core/lease/lease_manager.js';
import {type LocalConfig} from '../core/config/local_config.js';
import {type RemoteConfigManager} from '../core/config/remote/remote_config_manager.js';
import {type Helm} from '../core/helm.js';
import {type K8} from '../core/k8.js';
import {type ChartManager} from '../core/chart_manager.js';
import {type ConfigManager} from '../core/config_manager.js';
import {type DependencyManager} from '../core/dependency_managers/index.js';
import {type Opts} from '../types/command_types.js';
import {type CommandFlag} from '../types/flag_types.js';
import {type Lease} from '../core/lease/lease.js';
import {Listr} from 'listr2';
import path from 'path';
import * as constants from '../core/constants.js';
import fs from 'fs';
import {Task} from '../core/task.js';
export interface CommandHandlers {
parent: BaseCommand;
}
export abstract class BaseCommand extends ShellRunner {
protected readonly helm: Helm;
protected readonly k8: K8;
protected readonly chartManager: ChartManager;
protected readonly configManager: ConfigManager;
protected readonly depManager: DependencyManager;
protected readonly leaseManager: LeaseManager;
protected readonly _configMaps = new Map<string, any>();
protected readonly localConfig: LocalConfig;
protected readonly remoteConfigManager: RemoteConfigManager;
constructor(opts: Opts) {
if (!opts || !opts.helm) throw new Error('An instance of core/Helm is required');
if (!opts || !opts.k8) throw new Error('An instance of core/K8 is required');
if (!opts || !opts.chartManager) throw new Error('An instance of core/ChartManager is required');
if (!opts || !opts.configManager) throw new Error('An instance of core/ConfigManager is required');
if (!opts || !opts.depManager) throw new Error('An instance of core/DependencyManager is required');
if (!opts || !opts.localConfig) throw new Error('An instance of core/LocalConfig is required');
if (!opts || !opts.remoteConfigManager)
throw new Error('An instance of core/config/RemoteConfigManager is required');
super();
this.helm = opts.helm;
this.k8 = opts.k8;
this.chartManager = opts.chartManager;
this.configManager = opts.configManager;
this.depManager = opts.depManager;
this.leaseManager = opts.leaseManager;
this.localConfig = opts.localConfig;
this.remoteConfigManager = opts.remoteConfigManager;
}
async prepareChartPath(chartDir: string, chartRepo: string, chartReleaseName: string) {
if (!chartRepo) throw new MissingArgumentError('chart repo name is required');
if (!chartReleaseName) throw new MissingArgumentError('chart release name is required');
if (chartDir) {
const chartPath = path.join(chartDir, chartReleaseName);
await this.helm.dependency('update', chartPath);
return chartPath;
}
return `${chartRepo}/${chartReleaseName}`;
}
prepareValuesFiles(valuesFile: string) {
let valuesArg = '';
if (valuesFile) {
const valuesFiles = valuesFile.split(',');
valuesFiles.forEach(vf => {
const vfp = paths.resolve(vf);
valuesArg += ` --values ${vfp}`;
});
}
return valuesArg;
}
getConfigManager(): ConfigManager {
return this.configManager;
}
getChartManager(): ChartManager {
return this.chartManager;
}
/**
* Dynamically builds a class with properties from the provided list of flags
* and extra properties, will keep track of which properties are used. Call
* getUnusedConfigs() to get an array of unused properties.
*/
getConfig(configName: string, flags: CommandFlag[], extraProperties: string[] = []): object {
const configManager = this.configManager;
// build the dynamic class that will keep track of which properties are used
const NewConfigClass = class {
private usedConfigs: Map<string, number>;
constructor() {
// the map to keep track of which properties are used
this.usedConfigs = new Map();
// add the flags as properties to this class
flags?.forEach(flag => {
// @ts-ignore
this[`_${flag.constName}`] = configManager.getFlag(flag);
Object.defineProperty(this, flag.constName, {
get() {
this.usedConfigs.set(flag.constName, this.usedConfigs.get(flag.constName) + 1 || 1);
return this[`_${flag.constName}`];
},
});
});
// add the extra properties as properties to this class
extraProperties?.forEach(name => {
// @ts-ignore
this[`_${name}`] = '';
Object.defineProperty(this, name, {
get() {
this.usedConfigs.set(name, this.usedConfigs.get(name) + 1 || 1);
return this[`_${name}`];
},
set(value) {
this[`_${name}`] = value;
},
});
});
}
/** Get the list of unused configurations that were not accessed */
getUnusedConfigs() {
const unusedConfigs: string[] = [];
// add the flag constName to the unusedConfigs array if it was not accessed
flags?.forEach(flag => {
if (!this.usedConfigs.has(flag.constName)) {
unusedConfigs.push(flag.constName);
}
});
// add the extra properties to the unusedConfigs array if it was not accessed
extraProperties?.forEach(item => {
if (!this.usedConfigs.has(item)) {
unusedConfigs.push(item);
}
});
return unusedConfigs;
}
};
const newConfigInstance = new NewConfigClass();
// add the new instance to the configMaps so that it can be used to get the
// unused configurations using the configName from the BaseCommand
this._configMaps.set(configName, newConfigInstance);
return newConfigInstance;
}
getLeaseManager(): LeaseManager {
return this.leaseManager;
}
/**
* Get the list of unused configurations that were not accessed
* @returns an array of unused configurations
*/
getUnusedConfigs(configName: string): string[] {
return this._configMaps.get(configName).getUnusedConfigs();
}
getK8() {
return this.k8;
}
getLocalConfig() {
return this.localConfig;
}
getRemoteConfigManager() {
return this.remoteConfigManager;
}
abstract close(): Promise<void>;
commandActionBuilder(actionTasks: any, options: any, errorString: string, lease: Lease | null) {
return async function (argv: any, commandDef: CommandHandlers) {
const tasks = new Listr([...actionTasks], options);
try {
await tasks.run();
} catch (e: Error | any) {
commandDef.parent.logger.error(`${errorString}: ${e.message}`, e);
throw new SoloError(`${errorString}: ${e.message}`, e);
} finally {
const promises = [];
promises.push(commandDef.parent.close());
if (lease) promises.push(lease.release());
await Promise.all(promises);
}
};
}
/**
* Setup home directories
* @param dirs a list of directories that need to be created in sequence
*/
setupHomeDirectory(
dirs: string[] = [
constants.SOLO_HOME_DIR,
constants.SOLO_LOGS_DIR,
constants.SOLO_CACHE_DIR,
constants.SOLO_VALUES_DIR,
],
) {
const self = this;
try {
dirs.forEach(dirPath => {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, {recursive: true});
}
self.logger.debug(`OK: setup directory: ${dirPath}`);
});
} catch (e: Error | any) {
this.logger.error(e);
throw new SoloError(`failed to create directory: ${e.message}`, e);
}
return dirs;
}
setupHomeDirectoryTask() {
return new Task('Setup home directory', async () => {
this.setupHomeDirectory();
});
}
}