|
|
@@ -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'] ?? '';
|