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

Building a lock:sync command #2120

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/Commands/Lock/SyncCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Pantheon\Terminus\Commands\Lock;

use Consolidation\OutputFormatters\StructuredData\PropertyList;
use Pantheon\Terminus\Commands\TerminusCommand;
use Pantheon\Terminus\Commands\StructuredListTrait;
use Pantheon\Terminus\Commands\WorkflowProcessingTrait;
use Pantheon\Terminus\Site\SiteAwareInterface;
use Pantheon\Terminus\Site\SiteAwareTrait;

/**
* Class SyncCommand
* @package Pantheon\Terminus\Commands\Lock
*/
class SyncCommand extends TerminusCommand implements SiteAwareInterface
{
use SiteAwareTrait;
use WorkflowProcessingTrait;

/**
* Displays HTTP basic authentication status and configuration for the environment.
*
* @authorize
*
* @command lock:sync
*
* @param string $site_env Origin site & environment in the format `site-name.env`
* @param string $target_env Target environment
*
* @usage <site>.<env> <target_env> Synchronizes the lock settings from <site>'s <env> environment to <target_env>.
*/
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,]
);
}
}
1 change: 1 addition & 0 deletions src/Terminus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down