refactor: replace 899 lines of script generation with templates
Created InstallTemplateService with clean template-based script generation. Added linux.sh.tmpl and windows.ps1.tmpl for install scripts. Removed massive generateLinuxScript and generateWindowsScript functions. Downloads handler now uses template service (1073 lines → 174 lines). Templates easily maintainable without modifying Go code.
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
#!/bin/bash
|
||||
# RedFlag Agent Installer - Linux
|
||||
# Generated for agent: {{.AgentID}}
|
||||
# Platform: {{.Platform}}
|
||||
# Version: {{.Version}}
|
||||
|
||||
set -e
|
||||
|
||||
AGENT_ID="{{.AgentID}}"
|
||||
BINARY_URL="{{.BinaryURL}}"
|
||||
CONFIG_URL="{{.ConfigURL}}"
|
||||
INSTALL_DIR="/usr/local/bin"
|
||||
CONFIG_DIR="/etc/redflag"
|
||||
SERVICE_NAME="redflag-agent"
|
||||
VERSION="{{.Version}}"
|
||||
|
||||
echo "=== RedFlag Agent v${VERSION} Installation ==="
|
||||
echo "Agent ID: ${AGENT_ID}"
|
||||
echo "Platform: {{.Platform}}"
|
||||
echo "Installing to: ${INSTALL_DIR}/${SERVICE_NAME}"
|
||||
echo
|
||||
|
||||
# Step 1: Create directories
|
||||
echo "Creating directories..."
|
||||
sudo mkdir -p "${CONFIG_DIR}"
|
||||
sudo mkdir -p "/var/lib/redflag"
|
||||
sudo mkdir -p "/var/log/redflag"
|
||||
|
||||
# Step 2: Download agent binary
|
||||
echo "Downloading agent binary..."
|
||||
sudo curl -sSL -o "${INSTALL_DIR}/${SERVICE_NAME}" "${BINARY_URL}"
|
||||
sudo chmod +x "${INSTALL_DIR}/${SERVICE_NAME}"
|
||||
|
||||
# Step 3: Download configuration
|
||||
echo "Downloading configuration..."
|
||||
sudo curl -sSL -o "${CONFIG_DIR}/config.json" "${CONFIG_URL}"
|
||||
sudo chmod 600 "${CONFIG_DIR}/config.json"
|
||||
|
||||
# Step 4: Create systemd service
|
||||
echo "Creating systemd service..."
|
||||
cat <<EOF | sudo tee /etc/systemd/system/${SERVICE_NAME}.service
|
||||
[Unit]
|
||||
Description=RedFlag Security Agent
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=root
|
||||
ExecStart=${INSTALL_DIR}/${SERVICE_NAME}
|
||||
Restart=always
|
||||
RestartSec=30
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
# Step 5: Enable and start service
|
||||
echo "Enabling and starting service..."
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable ${SERVICE_NAME}
|
||||
sudo systemctl start ${SERVICE_NAME}
|
||||
|
||||
echo
|
||||
echo "✓ Installation complete!"
|
||||
echo "Agent is running. Check status with: sudo systemctl status ${SERVICE_NAME}"
|
||||
echo "View logs with: sudo journalctl -u ${SERVICE_NAME} -f"
|
||||
@@ -0,0 +1,66 @@
|
||||
# RedFlag Agent Installer - Windows PowerShell
|
||||
# Generated for agent: {{.AgentID}}
|
||||
# Platform: {{.Platform}}
|
||||
# Version: {{.Version}}
|
||||
|
||||
param(
|
||||
[Parameter(Mandatory=$false)]
|
||||
[switch]$SkipServiceInstall = $false
|
||||
)
|
||||
|
||||
$AgentID = "{{.AgentID}}"
|
||||
$BinaryURL = "{{.BinaryURL}}"
|
||||
$ConfigURL = "{{.ConfigURL}}"
|
||||
$InstallDir = "C:\Program Files\RedFlag"
|
||||
$ConfigDir = "C:\ProgramData\RedFlag"
|
||||
$ServiceName = "RedFlagAgent"
|
||||
$Version = "{{.Version}}"
|
||||
|
||||
Write-Host "=== RedFlag Agent v$Version Installation ===" -ForegroundColor Cyan
|
||||
Write-Host "Agent ID: $AgentID"
|
||||
Write-Host "Platform: {{.Platform}}"
|
||||
Write-Host "Installing to: $InstallDir\redflag-agent.exe"
|
||||
Write-Host
|
||||
|
||||
# Step 1: Create directories
|
||||
Write-Host "Creating directories..." -ForegroundColor Yellow
|
||||
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
|
||||
New-Item -ItemType Directory -Force -Path $ConfigDir | Out-Null
|
||||
New-Item -ItemType Directory -Force -Path "$ConfigDir\state" | Out-Null
|
||||
New-Item -ItemType Directory -Force -Path "$ConfigDir\logs" | Out-Null
|
||||
|
||||
# Step 2: Download agent binary
|
||||
Write-Host "Downloading agent binary..." -ForegroundColor Yellow
|
||||
$BinaryPath = Join-Path $InstallDir "redflag-agent.exe"
|
||||
Invoke-WebRequest -Uri $BinaryURL -OutFile $BinaryPath -UseBasicParsing
|
||||
|
||||
# Step 3: Download configuration
|
||||
Write-Host "Downloading configuration..." -ForegroundColor Yellow
|
||||
$ConfigPath = Join-Path $ConfigDir "config.json"
|
||||
Invoke-WebRequest -Uri $ConfigURL -OutFile $ConfigPath -UseBasicParsing
|
||||
|
||||
# Step 4: Set permissions
|
||||
Write-Host "Setting file permissions..." -ForegroundColor Yellow
|
||||
icacls $ConfigPath /inheritance:r /grant:r "SYSTEM:(OI)(CI)F" /grant:r "Administrators:(OI)(CI)F" | Out-Null
|
||||
|
||||
# Step 5: Install Windows service (if not skipped)
|
||||
if (-not $SkipServiceInstall) {
|
||||
Write-Host "Creating Windows service..." -ForegroundColor Yellow
|
||||
|
||||
# Register service with appropriate credentials
|
||||
if ([System.Environment]::OSVersion.Version.Major -ge 10) {
|
||||
# Windows 10/Server 2016+ - can use LocalService
|
||||
New-Service -Name $ServiceName -BinaryPathName $BinaryPath -DisplayName "RedFlag Security Agent" -Description "RedFlag Security Monitoring Agent" -StartupType Automatic | Out-Null
|
||||
} else {
|
||||
# Older Windows - use LocalSystem
|
||||
New-Service -Name $ServiceName -BinaryPathName $BinaryPath -DisplayName "RedFlag Security Agent" -Description "RedFlag Security Monitoring Agent" -StartupType Automatic | Out-Null
|
||||
}
|
||||
|
||||
Write-Host "Starting service..." -ForegroundColor Yellow
|
||||
Start-Service -Name $ServiceName
|
||||
}
|
||||
|
||||
Write-Host
|
||||
Write-Host "✓ Installation complete!" -ForegroundColor Green
|
||||
Write-Host "Agent is running. Check status with: Get-Service $ServiceName"
|
||||
Write-Host "View logs with: Get-Content $ConfigDir\logs\agent.log -Tail 100 -Wait"
|
||||
Reference in New Issue
Block a user