Removed populate-agent-version.js as now agent is being imported if newer than existing upon service start

This commit is contained in:
Muhammad Ibrahim
2025-09-22 02:30:12 +01:00
parent d72f96b598
commit 9a01d27d8b
4 changed files with 3 additions and 184 deletions

View File

@@ -6,7 +6,7 @@
# Configuration
PATCHMON_SERVER="${PATCHMON_SERVER:-http://localhost:3001}"
API_VERSION="v1"
AGENT_VERSION="1.2.7"
AGENT_VERSION="1.2.6"
CONFIG_FILE="/etc/patchmon/agent.conf"
CREDENTIALS_FILE="/etc/patchmon/credentials"
LOG_FILE="/var/log/patchmon-agent.log"
@@ -1219,4 +1219,4 @@ main() {
}
# Run main function
main "$@"
main "$@"

View File

@@ -19,4 +19,4 @@ RUN mkdir -p logs
EXPOSE 3001
CMD ["sh", "-c", "sleep 10 && npx prisma migrate deploy && node populate-agent-version.js && npm start"]
CMD ["sh", "-c", "sleep 10 && npx prisma migrate deploy && npm start"]

View File

@@ -1,67 +0,0 @@
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
async function checkAgentVersion() {
try {
// Check current agent version in database
const agentVersion = await prisma.agent_versions.findFirst({
where: { version: '1.2.6' }
});
if (agentVersion) {
console.log('✅ Agent version 1.2.6 found in database');
console.log('Version:', agentVersion.version);
console.log('Is Default:', agentVersion.is_default);
console.log('Script Content Length:', agentVersion.script_content?.length || 0);
console.log('Created At:', agentVersion.created_at);
console.log('Updated At:', agentVersion.updated_at);
// Check if script content contains the current version
if (agentVersion.script_content && agentVersion.script_content.includes('AGENT_VERSION="1.2.6"')) {
console.log('✅ Script content contains correct version 1.2.6');
} else {
console.log('❌ Script content does not contain version 1.2.6');
}
// Check if script content contains system info functions
if (agentVersion.script_content && agentVersion.script_content.includes('get_hardware_info()')) {
console.log('✅ Script content contains hardware info function');
} else {
console.log('❌ Script content missing hardware info function');
}
if (agentVersion.script_content && agentVersion.script_content.includes('get_network_info()')) {
console.log('✅ Script content contains network info function');
} else {
console.log('❌ Script content missing network info function');
}
if (agentVersion.script_content && agentVersion.script_content.includes('get_system_info()')) {
console.log('✅ Script content contains system info function');
} else {
console.log('❌ Script content missing system info function');
}
} else {
console.log('❌ Agent version 1.2.6 not found in database');
}
// List all agent versions
console.log('\n=== All Agent Versions ===');
const allVersions = await prisma.agent_versions.findMany({
orderBy: { created_at: 'desc' }
});
allVersions.forEach(version => {
console.log(`Version: ${version.version}, Default: ${version.is_default}, Length: ${version.script_content?.length || 0}`);
});
} catch (error) {
console.error('❌ Error checking agent version:', error);
} finally {
await prisma.$disconnect();
}
}
checkAgentVersion();

View File

@@ -1,114 +0,0 @@
const fs = require('fs');
const path = require('path');
const { v4: uuidv4 } = require('uuid');
require('dotenv').config();
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
function compareVersions(version1, version2) {
const v1parts = version1.split('.').map(Number);
const v2parts = version2.split('.').map(Number);
for (let i = 0; i < Math.max(v1parts.length, v2parts.length); i++) {
const v1part = v1parts[i] || 0;
const v2part = v2parts[i] || 0;
if (v1part > v2part) return 1;
if (v1part < v2part) return -1;
}
return 0;
}
async function populateAgentVersion() {
try {
const agentScriptPath = path.join(__dirname, 'agents', 'patchmon-agent.sh');
if (!fs.existsSync(agentScriptPath)) {
console.log('Agent script not found at:', agentScriptPath);
console.log('Skipping agent version population - this is expected if agents folder is not mounted');
return;
}
const agentScript = fs.readFileSync(agentScriptPath, 'utf8');
const versionMatch = agentScript.match(/^AGENT_VERSION="([^"]*)"/m);
const currentVersion = versionMatch ? versionMatch[1] : '1.2.5';
console.log('Populating agent version:', currentVersion);
const existingVersion = await prisma.agent_versions.findUnique({
where: { version: currentVersion }
});
if (existingVersion) {
console.log('Updating existing agent version', currentVersion, 'with latest script content...');
await prisma.agent_versions.update({
where: { version: currentVersion },
data: {
script_content: agentScript,
is_current: true,
release_notes: `Version ${currentVersion} - Updated Agent Script\n\nThis version contains the latest agent script from the Docker container initialization.`,
download_url: `/api/v1/hosts/agent/download?version=${currentVersion}`,
updated_at: new Date()
}
});
console.log('Agent version', currentVersion, 'updated successfully');
} else {
console.log('Creating new agent version', currentVersion, '...');
await prisma.agent_versions.create({
data: {
id: uuidv4(),
version: currentVersion,
script_content: agentScript,
is_current: true,
is_default: true,
release_notes: `Version ${currentVersion} - Docker Agent Script\n\nThis version contains the agent script from the Docker container initialization.`,
download_url: `/api/v1/hosts/agent/download?version=${currentVersion}`,
min_server_version: '1.2.0',
updated_at: new Date()
}
});
console.log('Agent version', currentVersion, 'created successfully');
}
await prisma.agent_versions.updateMany({
where: { version: { not: currentVersion } },
data: {
is_current: false,
updated_at: new Date()
}
});
const allVersions = await prisma.agent_versions.findMany({
orderBy: { version: 'desc' }
});
for (const version of allVersions) {
if (version.version !== currentVersion && compareVersions(currentVersion, version.version) > 0) {
console.log('🔄 Updating older version', version.version, 'with new script content...');
await prisma.agent_versions.update({
where: { id: version.id },
data: {
script_content: agentScript,
release_notes: `Version ${version.version} - Updated with latest script from ${currentVersion}\n\nThis version has been updated with the latest agent script content.`,
updated_at: new Date()
}
});
}
}
console.log('Agent version population completed successfully');
} catch (error) {
console.error('Error populating agent version:', error.message);
process.exit(1);
} finally {
await prisma.$disconnect();
}
}
if (require.main === module) {
populateAgentVersion();
}
module.exports = { populateAgentVersion };