Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed May 16, 2020
1 parent 5524c9a commit fd35b4e
Show file tree
Hide file tree
Showing 23 changed files with 183 additions and 141 deletions.
6 changes: 3 additions & 3 deletions contrib/rsync.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@
return;
}

$host = $server->hostname();
$port = $server->port() ? ' -p' . $server->port() : '';
$host = $server->getHostname();
$port = $server->getPort() ? ' -p' . $server->getPort() : '';
$sshArguments = $server->getSshArguments();
$user = !$server->user() ? '' : $server->user() . '@';
$user = !$server->getRemoteUser() ? '' : $server->getRemoteUser() . '@';

runLocally("rsync -{$config['flags']} -e 'ssh$port $sshArguments' {{rsync_options}}{{rsync_includes}}{{rsync_excludes}}{{rsync_filter}} '$src/' '$user$host:$dst/'", $config);
});
4 changes: 2 additions & 2 deletions docs/examples/simple-deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ set( 'allow_anonymous_stats', true );

// note that staging server uses a non-standard port for SSH so I have to specify it here
host( 'staging.example.com' )
->user( 'eric' )
->getRemoteUser( 'eric' )
->stage( 'staging' )
->port( 30122 )
->set( 'deploy_path', '/usr/share/nginx/staging.example.com' )
->identityFile( '~/.ssh/id_rsa' );

// note that live server also uses a non-standard port for SSH
host( 'example.com' )
->user( 'eric' )
->getRemoteUser( 'eric' )
->stage( 'production' )
->port( 30122 )
->set( 'deploy_path', '/usr/share/nginx/example.com' )
Expand Down
6 changes: 3 additions & 3 deletions recipe/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
*/

set('hostname', function () {
return currentHost()->hostname();
return currentHost()->getHostname();
});

set('remote_user', function () {
return currentHost()->get('remote_user');
return currentHost()->getRemoteUser();
});

set('user', function () {
Expand Down Expand Up @@ -168,7 +168,7 @@
* Success message
*/
task('deploy:success', function () {
info(currentHost()->tag() . ' successfully deployed!');
info(currentHost()->getTag() . ' successfully deployed!');
})
->shallow()
->hidden();
Expand Down
18 changes: 0 additions & 18 deletions recipe/contao.php

This file was deleted.

2 changes: 1 addition & 1 deletion recipe/deploy/info.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
$what = "<fg=magenta;options=bold>HEAD</>";
}

info("deploy $what on " . currentHost()->tag());
info("deploy $what on " . currentHost()->getTag());
})
->shallow();
6 changes: 3 additions & 3 deletions src/Component/ProcessRunner/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function command(Host $host, string $command)
{
// -v for run command
if ($this->output->isVerbose()) {
$this->output->writeln("[{$host->tag()}] <fg=green;options=bold>run</> $command");
$this->output->writeln("[{$host->getTag()}] <fg=green;options=bold>run</> $command");
}
}

Expand Down Expand Up @@ -72,9 +72,9 @@ public function writeln(string $type, Host $host, string $line)
}

if ($type === Process::ERR) {
$line = "[{$host->tag()}] <fg=red>err</> $line";
$line = "[{$host->getTag()}] <fg=red>err</> $line";
} else {
$line = "[{$host->tag()}] $line";
$line = "[{$host->getTag()}] $line";
}

$this->output->writeln($line);
Expand Down
4 changes: 2 additions & 2 deletions src/Component/Ssh/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function generateControlPath(Host $host)
$connectionHashLength = 16; // Length of connection hash that OpenSSH appends to controlpath
$unixMaxPath = 104; // Theoretical max limit for path length
$homeDir = Unix::parseHomeDir('~');
$port = empty($host->port()) ? '' : ':' . $host->port();
$port = empty($host->getPort()) ? '' : ':' . $host->getPort();

// TODO: Reuse connection to same host.
// If a few host point to same hostname, we get with a few connections.
Expand All @@ -139,7 +139,7 @@ private function generateControlPath(Host $host)
//
// If simple change $connectionData to "{$host->getHostname()}$port" then
// only first host be able to perform runs.
$connectionData = "{$host->alias()}$port";
$connectionData = "{$host->getAlias()}$port";

