|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * Copyright 2022 Bastian Schwarz <bastian@codename-php.de>. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace de\codenamephp\deployer\base\task\media; |
| 19 | + |
| 20 | +use de\codenamephp\deployer\base\functions\All; |
| 21 | +use de\codenamephp\deployer\base\functions\iAll; |
| 22 | +use de\codenamephp\deployer\base\functions\iInput; |
| 23 | +use de\codenamephp\deployer\base\hostCheck\DoNotRunOnProduction; |
| 24 | +use de\codenamephp\deployer\base\hostCheck\iHostCheck; |
| 25 | +use de\codenamephp\deployer\base\iConfigurationKeys; |
| 26 | +use de\codenamephp\deployer\base\MissingConfigurationException; |
| 27 | +use de\codenamephp\deployer\base\ssh\client\iClient; |
| 28 | +use de\codenamephp\deployer\base\ssh\client\StaticProxy; |
| 29 | +use de\codenamephp\deployer\base\task\iTask; |
| 30 | +use de\codenamephp\deployer\base\transferable\iTransferable; |
| 31 | +use de\codenamephp\deployer\base\UnsafeOperationException; |
| 32 | +use Deployer\Exception\RunException; |
| 33 | + |
| 34 | +/** |
| 35 | + * Task to copy media between two remotes. The copy uses rsync over a ssh tunnel with agent forwarding so the remotes do not need each others credentials but |
| 36 | + * the local credentials are used. |
| 37 | + * |
| 38 | + * @psalm-suppress PropertyNotSetInConstructor see https://github.com/vimeo/psalm/issues/4393 |
| 39 | + */ |
| 40 | +final class Copy implements iTask { |
| 41 | + |
| 42 | + public const OPTION_SOURCE_HOST = 'cpd:media:copy:sourceHost'; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var array<iTransferable> |
| 46 | + */ |
| 47 | + private array $transferables; |
| 48 | + |
| 49 | + /** |
| 50 | + * @param array<iTransferable> $transferables Array of transferables to copy between two remotes |
| 51 | + * @param iAll $deployerFunctions Deployer function to set options and run the copy |
| 52 | + * @param iHostCheck $hostCheck HostCheck to prevent accidental execution |
| 53 | + */ |
| 54 | + public function __construct(array $transferables, |
| 55 | + public iAll $deployerFunctions = new All(), |
| 56 | + public iHostCheck $hostCheck = new DoNotRunOnProduction(), |
| 57 | + public iClient $sshClient = new StaticProxy()) { |
| 58 | + $this->setTransferables(...$transferables); |
| 59 | + $deployerFunctions->option( |
| 60 | + self::OPTION_SOURCE_HOST, |
| 61 | + null, |
| 62 | + iInput::OPTION_VALUE_REQUIRED, |
| 63 | + 'The source host to copy the media from', |
| 64 | + iConfigurationKeys::PRODUCTION |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @return iTransferable[] |
| 70 | + */ |
| 71 | + public function getTransferables() : array { |
| 72 | + return $this->transferables; |
| 73 | + } |
| 74 | + |
| 75 | + public function setTransferables(iTransferable ...$transferables) : Copy { |
| 76 | + $this->transferables = $transferables; |
| 77 | + return $this; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Uses the option for the source host to get the source host, the target host is the host for the current stage and uses |
| 82 | + * those hosts to build an ssh command that connects to the source host and an rsync command that will be executed on the source host |
| 83 | + * and syncs to the target host. |
| 84 | + * |
| 85 | + * The idea is that ssh agent forwarding is enabled so the hosts can talk to each other using the local key. |
| 86 | + * |
| 87 | + * The base command is then used with all transferables so all folders are synced. |
| 88 | + * |
| 89 | + * The command cannot be run on the production stage |
| 90 | + * |
| 91 | + * @return void |
| 92 | + * @throws UnsafeOperationException if the stage is production (more precisely: if the stage check fails) |
| 93 | + * @throws MissingConfigurationException if one of the target or source host could not be found |
| 94 | + * @throws RunException |
| 95 | + */ |
| 96 | + public function __invoke() : void { |
| 97 | + $this->hostCheck->check(); |
| 98 | + |
| 99 | + $sourceHost = $this->deployerFunctions->firstHost((string) $this->deployerFunctions->getOption(self::OPTION_SOURCE_HOST)); |
| 100 | + $targetHost = $this->deployerFunctions->currentHost(); |
| 101 | + |
| 102 | + $sshCommand = "ssh {$this->sshClient->connectionOptionsString($sourceHost)} {$sourceHost->getConnectionString()}"; |
| 103 | + $rsyncCommand = "rsync -e 'ssh -o StrictHostKeyChecking=no {$this->sshClient->connectionOptionsString($targetHost)}' -azP %s %s {$targetHost->getConnectionString()}:%s"; |
| 104 | + |
| 105 | + foreach($this->getTransferables() as $transferable) { |
| 106 | + $sourcePath = $this->deployerFunctions->parseOnHost($sourceHost, $transferable->getLocalPath()); |
| 107 | + $targetPath = $this->deployerFunctions->parseOnHost($targetHost, $transferable->getRemotePath()); |
| 108 | + |
| 109 | + $this->deployerFunctions->runLocally(sprintf('%s "%s"', $sshCommand, sprintf($rsyncCommand, implode(' ', $transferable->getConfig()['options'] ?? []), $sourcePath, $targetPath))); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments