Skip to content

Commit

Permalink
Merge pull request #29 from sitegeist/feature/proxyAssetSupport
Browse files Browse the repository at this point in the history
!!! FEATURE: Proxy-asset support
  • Loading branch information
mficzel authored Feb 20, 2019
2 parents f2c7d7d + 1e1499e commit 777148d
Show file tree
Hide file tree
Showing 15 changed files with 707 additions and 120 deletions.
22 changes: 14 additions & 8 deletions Classes/Command/AbstractCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Cli\CommandController;
use Sitegeist\MagicWand\Domain\Service\ConfigurationService;

abstract class AbstractCommandController extends CommandController
{

const HIDE_RESULT = 1;
const HIDE_COMMAND = 2;

Expand Down Expand Up @@ -44,6 +44,12 @@ abstract class AbstractCommandController extends CommandController
*/
protected $flowCommand;

/**
* @Flow\Inject
* @var ConfigurationService
*/
protected $configurationService;

/**
* @param string $commands
* @param array $arguments
Expand All @@ -53,11 +59,11 @@ protected function executeLocalShellCommand($command, $arguments = [], $options
{
$customizedCommand = call_user_func_array('sprintf', array_merge([$command], $arguments));
if (!in_array(self::HIDE_COMMAND, $options)) {
$this->outputLine($customizedCommand);
$this->renderLine($customizedCommand);
}
$customizedCommandResult = shell_exec($customizedCommand);
if (is_string($customizedCommandResult) && !in_array(self::HIDE_RESULT, $options)) {
$this->outputLine($customizedCommandResult);
$this->renderLine($customizedCommandResult);
}
return $customizedCommandResult;
}
Expand Down Expand Up @@ -87,19 +93,19 @@ protected function executeLocalFlowCommand($command, $arguments = [], $options =
/**
* @param $line
*/
protected function outputHeadLine($line = '', $arguments = [])
protected function renderHeadLine($line = '', $arguments = [])
{
$this->headlineNumber++;
$this->outputLine();
$this->outputLine('<b>' . $this->headlineNumber . '. ' . $line . '</b>', $arguments);
$this->outputLine();
$this->renderLine();
$this->renderLine('<b>' . $this->headlineNumber . '. ' . $line . '</b>', $arguments);
$this->renderLine();
}

/**
* @param string $line
* @param array $arguments
*/
protected function outputLine(string $line = '', array $arguments = [])
protected function renderLine(string $line = '', array $arguments = [])
{
$filteredLine = $line;
foreach ($this->secrets as $secret) {
Expand Down
144 changes: 79 additions & 65 deletions Classes/Command/CloneCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Neos\Utility\Arrays;
use Neos\Flow\Core\Bootstrap;
use Sitegeist\MagicWand\DBAL\SimpleDBAL;
use Symfony\Component\Yaml\Yaml;

/**
* @Flow\Scope("singleton")
Expand Down Expand Up @@ -48,19 +49,11 @@ public function listCommand()
{
if ($this->clonePresets) {
foreach ($this->clonePresets as $presetName => $presetConfiguration) {
$this->outputHeadLine($presetName);
foreach ($presetConfiguration as $key => $value) {
if (is_array($value)) {
$this->outputLine(' - ' . $key . ':');

foreach ($value as $line) {
$this->outputLine(' ' . $line);
}

continue;
}

$this->outputLine(' - ' . $key . ': ' . $value);
$this->renderHeadLine($presetName);
$presetConfigurationAsYaml = Yaml::dump($presetConfiguration);
$lines = explode(PHP_EOL, $presetConfigurationAsYaml);
foreach ($lines as $line) {
$this->renderLine($line);
}
}
}
Expand All @@ -75,7 +68,7 @@ public function listCommand()
public function defaultCommand(bool $yes = false, bool $keepDb = false) : void
{
if ($this->defaultPreset === null || $this->defaultPreset === '') {
$this->outputLine('There is no default preset configured!');
$this->renderLine('There is no default preset configured!');
$this->quit(1);
}

Expand All @@ -93,31 +86,35 @@ public function presetCommand($presetName, $yes = false, $keepDb = false)
{
if (count($this->clonePresets) > 0) {
if ($this->clonePresets && array_key_exists($presetName, $this->clonePresets)) {
$this->outputLine('Clone by preset ' . $presetName);
$this->remoteHostCommand(
$this->clonePresets[$presetName]['host'],
$this->clonePresets[$presetName]['user'],
$this->clonePresets[$presetName]['port'],
$this->clonePresets[$presetName]['path'],
$this->clonePresets[$presetName]['context'],
(isset($this->clonePresets[$presetName]['postClone']) ?
$this->clonePresets[$presetName]['postClone'] : null

$this->configurationService->setCurrentPreset($presetName);
$configuration = $this->configurationService->getCurrentConfiguration();

$this->renderLine('Clone by preset ' . $presetName);
$this->cloneRemoteHost(
$configuration['host'],
$configuration['user'],
$configuration['port'],
$configuration['path'],
$configuration['context'],
(isset($configuration['postClone']) ?
$configuration['postClone'] : null
),
$yes,
$keepDb,
(isset($this->clonePresets[$presetName]['flowCommand']) ?
$this->clonePresets[$presetName]['flowCommand'] : null
(isset($configuration['flowCommand']) ?
$configuration['flowCommand'] : null
),
(isset($this->clonePresets[$presetName]['sshOptions']) ?
$this->clonePresets[$presetName]['sshOptions'] : ''
(isset($configuration['sshOptions']) ?
$configuration['sshOptions'] : ''
)
);
} else {
$this->outputLine('The preset ' . $presetName . ' was not found!');
$this->renderLine('The preset ' . $presetName . ' was not found!');
$this->quit(1);
}
} else {
$this->outputLine('No presets found!');
$this->renderLine('No presets found!');
$this->quit(1);
}
}
Expand All @@ -136,7 +133,7 @@ public function presetCommand($presetName, $yes = false, $keepDb = false)
* @param string $remoteFlowCommand the flow command to execute on the remote system
* @param string $sshOptions additional options for the ssh command
*/
public function remoteHostCommand(
protected function cloneRemoteHost(
$host,
$user,
$port,
Expand All @@ -155,12 +152,12 @@ public function remoteHostCommand(
}

// read local configuration
$this->outputHeadLine('Read local configuration');
$this->renderHeadLine('Read local configuration');

$localDataPersistentPath = FLOW_PATH_ROOT . 'Data/Persistent';

// read remote configuration
$this->outputHeadLine('Fetch remote configuration');
$this->renderHeadLine('Fetch remote configuration');
$remotePersistenceConfigurationYaml = $this->executeLocalShellCommand(
'ssh -p %s %s %s@%s "cd %s; FLOW_CONTEXT=%s '
. $remoteFlowCommand
Expand Down Expand Up @@ -188,15 +185,15 @@ public function remoteHostCommand(
#################

if (!$yes) {
$this->outputLine("Are you sure you want to do this? Type 'yes' to continue: ");
$this->renderLine("Are you sure you want to do this? Type 'yes' to continue: ");
$handle = fopen("php://stdin", "r");
$line = fgets($handle);
if (trim($line) != 'yes') {
$this->outputLine('exit');
$this->renderLine('exit');
$this->quit(1);
} else {
$this->outputLine();
$this->outputLine();
$this->renderLine();
$this->renderLine();
}
}

Expand Down Expand Up @@ -238,7 +235,7 @@ public function remoteHostCommand(
########################

if ($keepDb == false) {
$this->outputHeadLine('Drop and Recreate DB');
$this->renderHeadLine('Drop and Recreate DB');

$emptyLocalDbSql = $this->dbal->flushDbSql($this->databaseConfiguration['driver'], $this->databaseConfiguration['dbname']);

Expand All @@ -257,14 +254,14 @@ public function remoteHostCommand(
]
);
} else {
$this->outputHeadLine('Skipped (Drop and Recreate DB)');
$this->renderHeadLine('Skipped (Drop and Recreate DB)');
}

######################
# Transfer Database #
######################

$this->outputHeadLine('Transfer Database');
$this->renderHeadLine('Transfer Database');
$this->executeLocalShellCommand(
'ssh -p %s %s %s@%s -- %s | %s',
[
Expand Down Expand Up @@ -295,24 +292,41 @@ public function remoteHostCommand(
# Transfer Files #
##################

$this->outputHeadLine('Transfer Files');
$this->executeLocalShellCommand(
'rsync -e "ssh -p %s %s" -kLr %s@%s:%s/* %s',
[
$port,
addslashes($sshOptions),
$user,
$host,
$remoteDataPersistentPath,
$localDataPersistentPath
]
);
$resourceProxyConfiguration = $this->configurationService->getCurrentConfigurationByPath('resourceProxy');

if (!$resourceProxyConfiguration) {
$this->renderHeadLine('Transfer Files');
$this->executeLocalShellCommand(
'rsync -e "ssh -p %s %s" -kLr %s@%s:%s/* %s',
[
$port,
addslashes($sshOptions),
$user,
$host,
$remoteDataPersistentPath,
$localDataPersistentPath
]
);
} else {
$this->renderHeadLine('Transfer Files - without Resources because a resourceProxyConfiguration is found');
$this->executeLocalShellCommand(
'rsync -e "ssh -p %s %s" --exclude "Resources/*" -kLr %s@%s:%s/* %s',
[
$port,
addslashes($sshOptions),
$user,
$host,
$remoteDataPersistentPath,
$localDataPersistentPath
]
);
}

#########################
# Transfer Translations #
#########################

$this->outputHeadLine('Transfer Translations');
$this->renderHeadLine('Transfer Translations');

$remoteDataTranslationsPath = $path . '/Data/Translations';
$localDataTranslationsPath = FLOW_PATH_ROOT . 'Data/Translations';
Expand Down Expand Up @@ -348,37 +362,37 @@ public function remoteHostCommand(
# Clear Caches #
################

$this->outputHeadLine('Clear Caches');
$this->renderHeadLine('Clear Caches');
$this->executeLocalFlowCommand('flow:cache:flush');

##################
# Set DB charset #
##################
if ($this->databaseConfiguration['driver'] == 'pdo_mysql' && $remotePersistenceConfiguration['charset'] != 'utf8mb4') {
$this->outputHeadLine('Set DB charset');
if ($this->databaseConfiguration['driver'] == 'pdo_mysql' && $remotePersistenceConfiguration['charset'] != 'utf8mb4' ) {
$this->renderHeadLine('Set DB charset');
$this->executeLocalFlowCommand('database:setcharset');
}

##############
# Migrate DB #
##############

$this->outputHeadLine('Migrate cloned DB');
$this->renderHeadLine('Migrate cloned DB');
$this->executeLocalFlowCommand('doctrine:migrate');

#####################
# Publish Resources #
#####################

$this->outputHeadLine('Publish Resources');
$this->renderHeadLine('Publish Resources');
$this->executeLocalFlowCommand('resource:publish');

##############
# Post Clone #
##############

if ($postClone) {
$this->outputHeadLine('Execute post_clone commands');
$this->renderHeadLine('Execute post_clone commands');
if (is_array($postClone)) {
foreach ($postClone as $postCloneCommand) {
$this->executeLocalShellCommandWithFlowContext($postCloneCommand);
Expand All @@ -395,8 +409,8 @@ public function remoteHostCommand(
$endTimestamp = time();
$duration = $endTimestamp - $startTimestamp;

$this->outputHeadLine('Done');
$this->outputLine('Successfully cloned in %s seconds', [$duration]);
$this->renderHeadLine('Done');
$this->renderLine('Successfully cloned in %s seconds', [$duration]);
}

/**
Expand All @@ -406,22 +420,22 @@ public function remoteHostCommand(
*/
protected function checkConfiguration($remotePersistenceConfiguration)
{
$this->outputHeadLine('Check Configuration');
$this->renderHeadLine('Check Configuration');
if (!$this->dbal->driverIsSupported($remotePersistenceConfiguration['driver'])
&& !$this->dbal->driverIsSupported($this->databaseConfiguration['driver'])) {
$this->outputLine(sprintf('<error>ERROR:</error> Only pdo_pgsql and pdo_mysql drivers are supported! Remote: "%s" Local: "%s" configured.', $remotePersistenceConfiguration['driver'], $this->databaseConfiguration['driver']));
$this->renderLine(sprintf('<error>ERROR:</error> Only pdo_pgsql and pdo_mysql drivers are supported! Remote: "%s" Local: "%s" configured.', $remotePersistenceConfiguration['driver'], $this->databaseConfiguration['driver']));
$this->quit(1);
}
if ($remotePersistenceConfiguration['driver'] !== $this->databaseConfiguration['driver']) {
$this->outputLine('<error>ERROR:</error> Remote and local databases must use the same driver!');
$this->renderLine('<error>ERROR:</error> Remote and local databases must use the same driver!');
$this->quit(1);
}
if (in_array($remotePersistenceConfiguration['charset'], ['utf8', 'utf8mb4']) && in_array($this->databaseConfiguration['charset'], ['utf8', 'utf8mb4'])) {
// we accept utf8 and utf8mb4 being similar enough
} else if ($remotePersistenceConfiguration['charset'] != $this->databaseConfiguration['charset']) {
$this->outputLine(sprintf('<error>ERROR:</error> Remote and local databases must use the same charset! Remote: "%s", Local: "%s" configured.', $remotePersistenceConfiguration['charset'], $this->databaseConfiguration['charset']));
$this->renderLine(sprintf('<error>ERROR:</error> Remote and local databases must use the same charset! Remote: "%s", Local: "%s" configured.', $remotePersistenceConfiguration['charset'], $this->databaseConfiguration['charset']));
$this->quit(1);
}
$this->outputLine(' - Configuration seems ok ...');
$this->renderLine(' - Configuration seems ok ...');
}
}
Loading

0 comments on commit 777148d

Please sign in to comment.