From 9a01d27d8bcf1ed89349de48da97b40e1a557e63 Mon Sep 17 00:00:00 2001 From: Muhammad Ibrahim Date: Mon, 22 Sep 2025 02:30:12 +0100 Subject: [PATCH] Removed populate-agent-version.js as now agent is being imported if newer than existing upon service start --- agents/patchmon-agent.sh | 4 +- backend/Dockerfile | 2 +- backend/check-agent-version.js | 67 ------------------ backend/populate-agent-version.js | 114 ------------------------------ 4 files changed, 3 insertions(+), 184 deletions(-) delete mode 100644 backend/check-agent-version.js delete mode 100644 backend/populate-agent-version.js diff --git a/agents/patchmon-agent.sh b/agents/patchmon-agent.sh index a449ce8..f5aab46 100755 --- a/agents/patchmon-agent.sh +++ b/agents/patchmon-agent.sh @@ -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 "$@" \ No newline at end of file +main "$@" diff --git a/backend/Dockerfile b/backend/Dockerfile index 47e856f..7e7290d 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -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"] \ No newline at end of file +CMD ["sh", "-c", "sleep 10 && npx prisma migrate deploy && npm start"] \ No newline at end of file diff --git a/backend/check-agent-version.js b/backend/check-agent-version.js deleted file mode 100644 index c1ca32a..0000000 --- a/backend/check-agent-version.js +++ /dev/null @@ -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(); \ No newline at end of file diff --git a/backend/populate-agent-version.js b/backend/populate-agent-version.js deleted file mode 100644 index 1fbc051..0000000 --- a/backend/populate-agent-version.js +++ /dev/null @@ -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 }; \ No newline at end of file