Commit 0611a9fc authored by Ahmad's avatar Ahmad

dsdsd

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