|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Deployer; |
| 4 | + |
| 5 | +use Deployer\Exception\GracefulShutdownException; |
| 6 | +use SourceBroker\DeployerExtendedMedia\Utility\FileUtility; |
| 7 | +use SourceBroker\DeployerInstance\Configuration; |
| 8 | + |
| 9 | +/* |
| 10 | + * @see https://github.com/sourcebroker/deployer-extended-media#media-push |
| 11 | + */ |
| 12 | +task('media:push', function () { |
| 13 | + $targetName = get('argument_host'); |
| 14 | + if ($targetName === get('instance_live_name', 'live')) { |
| 15 | + if (!get('media_allow_push_live', true)) { |
| 16 | + throw new GracefulShutdownException( |
| 17 | + 'FORBIDDEN: For security its forbidden to push media to top instance: "' . $targetName . '"!' |
| 18 | + ); |
| 19 | + } |
| 20 | + if (!get('media_allow_push_live_force', false)) { |
| 21 | + writeln("<error>\n\n"); |
| 22 | + writeln(sprintf( |
| 23 | + "You going to push media from instance: \"%s\" to top instance: \"%s\". ", |
| 24 | + get('local_host'), |
| 25 | + $targetName |
| 26 | + )); |
| 27 | + writeln("This can be destructive.\n\n"); |
| 28 | + writeln("</error>"); |
| 29 | + if (!askConfirmation('Do you really want to continue?', false)) { |
| 30 | + throw new GracefulShutdownException('Process aborted.'); |
| 31 | + } |
| 32 | + if (!askConfirmation('Are you sure?', false)) { |
| 33 | + throw new GracefulShutdownException('Process aborted.'); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + $src = get('deploy_path') . '/' . 'shared'; |
| 38 | + if (!trim($src)) { |
| 39 | + throw new GracefulShutdownException('You need to specify a source path.'); |
| 40 | + } |
| 41 | + $src = (new FileUtility)->normalizeFolder($src); |
| 42 | + |
| 43 | + $dst = get('media_rsync_dest'); |
| 44 | + while (is_callable($dst)) { |
| 45 | + $dst = $dst(); |
| 46 | + } |
| 47 | + if (!trim($dst)) { |
| 48 | + throw new GracefulShutdownException('You need to specify a destination path.'); |
| 49 | + } |
| 50 | + $dst = (new FileUtility)->normalizeFolder($dst); |
| 51 | + |
| 52 | + $targetServer = Configuration::getHost($targetName); |
| 53 | + $host = $targetServer->getHostname(); |
| 54 | + $user = !$targetServer->getRemoteUser() ? '' : $targetServer->getRemoteUser() . '@'; |
| 55 | + |
| 56 | + $rsyncSshOptions = ''; |
| 57 | + $connectionOptions = $targetServer->connectionOptionsString(); |
| 58 | + if ($connectionOptions !== '') { |
| 59 | + $rsyncSshOptions = '-e "ssh ' . $connectionOptions . ' "'; |
| 60 | + } |
| 61 | + runLocally( |
| 62 | + 'rsync ' . $rsyncSshOptions . ' {{media_rsync_flags}}{{media_rsync_options}}{{media_rsync_includes}}{{media_rsync_excludes}}{{media_rsync_filter}}' |
| 63 | + . ' ' |
| 64 | + . escapeshellarg($dst) |
| 65 | + . ' ' |
| 66 | + . escapeshellarg($user . $host . ':' . $src) |
| 67 | + ); |
| 68 | +})->desc('Synchronize media from local instance to remote instance'); |
0 commit comments