diff --git a/frontend/src/pages/Hosts.jsx b/frontend/src/pages/Hosts.jsx index 3a5e49d..a2f2c8c 100644 --- a/frontend/src/pages/Hosts.jsx +++ b/frontend/src/pages/Hosts.jsx @@ -9,11 +9,8 @@ import { ChevronDown, Clock, Columns, - Copy, ExternalLink, - Eye, Eye as EyeIcon, - EyeOff, EyeOff as EyeOffIcon, Filter, GripVertical, @@ -35,7 +32,6 @@ import { dashboardAPI, formatRelativeTime, hostGroupsAPI, - settingsAPI, } from "../utils/api"; import { OSIcon } from "../utils/osIcons.jsx"; @@ -232,565 +228,6 @@ const AddHostModal = ({ isOpen, onClose, onSuccess }) => { ); }; -// Credentials Modal Component -const CredentialsModal = ({ host, isOpen, onClose }) => { - const apiIdId = useId(); - const apiKeyId = useId(); - const [showApiKey, setShowApiKey] = useState(false); - const [activeTab, setActiveTab] = useState( - host?.isNewHost ? "quick" : "credentials", - ); - - // Update active tab when host changes - React.useEffect(() => { - if (host?.isNewHost) { - setActiveTab("quick"); - } else { - setActiveTab("credentials"); - } - }, [host?.isNewHost]); - - const copyToClipboard = async (text, label) => { - try { - // Try modern clipboard API first - if (navigator.clipboard && window.isSecureContext) { - await navigator.clipboard.writeText(text); - alert(`${label} copied to clipboard!`); - return; - } - - // Fallback for older browsers or non-secure contexts - const textArea = document.createElement("textarea"); - textArea.value = text; - textArea.style.position = "fixed"; - textArea.style.left = "-999999px"; - textArea.style.top = "-999999px"; - document.body.appendChild(textArea); - textArea.focus(); - textArea.select(); - - try { - const successful = document.execCommand("copy"); - if (successful) { - alert(`${label} copied to clipboard!`); - } else { - throw new Error("Copy command failed"); - } - } catch { - // If all else fails, show the text in a prompt - prompt(`Copy this ${label.toLowerCase()}:`, text); - } finally { - document.body.removeChild(textArea); - } - } catch (err) { - console.error("Failed to copy to clipboard:", err); - // Show the text in a prompt as last resort - prompt(`Copy this ${label.toLowerCase()}:`, text); - } - }; - - // Fetch server URL from settings - const { data: settings } = useQuery({ - queryKey: ["settings"], - queryFn: () => settingsAPI.get().then((res) => res.data), - enabled: isOpen, // Only fetch when modal is open - }); - - const serverUrl = - settings?.server_url || window.location.origin.replace(":3000", ":3001"); - - const getSetupCommands = () => { - // Get current time for crontab scheduling - const now = new Date(); - const currentMinute = now.getMinutes(); - const currentHour = now.getHours(); - - return { - oneLine: `curl -sSL ${serverUrl}/api/v1/hosts/install | bash -s -- ${serverUrl} "${host?.api_id}" "${host?.api_key}"`, - - download: `# Download and setup PatchMon agent -curl -o /tmp/patchmon-agent.sh ${serverUrl}/api/v1/hosts/agent/download -sudo mkdir -p /etc/patchmon -sudo mv /tmp/patchmon-agent.sh /usr/local/bin/patchmon-agent.sh -sudo chmod +x /usr/local/bin/patchmon-agent.sh`, - - configure: `# Configure API credentials -sudo /usr/local/bin/patchmon-agent.sh configure "${host?.api_id}" "${host?.api_key}"`, - - test: `# Test the configuration -sudo /usr/local/bin/patchmon-agent.sh test`, - - initialUpdate: `# Send initial package data -sudo /usr/local/bin/patchmon-agent.sh update`, - - crontab: `# Add to crontab for hourly updates starting at current time (run as root) -echo "${currentMinute} * * * * /usr/local/bin/patchmon-agent.sh update >/dev/null 2>&1" | sudo crontab -`, - - fullSetup: `#!/bin/bash -# Complete PatchMon Agent Setup Script -# Run this on the target host: ${host?.friendly_name} - -echo "๐ Setting up PatchMon agent..." - -# Download and install agent -echo "๐ฅ Downloading agent script..." -curl -o /tmp/patchmon-agent.sh ${serverUrl}/api/v1/hosts/agent/download -sudo mkdir -p /etc/patchmon -sudo mv /tmp/patchmon-agent.sh /usr/local/bin/patchmon-agent.sh -sudo chmod +x /usr/local/bin/patchmon-agent.sh - -# Configure credentials -echo "๐ Configuring API credentials..." -sudo /usr/local/bin/patchmon-agent.sh configure "${host?.api_id}" "${host?.api_key}" - -# Test configuration -echo "๐งช Testing configuration..." -sudo /usr/local/bin/patchmon-agent.sh test - -# Send initial update -echo "๐ Sending initial package data..." -sudo /usr/local/bin/patchmon-agent.sh update - -# Setup crontab starting at current time -echo "โฐ Setting up hourly crontab starting at ${currentHour.toString().padStart(2, "0")}:${currentMinute.toString().padStart(2, "0")}..." -echo "${currentMinute} * * * * /usr/local/bin/patchmon-agent.sh update >/dev/null 2>&1" | sudo crontab - - -echo "โ PatchMon agent setup complete!" -echo " - Agent installed: /usr/local/bin/patchmon-agent.sh" -echo " - Config directory: /etc/patchmon/" -echo " - Updates: Every hour via crontab (starting at ${currentHour.toString().padStart(2, "0")}:${currentMinute.toString().padStart(2, "0")})" -echo " - View logs: tail -f /var/log/patchmon-agent.log"`, - }; - }; - - if (!isOpen || !host) return null; - - const commands = getSetupCommands(); - - return ( -
- Copy and paste this single command on{" "} - {host.friendly_name} to install and configure - the PatchMon agent automatically. -
-
- {commands.oneLine}
-
-
- - This command will download, install, configure, and set up - automatic updates for the PatchMon agent. -
-/usr/local/bin/patchmon-agent.sh
- sudo)
- curl and bash to be
- installed
- - Keep these credentials secure. They provide access to update - package information for {host.friendly_name}{" "} - only. -
-- Follow these commands on {host.friendly_name}{" "} - to install and configure the PatchMon agent. -
-
- {commands.download}
-
-
- {commands.configure}
-
- {commands.test}
-
- - This will automatically detect and send system information (OS, - IP, architecture) along with package data. -
-{commands.initialUpdate}
-
- {commands.crontab}
-
- - This sets up automatic package updates every hour at the top of - the hour. -
-- Copy this complete setup script to{" "} - {host.friendly_name} and run it to - automatically install and configure everything. -
-{commands.fullSetup}
- - Usage: -
-1. Copy the script above
-
- 2. Save it to a file on {host.friendly_name} (e.g.,{" "}
- setup-patchmon.sh)
-
- 3. Run:{" "}
-
- chmod +x setup-patchmon.sh && sudo ./setup-patchmon.sh
-
-