Enhance Add Regional Agent dialog.

Implement one-click installation with automatic script generation and environment variable configuration.
This commit is contained in:
Tola Leng
2025-06-21 21:02:40 +07:00
parent d385f24c28
commit 1e7c20593f
2 changed files with 285 additions and 135 deletions
@@ -7,7 +7,7 @@ import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea"; import { Textarea } from "@/components/ui/textarea";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Copy, Download, Terminal, CheckCircle } from "lucide-react"; import { Copy, Download, Terminal, CheckCircle, Zap, Play } from "lucide-react";
import { regionalService } from "@/services/regionalService"; import { regionalService } from "@/services/regionalService";
import { InstallCommand } from "@/types/regional.types"; import { InstallCommand } from "@/types/regional.types";
import { useToast } from "@/hooks/use-toast"; import { useToast } from "@/hooks/use-toast";
@@ -59,7 +59,7 @@ export const AddRegionalAgentDialog: React.FC<AddRegionalAgentDialogProps> = ({
await navigator.clipboard.writeText(text); await navigator.clipboard.writeText(text);
toast({ toast({
title: "Copied!", title: "Copied!",
description: "Installation command copied to clipboard.", description: "Installation script copied to clipboard.",
}); });
} catch (error) { } catch (error) {
toast({ toast({
@@ -89,6 +89,16 @@ export const AddRegionalAgentDialog: React.FC<AddRegionalAgentDialogProps> = ({
}); });
}; };
const copyOneClickCommand = () => {
if (!installCommand) return;
const oneClickCommand = `curl -fsSL -H "User-Agent: CheckCle-Installer" \\
"data:text/plain;base64,$(echo '${installCommand.bash_script}' | base64 -w 0)" \\
| sudo bash`;
copyToClipboard(oneClickCommand);
};
const handleComplete = () => { const handleComplete = () => {
onAgentAdded(); onAgentAdded();
setStep(1); setStep(1);
@@ -107,11 +117,11 @@ export const AddRegionalAgentDialog: React.FC<AddRegionalAgentDialogProps> = ({
return ( return (
<Dialog open={open} onOpenChange={onOpenChange}> <Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-[800px] max-h-[90vh] overflow-auto"> <DialogContent className="sm:max-w-[900px] max-h-[90vh] overflow-auto">
<DialogHeader> <DialogHeader>
<DialogTitle>Add Regional Monitoring Agent</DialogTitle> <DialogTitle>Add Regional Monitoring Agent</DialogTitle>
<DialogDescription> <DialogDescription>
Configure a new regional monitoring agent to extend your monitoring coverage. Deploy a regional monitoring agent with automatic one-click installation.
</DialogDescription> </DialogDescription>
</DialogHeader> </DialogHeader>
@@ -122,7 +132,7 @@ export const AddRegionalAgentDialog: React.FC<AddRegionalAgentDialogProps> = ({
<Label htmlFor="regionName">Region Name</Label> <Label htmlFor="regionName">Region Name</Label>
<Input <Input
id="regionName" id="regionName"
placeholder="e.g., US East, Europe West, Asia Pacific" placeholder="e.g., us-east-1, europe-west-1, asia-pacific-1"
value={regionName} value={regionName}
onChange={(e) => setRegionName(e.target.value)} onChange={(e) => setRegionName(e.target.value)}
required required
@@ -133,7 +143,7 @@ export const AddRegionalAgentDialog: React.FC<AddRegionalAgentDialogProps> = ({
<Label htmlFor="agentIp">Agent Server IP Address</Label> <Label htmlFor="agentIp">Agent Server IP Address</Label>
<Input <Input
id="agentIp" id="agentIp"
placeholder="e.g., 192.168.1.100 or your-server.com" placeholder="e.g., 192.168.1.100 or your-server.example.com"
value={agentIp} value={agentIp}
onChange={(e) => setAgentIp(e.target.value)} onChange={(e) => setAgentIp(e.target.value)}
required required
@@ -146,7 +156,7 @@ export const AddRegionalAgentDialog: React.FC<AddRegionalAgentDialogProps> = ({
Cancel Cancel
</Button> </Button>
<Button type="submit" disabled={isSubmitting}> <Button type="submit" disabled={isSubmitting}>
{isSubmitting ? "Creating..." : "Generate Installation"} {isSubmitting ? "Generating..." : "Generate Installation"}
</Button> </Button>
</div> </div>
</form> </form>
@@ -156,24 +166,94 @@ export const AddRegionalAgentDialog: React.FC<AddRegionalAgentDialogProps> = ({
<div className="space-y-6"> <div className="space-y-6">
<div className="text-center space-y-2"> <div className="text-center space-y-2">
<CheckCircle className="h-12 w-12 text-green-600 mx-auto" /> <CheckCircle className="h-12 w-12 text-green-600 mx-auto" />
<h3 className="text-lg font-semibold">Agent Configuration Created!</h3> <h3 className="text-lg font-semibold">Agent Configuration Ready!</h3>
<p className="text-muted-foreground"> <p className="text-muted-foreground">
Use the installation script below to set up your regional monitoring agent. One-click installation script generated with automatic configuration.
</p> </p>
</div> </div>
<Tabs defaultValue="details" className="w-full"> <Tabs defaultValue="oneclicK" className="w-full">
<TabsList className="grid w-full grid-cols-2"> <TabsList className="grid w-full grid-cols-3">
<TabsTrigger value="oneclicK">One-Click Install</TabsTrigger>
<TabsTrigger value="details">Agent Details</TabsTrigger> <TabsTrigger value="details">Agent Details</TabsTrigger>
<TabsTrigger value="install">Installation</TabsTrigger> <TabsTrigger value="manual">Manual Install</TabsTrigger>
</TabsList> </TabsList>
<TabsContent value="oneclicK" className="space-y-4">
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Zap className="h-5 w-5 text-yellow-500" />
One-Click Automatic Installation
</CardTitle>
<CardDescription>
Complete installation, configuration, and service startup in one command
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<div className="bg-green-50 border border-green-200 rounded-lg p-4">
<div className="flex items-center gap-2 text-green-800 font-medium mb-2">
<Play className="h-4 w-4" />
What this script does automatically:
</div>
<ul className="text-sm text-green-700 space-y-1 ml-6">
<li> Downloads the latest .deb package</li>
<li> Installs the regional monitoring agent</li>
<li> Creates configuration file with your settings</li>
<li> Starts and enables the service</li>
<li> Runs health checks</li>
</ul>
</div>
<div className="space-y-2">
<Label>Run this command on your target server:</Label>
<div className="relative">
<Textarea
readOnly
value={`# One-click installation command:
curl -fsSL https://github.com/operacle/checkcle/blob/main/scripts/regional-agent.sh | sudo bash -s -- \\
--region-name="${regionName}" \\
--agent-id="${installCommand.agent_id}" \\
--agent-ip="${agentIp}" \\
--token="${installCommand.token}" \\
--pocketbase-url="${installCommand.api_endpoint}"`}
className="font-mono text-sm min-h-[120px] pr-12"
/>
<Button
size="sm"
variant="outline"
className="absolute top-2 right-2"
onClick={() => copyToClipboard(`curl -fsSL https://github.com/operacle/checkcle/blob/main/scripts/regional-agent.sh | sudo bash -s -- --region-name="${regionName}" --agent-id="${installCommand.agent_id}" --agent-ip="${agentIp}" --token="${installCommand.token}" --pocketbase-url="${installCommand.api_endpoint}"`)}
>
<Copy className="h-3 w-3" />
</Button>
</div>
</div>
<div className="flex gap-2">
<Button onClick={downloadScript} variant="outline" className="flex-1">
<Download className="mr-2 h-4 w-4" />
Download Complete Script
</Button>
<Button
onClick={() => copyToClipboard(installCommand.bash_script)}
variant="outline"
className="flex-1"
>
<Copy className="mr-2 h-4 w-4" />
Copy Full Script
</Button>
</div>
</CardContent>
</Card>
</TabsContent>
<TabsContent value="details" className="space-y-4"> <TabsContent value="details" className="space-y-4">
<Card> <Card>
<CardHeader> <CardHeader>
<CardTitle>Agent Information</CardTitle> <CardTitle>Generated Agent Configuration</CardTitle>
<CardDescription> <CardDescription>
Configuration details for your regional monitoring agent These values will be automatically configured during installation
</CardDescription> </CardDescription>
</CardHeader> </CardHeader>
<CardContent className="space-y-3"> <CardContent className="space-y-3">
@@ -197,7 +277,7 @@ export const AddRegionalAgentDialog: React.FC<AddRegionalAgentDialogProps> = ({
</div> </div>
</div> </div>
<div> <div>
<Label className="text-sm font-medium text-foreground">Region</Label> <Label className="text-sm font-medium text-foreground">Region Name</Label>
<Input <Input
value={regionName} value={regionName}
readOnly readOnly
@@ -240,65 +320,67 @@ export const AddRegionalAgentDialog: React.FC<AddRegionalAgentDialogProps> = ({
</Button> </Button>
</div> </div>
</div> </div>
<div className="mt-4 p-3 bg-blue-50 border border-blue-200 rounded-lg">
<p className="text-sm text-blue-800">
<strong>Configuration file location:</strong> /etc/regional-check-agent/regional-check-agent.env
</p>
</div>
</CardContent> </CardContent>
</Card> </Card>
</TabsContent> </TabsContent>
<TabsContent value="install" className="space-y-4"> <TabsContent value="manual" className="space-y-4">
<Card> <Card>
<CardHeader> <CardHeader>
<CardTitle className="flex items-center gap-2"> <CardTitle className="flex items-center gap-2">
<Terminal className="h-5 w-5" /> <Terminal className="h-5 w-5" />
One-Click Installation Manual Installation Steps
</CardTitle> </CardTitle>
<CardDescription> <CardDescription>
Run this command on your target server to automatically install and configure the agent Step-by-step manual installation process
</CardDescription> </CardDescription>
</CardHeader> </CardHeader>
<CardContent className="space-y-4"> <CardContent className="space-y-4">
<div className="space-y-2"> <div className="text-sm space-y-4">
<Label>Installation Command</Label> <div className="border-l-4 border-blue-500 pl-4">
<div className="relative"> <p className="font-medium">Step 1: Download Package</p>
<Textarea <code className="text-xs bg-muted p-2 rounded block mt-1">
readOnly wget https://github.com/checkcle/distributed-regional-monitoring/releases/latest/download/distributed-regional-check-agent_1.0.0_amd64.deb
value={`curl -fsSL | sudo bash -s -- <<'EOF'\n${installCommand.bash_script}\nEOF`} </code>
className="font-mono text-sm min-h-[100px]"
/>
<Button
size="sm"
variant="outline"
className="absolute top-2 right-2"
onClick={() => copyToClipboard(`curl -fsSL | sudo bash -s -- <<'EOF'\n${installCommand.bash_script}\nEOF`)}
>
<Copy className="h-3 w-3" />
</Button>
</div>
</div> </div>
<div className="flex gap-2"> <div className="border-l-4 border-blue-500 pl-4">
<Button onClick={downloadScript} variant="outline" className="flex-1"> <p className="font-medium">Step 2: Install Package</p>
<Download className="mr-2 h-4 w-4" /> <code className="text-xs bg-muted p-2 rounded block mt-1">
Download Script sudo dpkg -i distributed-regional-check-agent_1.0.0_amd64.deb<br/>
</Button> sudo apt-get install -f
<Button </code>
onClick={() => copyToClipboard(installCommand.bash_script)}
variant="outline"
className="flex-1"
>
<Copy className="mr-2 h-4 w-4" />
Copy Script
</Button>
</div> </div>
<div className="text-sm text-muted-foreground space-y-2"> <div className="border-l-4 border-blue-500 pl-4">
<p className="font-medium">Installation Steps:</p> <p className="font-medium">Step 3: Configure Agent</p>
<ol className="list-decimal list-inside space-y-1"> <code className="text-xs bg-muted p-2 rounded block mt-1">
<li>Copy the installation command above</li> sudo nano /etc/regional-check-agent/regional-check-agent.env
<li>SSH into your target server</li> </code>
<li>Run the command as root (with sudo)</li> <p className="text-xs text-muted-foreground mt-1">Add the configuration values shown in the Agent Details tab</p>
<li>The agent will be automatically installed and started</li> </div>
<li>Check the agent status in the Regional Monitoring dashboard</li>
</ol> <div className="border-l-4 border-blue-500 pl-4">
<p className="font-medium">Step 4: Start Service</p>
<code className="text-xs bg-muted p-2 rounded block mt-1">
sudo systemctl enable regional-check-agent<br/>
sudo systemctl start regional-check-agent
</code>
</div>
<div className="border-l-4 border-green-500 pl-4">
<p className="font-medium">Step 5: Verify Installation</p>
<code className="text-xs bg-muted p-2 rounded block mt-1">
sudo systemctl status regional-check-agent<br/>
curl http://localhost:8091/health
</code>
</div>
</div> </div>
</CardContent> </CardContent>
</Card> </Card>
@@ -307,7 +389,7 @@ export const AddRegionalAgentDialog: React.FC<AddRegionalAgentDialogProps> = ({
<div className="flex justify-end space-x-2"> <div className="flex justify-end space-x-2">
<Button variant="outline" onClick={resetDialog}> <Button variant="outline" onClick={resetDialog}>
Add Another Add Another Agent
</Button> </Button>
<Button onClick={handleComplete}> <Button onClick={handleComplete}>
Complete Setup Complete Setup
+141 -73
View File
@@ -68,7 +68,7 @@ export const regionalService = {
token: token, token: token,
agent_id: agentId, agent_id: agentId,
api_endpoint: pb.baseUrl, api_endpoint: pb.baseUrl,
bash_script: this.generateBashScript(token, agentId, pb.baseUrl, params.agent_ip_address) bash_script: this.generateAutomaticInstallScript(token, agentId, pb.baseUrl, params.agent_ip_address, params.region_name)
}; };
return { service, installCommand }; return { service, installCommand };
@@ -87,112 +87,180 @@ export const regionalService = {
} }
}, },
generateBashScript(token: string, agentId: string, apiEndpoint: string, agentIp: string): string { generateAutomaticInstallScript(token: string, agentId: string, apiEndpoint: string, agentIp: string, regionName: string): string {
return `#!/bin/bash return `#!/bin/bash
# CheckCle - Regional Monitoring Agent Installation Script # CheckCle Regional Monitoring Agent - Automatic Installation Script
# Generated on: $(date) # Generated on: $(date)
# This script will automatically download, install, configure and start the regional monitoring agent
echo "🚀 Installing CheckCle Regional Monitoring Agent..." echo "🚀 CheckCle Regional Monitoring Agent - Automatic Installation"
echo "======================================================" echo "=============================================================="
echo ""
# Configuration # Configuration variables
REGION_NAME="${regionName}"
AGENT_ID="${agentId}" AGENT_ID="${agentId}"
TOKEN="${token}" AGENT_IP_ADDRESS="${agentIp}"
API_ENDPOINT="${apiEndpoint}" AGENT_TOKEN="${token}"
AGENT_IP="${agentIp}" POCKETBASE_URL="${apiEndpoint}"
INSTALL_DIR="/opt/checkcle-regional-agent"
SERVICE_NAME="checkcle-regional-agent" # Package information
PACKAGE_URL="https://github.com/operacle/distributed-regional-monitoring/releases/latest/download/distributed-regional-check-agent_1.0.0_amd64.deb"
PACKAGE_NAME="distributed-regional-check-agent_1.0.0_amd64.deb"
SERVICE_NAME="regional-check-agent"
# Check if running as root # Check if running as root
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
echo "❌ This script must be run as root (use sudo)" echo "❌ This script must be run as root (use sudo)"
echo " Usage: sudo bash install-regional-agent.sh"
exit 1 exit 1
fi fi
# Create installation directory echo "📋 Installation Configuration:"
echo "📁 Creating installation directory..." echo " Region Name: $REGION_NAME"
mkdir -p $INSTALL_DIR echo " Agent ID: $AGENT_ID"
cd $INSTALL_DIR echo " Agent IP: $AGENT_IP_ADDRESS"
echo " PocketBase URL: $POCKETBASE_URL"
echo ""
# Download the agent binary # Create temporary directory
echo "📥 Downloading Regional Monitoring Agent..." TEMP_DIR=$(mktemp -d)
wget -O regional-check-agent https://github.com/operacle/distributed-regional-monitoring/releases/latest/download/distributed-regional-check-agent-linux-amd64 echo "📁 Created temporary directory: $TEMP_DIR"
# Make it executable # Download the .deb package
chmod +x regional-check-agent echo "📥 Downloading Regional Monitoring Agent package..."
cd "$TEMP_DIR"
if wget -q --show-progress "$PACKAGE_URL" -O "$PACKAGE_NAME"; then
echo "✅ Package downloaded successfully"
else
echo "❌ Failed to download package from $PACKAGE_URL"
echo " Please check your internet connection and try again"
rm -rf "$TEMP_DIR"
exit 1
fi
# Create configuration file # Install the package
echo "⚙️ Creating configuration file..." echo ""
cat > config.yaml << EOF echo "📦 Installing Regional Monitoring Agent package..."
# CheckCle Regional Monitoring Agent Configuration if dpkg -i "$PACKAGE_NAME"; then
agent: echo "✅ Package installed successfully"
id: "$AGENT_ID" else
region: "$(curl -s ipinfo.io/region 2>/dev/null || echo 'Unknown')" echo "⚠️ Package installation had issues, attempting to fix dependencies..."
ip_address: "$AGENT_IP" if apt-get install -f -y; then
echo "✅ Dependencies fixed and package installed successfully"
else
echo "❌ Failed to install package and fix dependencies"
rm -rf "$TEMP_DIR"
exit 1
fi
fi
api: # Configure the agent
endpoint: "$API_ENDPOINT" echo ""
token: "$TOKEN" echo "⚙️ Configuring Regional Monitoring Agent..."
collection: "regional_service"
monitoring: # Create the environment configuration file
interval: 30s cat > /etc/regional-check-agent/regional-check-agent.env << EOF
timeout: 10s # Distributed Regional Check Agent Configuration
max_retries: 3 # Auto-generated on $(date)
logging: # Server Configuration
level: "info" PORT=8091
file: "/var/log/checkcle-regional-agent.log"
# Operation defaults
DEFAULT_COUNT=4
DEFAULT_TIMEOUT=3s
MAX_COUNT=20
MAX_TIMEOUT=30s
# Logging
ENABLE_LOGGING=true
# PocketBase integration
POCKETBASE_ENABLED=true
POCKETBASE_URL=$POCKETBASE_URL
# Regional Agent Configuration - Auto-configured
REGION_NAME=$REGION_NAME
AGENT_ID=$AGENT_ID
AGENT_IP_ADDRESS=$AGENT_IP_ADDRESS
AGENT_TOKEN=$AGENT_TOKEN
# Monitoring configuration
CHECK_INTERVAL=30s
MAX_RETRIES=3
REQUEST_TIMEOUT=10s
EOF EOF
# Create systemd service echo "✅ Configuration file created at /etc/regional-check-agent/regional-check-agent.env"
echo "🔧 Creating systemd service..."
cat > /etc/systemd/system/$SERVICE_NAME.service << EOF
[Unit]
Description=CheckCle Regional Monitoring Agent
After=network.target
Wants=network.target
[Service] # Set proper permissions
Type=simple chown root:regional-check-agent /etc/regional-check-agent/regional-check-agent.env
User=root chmod 640 /etc/regional-check-agent/regional-check-agent.env
WorkingDirectory=$INSTALL_DIR
ExecStart=$INSTALL_DIR/regional-check-agent --config=$INSTALL_DIR/config.yaml
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
# Enable and start the service # Enable and start the service
echo "🎯 Enabling and starting the service..." echo ""
systemctl daemon-reload echo "🔧 Starting Regional Monitoring Agent service..."
systemctl enable $SERVICE_NAME
systemctl start $SERVICE_NAME
# Check status # Reload systemd daemon
echo "✅ Installation completed!" systemctl daemon-reload
# Enable the service for auto-start
if systemctl enable $SERVICE_NAME; then
echo "✅ Service enabled for auto-start"
else
echo "❌ Failed to enable service"
rm -rf "$TEMP_DIR"
exit 1
fi
# Start the service
if systemctl start $SERVICE_NAME; then
echo "✅ Service started successfully"
else
echo "❌ Failed to start service"
echo " Check the configuration and try: sudo systemctl start $SERVICE_NAME"
rm -rf "$TEMP_DIR"
exit 1
fi
# Wait a moment for service to initialize
sleep 3
# Check service status
echo "" echo ""
echo "📊 Service Status:" echo "📊 Service Status:"
systemctl status $SERVICE_NAME --no-pager -l systemctl status $SERVICE_NAME --no-pager -l
# Test health endpoint
echo "" echo ""
echo "🔍 Quick Status Check:" echo "🩺 Testing agent health endpoint..."
if curl -s -f http://localhost:8091/health > /dev/null; then
echo "✅ Agent health endpoint is responding"
else
echo "⚠️ Agent health endpoint not responding yet (this is normal, may take a few moments)"
fi
# Cleanup
rm -rf "$TEMP_DIR"
echo ""
echo "🎉 Regional Monitoring Agent Installation Complete!"
echo ""
echo "📋 Installation Summary:"
echo " Agent ID: $AGENT_ID" echo " Agent ID: $AGENT_ID"
echo " API Endpoint: $API_ENDPOINT" echo " Region: $REGION_NAME"
echo " Log file: /var/log/checkcle-regional-agent.log" echo " Status: $(systemctl is-active $SERVICE_NAME)"
echo " Health URL: http://localhost:8091/health"
echo " Service endpoint: http://localhost:8091/operation"
echo "" echo ""
echo "📝 Useful commands:" echo "📝 Useful commands:"
echo " - Check status: systemctl status $SERVICE_NAME" echo " Check status: sudo systemctl status $SERVICE_NAME"
echo " - View logs: journalctl -u $SERVICE_NAME -f" echo " View logs: sudo journalctl -u $SERVICE_NAME -f"
echo " - Restart: systemctl restart $SERVICE_NAME" echo " Restart: sudo systemctl restart $SERVICE_NAME"
echo " - Stop: systemctl stop $SERVICE_NAME" echo " Stop: sudo systemctl stop $SERVICE_NAME"
echo "" echo ""
echo "🎉 Regional Monitoring Agent is now running!" echo "✨ The agent is now monitoring and reporting to your CheckCle dashboard!"
`; `;
} }
}; };