diff --git a/src/Commands/Lock/SyncCommand.php b/src/Commands/Lock/SyncCommand.php new file mode 100644 index 000000000..0980f02d9 --- /dev/null +++ b/src/Commands/Lock/SyncCommand.php @@ -0,0 +1,69 @@ +. Synchronizes the lock settings from 's environment to . + */ + public function sync($site_env, $target_env) + { + /** @var \Pantheon\Terminus\Models\Site $site */ + /** @var \Pantheon\Terminus\Models\Environment $source_env */ + list($site, $source_env) = $this->getSiteEnv($site_env); + + if ($source_env->id === $target_env) { + $this->log()->notice('The sync has been skipped because the source and target environments are the same.'); + return; + } + + $target = $site->getEnvironments()->get($target_env); + + /** @var \Pantheon\Terminus\Models\Lock $source_lock */ + $source_lock = $source_env->getLock(); + $target_lock = $target->getLock(); + + if ($source_lock->serialize() == $target_lock->serialize()) { + $this->log()->notice('The source and target environment locks already match.'); + return; + } + + if (!$source_lock->isLocked()) { + $this->processWorkflow($target_lock->disable()); + } else { + $this->processWorkflow($target_lock->enable([ + 'username' => $source_lock->get('username'), + 'password' => $source_lock->get('password'), + ])); + } + + $this->log()->notice( + '{site}.{env} lock has been synced to {target_env}.', + ['site' => $site->get('name'), 'env' => $source_env->id, 'target_env' => $target->id,] + ); + } +} diff --git a/src/Terminus.php b/src/Terminus.php index d08302dc0..4fca0cb24 100644 --- a/src/Terminus.php +++ b/src/Terminus.php @@ -173,6 +173,7 @@ private function addBuiltInCommandsAndHooks() 'Pantheon\\Terminus\\Commands\\Lock\\DisableCommand', 'Pantheon\\Terminus\\Commands\\Lock\\EnableCommand', 'Pantheon\\Terminus\\Commands\\Lock\\InfoCommand', + 'Pantheon\\Terminus\\Commands\\Lock\\SyncCommand', 'Pantheon\\Terminus\\Commands\\MachineToken\\DeleteAllCommand', 'Pantheon\\Terminus\\Commands\\MachineToken\\DeleteCommand', 'Pantheon\\Terminus\\Commands\\MachineToken\\ListCommand',