|
|
@@ -0,0 +1,334 @@
|
|
|
+<?php
|
|
|
+namespace application\controllers;
|
|
|
+
|
|
|
+class SiteController {
|
|
|
+ public static function init($data): void {
|
|
|
+ $username = $data['username'];
|
|
|
+ $domain = $data['domain'];
|
|
|
+ $adminName = $data['admin_name'] ?? '';
|
|
|
+ $adminPassword = $data['admin_password'];
|
|
|
+ $webDir = "/home/$username/$domain";
|
|
|
+ $placeholderdir = "/var/www/catchall";
|
|
|
+ $configTemplate = '/etc/apache2/site-config.in';
|
|
|
+ $configFile = "/etc/apache2/sites-enabled/$domain.conf";
|
|
|
+
|
|
|
+ if (empty($username) || empty($domain) || empty($adminName) || empty($adminPassword)) {
|
|
|
+ error_log("deploy: ERROR: No username, domain, admin_name or admin_password provided");
|
|
|
+ http_response_code(400);
|
|
|
+ error_log("deploy: ERROR: UserName 1 " . $username);
|
|
|
+ error_log("deploy: ERROR: Domain 1 " . $domain);
|
|
|
+ error_log("deploy: ERROR: UserName 2 " . $data['username'] );
|
|
|
+ error_log("deploy: ERROR: Domain 2 " . $data['domain'] );
|
|
|
+ error_log("deploy: ERROR: AdminName " . $adminName);
|
|
|
+ error_log("deploy: ERROR: AdminPasswd" . $adminPassword);
|
|
|
+
|
|
|
+ error_log(print_r($data,true));
|
|
|
+ echo json_encode(['error' => 'Missing required parameters']);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ error_log(" Starting function deploy for " . $username . " and " . $domain . " DebugMode: " . $GLOBALS['debug']);
|
|
|
+
|
|
|
+ if ($GLOBALS['debug'] == true) { error_log("Creating Webdir ($webDir) for : " . $username); }
|
|
|
+ exec("sudo /usr/bin/mkdir -p $webDir 2>&1", $mkdirOutput, $mkdirReturnCode);
|
|
|
+ if ($mkdirReturnCode !== 0) {
|
|
|
+ error_log("deploy: ERROR: Create Webdir $webDir for $username failed, details => ". implode("\n", $mkdirOutput));
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to create web dir', 'details' => implode("\n", $mkdirOutput)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($GLOBALS['debug'] == true) { error_log("Chown webdir: " . $username); }
|
|
|
+ exec("sudo /usr/bin/chown $username:$username /home/$username/$webDir -R 2>&1", $chownOutput, $chownReturnCode);
|
|
|
+ if ($chownReturnCode !== 0) {
|
|
|
+ error_log("deploy: ERROR: chown on /home/$username/$webDir failed, details => " . implode("\n", $chownOutput));
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to chown homedir', 'details' => implode("\n", $chownOutput)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($GLOBALS['debug'] == true) { error_log("Reading Apache Config Template /etc/apache2/site-config.in"); }
|
|
|
+ $configContent = file_get_contents($configTemplate);
|
|
|
+
|
|
|
+ if ($GLOBALS['debug'] == true) { error_log("Replace config settings in Apache Config Template"); }
|
|
|
+ $configContent = str_replace(['DOCUMENTROOT', 'SERVERNAME','USERNAME', 'DOMAINNAME', 'SERVERALIAS'], [$webDir, $domain, $username, $domain, "" ], $configContent);
|
|
|
+
|
|
|
+ if ($GLOBALS['debug'] == true) { error_log("Running Certbot for Domain " . $domain); }
|
|
|
+ exec("sudo /usr/bin/certbot certonly --webroot -w /etc/apache2/letsencrypt -d $domain --non-interactive --agree-tos --email admin@$domain 2>&1", $output, $returnCode);
|
|
|
+ if ($returnCode !== 0) {
|
|
|
+ error_log("deploy: ERROR: certbot failed to create certificate on $domain, details => " . implode("\n", $output));
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Certbot failed', 'details' => implode("\n", $output)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if ($GLOBALS['debug'] == true) { error_log("Replace sslsettings in in Apache Config Template"); }
|
|
|
+ $configContent = str_replace('DOMAINNAME', $domain, $configContent);
|
|
|
+
|
|
|
+ if ($GLOBALS['debug'] == true) { error_log("Writing apache config file to " . $configFile); }
|
|
|
+ if (file_put_contents($configFile, $configContent) != true) {
|
|
|
+ error_log("deploy: ERROR: while writing apache config");
|
|
|
+ echo json_encode(['error' => 'Failed to write Apache config', 'details' => []]);
|
|
|
+ }
|
|
|
+
|
|
|
+ exec('sudo /usr/bin/systemctl reload apache2 2>&1', $apacheOutput, $apacheReturnCode);
|
|
|
+ if ($GLOBALS['debug'] == true) { error_log("Restarting Apache"); }
|
|
|
+ if ($apacheReturnCode !== 0) {
|
|
|
+ error_log("deploy: ERROR: Apache Reload error, details => " . implode("\n", $apacheOutput));
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $apacheOutput)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($GLOBALS['debug'] == true) { error_log("Install Placeholder on : " . $webDir); }
|
|
|
+ exec("sudo /usr/bin/cp -rf $placeholderdir/* $webDir/. 2>&1", $mkdirOutput, $cpReturnCode);
|
|
|
+ if ($cpReturnCode !== 0) {
|
|
|
+ error_log("deploy: ERROR: Copy placeholder for $username failed, details => ". implode("\n", $mkdirOutput));
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to copy placeholder', 'details' => implode("\n", $mkdirOutput)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ echo json_encode(['success' => 'Development site deployed successfully','details' => '']);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function revert($data): void {
|
|
|
+ $username = $data['username'] ?? '';
|
|
|
+ $domain = $data['domain'] ?? '';
|
|
|
+ $adminName = $data['admin_name'] ?? '';
|
|
|
+ $adminPassword = $data['admin_password'] ?? '';
|
|
|
+ $webDir = "/home/$username/$domain";
|
|
|
+ $placeholderdir = "/var/www/catchall";
|
|
|
+
|
|
|
+ exec("sudo /usr/bin/rm -rf $webDir 2>&1", $rmOutput, $rmReturnCode);
|
|
|
+ if ($rmReturnCode !== 0) {
|
|
|
+ error_log("revert: error on rm $webDir -r, details => " . implode("\n", $rmOutput));
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to remove webdir dir', 'details' => implode("\n", $rmOutput)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ exec("sudo /usr/bin/mkdir $webDir 2>&1", $mkdirOutput, $mkdirReturnCode);
|
|
|
+ error_log("revert: error on mkdir $webDir, details => " . implode("\n", $mkdirOutput));
|
|
|
+ if ($mkdirReturnCode !== 0) {
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to create web dir', 'details' => implode("\n", $mkdirOutput)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($GLOBALS['debug'] == true) { error_log("Install Placeholder on : " . $webDir); }
|
|
|
+ exec("sudo /usr/bin/cp -rf $placeholderdir/* $webDir/. 2>&1", $mkdirOutput, $cpReturnCode);
|
|
|
+ if ($cpReturnCode !== 0) {
|
|
|
+ error_log("deploy: ERROR: Copy placeholder for $username failed, details => ". implode("\n", $mkdirOutput));
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to copy placeholder', 'details' => implode("\n", $mkdirOutput)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($GLOBALS['debug'] == true) { error_log("Install Placeholder on : " . $webDir); }
|
|
|
+ exec("sudo /usr/bin/cp -rf $placeholderdir/* $webDir/. 2>&1", $mkdirOutput, $cpReturnCode);
|
|
|
+ if ($cpReturnCode !== 0) {
|
|
|
+ error_log("deploy: ERROR: Copy placeholder for $username failed, details => ". implode("\n", $mkdirOutput));
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to copy placeholder', 'details' => implode("\n", $mkdirOutput)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($GLOBALS['debug'] == true) { error_log("Change ownership on : " . $webDir); }
|
|
|
+ exec("sudo /usr/bin/chown $username:$username $webDir 2>&1", $chownOutput, $chownReturnCode);
|
|
|
+ if ($chownReturnCode !== 0) {
|
|
|
+ error_log("revert: chown error on $webDir, details => " . implode("\n", $chownOutput));
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to chown web dir', 'details' => implode("\n", $chownOutput)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ echo json_encode(['success' => 'Development site deployed successfully']);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function disable($data): void {
|
|
|
+ $domain = $data['domain'] ?? '';
|
|
|
+
|
|
|
+ if (empty($domain)) {
|
|
|
+ http_response_code(400);
|
|
|
+ echo json_encode(['error' => 'Missing required parameter: domainname']);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $enabledConfig = "/etc/apache2/sites-enabled/$domain.conf";
|
|
|
+ $availableConfig = "/etc/apache2/sites-available/$domain.conf";
|
|
|
+
|
|
|
+ if (!file_exists($enabledConfig)) {
|
|
|
+ http_response_code(404);
|
|
|
+ echo json_encode(['error' => 'Configuration file not found']);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!rename($enabledConfig, $availableConfig)) {
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to move configuration file']);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ exec('systemctl reload apache2', $output, $returnCode);
|
|
|
+ if ($returnCode !== 0) {
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $output)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ echo json_encode(['success' => 'Production site disabled successfully']);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function enable($data): void {
|
|
|
+ $domain = $data['domain'] ?? '';
|
|
|
+
|
|
|
+ if (empty($domain)) {
|
|
|
+ http_response_code(400);
|
|
|
+ echo json_encode(['error' => 'Missing required parameter: domainname']);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $availableConfig = "/etc/apache2/sites-available/$domain.conf";
|
|
|
+ $enabledConfig = "/etc/apache2/sites-enabled/$domain.conf";
|
|
|
+
|
|
|
+ if (!file_exists($availableConfig)) {
|
|
|
+ http_response_code(404);
|
|
|
+ echo json_encode(['error' => 'Configuration file not found']);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!rename($availableConfig, $enabledConfig)) {
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to move configuration file']);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ exec('systemctl reload apache2', $output, $returnCode);
|
|
|
+ if ($returnCode !== 0) {
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $output)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ echo json_encode(['success' => 'Production site enabled successfully']);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function isenabled($data): void {
|
|
|
+ $domain = $data['domain'] ?? '';
|
|
|
+
|
|
|
+ if (file_exists("/etc/apache2/sites-enabled/$domain.conf")) {
|
|
|
+ echo json_encode(['isenabled' => 'YES']);
|
|
|
+ } else {
|
|
|
+ echo json_encode(['isenabled' => 'NO']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function migrate($data): void {
|
|
|
+ $username = $data['username'] ?? '';
|
|
|
+ $domain = $data['domain'] ?? '';
|
|
|
+ $adminName = $data['admin_name'] ?? '';
|
|
|
+ $adminPassword = $data['admin_password'] ?? '';
|
|
|
+ if (empty($username) || empty($domain) || empty($adminName) || empty($adminPassword)) {
|
|
|
+ http_response_code(400);
|
|
|
+ echo json_encode(['error' => 'Missing required parameters']);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $webDir = "/home/$username/prod.$domain";
|
|
|
+ $configFile = "/etc/apache2/sites-enabled/prod.$domain.conf";
|
|
|
+ $configTemplate = '/etc/apache2/site-config.in';
|
|
|
+ exec("sudo /usr/bin/mkdir $webDir", $mkdirOutput, $mkdirReturnCode);
|
|
|
+ if ($mkdirReturnCode !== 0) {
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to create web dir', 'details' => implode("\n", $mkdirOutput)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ exec("sudo /usr/bin/chown $username:$username $webDir", $chownOutput, $chownReturnCode);
|
|
|
+ if ($chownReturnCode !== 0) {
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to chown web dir', 'details' => implode("\n", $chownOutput)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $configContent = file_get_contents($configTemplate);
|
|
|
+ $configContent = str_replace(['DOCUMENTROOT', 'SERVERNAME','USERNAME','DOMAINNAME', 'SERVERALIAS'], [$webDir, "prod.$domain","prod.$domain", "www.$domain" ], $configContent);
|
|
|
+ file_put_contents($configFile, $configContent);
|
|
|
+
|
|
|
+ exec("sudo /usr/bin/certbot certonly --webroot -w /etc/apache/letsencrypt -d www.$domain -d $domain --non-interactive --agree-tos --email admin@$domain", $output, $returnCode);
|
|
|
+ if ($returnCode !== 0) {
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Certbot failed', 'details' => implode("\n", $output)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ exec('sudo /usr/bin/systemctl reload apache2', $apacheOutput, $apacheReturnCode);
|
|
|
+ if ($apacheReturnCode !== 0) {
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $apacheOutput)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Deployment from dev to prod
|
|
|
+ exec("sudo /usr/bin/cp -r /home/$username/dev.$domain/* $webDir",$cpOutput,$cpReturnCode);
|
|
|
+ if ($cpReturnCode !== 0) {
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to copy dev installation into prod directory', 'details' => implode("\n", $cpOutput)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //TODO Replace Settings in Prod Environment
|
|
|
+
|
|
|
+ if ($GLOBALS['debug'] == true) { error_log("Chown WebDir $webDir"); }
|
|
|
+ exec("sudo /usr/bin/chown $username:$username $webDir -R 2>&1", $chownOutput, $chownReturnCode);
|
|
|
+ if ($chownReturnCode !== 0) {
|
|
|
+ error_log("deploy: chown error on $webDir, details => " . implode("\n", $chownOutput));
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to chown web dir', 'details' => implode("\n", $chownOutput)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ echo json_encode(['success' => 'Production site successfully from dev']);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function undeploy($data): void {
|
|
|
+ $username = $data['username'] ?? '';
|
|
|
+ $domain = $data['domain'] ?? '';
|
|
|
+ if (empty($username)) {
|
|
|
+ error_log("terminate: ERROR: No username provided");
|
|
|
+ http_response_code(400);
|
|
|
+ error_log(print_r($data,true));
|
|
|
+ echo json_encode(['error' => 'Missing username']);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if( strpos(file_get_contents("/etc/passwd"),$username) == false) {
|
|
|
+ error_log("terminate: ERROR: User $username does not exist");
|
|
|
+ http_response_code(400);
|
|
|
+ error_log(print_r($data,true));
|
|
|
+ echo json_encode(['error' => 'Unknown user']);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if(!empty($domain)) {
|
|
|
+ $configFile = "/etc/apache2/sites-enabled/$domain.conf";
|
|
|
+ if ($GLOBALS['debug'] == true) { error_log("Remove config of user : " . $username); }
|
|
|
+ exec("sudo /usr/bin/rm -f $configFile 2>&1", $userOutput, $userReturnCode);
|
|
|
+ exec("sudo /usr/bin/certbot delete --cert-name $domain --non-interactive 2>&1", $output, $returnCode);
|
|
|
+ if ($returnCode !== 0) {
|
|
|
+ error_log("deploy: ERROR: certbot failed to delete certificate on $domain, details => " . implode("\n", $output));
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Certbot failed', 'details' => implode("\n", $output)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ exec('sudo /usr/bin/systemctl reload apache2 2>&1', $apacheOutput, $apacheReturnCode);
|
|
|
+ if ($GLOBALS['debug'] == true) { error_log("Restarting Apache"); }
|
|
|
+ if ($apacheReturnCode !== 0) {
|
|
|
+ error_log("deploy: ERROR: Apache Reload error, details => " . implode("\n", $apacheOutput));
|
|
|
+ http_response_code(500);
|
|
|
+ echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $apacheOutput)]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ echo json_encode(['success' => 'Undeploy site prod.' . $domain . ' successfully']);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|