Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid the need to have 2-step deployments to get rid of a module #773

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
56 changes: 56 additions & 0 deletions robo-components/DeploymentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ public function deployPantheon(string $branch_name = 'master', ?string $commit_m
// Compile theme.
$this->themeCompile();

$this->uninstallExtraModules($pantheon_env);

// Remove the dev dependencies before pushing up to Pantheon.
$this->taskExec("composer install --no-dev")->run();

Expand Down Expand Up @@ -793,4 +795,58 @@ public function deployNotify(string $pantheon_environment = 'qa', string $issue_
}
}

/**
* Uninstalls modules that are not specified in core.extension.yml.
*
* @param string $pantheon_environment
* The Pantheon environment to run the commands against.
*
* @throws \Exception
*/
public function uninstallExtraModules(string $pantheon_environment): void {
$pantheon_info = $this->getPantheonNameAndEnv();
$pantheon_terminus_environment = $pantheon_info['name'] . '.' . $pantheon_environment;

// Get the list of currently enabled modules.
$installed_modules_result = $this->taskExec("terminus remote:drush $pantheon_terminus_environment pm:list -- --status=enabled --format=json --type=module")
->printOutput(FALSE)
->run();

if ($installed_modules_result->getExitCode() !== 0) {
throw new \Exception("Failed to get the list of installed modules.");
}

$installed_modules_data = json_decode($installed_modules_result->getMessage(), TRUE);
$installed_modules = array_keys($installed_modules_data);

$core_extension_file = 'config/sync/core.extension.yml';
if (!file_exists($core_extension_file)) {
throw new \Exception("core.extension.yml file not found.");
}

$core_extensions = Yaml::parseFile($core_extension_file);
$required_modules = array_keys($core_extensions['module']);

// Determine extra modules and uninstall them.
$modules_to_uninstall = array_diff($installed_modules, $required_modules);

if (!empty($modules_to_uninstall)) {
try {
$uninstall_success = $this->taskExec("terminus remote:drush $pantheon_terminus_environment -- pm:uninstall " . implode(' ', $modules_to_uninstall) . " --yes")
->run()
->getExitCode();
$needs_revert = !$uninstall_success;
}
catch (\Exception $e) {
$needs_revert = TRUE;
}

if ($needs_revert) {
// If uninstallation fails, reset configuration.
$this->taskExec("terminus remote:drush $pantheon_terminus_environment -- config:import --yes")->run();
throw new \Exception("Failed to uninstall modules. Configuration has been reset.");
}
}
}

}