ProdController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace application\controllers;
  3. class ProdController {
  4. public static function disable($data): void {
  5. $domain = $data['domain'] ?? '';
  6. if (empty($domain)) {
  7. http_response_code(400);
  8. echo json_encode(['error' => 'Missing required parameter: domainname']);
  9. return;
  10. }
  11. $enabledConfig = "/etc/apache2/sites-enabled/prod.$domain.conf";
  12. $availableConfig = "/etc/apache2/sites-available/prod.$domain.conf";
  13. if (!file_exists($enabledConfig)) {
  14. http_response_code(404);
  15. echo json_encode(['error' => 'Configuration file not found']);
  16. return;
  17. }
  18. if (!rename($enabledConfig, $availableConfig)) {
  19. http_response_code(500);
  20. echo json_encode(['error' => 'Failed to move configuration file']);
  21. return;
  22. }
  23. exec('systemctl reload apache2', $output, $returnCode);
  24. if ($returnCode !== 0) {
  25. http_response_code(500);
  26. echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $output)]);
  27. return;
  28. }
  29. echo json_encode(['success' => 'Production site disabled successfully']);
  30. }
  31. public static function enable($data): void {
  32. $domain = $data['domain'] ?? '';
  33. if (empty($domain)) {
  34. http_response_code(400);
  35. echo json_encode(['error' => 'Missing required parameter: domainname']);
  36. return;
  37. }
  38. $availableConfig = "/etc/apache2/sites-available/prod.$domain.conf";
  39. $enabledConfig = "/etc/apache2/sites-enabled/prod.$domain.conf";
  40. if (!file_exists($availableConfig)) {
  41. http_response_code(404);
  42. echo json_encode(['error' => 'Configuration file not found']);
  43. return;
  44. }
  45. if (!rename($availableConfig, $enabledConfig)) {
  46. http_response_code(500);
  47. echo json_encode(['error' => 'Failed to move configuration file']);
  48. return;
  49. }
  50. exec('systemctl reload apache2', $output, $returnCode);
  51. if ($returnCode !== 0) {
  52. http_response_code(500);
  53. echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $output)]);
  54. return;
  55. }
  56. echo json_encode(['success' => 'Production site enabled successfully']);
  57. }
  58. public static function isenabled($data): void {
  59. $domain = $data['domain'] ?? '';
  60. if (file_exists("/etc/apache2/sites-enabled/prod.$domain.conf")) {
  61. echo json_encode(['isenabled' => 'YES']);
  62. } else {
  63. echo json_encode(['isenabled' => 'NO']);
  64. }
  65. }
  66. public static function migrateFromDev($data): void {
  67. $username = $data['username'] ?? '';
  68. $domain = $data['domain'] ?? '';
  69. $adminName = $data['admin_name'] ?? '';
  70. $adminPassword = $data['admin_password'] ?? '';
  71. if (empty($username) || empty($domain) || empty($adminName) || empty($adminPassword)) {
  72. http_response_code(400);
  73. echo json_encode(['error' => 'Missing required parameters']);
  74. return;
  75. }
  76. $webDir = "/home/$username/prod.$domain";
  77. $configFile = "/etc/apache2/sites-enabled/prod.$domain.conf";
  78. $configTemplate = '/etc/apache2/site-config.in';
  79. $devDatabaseName = $username . "_dev_";
  80. $devDatabaseName = $devDatabaseName . str_replace(['-', '.'], ["_", "_"], $domain);
  81. $prodDatabaseName = $username . "_prod_";
  82. $prodDatabaseName = $prodDatabaseName . str_replace(['-', '.'], ["_", "_"], $domain);
  83. exec("sudo /usr/bin/mkdir $webDir", $mkdirOutput, $mkdirReturnCode);
  84. if ($mkdirReturnCode !== 0) {
  85. http_response_code(500);
  86. echo json_encode(['error' => 'Failed to create web dir', 'details' => implode("\n", $mkdirOutput)]);
  87. return;
  88. }
  89. exec("sudo /usr/bin/chown $username:$username $webDir", $chownOutput, $chownReturnCode);
  90. if ($chownReturnCode !== 0) {
  91. http_response_code(500);
  92. echo json_encode(['error' => 'Failed to chown web dir', 'details' => implode("\n", $chownOutput)]);
  93. return;
  94. }
  95. $configContent = file_get_contents($configTemplate);
  96. $configContent = str_replace(['DOCUMENTROOT', 'SERVERNAME','USERNAME','DOMAINNAME', 'SERVERALIAS'], [$webDir, "prod.$domain","prod.$domain", "www.$domain" ], $configContent);
  97. file_put_contents($configFile, $configContent);
  98. 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);
  99. if ($returnCode !== 0) {
  100. http_response_code(500);
  101. echo json_encode(['error' => 'Certbot failed', 'details' => implode("\n", $output)]);
  102. return;
  103. }
  104. exec('sudo /usr/bin/systemctl reload apache2', $apacheOutput, $apacheReturnCode);
  105. if ($apacheReturnCode !== 0) {
  106. http_response_code(500);
  107. echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $apacheOutput)]);
  108. return;
  109. }
  110. //Create MySQL Database
  111. $sqlCommand = "sudo mysql \"create database $prodDatabaseName;\"";
  112. exec($sqlCommand,$mysqlOutput,$mysqlReturnCode);
  113. if ($mysqlReturnCode !== 0) {
  114. http_response_code(500);
  115. echo json_encode(['error' => 'Failed to create database $username', 'details' => implode("\n", $mysqlOutput)]);
  116. return;
  117. }
  118. //Grant permission
  119. $sqlCommand = "sudo mysql \"grant all on " . $prodDatabaseName . ".* to '" . $username . "'@'localhost';\"";
  120. exec($sqlCommand,$mysqlOutput,$mysqlReturnCode);
  121. if ($mysqlReturnCode !== 0) {
  122. http_response_code(500);
  123. echo json_encode(['error' => 'Failed to grant permission on $prodDatabaseName ot $username', 'details' => implode("\n", $mysqlOutput)]);
  124. return;
  125. }
  126. exec("sudo /usr/bin/mysqldump $devDatabaseName > /home/$username/$domain-dev-export.sql';",$mysqlOutput,$mysqlReturnCode);
  127. if ($mysqlReturnCode !== 0) {
  128. http_response_code(500);
  129. echo json_encode(['error' => 'Failed to dump development database', 'details' => implode("\n", $mysqlOutput)]);
  130. return;
  131. }
  132. exec("sudo /usr/bin/mysql $prodDatabaseName < /home/$username/$domain-dev-export.sql';",$mysqlOutput,$mysqlReturnCode);
  133. if ($mysqlReturnCode !== 0) {
  134. http_response_code(500);
  135. echo json_encode(['error' => 'Failed to import development database to prod database', 'details' => implode("\n", $mysqlOutput)]);
  136. return;
  137. }
  138. // Deployment of the Concrete CMS Application
  139. exec("sudo /usr/bin/cp -r /home/$username/dev.$domain/* $webDir",$cpOutput,$cpReturnCode);
  140. if ($cpReturnCode !== 0) {
  141. http_response_code(500);
  142. echo json_encode(['error' => 'Failed to copy dev installation into prod directory', 'details' => implode("\n", $cpOutput)]);
  143. return;
  144. }
  145. //TODO Replace Settings in Prod Environment
  146. if ($GLOBALS['debug'] == true) { error_log("Chown WebDir $webDir"); }
  147. exec("sudo /usr/bin/chown $username:$username $webDir -R 2>&1", $chownOutput, $chownReturnCode);
  148. if ($chownReturnCode !== 0) {
  149. error_log("deploy: chown error on $webDir, details => " . implode("\n", $chownOutput));
  150. http_response_code(500);
  151. echo json_encode(['error' => 'Failed to chown web dir', 'details' => implode("\n", $chownOutput)]);
  152. return;
  153. }
  154. echo json_encode(['success' => 'Production site successfully from dev']);
  155. }
  156. public static function terminate($data): void {
  157. }
  158. }