2016-12-31 07:52:25 +01:00
|
|
|
<?php
|
2022-04-10 06:20:03 +02:00
|
|
|
|
2016-12-31 07:52:25 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Magallanes package.
|
|
|
|
*
|
|
|
|
* (c) Andrés Montañez <andres@andresmontanez.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Mage\Runtime;
|
|
|
|
|
2017-01-12 02:22:21 +01:00
|
|
|
use Mage\Deploy\Strategy\ReleasesStrategy;
|
|
|
|
use Mage\Deploy\Strategy\RsyncStrategy;
|
2017-01-11 01:28:32 +01:00
|
|
|
use Mage\Deploy\Strategy\StrategyInterface;
|
2016-12-31 07:52:25 +01:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Psr\Log\LogLevel;
|
|
|
|
use Symfony\Component\Process\Process;
|
2017-01-07 05:53:57 +01:00
|
|
|
use Mage\Runtime\Exception\RuntimeException;
|
2022-04-10 06:20:03 +02:00
|
|
|
use Mage\Task\AbstractTask;
|
2016-12-31 07:52:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Runtime is a container of all run in time configuration, stages of progress, hosts being deployed, etc.
|
|
|
|
*
|
|
|
|
* @author Andrés Montañez <andresmontanez@gmail.com>
|
|
|
|
*/
|
2017-01-01 05:33:34 +01:00
|
|
|
class Runtime
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
2022-04-10 06:20:03 +02:00
|
|
|
public const PRE_DEPLOY = 'pre-deploy';
|
|
|
|
public const ON_DEPLOY = 'on-deploy';
|
|
|
|
public const POST_DEPLOY = 'post-deploy';
|
|
|
|
public const ON_RELEASE = 'on-release';
|
|
|
|
public const POST_RELEASE = 'post-release';
|
2017-01-01 05:33:34 +01:00
|
|
|
|
2016-12-31 07:52:25 +01:00
|
|
|
/**
|
2022-04-10 06:20:03 +02:00
|
|
|
* @var array<string, mixed> Magallanes configuration
|
2016-12-31 07:52:25 +01:00
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
protected array $configuration = [];
|
2016-12-31 07:52:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string|null Environment being deployed
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
protected ?string $environment = null;
|
2016-12-31 07:52:25 +01:00
|
|
|
|
|
|
|
/**
|
2022-04-10 06:20:03 +02:00
|
|
|
* @var string|null Stage of Deployment
|
2016-12-31 07:52:25 +01:00
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
protected ?string $stage = null;
|
2016-12-31 07:52:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string|null The host being deployed to
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
protected ?string $workingHost = null;
|
2016-12-31 07:52:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string|null The Release ID
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
protected ?string $releaseId = null;
|
2016-12-31 07:52:25 +01:00
|
|
|
|
|
|
|
/**
|
2022-04-10 06:20:03 +02:00
|
|
|
* @var array<string, string> Hold a bag of variables for sharing information between tasks, if needed
|
2016-12-31 07:52:25 +01:00
|
|
|
*/
|
|
|
|
protected $vars = [];
|
|
|
|
|
2022-04-10 06:20:03 +02:00
|
|
|
protected ?LoggerInterface $logger = null;
|
2016-12-31 07:52:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool Indicates if a Rollback operation is in progress
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
protected bool $rollback = false;
|
2016-12-31 07:52:25 +01:00
|
|
|
|
2022-04-10 06:20:03 +02:00
|
|
|
public function isWindows(): bool
|
2021-02-20 21:16:24 +01:00
|
|
|
{
|
|
|
|
return stripos(PHP_OS, 'WIN') === 0;
|
|
|
|
}
|
|
|
|
|
2017-01-01 05:28:58 +01:00
|
|
|
/**
|
|
|
|
* Generate the Release ID
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function generateReleaseId(): self
|
2017-01-01 05:28:58 +01:00
|
|
|
{
|
|
|
|
$this->setReleaseId(date('YmdHis'));
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-12-31 07:52:25 +01:00
|
|
|
/**
|
|
|
|
* Sets the Release ID
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function setReleaseId(string $releaseId): self
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
$this->releaseId = $releaseId;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the current Release ID
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getReleaseId(): ?string
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
return $this->releaseId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Runtime in Rollback mode On or Off
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function setRollback(bool $inRollback): self
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
$this->rollback = $inRollback;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates if Runtime is in rollback
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function inRollback(): bool
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
return $this->rollback;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a value in the Vars bag
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function setVar(string $key, string $value): self
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
$this->vars[$key] = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-10 06:20:03 +02:00
|
|
|
* Retrieve a value from the Vars bag, or a default (null) if not set
|
2016-12-31 07:52:25 +01:00
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getVar(string $key, mixed $default = null): ?string
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
if (array_key_exists($key, $this->vars)) {
|
|
|
|
return $this->vars[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Logger instance
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function setLogger(?LoggerInterface $logger = null): self
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
$this->logger = $logger;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Magallanes Configuration to the Runtime
|
|
|
|
*
|
2022-04-10 06:20:03 +02:00
|
|
|
* @param array<string, mixed> $configuration
|
2016-12-31 07:52:25 +01:00
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function setConfiguration(array $configuration): self
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
$this->configuration = $configuration;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the Configuration
|
|
|
|
*
|
2022-04-10 06:20:03 +02:00
|
|
|
* @return array<string, mixed> $configuration
|
2016-12-31 07:52:25 +01:00
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getConfiguration(): array
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
return $this->configuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-12 02:22:21 +01:00
|
|
|
* Retrieves the Configuration Option for a specific section in the configuration
|
2016-12-31 07:52:25 +01:00
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getConfigOption(string $key, mixed $default = null): mixed
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
if (array_key_exists($key, $this->configuration)) {
|
|
|
|
return $this->configuration[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-12 02:22:21 +01:00
|
|
|
* Returns the Configuration Option for a specific section the current Environment
|
2017-01-11 21:59:41 +01:00
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getEnvOption(string $key, mixed $default = null): mixed
|
2017-01-11 21:59:41 +01:00
|
|
|
{
|
2022-04-10 06:20:03 +02:00
|
|
|
if (
|
|
|
|
!array_key_exists('environments', $this->configuration) ||
|
|
|
|
!is_array($this->configuration['environments'])
|
|
|
|
) {
|
2017-01-11 21:59:41 +01:00
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!array_key_exists($this->environment, $this->configuration['environments'])) {
|
|
|
|
return $default;
|
2016-12-31 07:52:25 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 21:59:41 +01:00
|
|
|
if (array_key_exists($key, $this->configuration['environments'][$this->environment])) {
|
|
|
|
return $this->configuration['environments'][$this->environment][$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $default;
|
2016-12-31 07:52:25 +01:00
|
|
|
}
|
|
|
|
|
2017-02-01 15:23:31 +01:00
|
|
|
/**
|
|
|
|
* Shortcut to get the the configuration option for a specific environment and merge it with
|
|
|
|
* the global one (environment specific overrides the global one if present).
|
|
|
|
*
|
2022-04-10 06:20:03 +02:00
|
|
|
* @param array<string, mixed> $defaultEnv
|
|
|
|
* @return array<string, mixed>
|
2017-02-01 15:23:31 +01:00
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getMergedOption(string $key, array $defaultEnv = []): array
|
2017-02-01 15:23:31 +01:00
|
|
|
{
|
2017-02-03 08:43:24 +01:00
|
|
|
$userGlobalOptions = $this->getConfigOption($key, $defaultEnv);
|
2017-02-01 15:23:31 +01:00
|
|
|
$userEnvOptions = $this->getEnvOption($key, $defaultEnv);
|
|
|
|
|
|
|
|
return array_merge(
|
|
|
|
(is_array($userGlobalOptions) ? $userGlobalOptions : []),
|
|
|
|
(is_array($userEnvOptions) ? $userEnvOptions : [])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-04 05:13:51 +01:00
|
|
|
/**
|
2017-01-12 02:22:21 +01:00
|
|
|
* Overwrites an Environment Configuration Option
|
2017-01-04 05:13:51 +01:00
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function setEnvOption(string $key, mixed $value): self
|
2017-01-04 05:13:51 +01:00
|
|
|
{
|
|
|
|
if (array_key_exists('environments', $this->configuration) && is_array($this->configuration['environments'])) {
|
|
|
|
if (array_key_exists($this->environment, $this->configuration['environments'])) {
|
|
|
|
$this->configuration['environments'][$this->environment][$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-12-31 07:52:25 +01:00
|
|
|
/**
|
|
|
|
* Sets the working Environment
|
|
|
|
*
|
2017-01-07 05:53:57 +01:00
|
|
|
* @throws RuntimeException
|
2016-12-31 07:52:25 +01:00
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function setEnvironment(string $environment): self
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
2022-04-10 06:20:03 +02:00
|
|
|
if (
|
|
|
|
array_key_exists('environments', $this->configuration) &&
|
|
|
|
array_key_exists($environment, $this->configuration['environments'])
|
|
|
|
) {
|
2016-12-31 07:52:25 +01:00
|
|
|
$this->environment = $environment;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-01-07 05:53:57 +01:00
|
|
|
throw new RuntimeException(sprintf('The environment "%s" does not exists.', $environment), 100);
|
2016-12-31 07:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current working Environment
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getEnvironment(): ?string
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
return $this->environment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the working stage
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function setStage(string $stage): self
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
$this->stage = $stage;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-10 03:45:30 +01:00
|
|
|
* Retrieve the current working Stage
|
2016-12-31 07:52:25 +01:00
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getStage(): ?string
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
return $this->stage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the defined Tasks for the current Environment and Stage
|
|
|
|
*
|
2022-04-10 06:20:03 +02:00
|
|
|
* @return string[]
|
2016-12-31 07:52:25 +01:00
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getTasks(): array
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
2022-04-10 06:20:03 +02:00
|
|
|
if (
|
|
|
|
!array_key_exists('environments', $this->configuration) ||
|
|
|
|
!is_array($this->configuration['environments'])
|
|
|
|
) {
|
2017-01-12 02:22:21 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!array_key_exists($this->environment, $this->configuration['environments'])) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists($this->stage, $this->configuration['environments'][$this->environment])) {
|
|
|
|
if (is_array($this->configuration['environments'][$this->environment][$this->stage])) {
|
|
|
|
return $this->configuration['environments'][$this->environment][$this->stage];
|
2016-12-31 07:52:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the working Host
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function setWorkingHost(?string $host): self
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
$this->workingHost = $host;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the working Host
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getWorkingHost(): ?string
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
return $this->workingHost;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs a Message into the Logger
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function log(string $message, string $level = LogLevel::DEBUG): void
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
if ($this->logger instanceof LoggerInterface) {
|
|
|
|
$this->logger->log($level, $message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes a command, it will be run Locally or Remotely based on the working Stage
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function runCommand(string $cmd, int $timeout = 120): Process
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
switch ($this->getStage()) {
|
2017-01-01 05:33:34 +01:00
|
|
|
case self::ON_DEPLOY:
|
|
|
|
case self::ON_RELEASE:
|
|
|
|
case self::POST_RELEASE:
|
2016-12-31 07:52:25 +01:00
|
|
|
return $this->runRemoteCommand($cmd, true, $timeout);
|
|
|
|
default:
|
|
|
|
return $this->runLocalCommand($cmd, $timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a command locally
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function runLocalCommand(string $cmd, int $timeout = 120): Process
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
$this->log($cmd, LogLevel::INFO);
|
|
|
|
|
2021-02-11 17:57:40 +01:00
|
|
|
$process = Process::fromShellCommandline($cmd);
|
2016-12-31 07:52:25 +01:00
|
|
|
$process->setTimeout($timeout);
|
|
|
|
$process->run();
|
|
|
|
|
|
|
|
$this->log($process->getOutput(), LogLevel::DEBUG);
|
|
|
|
if (!$process->isSuccessful()) {
|
|
|
|
$this->log($process->getErrorOutput(), LogLevel::ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $process;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes a command remotely, if jail is true, it will run inside the Host Path and the Release (if available)
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function runRemoteCommand(string $cmd, bool $jail, int $timeout = 120): Process
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
2017-01-30 11:24:43 +01:00
|
|
|
$user = $this->getEnvOption('user', $this->getCurrentUser());
|
2017-01-12 02:22:21 +01:00
|
|
|
$sudo = $this->getEnvOption('sudo', false);
|
2018-03-29 22:33:20 +02:00
|
|
|
$host = $this->getHostName();
|
2016-12-31 21:51:47 +01:00
|
|
|
$sshConfig = $this->getSSHConfig();
|
2016-12-31 07:52:25 +01:00
|
|
|
|
2017-01-11 02:02:35 +01:00
|
|
|
$cmdDelegate = $cmd;
|
2017-01-03 00:35:03 +01:00
|
|
|
if ($sudo === true) {
|
|
|
|
$cmdDelegate = sprintf('sudo %s', $cmd);
|
|
|
|
}
|
|
|
|
|
2017-01-12 02:22:21 +01:00
|
|
|
$hostPath = rtrim($this->getEnvOption('host_path'), '/');
|
2017-01-24 21:28:37 +01:00
|
|
|
if ($jail && $this->getReleaseId() !== null) {
|
2017-01-12 01:30:12 +01:00
|
|
|
$cmdDelegate = sprintf('cd %s/releases/%s && %s', $hostPath, $this->getReleaseId(), $cmdDelegate);
|
|
|
|
} elseif ($jail) {
|
|
|
|
$cmdDelegate = sprintf('cd %s && %s', $hostPath, $cmdDelegate);
|
2016-12-31 07:52:25 +01:00
|
|
|
}
|
|
|
|
|
2017-02-11 02:59:04 +01:00
|
|
|
$cmdRemote = str_replace('"', '\"', $cmdDelegate);
|
2022-04-10 06:20:03 +02:00
|
|
|
$cmdLocal = sprintf(
|
|
|
|
'ssh -p %d %s %s@%s "%s"',
|
|
|
|
$sshConfig['port'],
|
|
|
|
$sshConfig['flags'],
|
|
|
|
$user,
|
|
|
|
$host,
|
|
|
|
$cmdRemote
|
|
|
|
);
|
2016-12-31 07:52:25 +01:00
|
|
|
|
|
|
|
return $this->runLocalCommand($cmdLocal, $timeout);
|
|
|
|
}
|
2016-12-31 21:51:47 +01:00
|
|
|
|
2017-01-01 02:53:01 +01:00
|
|
|
/**
|
|
|
|
* Get the SSH configuration based on the environment
|
|
|
|
*
|
2022-04-10 06:20:03 +02:00
|
|
|
* @return array<string, string>
|
2017-01-01 02:53:01 +01:00
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getSSHConfig(): array
|
2016-12-31 21:51:47 +01:00
|
|
|
{
|
2022-04-10 06:20:03 +02:00
|
|
|
$sshConfig = $this->getEnvOption(
|
|
|
|
'ssh',
|
|
|
|
[
|
|
|
|
'port' => 22,
|
|
|
|
'flags' => '-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
|
|
|
|
]
|
|
|
|
);
|
2016-12-31 21:51:47 +01:00
|
|
|
|
2017-07-22 21:35:28 +02:00
|
|
|
if ($this->getHostPort() !== null) {
|
2017-07-05 12:13:09 +02:00
|
|
|
$sshConfig['port'] = $this->getHostPort();
|
2016-12-31 21:51:47 +01:00
|
|
|
}
|
|
|
|
|
2017-07-22 21:35:28 +02:00
|
|
|
if (!array_key_exists('port', $sshConfig)) {
|
|
|
|
$sshConfig['port'] = '22';
|
|
|
|
}
|
|
|
|
|
2016-12-31 21:51:47 +01:00
|
|
|
if (!array_key_exists('flags', $sshConfig)) {
|
|
|
|
$sshConfig['flags'] = '-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no';
|
|
|
|
}
|
|
|
|
|
2019-08-25 12:23:30 +02:00
|
|
|
if (!array_key_exists('timeout', $sshConfig)) {
|
|
|
|
$sshConfig['timeout'] = 300;
|
|
|
|
}
|
|
|
|
|
2016-12-31 21:51:47 +01:00
|
|
|
return $sshConfig;
|
|
|
|
}
|
2017-01-01 05:28:58 +01:00
|
|
|
|
2017-07-05 12:13:09 +02:00
|
|
|
/**
|
|
|
|
* Get the current Host Port or default ssh port
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getHostPort(): ?int
|
2017-07-05 12:13:09 +02:00
|
|
|
{
|
2022-04-10 06:41:27 +02:00
|
|
|
$info = explode(':', strval($this->getWorkingHost()));
|
2022-04-10 06:20:03 +02:00
|
|
|
return isset($info[1]) ? intval($info[1]) : null;
|
2017-07-05 12:13:09 +02:00
|
|
|
}
|
|
|
|
|
2018-03-29 22:33:20 +02:00
|
|
|
/**
|
|
|
|
* Get the current Host Name
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getHostName(): ?string
|
2018-03-29 22:33:20 +02:00
|
|
|
{
|
2022-04-10 06:41:27 +02:00
|
|
|
if (strpos(strval($this->getWorkingHost()), ':') === false) {
|
2018-03-29 22:38:28 +02:00
|
|
|
return $this->getWorkingHost();
|
|
|
|
}
|
|
|
|
|
2018-03-29 22:33:20 +02:00
|
|
|
$info = explode(':', $this->getWorkingHost());
|
2022-04-10 06:20:03 +02:00
|
|
|
return strval($info[0]);
|
2018-03-29 22:33:20 +02:00
|
|
|
}
|
|
|
|
|
2017-01-01 05:28:58 +01:00
|
|
|
/**
|
|
|
|
* Gets a Temporal File name
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getTempFile(): string
|
2017-01-01 05:28:58 +01:00
|
|
|
{
|
|
|
|
return tempnam(sys_get_temp_dir(), 'mage');
|
|
|
|
}
|
2017-01-04 02:43:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current user
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getCurrentUser(): string
|
2017-01-04 02:43:08 +01:00
|
|
|
{
|
2018-09-10 18:27:56 +02:00
|
|
|
// Windows fallback
|
|
|
|
if (!function_exists('posix_getpwuid')) {
|
|
|
|
return getenv('USERNAME') ?: '';
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:43:08 +01:00
|
|
|
$userData = posix_getpwuid(posix_geteuid());
|
|
|
|
return $userData['name'];
|
|
|
|
}
|
2017-01-11 01:28:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for getting Branch information
|
|
|
|
*
|
2022-04-10 06:20:03 +02:00
|
|
|
* @return bool|string
|
2017-01-11 01:28:32 +01:00
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getBranch(): mixed
|
2017-01-11 01:28:32 +01:00
|
|
|
{
|
2017-01-12 02:22:21 +01:00
|
|
|
return $this->getEnvOption('branch', false);
|
2017-01-11 01:28:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Guesses the Deploy Strategy to use
|
|
|
|
*/
|
2022-04-10 06:20:03 +02:00
|
|
|
public function guessStrategy(): StrategyInterface
|
2017-01-11 01:28:32 +01:00
|
|
|
{
|
|
|
|
$strategy = new RsyncStrategy();
|
|
|
|
|
2017-01-12 02:22:21 +01:00
|
|
|
if ($this->getEnvOption('releases', false)) {
|
2017-01-11 01:28:32 +01:00
|
|
|
$strategy = new ReleasesStrategy();
|
|
|
|
}
|
|
|
|
|
|
|
|
$strategy->setRuntime($this);
|
|
|
|
return $strategy;
|
|
|
|
}
|
2016-12-31 07:52:25 +01:00
|
|
|
}
|