auto-enrolment enhancements

This commit is contained in:
Muhammad Ibrahim
2025-11-14 23:57:43 +00:00
parent 1ca8bf8581
commit 9da341f84c
3 changed files with 112 additions and 101 deletions

View File

@@ -16,6 +16,9 @@ SCRIPT_VERSION="1.0.0"
# Usage: # Usage:
# curl -s "https://patchmon.example.com/api/v1/auto-enrollment/script?type=direct-host&token_key=KEY&token_secret=SECRET" | sh # curl -s "https://patchmon.example.com/api/v1/auto-enrollment/script?type=direct-host&token_key=KEY&token_secret=SECRET" | sh
# #
# With custom friendly name:
# curl -s "https://patchmon.example.com/api/v1/auto-enrollment/script?type=direct-host&token_key=KEY&token_secret=SECRET" | FRIENDLY_NAME="My Server" sh
#
# Requirements: # Requirements:
# - Run as root or with sudo # - Run as root or with sudo
# - Auto-enrollment token from PatchMon # - Auto-enrollment token from PatchMon
@@ -28,6 +31,7 @@ AUTO_ENROLLMENT_KEY="${AUTO_ENROLLMENT_KEY:-}"
AUTO_ENROLLMENT_SECRET="${AUTO_ENROLLMENT_SECRET:-}" AUTO_ENROLLMENT_SECRET="${AUTO_ENROLLMENT_SECRET:-}"
CURL_FLAGS="${CURL_FLAGS:--s}" CURL_FLAGS="${CURL_FLAGS:--s}"
FORCE_INSTALL="${FORCE_INSTALL:-false}" FORCE_INSTALL="${FORCE_INSTALL:-false}"
FRIENDLY_NAME="${FRIENDLY_NAME:-}" # Optional: Custom friendly name for the host
# ===== COLOR OUTPUT ===== # ===== COLOR OUTPUT =====
RED='\033[0;31m' RED='\033[0;31m'
@@ -77,7 +81,7 @@ fi
# Check for required commands # Check for required commands
for cmd in curl; do for cmd in curl; do
if ! command -v $cmd &> /dev/null; then if ! command -v $cmd >/dev/null 2>&1; then
error "Required command '$cmd' not found. Please install it first." error "Required command '$cmd' not found. Please install it first."
fi fi
done done
@@ -91,7 +95,14 @@ info "Gathering host information..."
# Get hostname # Get hostname
hostname=$(hostname) hostname=$(hostname)
friendly_name="$hostname"
# Use FRIENDLY_NAME env var if provided, otherwise use hostname
if [ -n "$FRIENDLY_NAME" ]; then
friendly_name="$FRIENDLY_NAME"
info "Using custom friendly name: $friendly_name"
else
friendly_name="$hostname"
fi
# Try to get machine_id (optional, for tracking) # Try to get machine_id (optional, for tracking)
machine_id="" machine_id=""
@@ -132,6 +143,7 @@ case "$arch_raw" in
esac esac
info "Hostname: $hostname" info "Hostname: $hostname"
info "Friendly Name: $friendly_name"
info "IP Address: $ip_address" info "IP Address: $ip_address"
info "OS: $os_info" info "OS: $os_info"
info "Architecture: $architecture" info "Architecture: $architecture"

View File

@@ -551,8 +551,11 @@ router.post(
updated_at: new Date(), updated_at: new Date(),
}; };
// Update machine_id if provided and current one is a placeholder // Update machine_id if provided and current one is a placeholder or null
if (req.body.machineId && host.machine_id.startsWith("pending-")) { if (
req.body.machineId &&
(host.machine_id === null || host.machine_id.startsWith("pending-"))
) {
updateData.machine_id = req.body.machineId; updateData.machine_id = req.body.machineId;
} }

View File

