forked from nasa-gcn/architect-plugin-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
122 lines (110 loc) · 3.48 KB
/
index.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
/*!
* Copyright © 2023 United States Government as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
import { exists } from './paths'
import { join } from 'path'
import { pathToFileURL } from 'url'
import { launch } from './run.js'
import { populate } from './data.js'
import { search as getSearchClient } from '@nasa-gcn/architect-functions-search'
import {
cloudformationResources as serverlessCloudformationResources,
services as serverlessServices,
} from './serverless.js'
import {
cloudformationResources as serviceCloudformationResources,
services as serviceServices,
} from './service.js'
/**
* Convert a string to a suitable name for an OpenSearch Serverless collection.
*
* See valid name criteria at
* https://docs.aws.amazon.com/opensearch-service/latest/developerguide/serverless-manage.html#serverless-create
*/
function toCollectionName(name: string) {
return name
.toLowerCase()
.replace(/[^a-z0-9-]/g, '')
.padEnd(3, '-')
.slice(0, 32)
}
function getConfig(arc: {
search?: string[][]
}): Record<string, string | undefined> {
if (arc.search) return Object.fromEntries(arc.search)
else return {}
}
const searchApiFile = 'postdeploy-search.js'
async function executeSearchRequests(cwd: string) {
//Load api call file and run all api calls to cluster
const apiPath = join(cwd, searchApiFile)
if (await exists(apiPath)) {
console.log(`Found ${searchApiFile} file, running it...`)
let result = (await import(pathToFileURL(apiPath).toString())).default
const client = await getSearchClient()
// result should be a function that returns a promise
if (typeof result === 'function') {
result = result(client)
}
await result
}
}
export const deploy = {
// @ts-expect-error: The Architect plugins API has no type definitions.
start({ cloudformation, inventory, arc, stage }) {
let resources
const config = getConfig(arc)
if (config.availabilityZoneCount) {
resources = serviceCloudformationResources(config)
} else {
const { app } = inventory.inv
const collectionName = toCollectionName(`${app}-${stage}`)
resources = serverlessCloudformationResources(collectionName)
}
Object.assign(cloudformation.Resources, resources)
return cloudformation
},
// @ts-expect-error: The Architect plugins API has no type definitions.
services({ stage, arc }) {
if (stage !== 'production') {
return { node: 'http://localhost:9200' }
} else if (arc.search) {
return serviceServices
} else {
return serverlessServices
}
},
// @ts-expect-error: The Architect plugins API has no type definitions.
async end({ inventory }) {
executeSearchRequests(inventory.inv._project.cwd)
},
}
let local: Awaited<ReturnType<typeof launch>>
function getEngine(name?: string) {
if (name?.toLowerCase() === 'opensearch') return 'opensearch'
else return 'elasticsearch'
}
export const sandbox = {
async start({
// @ts-expect-error: The Architect plugins API has no type definitions.
arc,
inventory: {
inv: {
// @ts-expect-error: The Architect plugins API has no type definitions.
_project: { cwd },
},
},
}) {
const engine = getEngine(getConfig(arc).sandboxEngine)
local = await launch({ engine })
await executeSearchRequests(cwd)
await populate(cwd, { node: local.url })
},
async end() {
await local.stop()
},
}