|
|
@@ -81,10 +81,6 @@ class ProdController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
public static function migrateFromDev($data): void {
|
|
|
$username = $data['username'] ?? '';
|
|
|
$domain = $data['domain'] ?? '';
|
|
|
@@ -104,8 +100,6 @@ class ProdController {
|
|
|
$prodDatabaseName = $username . "_prod_";
|
|
|
$prodDatabaseName = $prodDatabaseName . str_replace(['-', '.'], ["_", "_"], $domain);
|
|
|
|
|
|
-
|
|
|
-
|
|
|
exec("sudo /usr/bin/mkdir $webDir", $mkdirOutput, $mkdirReturnCode);
|
|
|
if ($mkdirReturnCode !== 0) {
|
|
|
http_response_code(500);
|
|
|
@@ -124,7 +118,6 @@ class ProdController {
|
|
|
$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);
|
|
|
@@ -139,42 +132,7 @@ class ProdController {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- //Create MySQL Database
|
|
|
- $sqlCommand = "sudo mysql \"create database $prodDatabaseName;\"";
|
|
|
- exec($sqlCommand,$mysqlOutput,$mysqlReturnCode);
|
|
|
- if ($mysqlReturnCode !== 0) {
|
|
|
- http_response_code(500);
|
|
|
- echo json_encode(['error' => 'Failed to create database $username', 'details' => implode("\n", $mysqlOutput)]);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- //Grant permission
|
|
|
- $sqlCommand = "sudo mysql \"grant all on " . $prodDatabaseName . ".* to '" . $username . "'@'localhost';\"";
|
|
|
- exec($sqlCommand,$mysqlOutput,$mysqlReturnCode);
|
|
|
- if ($mysqlReturnCode !== 0) {
|
|
|
- http_response_code(500);
|
|
|
- echo json_encode(['error' => 'Failed to grant permission on $prodDatabaseName ot $username', 'details' => implode("\n", $mysqlOutput)]);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- exec("sudo /usr/bin/mysqldump $devDatabaseName > /home/$username/$domain-dev-export.sql';",$mysqlOutput,$mysqlReturnCode);
|
|
|
- if ($mysqlReturnCode !== 0) {
|
|
|
- http_response_code(500);
|
|
|
- echo json_encode(['error' => 'Failed to dump development database', 'details' => implode("\n", $mysqlOutput)]);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- exec("sudo /usr/bin/mysql $prodDatabaseName < /home/$username/$domain-dev-export.sql';",$mysqlOutput,$mysqlReturnCode);
|
|
|
- if ($mysqlReturnCode !== 0) {
|
|
|
- http_response_code(500);
|
|
|
- echo json_encode(['error' => 'Failed to import development database to prod database', 'details' => implode("\n", $mysqlOutput)]);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // Deployment of the Concrete CMS Application
|
|
|
+ // 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);
|
|
|
@@ -197,4 +155,44 @@ class ProdController {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ 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/prod.$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 prod.$domain --non-interactive 2>&1", $output, $returnCode);
|
|
|
+ if ($returnCode !== 0) {
|
|
|
+ error_log("deploy: ERROR: certbot failed to delete certificate on prod.$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']);
|
|
|
+ }
|
|
|
+
|
|
|
}
|