Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | #!/usr/bin/env tsx
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { GetCallerIdentityCommand, STSClient } from "@aws-sdk/client-sts";
import type { Client } from "@opensearch-project/opensearch";
import { getClient } from "../lib/libs/opensearch-lib";
interface ExportOptions {
bucket: string;
domain: string;
batchSize?: number;
keyPrefix?: string;
}
interface ScrollResponse {
_scroll_id?: string;
hits: {
hits: Array<{ _source: unknown }>;
total: {
value: number;
relation: string;
};
};
}
const INDEXES_TO_EXPORT = [
"productionmain",
"productionchangelog",
"productiontypes",
"productionsubtypes",
"productioncpocs",
"productioninsights",
"productionlegacyinsights",
"productionusers",
"productionroles",
] as const;
class OpenSearchExporter {
private s3Client: S3Client;
private osClient: Client | null = null;
private domain: string;
private options: ExportOptions;
constructor(options: ExportOptions) {
this.options = options;
this.s3Client = new S3Client({});
this.domain = options.domain;
}
private async getOSClient(): Promise<Client> {
if (!this.osClient) {
this.osClient = await getClient(this.domain);
}
return this.osClient;
}
private async *scrollSearch(
indexName: string,
batchSize: number = 1000,
): AsyncGenerator<unknown[], void, unknown> {
const client = await this.getOSClient();
console.log(`Starting scroll search for index: ${indexName}`);
try {
// Initial search with scroll
const initialResponse = await client.search({
index: indexName,
scroll: "2m", // Keep scroll context for 2 minutes
size: batchSize,
body: {
query: { match_all: {} },
},
});
console.log(`OpenSearch response status: ${JSON.stringify(initialResponse.meta)}`);
// Check if response has expected structure
if (!initialResponse.body || !initialResponse.body.hits) {
console.error(
`Unexpected response structure for ${indexName}:`,
JSON.stringify(initialResponse, null, 2),
);
throw new Error(
`Invalid response structure - missing hits property for index ${indexName}`,
);
}
const responseBody = initialResponse.body as ScrollResponse;
let hits = responseBody.hits.hits;
let scrollId = responseBody._scroll_id;
let totalRetrieved = hits.length;
console.log(`Total documents in ${indexName}: ${responseBody.hits.total.value}`);
console.log(`Retrieved batch 1: ${hits.length} documents`);
if (hits.length > 0) {
yield hits.map((hit) => hit._source);
}
// Continue scrolling while there are more results
while (hits.length > 0 && scrollId) {
const scrollResponse = await client.scroll({
scroll_id: scrollId,
scroll: "2m",
});
if (!scrollResponse.body || !scrollResponse.body.hits) {
console.error(
`Unexpected scroll response structure for ${indexName}:`,
JSON.stringify(scrollResponse, null, 2),
);
break;
}
const scrollBody = scrollResponse.body as ScrollResponse;
hits = scrollBody.hits.hits;
scrollId = scrollBody._scroll_id;
totalRetrieved += hits.length;
if (hits.length > 0) {
console.log(`Retrieved batch: ${hits.length} documents (total: ${totalRetrieved})`);
yield hits.map((hit) => hit._source);
}
}
// Clear scroll context
if (scrollId) {
try {
await client.clearScroll({
scroll_id: scrollId,
});
} catch (error) {
console.warn(`Warning: Failed to clear scroll context: ${error}`);
}
}
console.log(`Completed scrolling ${indexName}. Total documents retrieved: ${totalRetrieved}`);
} catch (error) {
console.error(`Error in scroll search for ${indexName}:`, error);
if (error instanceof Error && "response" in error) {
console.error(
`OpenSearch error response:`,
JSON.stringify((error as any).response, null, 2),
);
}
throw error;
}
}
private async exportIndexToS3(indexName: string): Promise<void> {
console.log(`\n=== Exporting ${indexName} ===`);
const allDocuments: unknown[] = [];
try {
const batchSize = this.options.batchSize || (indexName === "productionmain" ? 1000 : 5000);
for await (const batch of this.scrollSearch(indexName, batchSize)) {
allDocuments.push(...batch);
}
console.log(`Total documents collected for ${indexName}: ${allDocuments.length}`);
// Upload to S3
const s3Key = this.options.keyPrefix
? `${this.options.keyPrefix}/${indexName}.json`
: `${indexName}.json`;
console.log(`Uploading ${indexName}.json to S3...`);
const putCommand = new PutObjectCommand({
Bucket: this.options.bucket,
Key: s3Key,
Body: JSON.stringify(allDocuments, null, 2),
ContentType: "application/json",
Metadata: {
sourceIndex: indexName,
exportedAt: new Date().toISOString(),
documentCount: allDocuments.length.toString(),
},
});
await this.s3Client.send(putCommand);
console.log(`✅ Successfully exported ${indexName} to s3://${this.options.bucket}/${s3Key}`);
console.log(` Documents: ${allDocuments.length}`);
} catch (error) {
console.error(`❌ Failed to export ${indexName}:`, error);
throw error;
}
}
async exportAll(): Promise<void> {
// Verify AWS credentials and S3 access
console.log("Verifying AWS credentials...");
const stsClient = new STSClient({});
const identity = await stsClient.send(new GetCallerIdentityCommand({}));
console.log(`✅ Authenticated as: ${identity.Arn}`);
console.log(`Starting OpenSearch export to S3 bucket: ${this.options.bucket}`);
console.log(`OpenSearch domain: ${this.domain}`);
const startTime = Date.now();
let successCount = 0;
let errorCount = 0;
for (const indexName of INDEXES_TO_EXPORT) {
try {
await this.exportIndexToS3(indexName);
successCount++;
} catch (error) {
console.error(`Failed to export ${indexName}:`, error);
errorCount++;
}
}
const duration = (Date.now() - startTime) / 1000;
console.log(`\n=== Export Summary ===`);
console.log(`Total time: ${duration.toFixed(2)} seconds`);
console.log(`Successful exports: ${successCount}`);
console.log(`Failed exports: ${errorCount}`);
console.log(`S3 bucket: ${this.options.bucket}`);
if (errorCount > 0) {
process.exit(1);
}
}
}
function parseArgs(argv: string[]): ExportOptions {
let bucket = "";
let domain = "";
let batchSize: number | undefined;
let keyPrefix: string | undefined;
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
switch (arg) {
case "-b":
case "--bucket":
bucket = argv[index + 1] || "";
index += 1;
break;
case "-d":
case "--domain":
domain = argv[index + 1] || "";
index += 1;
break;
case "--batch-size": {
const parsed = Number.parseInt(argv[index + 1] || "", 10);
if (Number.isFinite(parsed) && parsed > 0) {
batchSize = parsed;
}
index += 1;
break;
}
case "--key-prefix":
keyPrefix = argv[index + 1];
index += 1;
break;
case "--help":
case "-h":
printUsage();
process.exit(0);
break;
default:
break;
}
}
if (!bucket) {
console.error("Error: S3 bucket name is required");
printUsage();
process.exit(1);
}
if (!domain) {
console.error("Error: OpenSearch domain URL is required");
printUsage();
process.exit(1);
}
return {
bucket,
domain,
batchSize,
keyPrefix: keyPrefix || `opensearch-exports/${new Date().toISOString().split("T")[0]}`,
};
}
function printUsage() {
console.log(`
Usage: tsx scripts/export-index.ts [OPTIONS]
Export OpenSearch production indexes to S3 as JSON files
OPTIONS:
-b, --bucket <bucket> S3 bucket name for export (required)
-d, --domain <domain> OpenSearch domain URL (required)
--batch-size <size> Batch size for scrolling (default: 1000 for main, 5000 for others)
--key-prefix <prefix> S3 key prefix for exported files
-h, --help Show this help message
EXAMPLES:
tsx scripts/export-index.ts --bucket my-export-bucket --domain https://vpc-domain.us-east-1.es.amazonaws.com
tsx scripts/export-index.ts --bucket my-bucket --domain https://vpc-domain.us-east-1.es.amazonaws.com --key-prefix opensearch-exports/2024-04-28
`);
}
async function main(): Promise<void> {
try {
// Check if running in AWS CloudShell environment
if (
!process.env.AWS_EXECUTION_ENV &&
!process.env.AWS_REGION &&
!process.env.AWS_DEFAULT_REGION
) {
console.warn(
"⚠️ Warning: This script is designed to run in AWS CloudShell with VPC connectivity to OpenSearch.",
);
console.warn(" If you're not in CloudShell, make sure you have:");
console.warn(" - Proper AWS credentials configured");
console.warn(" - Network access to the OpenSearch domain");
console.warn(" - Required IAM permissions");
console.warn("");
}
const options = parseArgs(process.argv.slice(2));
const exporter = new OpenSearchExporter(options);
await exporter.exportAll();
} catch (error) {
console.error("Export failed:", error);
process.exit(1);
}
}
// Handle graceful shutdown
process.on("SIGINT", () => {
console.log("\n⚠️ Export interrupted by user");
process.exit(130);
});
process.on("uncaughtException", (error) => {
console.error("Uncaught exception:", error);
process.exit(1);
});
process.on("unhandledRejection", (reason, promise) => {
console.error("Unhandled rejection at:", promise, "reason:", reason);
process.exit(1);
});
main().catch((error) => {
console.error("Export failed:", error);
process.exit(1);
});
|