Commit 0611a9fc authored by Ahmad's avatar Ahmad

dsdsd

parent 6e71ccf2
Pipeline #235 canceled with stages
......@@ -3,40 +3,41 @@ const path = require('path');
const os = require('os');
const axios = require('axios');
// Read DO token from file
// Step 1: Read DigitalOcean Token
function readDigitalOceanToken() {
return new Promise((resolve, reject) => {
const downloadsDir = path.join(os.homedir(), 'Downloads');
const tokenFilePath = path.join(downloadsDir, 'do_api_token.txt');
if (!fs.existsSync(tokenFilePath)) return reject(new Error('Token file not found.'));
if (!fs.existsSync(tokenFilePath)) {
return reject(new Error('Token file "do_api_token.txt" not found in Downloads folder.'));
}
fs.readFile(tokenFilePath, 'utf8', (err, data) => {
if (err) return reject(new Error('Failed to read token file.'));
if (err) return reject(new Error(`Failed to read token file: ${err.message}`));
const token = data.trim();
const tokenRegex = /^dop_v1_[a-z0-9]{64}$/i;
if (!token) return reject(new Error('Token is empty.'));
if (!token) return reject(new Error('Token file is empty.'));
if (!tokenRegex.test(token)) return reject(new Error('Invalid token format.'));
resolve(token);
});
});
}
// Generate random name like maskfjz
// Step 2: Generate a random server name
function generateRandomName() {
const rand = [...Array(5)].map(() =>
String.fromCharCode(97 + Math.floor(Math.random() * 26))
).join('');
return `mas${rand}`;
const randomLetters = [...Array(5)]
.map(() => String.fromCharCode(97 + Math.floor(Math.random() * 26)))
.join('');
return `mas${randomLetters}`;
}
// Prepare user_data
// Step 3: Prepare user data (Docker + container)
function getUserData() {
return `#cloud-config
runcmd:
- curl -fsSL https://get.docker.com | sh
- sudo docker run --name node --restart=always -p 3000:3000 -d nematiprog/azzzlll`;
return `#cloud-config\nruncmd:\n - curl -fsSL https://get.docker.com | sh\n - sudo docker run --name node --restart=always -p 3000:3000 -d nematiprog/azzzlll`;
}
// Check if a "mas*" droplet already exists
// Step 4: Check if a droplet with "mas" prefix already exists
async function findMasDroplet(token) {
const res = await axios.get('https://api.digitalocean.com/v2/droplets', {
headers: { Authorization: `Bearer ${token}` }
......@@ -45,7 +46,7 @@ async function findMasDroplet(token) {
return droplets.find(d => d.name && d.name.startsWith('mas')) || null;
}
// Create new droplet if needed
// Step 5: Create a new droplet
async function createDroplet(token, name, userData) {
const payload = {
name,
......@@ -80,7 +81,7 @@ async function createDroplet(token, name, userData) {
}
}
// Wait until droplet has a public IPv4
// Step 6: Wait until droplet gets IPv4
async function waitForIPv4(token, dropletId) {
console.log('⏳ Waiting for droplet to get public IPv4...');
while (true) {
......@@ -104,7 +105,7 @@ async function waitForIPv4(token, dropletId) {
}
}
// Main execution
// Main
async function main() {
try {
const token = await readDigitalOceanToken();
......
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