-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathscan.ts
More file actions
336 lines (300 loc) · 10.2 KB
/
scan.ts
File metadata and controls
336 lines (300 loc) · 10.2 KB
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
import chalk from 'chalk'
import fs from 'fs'
import path from 'path'
import { Opts, pluginMap, PluginType, StorageEngine } from '@cloudgraph/sdk'
import { range } from 'lodash'
import Command from './base'
import { cleanString, fileUtils, getStoredSchema } from '../utils'
import DgraphEngine from '../storage/dgraph'
import { scanReport } from '../reports'
import { loadAllData, processConnectionsBetweenEntities } from '../utils/data'
export default class Scan extends Command {
static description =
'Scan one or multiple providers data to be queried through Dgraph'
static examples = [
'$ cg scan',
'$ cg scan aws',
'$ cg scan aws --dgraph http://localhost:1000 [Save data in dgraph running on port 1000]',
'$ cg scan aws --no-serve [Do not start the query engine]',
]
static strict = false
static flags = {
...Command.flags,
}
static hidden = false
static args = Command.args
private async plugins({
storage: { isRunning, engine },
flags,
}: {
storage: {
isRunning: boolean
engine: StorageEngine
}
flags: {
[flag: string]: any
}
}): Promise<void> {
const config = this.getCGConfig('cloudGraph')
const { plugins = {} } = config
for (const pluginType in plugins) {
if (pluginType) {
try {
// Get Plugin Interface
const Plugin = pluginMap[pluginType]
// Execute Plugins by Provider
for (const provider in this.providers) {
if (provider) {
const { schemasMap, serviceKey } = this.providers[provider]
// Initialize
const PluginInstance = new Plugin({
config,
provider: {
name: provider,
schemasMap,
serviceKey,
},
flags: flags as { [flag: string]: any },
logger: this.logger,
})
// Get the Plugin Manager
const pluginManager = await this.getPluginManager(
pluginType as PluginType
)
// Configure
await PluginInstance.configure(pluginManager, plugins[pluginType])
// Execute plugins
await PluginInstance.execute({
storageRunning: isRunning,
storageEngine: engine,
processConnectionsBetweenEntities,
})
}
}
} catch (error) {
this.logger.warn('Plugin not supported by CG')
}
}
}
}
// Indicates when schema has new changes
private schemaHasChange(oldSchema: string, newSchema: string): boolean {
return !!Buffer.compare(
Buffer.from(cleanString(oldSchema), 'utf8'),
Buffer.from(cleanString(newSchema), 'utf8')
)
}
async run(): Promise<void> {
const { argv, flags } = await this.parse(Scan)
const { dev: devMode } = flags as {
[flag: string]: any
}
const { dataDir } = this.config
const opts: Opts = { logger: this.logger, debug: true, devMode }
let allProviders = argv
// Run dgraph health check
const storageEngine = (await this.getStorageEngine()) as DgraphEngine
const storageRunning = await storageEngine.healthCheck()
/**
* Handle 2 methods of scanning, either for explicitly passed providers OR
* try to scan for all providers found within the config file
* if we still have 0 providers, fail and exit.
*/
if (allProviders.length >= 1) {
this.logger.debug(`Scanning for providers: ${allProviders}`)
} else {
this.logger.debug('Scanning for providers found in config')
const config = this.getCGConfig()
allProviders = Object.keys(config).filter(
(val: string) => val !== 'cloudGraph'
)
if (allProviders.length === 0) {
this.logger.error(
'There are no providers configured and none were passed to scan'
)
this.exit()
}
}
// Build folder structure for saving CloudGraph data by version
const schema: any[] = []
let folders = fileUtils.getVersionFolders(
path.join(dataDir, this.versionDirectory)
)
let dataFolder = 'version-1'
if (folders.length >= this.versionLimit) {
this.logger.warn(
`Maximum number of data versions has been reached, deleting version-1 and creating a new version-${this.versionLimit}`
)
// version 1 gets deleted, version 2 becomes version 1 … new version gets created
const pathPrefix = path.join(dataDir, this.versionDirectory)
const versionPrefix = path.join(pathPrefix, 'version-')
for (const version of [
1,
...range(this.versionLimit + 1, folders.length + 1),
]) {
fs.rmSync(versionPrefix + version, { recursive: true })
}
for (const version of range(1, this.versionLimit)) {
fs.renameSync(versionPrefix + (version + 1), versionPrefix + version)
}
folders = fileUtils.getVersionFolders(
path.join(dataDir, this.versionDirectory)
)
}
if (folders) {
dataFolder = `version-${folders.length + 1}`
}
const dataStorageLocation = path.join(
dataDir,
`${this.versionDirectory}/${dataFolder}`
)
fileUtils.makeDirIfNotExists(dataStorageLocation)
/**
* loop through providers and attempt to scan each of them
*/
const failedProviderList: string[] = []
for (const provider of allProviders) {
this.logger.info(
`Beginning ${chalk.italic.green('SCAN')} for ${provider}`
)
const providerPlugin = await this.getProviderClient(provider)
const { client: providerClient, schemasMap } = providerPlugin
if (!providerClient) {
failedProviderList.push(provider)
this.logger.warn(`No valid client found for ${provider}, skipping...`)
continue // eslint-disable-line no-continue
}
const config = this.getCGConfig(provider)
this.providers[provider] = providerPlugin
this.logger.debug(config)
if (!config) {
failedProviderList.push(provider)
this.logger.warn(
`No configuration found for ${provider}, run "cg init ${provider}" to create one`
)
continue // eslint-disable-line no-continue
}
this.logger.startSpinner(
`${chalk.italic.green('SCANNING')} data for ${chalk.italic.green(
provider
)}`
)
const providerData = await providerClient.getData({
opts,
})
this.logger.successSpinner(
`${chalk.italic.green(provider)} data scanned successfully`
)
// Handle schema, write provider and combined schema to file and store in Dgraph if running
this.logger.startSpinner(
`updating ${chalk.italic.green('Schema')} for ${chalk.italic.green(
provider
)}`
)
const providerSchema: string = providerClient.getSchema()
if (!providerSchema) {
this.logger.warn(`No schema found for ${provider}, moving on`)
continue // eslint-disable-line no-continue
}
schema.push(providerSchema)
fileUtils.writeGraphqlSchemaToFile(
dataStorageLocation,
providerSchema,
provider
)
if (allProviders.indexOf(provider) === allProviders.length - 1) {
fileUtils.writeGraphqlSchemaToFile(dataStorageLocation, schema.join())
if (storageRunning) {
try {
if (storageEngine instanceof DgraphEngine) {
await storageEngine.validateSchema(schema, dataFolder)
}
const currentSchema = getStoredSchema(dataDir)
// Only drops and changes data when the schema changes
if (this.schemaHasChange(currentSchema, schema.join())) {
await storageEngine.dropAll() // Delete schema before change it
await storageEngine.setSchema(schema, { overwrite: dataDir })
}
} catch (error: any) {
this.logger.error(
`There was an issue pushing schema for providers: ${allProviders.join(
' | '
)} to dgraph at ${storageEngine.host}`
)
this.logger.debug(error)
fileUtils.deleteFolder(dataStorageLocation)
this.exit()
}
}
}
this.logger.successSpinner(
`${chalk.italic.green(
'Schema'
)} loaded successfully for ${chalk.italic.green(provider)}`
)
try {
const dataPath = path.join(
dataStorageLocation,
`/${provider}_${Date.now()}.json`
)
fs.writeFileSync(dataPath, JSON.stringify(providerData, null, 2))
} catch (error: any) {
this.logger.error(`There was a problem saving data for ${provider}`)
this.logger.debug(error)
fileUtils.deleteFolder(dataStorageLocation)
this.exit()
}
loadAllData(
providerClient,
{
provider,
providerData,
storageEngine,
storageRunning,
schemaMap: schemasMap,
},
this.logger
)
}
// If every provider that has been passed is a failure, just exit
if (failedProviderList.length === allProviders.length) {
this.logger.warn(
`No providers in list: [${allProviders.join(
' | '
)}] have a valid module and config, exiting`
)
this.exit()
}
if (storageRunning) {
this.logger.startSpinner(
'Inserting data into Dgraph and generating scan report'
)
// Execute services mutations promises
await storageEngine.run()
this.logger.successSpinner('Data insertion into Dgraph complete')
await this.plugins({
flags: flags as { [flag: string]: any },
storage: {
isRunning: storageRunning,
engine: storageEngine,
},
})
}
scanReport.print()
this.logger.success(
`Your data for ${allProviders.join(
' | '
)} has been saved to ${chalk.italic.green(dataStorageLocation)}`
)
if (storageRunning) {
this.logger.success(
`Your data for ${allProviders.join(
' | '
)} has been saved to Dgraph. Query at ${chalk.underline.green(
`${storageEngine.host}/graphql`
)}`
)
}
storageRunning && (await this.startQueryEngine())
}
}