@@ -35,6 +35,8 @@ const Integrations = () => {
const [server_url, setServerUrl] = useState(""); const [server_url, setServerUrl] = useState("");
const [force_proxmox_install, setForceProxmoxInstall] = useState(false); const [force_proxmox_install, setForceProxmoxInstall] = useState(false);
const [usage_type, setUsageType] = useState("proxmox-lxc"); const [usage_type, setUsageType] = useState("proxmox-lxc");
const [selected_script_type, setSelectedScriptType] = useState("proxmox-lxc");
const [curl_flags, setCurlFlags] = useState("-s");
// Form state // Form state
const [form_data, setFormData] = useState({ const [form_data, setFormData] = useState({
@@ -50,14 +52,9 @@ const Integrations = () => {
const [copy_success, setCopySuccess] = useState({}); const [copy_success, setCopySuccess] = useState({});
// Helper functions to build enrollment URLs with optional force flag // Helper function to build enrollment URL with optional force flag and selected type
const getProxmoxUrl = () => { const getEnrollmentUrl = (scriptType = selected_script_type) => {
const baseUrl = `${server_url}/api/v1/auto-enrollment/script?type=proxmox-lxc&token_key=${new_token.token_key}&token_secret=${new_token.token_secret}`; const baseUrl = `${server_url}/api/v1/auto-enrollment/script?type=${scriptType}&token_key=${new_token.token_key}&token_secret=${new_token.token_secret}`;
return force_proxmox_install ? `${baseUrl}&force=true` : baseUrl;
};
const getDirectHostUrl = () => {
const baseUrl = `${server_url}/api/v1/auto-enrollment/script?type=direct-host&token_key=${new_token.token_key}&token_secret=${new_token.token_secret}`;
return force_proxmox_install ? `${baseUrl}&force=true` : baseUrl; return force_proxmox_install ? `${baseUrl}&force=true` : baseUrl;
}; };
@@ -116,9 +113,12 @@ const Integrations = () => {
try { try {
const response = await api.get("/settings"); const response = await api.get("/settings");
setServerUrl(response.data.server_url || window.location.origin); setServerUrl(response.data.server_url || window.location.origin);
// Set curl flags based on SSL settings
setCurlFlags(response.data.ignore_ssl_self_signed ? "-sk" : "-s");
} catch (error) { } catch (error) {
console.error("Failed to load server URL:", error); console.error("Failed to load server URL:", error);
setServerUrl(window.location.origin); setServerUrl(window.location.origin);
setCurlFlags("-s");
} }
}; };
@@ -599,12 +599,12 @@ const Integrations = () => {
<div className="flex items-center gap-2 mb-3"> <div className="flex items-center gap-2 mb-3">
<Server className="h-5 w-5 text-blue-600 dark:text-blue-400" /> <Server className="h-5 w-5 text-blue-600 dark:text-blue-400" />
<h4 className="font-semibold text-secondary-900 dark:text-white"> <h4 className="font-semibold text-secondary-900 dark:text-white">
Proxmox LXC Auto-Enrollment Auto-enrollment
</h4> </h4>
</div> </div>
<p className="text-sm text-secondary-600 dark:text-secondary-400 mb-3"> <p className="text-sm text-secondary-600 dark:text-secondary-400 mb-3">
Automatically discover and enroll LXC containers from Automatically discover and enroll hosts from Proxmox or
Proxmox hosts. direct enrollment.
</p> </p>
<a <a
href="https://docs.patchmon.net/books/patchmon-application-documentation/page/proxmox-lxc-auto-enrollment-guide" href="https://docs.patchmon.net/books/patchmon-application-documentation/page/proxmox-lxc-auto-enrollment-guide"
@@ -622,12 +622,12 @@ const Integrations = () => {
<div className="flex items-center gap-2 mb-3"> <div className="flex items-center gap-2 mb-3">
<Server className="h-5 w-5 text-green-600 dark:text-green-400" /> <Server className="h-5 w-5 text-green-600 dark:text-green-400" />
<h4 className="font-semibold text-secondary-900 dark:text-white"> <h4 className="font-semibold text-secondary-900 dark:text-white">
API Credentials Scoped credentials
</h4> </h4>
</div> </div>
<p className="text-sm text-secondary-600 dark:text-secondary-400 mb-3"> <p className="text-sm text-secondary-600 dark:text-secondary-400 mb-3">
Programmatic access to PatchMon data using Basic Programmatic access to PatchMon data with granular
Authentication. scope-based permissions.
</p> </p>
<a <a
href="https://docs.patchmon.net/books/patchmon-application-documentation/page/integration-api-documentation" href="https://docs.patchmon.net/books/patchmon-application-documentation/page/integration-api-documentation"
@@ -1049,7 +1049,7 @@ const Integrations = () => {
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4"> <div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4">
<div className="bg-white dark:bg-secondary-800 rounded-lg max-w-2xl w-full max-h-[90vh] overflow-y-auto"> <div className="bg-white dark:bg-secondary-800 rounded-lg max-w-2xl w-full max-h-[90vh] overflow-y-auto">
<div className="p-6"> <div className="p-6">
<div className="flex items-center justify-between mb-6"> <div className="flex items-center justify-between mb-4">
<h2 className="text-xl font-bold text-secondary-900 dark:text-white"> <h2 className="text-xl font-bold text-secondary-900 dark:text-white">
{activeTab === "gethomepage" {activeTab === "gethomepage"
? "Create GetHomepage API Key" ? "Create GetHomepage API Key"
@@ -1060,6 +1060,7 @@ const Integrations = () => {
onClick={() => { onClick={() => {
setShowCreateModal(false); setShowCreateModal(false);
setUsageType("proxmox-lxc"); setUsageType("proxmox-lxc");
setSelectedScriptType("proxmox-lxc");
}} }}
className="text-secondary-400 hover:text-secondary-600 dark:hover:text-secondary-200" className="text-secondary-400 hover:text-secondary-600 dark:hover:text-secondary-200"
> >
@@ -1067,28 +1068,35 @@ const Integrations = () => {
</button> </button>
</div> </div>
<form onSubmit={create_token} className="space-y-4"> {/* Tabs for Auto-enrollment modal */}
{activeTab === "auto-enrollment" && ( {activeTab === "auto-enrollment" && (
<label className="block"> <div className="flex border-b border-secondary-200 dark:border-secondary-700 mb-6">
<span className="block text-sm font-medium text-secondary-700 dark:text-secondary-300 mb-1"> <button
Usage Type * type="button"
</span> onClick={() => setUsageType("proxmox-lxc")}
<select className={`px-4 py-2 text-sm font-medium border-b-2 transition-colors ${
value={usage_type} usage_type === "proxmox-lxc"
onChange={(e) => setUsageType(e.target.value)} ? "text-primary-600 dark:text-primary-400 border-primary-500"
className="w-full px-3 py-2 border border-secondary-300 dark:border-secondary-600 rounded-md bg-white dark:bg-secondary-700 text-secondary-900 dark:text-white" : "text-secondary-500 dark:text-secondary-400 border-transparent hover:text-secondary-700 dark:hover:text-secondary-300"
> }`}
<option value="proxmox-lxc"> >
Proxmox LXC Auto-Enrollment Auto-enrollment
</option> </button>
<option value="api">API Credentials</option> <button
</select> type="button"
<p className="mt-1 text-xs text-secondary-500 dark:text-secondary-400"> onClick={() => setUsageType("api")}
Select the type of token you want to create className={`px-4 py-2 text-sm font-medium border-b-2 transition-colors ${
</p> usage_type === "api"
</label> ? "text-primary-600 dark:text-primary-400 border-primary-500"
)} : "text-secondary-500 dark:text-secondary-400 border-transparent hover:text-secondary-700 dark:hover:text-secondary-300"
}`}
>
Scoped credentials
</button>
</div>
)}
<form onSubmit={create_token} className="space-y-4">
<label className="block"> <label className="block">
<span className="block text-sm font-medium text-secondary-700 dark:text-secondary-300 mb-1"> <span className="block text-sm font-medium text-secondary-700 dark:text-secondary-300 mb-1">
Token Name * Token Name *
@@ -1299,6 +1307,7 @@ const Integrations = () => {
setNewToken(null); setNewToken(null);
setShowSecret(false); setShowSecret(false);
setUsageType("proxmox-lxc"); setUsageType("proxmox-lxc");
setSelectedScriptType("proxmox-lxc");
}} }}
className="text-secondary-400 hover:text-secondary-600 dark:hover:text-secondary-200" className="text-secondary-400 hover:text-secondary-600 dark:hover:text-secondary-200"
> >
@@ -1518,11 +1527,39 @@ const Integrations = () => {
usage_type === "proxmox-lxc") && ( usage_type === "proxmox-lxc") && (
<div className="mt-6"> <div className="mt-6">
<div className="block text-sm font-medium text-secondary-700 dark:text-secondary-300 mb-2"> <div className="block text-sm font-medium text-secondary-700 dark:text-secondary-300 mb-2">
One-Line Installation Command Auto-Enrollment Command
</div> </div>
{/* Script Type Toggle Buttons */}
<div className="flex gap-2 mb-3">
<button
type="button"
onClick={() => setSelectedScriptType("proxmox-lxc")}
className={`px-3 py-1.5 text-sm font-medium rounded-md transition-colors ${
selected_script_type === "proxmox-lxc"
? "bg-primary-600 text-white dark:bg-primary-500"
: "bg-secondary-200 text-secondary-700 dark:bg-secondary-700 dark:text-secondary-300 hover:bg-secondary-300 dark:hover:bg-secondary-600"
}`}
>
Proxmox
</button>
<button
type="button"
onClick={() => setSelectedScriptType("direct-host")}
className={`px-3 py-1.5 text-sm font-medium rounded-md transition-colors ${
selected_script_type === "direct-host"
? "bg-primary-600 text-white dark:bg-primary-500"
: "bg-secondary-200 text-secondary-700 dark:bg-secondary-700 dark:text-secondary-300 hover:bg-secondary-300 dark:hover:bg-secondary-600"
}`}
>
Direct Host
</button>
</div>
<p className="text-xs text-secondary-600 dark:text-secondary-400 mb-2"> <p className="text-xs text-secondary-600 dark:text-secondary-400 mb-2">
Run this command on your Proxmox host to download and {selected_script_type === "proxmox-lxc"
execute the enrollment script: ? "Run this command on your Proxmox host to automatically discover and enroll all running LXC containers:"
: "Run this command on individual hosts to enroll them directly:"}
</p> </p>
{/* Force Install Toggle */} {/* Force Install Toggle */}
@@ -1537,19 +1574,19 @@ const Integrations = () => {
className="rounded border-secondary-300 dark:border-secondary-600 text-primary-600 focus:ring-primary-500 dark:focus:ring-primary-400 dark:bg-secondary-700" className="rounded border-secondary-300 dark:border-secondary-600 text-primary-600 focus:ring-primary-500 dark:focus:ring-primary-400 dark:bg-secondary-700"
/> />
<span className="text-secondary-800 dark:text-secondary-200"> <span className="text-secondary-800 dark:text-secondary-200">
Force install (bypass broken packages in containers) Force install (bypass broken packages)
</span> </span>
</label> </label>
<p className="text-xs text-secondary-600 dark:text-secondary-400 mt-1"> <p className="text-xs text-secondary-600 dark:text-secondary-400 mt-1">
Enable this if your LXC containers have broken packages Enable this if hosts have broken packages (CloudPanel,
(CloudPanel, WHM, etc.) that block apt-get operations WHM, etc.) that block apt-get operations
</p> </p>
</div> </div>
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<input <input
type="text" type="text"
value={`curl -s "${getProxmoxUrl()}" | sh`} value={`curl ${curl_flags} "${getEnrollmentUrl()}" | sh`}
readOnly readOnly
className="flex-1 px-3 py-2 border border-secondary-300 dark:border-secondary-600 rounded-md bg-secondary-50 dark:bg-secondary-900 text-secondary-900 dark:text-white font-mono text-xs" className="flex-1 px-3 py-2 border border-secondary-300 dark:border-secondary-600 rounded-md bg-secondary-50 dark:bg-secondary-900 text-secondary-900 dark:text-white font-mono text-xs"
/> />
@@ -1557,13 +1594,13 @@ const Integrations = () => {
type="button" type="button"
onClick={() => onClick={() =>
copy_to_clipboard( copy_to_clipboard(
`curl -s "${getProxmoxUrl()}" | sh`, `curl ${curl_flags} "${getEnrollmentUrl()}" | sh`,
"curl-command", "enrollment-command",
) )
} }
className="btn-primary flex items-center gap-1 px-3 py-2 whitespace-nowrap" className="btn-primary flex items-center gap-1 px-3 py-2 whitespace-nowrap"
> >
{copy_success["curl-command"] ? ( {copy_success["enrollment-command"] ? (
<> <>
<CheckCircle className="h-4 w-4" /> <CheckCircle className="h-4 w-4" />
Copied Copied
@@ -1576,58 +1613,16 @@ const Integrations = () => {
)} )}
</button> </button>
</div> </div>
<p className="text-xs text-secondary-500 dark:text-secondary-400 mt-2">
💡 This command will automatically discover and enroll all
running LXC containers.
</p>
</div>
)}
{(new_token.metadata?.integration_type === "proxmox-lxc" || {/* Usage hint for direct-host */}
usage_type === "proxmox-lxc") && ( {selected_script_type === "direct-host" && (
<div className="mt-6"> <p className="text-xs text-secondary-500 dark:text-secondary-400 mt-3 p-2 bg-blue-50 dark:bg-blue-900/20 rounded border border-blue-200 dark:border-blue-800">
<div className="block text-sm font-medium text-secondary-700 dark:text-secondary-300 mb-2"> 💡 <strong>Tip:</strong> Specify a custom name:{" "}
Direct Host Auto-Enrollment Command <code className="text-xs bg-secondary-200 dark:bg-secondary-700 px-1 py-0.5 rounded">
</div> FRIENDLY_NAME="My Server" sh
<p className="text-xs text-secondary-600 dark:text-secondary-400 mb-2"> </code>
Run this command on individual hosts to enroll them </p>
directly: )}
</p>
<div className="flex items-center gap-2">
<input
type="text"
value={`curl -s "${getDirectHostUrl()}" | sh`}
readOnly
className="flex-1 px-3 py-2 border border-secondary-300 dark:border-secondary-600 rounded-md bg-secondary-50 dark:bg-secondary-900 text-secondary-900 dark:text-white font-mono text-xs"
/>
<button
type="button"
onClick={() =>
copy_to_clipboard(
`curl -s "${getDirectHostUrl()}" | sh`,
"direct-host-command",
)
}
className="btn-primary flex items-center gap-1 px-3 py-2 whitespace-nowrap"
>
{copy_success["direct-host-command"] ? (
<>
<CheckCircle className="h-4 w-4" />
Copied
</>
) : (
<>
<Copy className="h-4 w-4" />
Copy
</>
)}
</button>
</div>
<p className="text-xs text-secondary-500 dark:text-secondary-400 mt-2">
💡 Run this on individual hosts for easy enrollment
without Proxmox.
</p>
</div> </div>
)} )}
@@ -1765,6 +1760,7 @@ const Integrations = () => {
setNewToken(null); setNewToken(null);
setShowSecret(false); setShowSecret(false);
setUsageType("proxmox-lxc"); setUsageType("proxmox-lxc");
setSelectedScriptType("proxmox-lxc");
}} }}
className="w-full btn-primary py-2 px-4 rounded-md" className="w-full btn-primary py-2 px-4 rounded-md"
> >