mirror of
https://github.com/9technologygroup/patchmon.net.git
synced 2025-11-14 10:55:43 +00:00
Refactor agent version handling to use consistent naming and add download URL
This commit is contained in:
@@ -1,102 +0,0 @@
|
||||
const { PrismaClient } = require('@prisma/client');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function addAgentVersion() {
|
||||
try {
|
||||
console.log('🚀 Adding agent version to database...');
|
||||
|
||||
// Read the agent script file
|
||||
const agentScriptPath = path.join(__dirname, '..', 'agents', 'patchmon-agent.sh');
|
||||
|
||||
if (!fs.existsSync(agentScriptPath)) {
|
||||
throw new Error(`Agent script not found at: ${agentScriptPath}`);
|
||||
}
|
||||
|
||||
const scriptContent = fs.readFileSync(agentScriptPath, 'utf8');
|
||||
console.log(`📄 Read agent script (${scriptContent.length} characters)`);
|
||||
|
||||
// Extract version from script content
|
||||
const versionMatch = scriptContent.match(/AGENT_VERSION="([^"]+)"/);
|
||||
if (!versionMatch) {
|
||||
throw new Error('Could not extract AGENT_VERSION from script');
|
||||
}
|
||||
|
||||
const version = versionMatch[1];
|
||||
console.log(`🔍 Found agent version: ${version}`);
|
||||
|
||||
// Check if this version already exists
|
||||
const existingVersion = await prisma.agentVersion.findUnique({
|
||||
where: { version: version }
|
||||
});
|
||||
|
||||
if (existingVersion) {
|
||||
console.log(`⚠️ Agent version ${version} already exists in database`);
|
||||
|
||||
// Update the existing version with current script content
|
||||
const updatedVersion = await prisma.agentVersion.update({
|
||||
where: { version: version },
|
||||
data: {
|
||||
scriptContent: scriptContent,
|
||||
isDefault: true,
|
||||
isCurrent: true,
|
||||
releaseNotes: `Agent script version ${version} - Updated during deployment`
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`✅ Updated existing agent version ${version}`);
|
||||
return updatedVersion;
|
||||
}
|
||||
|
||||
// Set all other versions to not be current/default
|
||||
await prisma.agentVersion.updateMany({
|
||||
where: {
|
||||
version: { not: version }
|
||||
},
|
||||
data: {
|
||||
isCurrent: false,
|
||||
isDefault: false
|
||||
}
|
||||
});
|
||||
|
||||
// Create new agent version
|
||||
const newVersion = await prisma.agentVersion.create({
|
||||
data: {
|
||||
version: version,
|
||||
scriptContent: scriptContent,
|
||||
isDefault: true,
|
||||
isCurrent: true,
|
||||
releaseNotes: `Agent script version ${version} - Initial deployment`
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`✅ Created new agent version ${version}`);
|
||||
console.log(`📊 Version ID: ${newVersion.id}`);
|
||||
console.log(`📝 Script content length: ${scriptContent.length} characters`);
|
||||
|
||||
return newVersion;
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error adding agent version:', error);
|
||||
throw error;
|
||||
} finally {
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
// Run the function if this script is executed directly
|
||||
if (require.main === module) {
|
||||
addAgentVersion()
|
||||
.then(() => {
|
||||
console.log('🎉 Agent version setup completed successfully!');
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('💥 Agent version setup failed:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { addAgentVersion };
|
||||
@@ -1,5 +1,6 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
require('dotenv').config();
|
||||
const { PrismaClient } = require('@prisma/client');
|
||||
|
||||
@@ -36,52 +37,62 @@ async function populateAgentVersion() {
|
||||
|
||||
console.log('Populating agent version:', currentVersion);
|
||||
|
||||
const existingVersion = await prisma.agentVersion.findUnique({
|
||||
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.agentVersion.update({
|
||||
await prisma.agent_versions.update({
|
||||
where: { version: currentVersion },
|
||||
data: {
|
||||
scriptContent: agentScript,
|
||||
isCurrent: true,
|
||||
releaseNotes: `Version ${currentVersion} - Updated Agent Script\n\nThis version contains the latest agent script from the Docker container initialization.`
|
||||
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.agentVersion.create({
|
||||
await prisma.agent_versions.create({
|
||||
data: {
|
||||
id: uuidv4(),
|
||||
version: currentVersion,
|
||||
scriptContent: agentScript,
|
||||
isCurrent: true,
|
||||
isDefault: true,
|
||||
releaseNotes: `Version ${currentVersion} - Docker Agent Script\n\nThis version contains the agent script from the Docker container initialization.`
|
||||
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.agentVersion.updateMany({
|
||||
await prisma.agent_versions.updateMany({
|
||||
where: { version: { not: currentVersion } },
|
||||
data: { isCurrent: false }
|
||||
data: {
|
||||
is_current: false,
|
||||
updated_at: new Date()
|
||||
}
|
||||
});
|
||||
|
||||
const allVersions = await prisma.agentVersion.findMany({
|
||||
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.agentVersion.update({
|
||||
await prisma.agent_versions.update({
|
||||
where: { id: version.id },
|
||||
data: {
|
||||
scriptContent: agentScript,
|
||||
releaseNotes: `Version ${version.version} - Updated with latest script from ${currentVersion}\n\nThis version has been updated with the latest agent script content.`
|
||||
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()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user