diff --git a/UI/includes/enrollment.php b/UI/includes/enrollment.php index 608d2ee..30db864 100644 --- a/UI/includes/enrollment.php +++ b/UI/includes/enrollment.php @@ -36,7 +36,7 @@ class EnrollmentManager { } /** - * Main enrollment process - calls the enrollment script + * Main enrollment process */ public function enrollRouter($hashKey, $apiEndpoint = null) { try { @@ -46,47 +46,87 @@ class EnrollmentManager { $this->reportProgress('INIT', 'Starting router enrollment process...', 0); - // Use the enrollment script with sudo - $command = "sudo /usr/local/bin/enroll-router.sh '$hashKey' '$apiEndpoint' 2>&1"; - $output = ''; - $returnCode = 0; - - $this->reportProgress('INIT', 'Executing enrollment script...', 10); - - // Execute the enrollment script - $success = executeCommand($command, $output, $returnCode); - - if ($success && $returnCode === 0) { - $this->reportProgress('COMPLETE', 'Router enrollment completed successfully!', 100); - - // Parse output to get router information if available - $routerName = 'Unknown'; - $routerId = 'Unknown'; - - // Try to extract router info from output - if (preg_match('/Router Name: (.+)/', $output, $matches)) { - $routerName = trim($matches[1]); - } - if (preg_match('/Router ID: (.+)/', $output, $matches)) { - $routerId = trim($matches[1]); - } - - return [ - 'success' => true, - 'routerName' => $routerName, - 'routerId' => $routerId, - 'message' => 'Router enrollment completed successfully', - 'output' => $output - ]; - } else { - throw new Exception('Enrollment script failed: ' . $output); + // Step 1: Check system requirements + $this->reportProgress('REQUIREMENTS', 'Checking system requirements...', 10); + if (!$this->checkSystemRequirements()) { + throw new Exception('System requirements check failed'); } + // Step 2: Install OpenZiti if needed + $this->reportProgress('INSTALL', 'Installing OpenZiti CLI...', 20); + if (!$this->installZiti()) { + throw new Exception('OpenZiti installation failed'); + } + + // Step 3: Create directories + $this->reportProgress('DIRECTORIES', 'Creating necessary directories...', 30); + if (!$this->createDirectories()) { + throw new Exception('Failed to create directories'); + } + + // Step 4: Register router with API + $this->reportProgress('REGISTER', 'Registering router with ZitiNexus Portal...', 40); + $result = $this->apiClient->registerRouter($hashKey); + if (!$result['success']) { + throw new Exception('Router registration failed: ' . $result['error']); + } + + $this->routerData = $result['data']; + $this->reportProgress('REGISTER', 'Router registered successfully: ' . $this->routerData['routerInfo']['name'], 50); + + // Step 5: Save configuration files + $this->reportProgress('CONFIG', 'Saving configuration files...', 60); + if (!$this->saveConfiguration()) { + throw new Exception('Failed to save configuration files'); + } + + // Step 6: Enroll router with OpenZiti + $this->reportProgress('ENROLL', 'Enrolling router with OpenZiti controller...', 70); + if (!$this->enrollWithZiti()) { + throw new Exception('Router enrollment with OpenZiti failed'); + } + + // Step 7: Create systemd service + $this->reportProgress('SERVICE', 'Creating systemd service...', 80); + if (!$this->createSystemdService()) { + throw new Exception('Failed to create systemd service'); + } + + // Step 8: Start router service + $this->reportProgress('START', 'Starting router service...', 90); + if (!$this->startRouter()) { + throw new Exception('Failed to start router service'); + } + + // Step 9: Report success status + $this->reportProgress('REPORT', 'Reporting enrollment status...', 95); + $this->reportSuccessStatus($hashKey); + + $this->reportProgress('COMPLETE', 'Router enrollment completed successfully!', 100); + + return [ + 'success' => true, + 'routerName' => $this->routerData['routerInfo']['name'], + 'routerId' => $this->routerData['routerInfo']['id'], + 'message' => 'Router enrollment completed successfully' + ]; + } catch (Exception $e) { $errorMsg = $e->getMessage(); logMessage('ERROR', $errorMsg); $this->reportProgress('ERROR', $errorMsg, null); + // Report failure status + if (!empty($hashKey) && !empty($this->routerData['callbackUrl'])) { + $this->apiClient->reportStatus( + $this->routerData['callbackUrl'], + $hashKey, + 'failed', + null, + $errorMsg + ); + } + return [ 'success' => false, 'error' => $errorMsg diff --git a/UI/install.sh b/UI/install.sh index 413bb0e..72c5bf4 100644 --- a/UI/install.sh +++ b/UI/install.sh @@ -193,15 +193,6 @@ deploy_ui() { # Copy main UI files (assets are now inside public) cp -r public includes "$WEB_DIR/" || error_exit "Failed to copy UI files" - # Copy the enrollment script to a system location - if [[ -f "../Router-enrollment-script/enroll-router.sh" ]]; then - cp "../Router-enrollment-script/enroll-router.sh" "/usr/local/bin/" || error_exit "Failed to copy enrollment script" - chmod +x "/usr/local/bin/enroll-router.sh" || error_exit "Failed to make enrollment script executable" - log "SUCCESS" "Copied enrollment script to /usr/local/bin/enroll-router.sh" - else - log "WARNING" "Enrollment script not found at ../Router-enrollment-script/enroll-router.sh" - fi - # Copy root-level PHP files for direct access (when document root is main directory) if [[ -f "index.php" ]]; then cp index.php "$WEB_DIR/" || log "WARNING" "Failed to copy root index.php" @@ -365,7 +356,6 @@ www-data ALL=(ALL) NOPASSWD: /usr/bin/which www-data ALL=(ALL) NOPASSWD: /usr/bin/hostname www-data ALL=(ALL) NOPASSWD: /usr/bin/uname www-data ALL=(ALL) NOPASSWD: /usr/bin/lsb_release -www-data ALL=(ALL) NOPASSWD: /usr/local/bin/enroll-router.sh EOF # Validate sudoers file