EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
link-agent-to-meshcentral.js
Go to the documentation of this file.
1#!/usr/bin/env node
2
3/**
4 * Link an agent to a MeshCentral device by updating meshcentral_nodeid
5 * Usage: node link-agent-to-meshcentral.js <agent_uuid> <meshcentral_nodeid>
6 */
7
8const { Pool } = require('pg');
9require('dotenv').config();
10
11const pool = new Pool({
12 host: process.env.POSTGRES_HOST,
13 port: process.env.POSTGRES_PORT || 25060,
14 database: process.env.POSTGRES_DATABASE || 'defaultdb',
15 user: process.env.POSTGRES_USER,
16 password: process.env.POSTGRES_PASSWORD,
17 ssl: process.env.POSTGRES_SSL === 'true' ? { rejectUnauthorized: false } : false
18});
19
20/**
21 *
22 * @param agentUuid
23 * @param meshcentralNodeId
24 */
25async function linkAgent(agentUuid, meshcentralNodeId) {
26 try {
27 // Check if agent exists
28 const agentCheck = await pool.query(
29 'SELECT agent_id, agent_uuid, hostname, meshcentral_nodeid FROM agents WHERE agent_uuid = $1',
30 [agentUuid]
31 );
32
33 if (agentCheck.rows.length === 0) {
34 console.error(`āŒ Agent not found: ${agentUuid}`);
35 process.exit(1);
36 }
37
38 const agent = agentCheck.rows[0];
39 console.log(`\nšŸ“‹ Current Agent State:`);
40 console.log(` Agent ID: ${agent.agent_id}`);
41 console.log(` UUID: ${agent.agent_uuid}`);
42 console.log(` Hostname: ${agent.hostname}`);
43 console.log(` Current Node ID: ${agent.meshcentral_nodeid || '(none)'}`);
44
45 // Update the agent
46 const result = await pool.query(
47 `UPDATE agents
48 SET meshcentral_nodeid = $1,
49 meshcentral_connected = true,
50 meshcentral_last_seen = NOW()
51 WHERE agent_uuid = $2
52 RETURNING agent_id, hostname, meshcentral_nodeid`,
53 [meshcentralNodeId, agentUuid]
54 );
55
56 console.log(`\nāœ… Agent Updated:`);
57 console.log(` New Node ID: ${result.rows[0].meshcentral_nodeid}`);
58 console.log(`\nšŸŽ‰ Link successful! Agent ${agentUuid} is now connected to MeshCentral device.`);
59 console.log(`\nšŸ“ View agent at: https://everydaytech.au/agents/${agentUuid}`);
60
61 } catch (error) {
62 console.error('āŒ Error linking agent:', error.message);
63 console.error('Full error:', error);
64 process.exit(1);
65 } finally {
66 await pool.end();
67 }
68}
69
70// Parse command line arguments
71const args = process.argv.slice(2);
72
73if (args.length !== 2) {
74 console.error('Usage: node link-agent-to-meshcentral.js <agent_uuid> <meshcentral_nodeid>');
75 console.error('\nExample:');
76 console.error(' node link-agent-to-meshcentral.js 2c3e2d7a-c521-4844-b44b-a30b4c2ec4d8 "yfNgVgiQyrbYaDLpK1El3YM81NyK3nwX9ggSYdjc1LdFPB9rL$zq2EYaAaOzi0xW"');
77 process.exit(1);
78}
79
80const [agentUuid, meshcentralNodeId] = args;
81
82linkAgent(agentUuid, meshcentralNodeId);