forked from Circuit-Overtime/elixpo_ai_chapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_update.js
99 lines (75 loc) · 2.85 KB
/
server_update.js
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
import { execSync, exec } from 'child_process';
import fetch from 'node-fetch';
import fs from 'fs';
import path from 'path';
import simpleGit from 'simple-git';
import admin from 'firebase-admin';
//importing the service account from firebase is needed to interact with the service
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://elixpoai-default-rtdb.firebaseio.com/'
});
const db = admin.firestore();
const ngrokConfigPath = './ngrok.yml';
const serverJsonPath = path.join(process.cwd(), 'server.json');
const isInternetAvailable = async () => {
try {
const response = await fetch('https://www.google.com', { method: 'HEAD' });
return response.ok;
} catch (error) {
return false;
}
};
const startNgrok = () => {
const command = `ngrok start --config=${ngrokConfigPath} --all`;
const tunnelStart = `bash /home/pi/Desktop/e;lixpo_ai_chapter/server.sh`;
exec(command, (error) => {
if (error) {
console.error(`Error starting ngrok: ${error.message}`);
}
});
exec(tunnelStart, (error) => {
if (error) {
console.error(`Error starting ngrok tunnel script: ${error.message}`);
}
});
};
const getNgrokUrl = async (port) => {
const response = await fetch('http://127.0.0.1:4040/api/tunnels');
const data = await response.json();
const tunnel = data.tunnels.find(tunnel => tunnel.config.addr.endsWith(`:${port}`));
return tunnel ? tunnel.public_url : null;
};
const updateServerUrls = async () => {
await new Promise(resolve => setTimeout(resolve, 5000));
const server1Url = await getNgrokUrl(3001); //node
console.log(`image and ping URL: ${server1Url}`);
const serverJson = JSON.parse(fs.readFileSync(serverJsonPath, 'utf-8'));
serverJson.servers.server1 = server1Url; //get image and ping
fs.writeFileSync(serverJsonPath, JSON.stringify(serverJson, null, 2));
console.log('Updated server.json');
console.log('Updating Firebase collection...');
const serverRef = db.collection('Server').doc('servers');
await serverRef.update({
download_image: server1Url,
get_ping: server1Url,
});
console.log('Firebase collection updated successfully!');
};
const main = async () => {
console.log('Checking internet connection...');
let internetAvailable = false;
while (!internetAvailable) {
internetAvailable = await isInternetAvailable();
if (!internetAvailable) {
console.log('No internet connection. Retrying in 5 seconds...');
await new Promise(resolve => setTimeout(resolve, 5000));
}
}
console.log('Internet connection established.');
startNgrok();
await updateServerUrls().catch(error => {
console.error('Error updating server URLs:', error);
});
};
main();