const { checkLogin } = require("../util-server"); const { AWSAccount } = require("../aws"); const { sendAwsAccountList } = require("../client"); const { log } = require("../../src/util"); /** * Socket handlers for AWS EC2 and CloudWatch resource monitoring * @param {Socket} socket Socket.io instance * @returns {void} */ module.exports.awsSocketHandler = (socket) => { // Save or update an AWS account socket.on("saveAwsAccount", async (account, accountID, callback) => { try { checkLogin(socket); const saved = await AWSAccount.save(account, accountID, socket.userID); await sendAwsAccountList(socket); if (typeof callback === "function") { callback({ ok: true, msg: "Saved.", msgi18n: true, account: saved, }); } } catch (e) { log.error("aws", `Failed to save AWS account: ${e.message}`); if (typeof callback === "function") { callback({ ok: false, msg: e.message, }); } } }); // Delete an AWS account socket.on("deleteAwsAccount", async (accountID, callback) => { try { checkLogin(socket); await AWSAccount.delete(accountID); await sendAwsAccountList(socket); if (typeof callback === "function") { callback({ ok: true, msg: "successDeleted", msgi18n: true, }); } } catch (e) { log.error("aws", `Failed to delete AWS account: ${e.message}`); if (typeof callback === "function") { callback({ ok: false, msg: e.message, }); } } }); // Test AWS connection socket.on("testAwsAccount", async (account, callback) => { try { checkLogin(socket); // If account has an ID and masked secret, fill in the actual secret from DB if (account.id && (!account.secretAccessKey || account.secretAccessKey.startsWith("••••"))) { const raw = await AWSAccount.getAccountRaw(account.id); account.secret_access_key = raw.secret_access_key; account.access_key_id = raw.access_key_id; account.session_token = raw.session_token; } const result = await AWSAccount.testConnection(account, account.region); if (typeof callback === "function") { callback({ ok: true, ...result, }); } } catch (e) { if (typeof callback === "function") { callback({ ok: false, msg: e.message, }); } } }); // Fetch EC2 instances socket.on("getEc2Instances", async (accountID, region, callback) => { try { checkLogin(socket); const rawAccount = await AWSAccount.getAccountRaw(accountID); const instances = await AWSAccount.getEc2Instances(rawAccount, region); if (typeof callback === "function") { callback({ ok: true, instances, }); } } catch (e) { log.error("aws", `Failed to get EC2 instances: ${e.message}`); if (typeof callback === "function") { callback({ ok: false, msg: e.message, }); } } }); // Fetch live metrics (CPU, Memory, Network, Status) for specified instances socket.on("getEc2LiveMetrics", async (accountID, region, instanceIds, callback) => { try { checkLogin(socket); const rawAccount = await AWSAccount.getAccountRaw(accountID); const metrics = await AWSAccount.getLiveInstanceMetrics(rawAccount, region, instanceIds); if (typeof callback === "function") { callback({ ok: true, metrics, }); } } catch (e) { log.error("aws", `Failed to get live metrics: ${e.message}`); if (typeof callback === "function") { callback({ ok: false, msg: e.message, }); } } }); // Fetch historical metric series for an instance deep-dive chart socket.on("getEc2InstanceHistory", async (accountID, region, instanceId, hours, callback) => { try { checkLogin(socket); const rawAccount = await AWSAccount.getAccountRaw(accountID); const history = await AWSAccount.getInstanceHistory(rawAccount, region, instanceId, hours); if (typeof callback === "function") { callback({ ok: true, history, }); } } catch (e) { log.error("aws", `Failed to get instance history: ${e.message}`); if (typeof callback === "function") { callback({ ok: false, msg: e.message, }); } } }); };