$tryLongestPossible = 0;
$controlPath = '';
Expand Down
10 changes: 5 additions & 5 deletions src/Component/Ssh/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(OutputInterface $output, Printer $pop, Logger $logge

public function run(Host $host, string $command, array $config = [])
{
$hostname = $host->hostname();
$hostname = $host->getHostname();
$connectionString = $host->getConnectionString();
$defaults = [
'timeout' => $host->get('default_timeout', 300),
Expand All @@ -39,7 +39,7 @@ public function run(Host $host, string $command, array $config = [])

$config = array_merge($defaults, $config);
$sshArguments = $host->getSshArguments();
if ($host->sshMultiplexing()) {
if ($host->getSshMultiplexing()) {
$sshArguments = $this->initMultiplexing($host);
}

Expand All @@ -48,7 +48,7 @@ public function run(Host $host, string $command, array $config = [])
$become = sprintf('sudo -H -u %s', $host->get('become'));
}

$shellCommand = $host->shell();
$shellCommand = $host->getShell();

if (strtolower(substr(PHP_OS, 0, 3)) === 'win') {
$ssh = "ssh $sshArguments $connectionString $become \"$shellCommand; printf '[exit_code:%s]' $?;\"";
Expand All @@ -62,7 +62,7 @@ public function run(Host $host, string $command, array $config = [])
}

$this->pop->command($host, $command);
$this->logger->log("[{$host->alias()}] run $command");
$this->logger->log("[{$host->getAlias()}] run $command");

$terminalOutput = $this->pop->callback($host);
$callback = function ($type, $buffer) use ($host, $terminalOutput) {
Expand Down Expand Up @@ -109,7 +109,7 @@ private function parseExitStatus(Process $process)

public function connect(Host $host)
{
if ($host->sshMultiplexing()) {
if ($host->getSshMultiplexing()) {
$this->initMultiplexing($host);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Console/SelectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function selectHosts(Input $input, Output $output)
} else if ($input->isInteractive()) {
$hostsAliases = [];
foreach ($this->deployer->hosts as $host) {
$hostsAliases[] = $host->alias();
$hostsAliases[] = $host->getAlias();
}
/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');
Expand All @@ -65,7 +65,7 @@ protected function selectHosts(Input $input, Output $output)
$answer = $helper->ask($input, $output, $question);
$answer = array_unique($answer);
$hosts = $this->deployer->hosts->select(function (Host $host) use ($answer) {
return in_array($host->alias(), $answer, true);
return in_array($host->getAlias(), $answer, true);
});
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/Console/SshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($host instanceof Localhost) {
continue;
}
$hostsAliases[] = $host->alias();
$hostsAliases[] = $host->getAlias();
}

if (count($hostsAliases) === 0) {
Expand Down Expand Up @@ -96,7 +96,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$options = $host->getSshArguments();
$deployPath = $host->get('deploy_path', '~');

passthru("ssh -t $options {$host->hostname()} 'cd '''$deployPath/current'''; $shell_path'");
passthru("ssh -t $options {$host->getHostname()} 'cd '''$deployPath/current'''; $shell_path'");
return 0;
}
}
22 changes: 11 additions & 11 deletions src/Executor/Messenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function endTask(Task $task)
public function endOnHost(Host $host)
{
if ($this->output->isVeryVerbose()) {
$this->output->writeln("[{$host->tag()}] <info>ok</info>");
$this->output->writeln("[{$host->getTag()}] <info>ok</info>");
}
}

Expand All @@ -77,23 +77,23 @@ public function renderException(Throwable $exception, Host $host)
if ($exception instanceof RunException) {

$message = "";
$message .= "[{$host->tag()}] <fg=white;bg=red> error </> <comment>in {$exception->getTaskFilename()} on line {$exception->getTaskLineNumber()}:</>\n";
$message .= "[{$host->getTag()}] <fg=white;bg=red> error </> <comment>in {$exception->getTaskFilename()} on line {$exception->getTaskLineNumber()}:</>\n";
if ($this->output->getVerbosity() === Output::VERBOSITY_NORMAL) {
$message .= "[{$host->tag()}] <fg=green;options=bold>run</> {$exception->getCommand()}\n";
$message .= "[{$host->getTag()}] <fg=green;options=bold>run</> {$exception->getCommand()}\n";
foreach (explode("\n", $exception->getErrorOutput()) as $line) {
$line = trim($line);
if ($line !== "") {
$message .= "[{$host->tag()}] <fg=red>err</> $line\n";
$message .= "[{$host->getTag()}] <fg=red>err</> $line\n";
}
}
foreach (explode("\n", $exception->getOutput()) as $line) {
$line = trim($line);
if ($line !== "") {
$message .= "[{$host->tag()}] $line\n";
$message .= "[{$host->getTag()}] $line\n";
}
}
}
$message .= "[{$host->tag()}] <fg=red>exit code</> {$exception->getExitCode()} ({$exception->getExitCodeText()})\n";
$message .= "[{$host->getTag()}] <fg=red>exit code</> {$exception->getExitCode()} ({$exception->getExitCodeText()})\n";
$this->output->write($message);

} else {
Expand All @@ -105,20 +105,20 @@ public function renderException(Throwable $exception, Host $host)
$file = $exception->getTaskFilename();
$line = $exception->getTaskLineNumber();
}
$message .= "[{$host->tag()}] <fg=white;bg=red> $class </> <comment>in $file on line $line:</>\n";
$message .= "[{$host->tag()}]\n";
$message .= "[{$host->getTag()}] <fg=white;bg=red> $class </> <comment>in $file on line $line:</>\n";
$message .= "[{$host->getTag()}]\n";
foreach (explode("\n", $exception->getMessage()) as $line) {
$line = trim($line);
if ($line !== "") {
$message .= "[{$host->tag()}] <comment>$line</comment>\n";
$message .= "[{$host->getTag()}] <comment>$line</comment>\n";
}
}
$message .= "[{$host->tag()}]\n";
$message .= "[{$host->getTag()}]\n";
if ($this->output->isDebug()) {
foreach (explode("\n", $exception->getTraceAsString()) as $line) {
$line = trim($line);
if ($line !== "") {
$message .= "[{$host->tag()}] $line\n";
$message .= "[{$host->getTag()}] $line\n";
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Executor/ParallelExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private function connect(array $hosts)

while ($process->isRunning()) {
$this->gatherOutput([$process], $callback);
$this->output->write(spinner(str_pad("connect {$host->tag()}", intval(getenv('COLUMNS')) - 1)));
$this->output->write(spinner(str_pad("connect {$host->getTag()}", intval(getenv('COLUMNS')) - 1)));
usleep(1000);
}
}
Expand Down Expand Up @@ -205,10 +205,10 @@ protected function getProcess(Host $host, Task $task): Process
$dep = PHP_BINARY . ' ' . DEPLOYER_BIN;
$configDirectory = $host->get('config_directory');
$decorated = $this->output->isDecorated() ? '--decorated' : '';
$command = "$dep worker $task {$host->alias()} $configDirectory {$this->input} $decorated";
$command = "$dep worker $task {$host->getAlias()} $configDirectory {$this->input} $decorated";

if ($this->output->isDebug()) {
$this->output->writeln("[{$host->tag()}] $command");
$this->output->writeln("[{$host->getTag()}] $command");
}

return Process::fromShellCommandline($command);
Expand Down
8 changes: 4 additions & 4 deletions src/Executor/Planner.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public function __construct(OutputInterface $output, $hosts)
$headers = [];
$this->template = [];
foreach ($hosts as $host) {
$headers[] = $host->tag();
$this->template[] = $host->alias();
$headers[] = $host->getTag();
$this->template[] = $host->getAlias();
}
$this->table = new Table($output);
$this->table->setHeaders($headers);
Expand All @@ -52,7 +52,7 @@ public function __construct(OutputInterface $output, $hosts)
*/
public function commit(array $hosts, Task $task)
{
if (count($hosts) === 1 && $hosts[0]->alias() === 'localhost') {
if (count($hosts) === 1 && $hosts[0]->getAlias() === 'localhost') {
$row = [];
foreach ($this->template as $alias) {
$row[] = "-";
Expand All @@ -65,7 +65,7 @@ public function commit(array $hosts, Task $task)
foreach ($this->template as $alias) {
$on = "-";
foreach ($hosts as $host) {
if ($alias === $host->alias()) {
if ($alias === $host->getAlias()) {
$on = $task->getName();
break;
}
Expand Down
9 changes: 5 additions & 4 deletions src/Host/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ public function load($file)
} else {
$host = new Host($hostname);
$methods = [
'sshOptions',
'sshFlags',
'sshOptions' => 'setSshOptions',
'sshFlags' => 'setSshFlags',
];

foreach ($methods as $method) {
foreach (array_keys($methods) as $method) {
if (isset($config[$method])) {
$host->$method($config[$method]);
$set = $methods[$method];
$host->$set($config[$method]);
}
}
}
Expand Down
Loading

0 comments on commit fd35b4e

Please sign in to comment.