Commit 405f5e44 authored by Ahmad's avatar Ahmad

inikt

parent 0c742cc2
@echo off
title Running Node.js Create
echo Starting Node.js Create...
cd /d "%~dp0" // Ensures the script runs from the directory where the batch file is located
node create.js // Runs your Node.js application
pause // Keeps the command prompt open after the script finishes
const fs = require('fs');
const path = require('path');
const AWS = require('aws-sdk');
const { Client } = require('ssh2');
const axios = require('axios');
const downloadsPath = path.join(process.env.HOME || process.env.USERPROFILE, 'Downloads');
const rootKeyFilePath = path.join(downloadsPath, 'rootkey.csv');
// Check if the rootkey.csv file exists
if (!fs.existsSync(rootKeyFilePath)) {
console.error(`File not found: ${rootKeyFilePath}`);
process.exit(1);
}
// Function to read the second line and extract AWS credentials
function extractAWSCredentials(filePath) {
const fileContent = fs.readFileSync(filePath, 'utf8');
const lines = fileContent.split('\n');
if (lines.length < 2) {
throw new Error('The rootkey.csv file does not contain enough lines.');
}
const secondLine = lines[1].trim();
const [accessKeyId, secretAccessKey] = secondLine.split(',');
if (accessKeyId && secretAccessKey) {
return {
accessKeyId: accessKeyId.trim(),
secretAccessKey: secretAccessKey.trim(),
};
} else {
throw new Error('Failed to extract AWS credentials from the second line of rootkey.csv');
}
}
// Function to create Lightsail instances
async function createLightsailInstances(lightsail, region, zone, count) {
const instanceParams = {
blueprintId: 'ubuntu_20_04', // Ubuntu 20.04
bundleId: 'large_2_0', // $44 USD plan for Lightsail
availabilityZone: `${region}a`, // Availability zone (usually 'a', 'b', or 'c')
instanceNames: [], // Will be populated below
};
// Create instances with names Ubuntu-1, Ubuntu-2, etc.
for (let i = 1; i <= count; i++) {
instanceParams.instanceNames.push(`Ubuntu-${i}`);
}
try {
const result = await lightsail.createInstances(instanceParams).promise();
console.log(`Created ${count} instances in region ${region}, zone ${zone}:`, result);
} catch (error) {
console.error(`Failed to create instances in ${region}:`, error.message);
}
}
// Function to add delay
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function main() {
try {
// Extract credentials
const credentials = extractAWSCredentials(rootKeyFilePath);
console.log('AWS Credentials Loaded:', credentials);
// Set AWS config globally
AWS.config.update({
accessKeyId: credentials.accessKeyId,
secretAccessKey: credentials.secretAccessKey,
});
// Define the regions and zones to create instances in
const regionsAndZones = [
{ region: 'us-west-2', zone: 'Oregon', count: 2 },
{ region: 'us-east-2', zone: 'Ohio', count: 2 },
{ region: 'us-east-1', zone: 'Virginia', count: 2 },
];
// Loop through each region and zone to create instances
for (const { region, zone, count } of regionsAndZones) {
const lightsail = new AWS.Lightsail({ region });
await createLightsailInstances(lightsail, region, zone, count);
console.log(`Waiting 30 seconds before creating instances in the next region...`);
await sleep(30000); // Wait for 30 seconds
}
} catch (error) {
console.error('Error:', error.message);
}
}
main().catch(console.error);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment