-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfra.js
More file actions
306 lines (271 loc) · 10.1 KB
/
infra.js
File metadata and controls
306 lines (271 loc) · 10.1 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
const { execSync } = require('child_process');
const os = require('os');
const net = require('net');
const { DynamoDBClient, CreateTableCommand } = require("@aws-sdk/client-dynamodb");
const { S3Client, CreateBucketCommand, PutBucketVersioningCommand, PutBucketCorsCommand, PutBucketPolicyCommand,
ListBucketsCommand } = require('@aws-sdk/client-s3');
const reset = "\x1b[0m";
const bold = "\x1b[1m";
const red = "\x1b[31m";
const yellow = "\x1b[33m";
const green = "\x1b[32m";
// Wrap figlet in a promise
const generateAsciiArt = async (text, figlet) => {
return new Promise((resolve, reject) => {
figlet(text, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
};
console.red = (data) => console.log(`${red}${data}${reset}`)
console.green = (data) => console.log(`${green}${bold}${data}${reset}`)
console.yellow = (data) => console.log(`${yellow}${bold}${data}${reset}`)
// ... (keep your console color functions and other utility functions as they are)
const CreateInfra = async (endpoint) => {
console.log('Create local infra')
const dynamodb = new DynamoDBClient({
region: 'us-east-1',
endpoint
});
try {
await dynamodb.send(new CreateTableCommand({
TableName: 'openkb-kb-chats',
AttributeDefinitions: [
{ AttributeName: 'chatId', AttributeType: 'S' },
{ AttributeName: 'kbId', AttributeType: 'S' },
{ AttributeName: 'updatedAt', AttributeType: 'N' }
],
KeySchema: [
{ AttributeName: 'kbId', KeyType: 'HASH' },
{ AttributeName: 'chatId', KeyType: 'RANGE' }
],
BillingMode: 'PAY_PER_REQUEST',
StreamSpecification: {
StreamEnabled: true,
StreamViewType: 'NEW_AND_OLD_IMAGES'
},
GlobalSecondaryIndexes: [
{
IndexName: 'kbId-updatedAt-index',
KeySchema: [
{ AttributeName: 'kbId', KeyType: 'HASH' },
{ AttributeName: 'updatedAt', KeyType: 'RANGE' }
],
Projection: {
ProjectionType: 'ALL'
}
}
],
DeletionProtectionEnabled: true
}));
console.green('Table created successfully!');
} catch (error) {
console.log('openkb-kb-chats already created')
}
try {
await dynamodb.send(new CreateTableCommand({
TableName: 'openkb-chat-messages',
AttributeDefinitions: [
{ AttributeName: 'chatId', AttributeType: 'S' },
{ AttributeName: 'msgId', AttributeType: 'S' }
],
KeySchema: [
{ AttributeName: 'chatId', KeyType: 'HASH' },
{ AttributeName: 'msgId', KeyType: 'RANGE' }
],
BillingMode: 'PAY_PER_REQUEST',
StreamSpecification: {
StreamEnabled: true,
StreamViewType: 'NEW_AND_OLD_IMAGES'
},
DeletionProtectionEnabled: true
}));
console.green('Table created successfully!');
} catch (error) {
console.log('openkb-chat-message already created')
}
try {
await dynamodb.send(new CreateTableCommand({
TableName: 'openkb-unread-messages',
AttributeDefinitions: [
{ AttributeName: 'chatId', AttributeType: 'S' },
{ AttributeName: 'userId_kbId', AttributeType: 'S' }
],
KeySchema: [
{ AttributeName: 'userId_kbId', KeyType: 'HASH' },
{ AttributeName: 'chatId', KeyType: 'RANGE' }
],
BillingMode: 'PAY_PER_REQUEST',
DeletionProtectionEnabled: true
}));
} catch (error) {
console.log('openkb-unread-messages already created')
}
try {
await dynamodb.send(new CreateTableCommand({
TableName: 'openkb-kb-apikey',
AttributeDefinitions: [
{ AttributeName: 'apiKey', AttributeType: 'S' },
{ AttributeName: 'kbId', AttributeType: 'S' }
],
KeySchema: [
{ AttributeName: 'kbId', KeyType: 'HASH' },
{ AttributeName: 'apiKey', KeyType: 'RANGE' }
],
BillingMode: 'PAY_PER_REQUEST',
DeletionProtectionEnabled: true
}));
} catch (error) {
console.log('openkb-kb-apikey already created')
}
/**
*
* S 3 B U C K E T S
*
*/
const s3Client = new S3Client({
region: 'us-east-1',
endpoint: 'http://localhost:4566', // LocalStack endpoint
forcePathStyle: true // Required for LocalStack
});
const bucketParams = {
Bucket: 'openkbs-files',
ACL: 'private' // You can set the ACL as per your requirement
};
const versioningParams = {
Bucket: 'openkbs-files',
VersioningConfiguration: {
Status: 'Enabled'
}
};
const corsParams = {
Bucket: 'openkbs-files',
CORSConfiguration: {
CORSRules: [
{
AllowedHeaders: ['*'],
AllowedMethods: ['GET', 'PUT', 'POST', 'DELETE'],
AllowedOrigins: ['*'],
ExposeHeaders: []
}
]
}
};
const policyParams = {
Bucket: 'openkbs-files',
Policy: JSON.stringify({
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Principal: '*',
Action: 's3:GetObject',
Resource: [
'arn:aws:s3:::openkbs-files/files/*',
'arn:aws:s3:::openkbs-files/share-chat/*',
'arn:aws:s3:::openkbs-files/frontend/*',
'arn:aws:s3:::openkbs-files/download/*'
]
}
]
})
};
try {
// Create the bucket
await s3Client.send(new CreateBucketCommand(bucketParams));
// Enable versioning
await s3Client.send(new PutBucketVersioningCommand(versioningParams));
// Set CORS configuration
await s3Client.send(new PutBucketCorsCommand(corsParams));
// Set bucket policy
await s3Client.send(new PutBucketPolicyCommand(policyParams));
const buckets = await s3Client.send(new ListBucketsCommand({}));
console.log('Buckets created and configured successfully')
} catch (error) {
console.log(error)
}
}
function isLocalstackInstalled() {
try {
// Check if 'localstack' command is available
execSync('localstack --version', { stdio: 'ignore' });
return true;
} catch (err) {
return false;
}
}
function isLocalstackRunning() {
return new Promise((resolve) => {
// Try connecting to the default LocalStack port
const client = net.connect({ port: 4566 }, () => {
client.end();
resolve(true);
});
client.on('error', () => {
resolve(false);
});
});
}
function printInstallationInstructions() {
console.red('LocalStack is not installed.\n');
console.red('Please install LocalStack by following the instructions for your operating system and try again:\n');
const platform = os.platform();
if (platform === 'linux') {
console.green('**For Linux:**\n');
console.green('```');
console.green('curl --output localstack-cli-3.7.2-linux-amd64-onefile.tar.gz \\');
console.green(' --location https://github.com/localstack/localstack-cli/releases/download/v3.7.2/localstack-cli-3.7.2-linux-amd64-onefile.tar.gz');
console.green('sudo tar xvzf localstack-cli-3.7.2-linux-*-onefile.tar.gz -C /usr/local/bin');
console.green(`sudo PERSISTENCE=1 localstack start -d`)
console.green('```');
} else if (platform === 'darwin') {
console.green('**For macOS:**\n');
console.green('```');
console.green('brew install localstack/tap/localstack-cli');
console.green(`sudo PERSISTENCE=1 localstack start -d`)
console.green('```');
} else if (platform === 'win32') {
console.green('**For Windows:**\n');
console.green('Please download and install from the following URL:');
console.green('https://github.com/localstack/localstack-cli/releases/download/v3.7.2/localstack-cli-3.7.2-windows-amd64-onefile.zip');
console.green(`sudo PERSISTENCE=1 localstack start -d`)
} else {
console.log('Unsupported OS. Please refer to the LocalStack installation documentation.');
}
console.log('\n**Note:** Some commands may require elevated permissions (e.g., `sudo`).');
}
async function main() {
const figlet = (await import('figlet')).default;
const chalk = (await import('chalk')).default;
const printRunning = async () => {
console.green('\n');
const asciiArt = await generateAsciiArt('OpenKBS', figlet);
console.log(chalk.blue(asciiArt));
console.log(chalk.blue(` Chat Server`));
}
if (process.env.LOCAL_STACK_REQUIRED) {
if (!isLocalstackInstalled()) {
printInstallationInstructions();
// console.log('\nContinuing without LocalStack...\n');
process.exit(-1)
}
const running = await isLocalstackRunning();
if (running) {
await CreateInfra('http://localhost:4566');
await printRunning();
} else {
console.red('LocalStack is NOT running, start it and try again.\n\n');
console.green('sudo PERSISTENCE=1 localstack start -d')
process.exit(-1)
}
} else if (process.env.AWS_REQUIRED) {
await printRunning();
} else if (process.env.AWS_CREATE_INFRA) {
await CreateInfra(undefined);
}
}
main();