andre 7 månader sedan
förälder
incheckning
4490dcdd11
3 ändrade filer med 76 tillägg och 2 borttagningar
  1. 10 0
      controllers/AccountController.php
  2. 63 0
      controllers/SiteController.php
  3. 3 2
      index.php

+ 10 - 0
controllers/AccountController.php

@@ -62,6 +62,16 @@ class AccountController {
                return;
            }
         }
+        if ($GLOBALS['debug'] == true) { error_log("Creating tmpdir for : " . $username); }
+        if (is_dir("/home/$username/tmp") != true) {
+             exec("sudo /usr/bin/mkdir -p /home/$username/tmp  2>&1", $mkdirOutput, $mkdirReturnCode);
+                if ($mkdirReturnCode !== 0) {
+                    error_log("deploy: ERROR: Failed to create temp directory for  $username failed, details => " . implode("\n", $mkdirOutput));
+                    http_response_code(500);
+                    echo json_encode(['error' => 'Failed to create tmp dir', 'details' => implode("\n", $mkdirOutput)]);
+                    return;
+                }
+        }
 
         if ($GLOBALS['debug'] == true) { error_log("Chown homedir: " . $username); }
         exec("sudo /usr/bin/chown $username:$username /home/$username -R 2>&1", $chownOutput, $chownReturnCode);

+ 63 - 0
controllers/SiteController.php

@@ -91,6 +91,69 @@ class SiteController {
         echo json_encode(['success' => 'Development site deployed successfully','details' => '']);
     }
 
+    public static function deploy($data): void  {
+        $username =  $data['username'] ?? '';
+        $domain = $data['domain'] ?? '';
+        $webFileUrl = $data['zip'] ?? '';
+        $webDir = "/home/$username/$domain";
+
+        error_log("deploy: DEBUG: " . print_r($data, true));
+        if (empty($username) || empty($domain) || empty($webFileUrl)) {
+            http_response_code(400);
+            echo json_encode(['error' => 'Missing required parameter']);
+            return;
+        }
+        $parts = explode('/', $uri);
+        $webFile = $parts[count($parts) - 1];
+        $tmpFile = sys_get_temp_dir() . '/' . $webFile;
+        if ($GLOBALS['debug'] == true) { error_log("Deploy new site on : " . $webDir); }
+        // download file
+        $options = array(
+            CURLOPT_FILE => fopen($tmpFile, 'w'),
+            CURLOPT_URL => $webFileUrl,
+            CURLOPT_TIMEOUT => 28800 // 8 hours timeout
+        );
+        $ch = curl_init();
+        curl_setopt_array($ch, $options);
+        curl_exec($ch);
+		$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+		$error = curl_error($ch);
+		if ($status != 200) {
+            http_response_code($status);
+            echo json_encode(['error' => 'Failed to download archive', 'details' => $error]);
+            return;
+		}
+        curl_close($ch);
+        // unzip file
+        exec("sudo /usr/bin/unzip -q -o $tmpFile -d $webDir/ 2>&1", $unzipOutput, $unzipReturnCode);
+        if ($cpReturnCode !== 0) {
+            error_log("deploy: ERROR: deplyoment of $domain failed, details => ". implode("\n", $unzipOutput));
+            http_response_code(500);
+            echo json_encode(['error' => 'Failed to copy placeholder', 'details' => implode("\n", $unzipOutput)]);
+            return;
+        }
+        // set ownership
+        if ($GLOBALS['debug'] == true) { error_log("Chown webdir: " . $domain); }
+        exec("sudo /usr/bin/chown $username:$username /home/$username/$domain -R 2>&1", $chownOutput, $chownReturnCode);
+        if ($chownReturnCode !== 0) {
+            error_log("deploy: ERROR: chown on /home/$username/$domain failed, details => " . implode("\n", $chownOutput));
+            http_response_code(500);
+            echo json_encode(['error' => 'Failed to chown backups dir', 'details' => implode("\n", $chownOutput)]);
+            return;
+        }
+        // remove tmpfile
+        if ($GLOBALS['debug'] == true) { error_log("Remove tmpdile: " . $tmpFile); }
+        exec("sudo /usr/bin/rm -f $tmpFile 2>&1", $rmOutput, $rmReturnCode);
+        if ($rmReturnCode !== 0) {
+            error_log("deploy: ERROR: could not remove $tmpFile, details => " . implode("\n", $rmOutput));
+            http_response_code(500);
+            echo json_encode(['error' => 'Failed to chown backups dir', 'details' => implode("\n", $rmOutput)]);
+            return;
+        }
+
+        echo json_encode(['success' => 'Site deployed successfully','details' => '']);
+    }
+
     public static function revert($data): void {
         $username = $data['username'] ?? '';
         $domain = $data['domain'] ?? '';

+ 3 - 2
index.php

@@ -96,8 +96,9 @@ switch ($endpoint) {
             $id = md5($domain.time());
             header('Content-Type: application/json; charset=utf-8', true);
             echo json_encode(array('result' => array('id' => $id, 'error' => $error)));
-            $deployment = new ZipController();
-            $deployment->extractZip(['zip' => $_POST['zip'], 'username' => $username, 'domain' => $domain]);
+            SiteController::deploy(['zip' => $_POST['zip'], 'username' => $username, 'domain' => $domain]);
+//            $deployment = new ZipController();
+//            $deployment->extractZip(['zip' => $_POST['zip'], 'username' => $username, 'domain' => $domain]);
         }
         if ($requestMethod == 'GET' && !empty($username) && !empty($domain)) {
             if ($GLOBALS['debug'] == true) {