mirror of
https://github.com/9technologygroup/patchmon.net.git
synced 2025-11-07 15:33:32 +00:00
Script actually downloads on deb systems (i only use deb, sm1 else will have to test the other distros)
This commit is contained in:
@@ -15,59 +15,60 @@ const prisma = new PrismaClient();
|
||||
// Public endpoint to download the agent script
|
||||
router.get('/agent/download', async (req, res) => {
|
||||
try {
|
||||
const { version } = req.query;
|
||||
const {version} = req.query;
|
||||
|
||||
let agentVersion;
|
||||
|
||||
if (version) {
|
||||
// Download specific version
|
||||
agentVersion = await prisma.agent_versions.findUnique({
|
||||
where: { version }
|
||||
where: {version}
|
||||
});
|
||||
|
||||
if (!agentVersion) {
|
||||
return res.status(404).json({ error: 'Agent version not found' });
|
||||
return res.status(404).json({error: 'Agent version not found'});
|
||||
}
|
||||
} else {
|
||||
// Download current version (latest)
|
||||
agentVersion = await prisma.agent_versions.findFirst({
|
||||
where: { is_current: true },
|
||||
orderBy: { created_at: 'desc' }
|
||||
where: {is_current: true},
|
||||
orderBy: {created_at: 'desc'}
|
||||
});
|
||||
|
||||
if (!agentVersion) {
|
||||
// Fallback to default version
|
||||
agentVersion = await prisma.agent_versions.findFirst({
|
||||
where: { is_default: true },
|
||||
orderBy: { created_at: 'desc' }
|
||||
where: {is_default: true},
|
||||
orderBy: {created_at: 'desc'}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!agentVersion) {
|
||||
return res.status(404).json({ error: 'No agent version available' });
|
||||
return res.status(404).json({error: 'No agent version available'});
|
||||
}
|
||||
|
||||
// Use script content from database if available, otherwise fallback to file
|
||||
if (agentVersion.script_content) {
|
||||
// Convert Windows line endings to Unix line endings
|
||||
const scriptContent = agentVersion.script_content.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
||||
res.setHeader('Content-Type', 'application/x-shellscript');
|
||||
res.setHeader('Content-Disposition', `attachment; filename="patchmon-agent-${agentVersion.version}.sh"`);
|
||||
res.send(agentVersion.script_content);
|
||||
res.send(scriptContent);
|
||||
} else {
|
||||
// Fallback to file system
|
||||
const agentPath = path.join(__dirname, '../../../agents/patchmon-agent.sh');
|
||||
|
||||
if (!fs.existsSync(agentPath)) {
|
||||
return res.status(404).json({ error: 'Agent script not found' });
|
||||
return res.status(404).json({error: 'Agent script not found'});
|
||||
}
|
||||
|
||||
// Read file and convert line endings
|
||||
const scriptContent = fs.readFileSync(agentPath, 'utf8').replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
||||
res.setHeader('Content-Type', 'application/x-shellscript');
|
||||
res.setHeader('Content-Disposition', `attachment; filename="patchmon-agent-${agentVersion.version}.sh"`);
|
||||
res.sendFile(path.resolve(agentPath));
|
||||
res.send(scriptContent);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Agent download error:', error);
|
||||
res.status(500).json({ error: 'Failed to download agent script' });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -832,7 +833,10 @@ router.get('/install', async (req, res) => {
|
||||
|
||||
let script = fs.readFileSync(scriptPath, 'utf8');
|
||||
|
||||
// Get the configured server URL from settings
|
||||
// Convert Windows line endings to Unix line endings
|
||||
script = script.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
||||
|
||||
// Get the configured server URL from settings
|
||||
try {
|
||||
const settings = await prisma.settings.findFirst();
|
||||
if (settings) {
|
||||
|
||||
Reference in New Issue
Block a user