1 | <?php |
||
2 | /* (c) HAKGER[hakger.pl] Hubert Kowalski <[email protected]> |
||
3 | * |
||
4 | * For the full copyright and license information, please view the LICENSE |
||
5 | * file that was distributed with this source code. |
||
6 | * |
||
7 | * @contributor Steve Mueller <[email protected]>, Niklas Vosskoetter <[email protected]> |
||
8 | */ |
||
9 | |||
10 | namespace Deployer; |
||
11 | |||
12 | set('rsync', [ |
||
13 | 'exclude' => [ |
||
14 | '.git', |
||
15 | 'deploy.php', |
||
16 | ], |
||
17 | 'exclude-file' => false, |
||
18 | 'include' => [], |
||
19 | 'include-file' => false, |
||
20 | 'filter' => [], |
||
21 | 'filter-file' => false, |
||
22 | 'filter-perdir' => false, |
||
23 | 'flags' => 'rz', |
||
24 | 'options' => ['delete'], |
||
25 | 'timeout' => 300, |
||
26 | ]); |
||
27 | |||
28 | set('rsync_src', __DIR__); |
||
29 | set('rsync_dest', '{{release_path}}'); |
||
30 | |||
31 | set('rsync_excludes', function () { |
||
32 | $config = get('rsync'); |
||
33 | $excludes = $config['exclude']; |
||
34 | $excludeFile = $config['exclude-file']; |
||
35 | $excludesRsync = ''; |
||
36 | foreach ($excludes as $exclude) { |
||
37 | $excludesRsync.=' --exclude=' . escapeshellarg($exclude); |
||
38 | } |
||
39 | if (!empty($excludeFile) && file_exists($excludeFile) && is_file($excludeFile) && is_readable($excludeFile)) { |
||
40 | $excludesRsync .= ' --exclude-from=' . escapeshellarg($excludeFile); |
||
41 | } |
||
42 | |||
43 | return $excludesRsync; |
||
44 | }); |
||
45 | |||
46 | set('rsync_includes', function () { |
||
47 | $config = get('rsync'); |
||
48 | $includes = $config['include']; |
||
49 | $includeFile = $config['include-file']; |
||
50 | $includesRsync = ''; |
||
51 | foreach ($includes as $include) { |
||
52 | $includesRsync.=' --include=' . escapeshellarg($include); |
||
53 | } |
||
54 | if (!empty($includeFile) && file_exists($includeFile) && is_file($includeFile) && is_readable($includeFile)) { |
||
55 | $includesRsync .= ' --include-from=' . escapeshellarg($includeFile); |
||
56 | } |
||
57 | |||
58 | return $includesRsync; |
||
59 | }); |
||
60 | |||
61 | set('rsync_filter', function () { |
||
62 | $config = get('rsync'); |
||
63 | $filters = $config['filter']; |
||
64 | $filterFile = $config['filter-file']; |
||
65 | $filterPerDir = $config['filter-perdir']; |
||
66 | $filtersRsync = ''; |
||
67 | foreach ($filters as $filter) { |
||
68 | $filtersRsync.=" --filter='$filter'"; |
||
69 | } |
||
70 | if (!empty($filterFile)) { |
||
71 | $filtersRsync .= " --filter='merge $filterFile'"; |
||
72 | } |
||
73 | if (!empty($filterPerDir)) { |
||
74 | $filtersRsync .= " --filter='dir-merge $filterPerDir'"; |
||
75 | } |
||
76 | return $filtersRsync; |
||
77 | }); |
||
78 | |||
79 | set('rsync_options', function () { |
||
80 | $config = get('rsync'); |
||
81 | $options = $config['options']; |
||
82 | $optionsRsync = []; |
||
83 | foreach ($options as $option) { |
||
84 | $optionsRsync[] = "--$option"; |
||
85 | } |
||
86 | return implode(' ', $optionsRsync); |
||
87 | }); |
||
88 | |||
89 | |||
90 | desc('Warmup remote Rsync target'); |
||
91 | task('rsync:warmup', function() { |
||
92 | $config = get('rsync'); |
||
93 | |||
94 | $source = "{{deploy_path}}/current"; |
||
95 | $destination = "{{deploy_path}}/release"; |
||
96 | |||
97 | if (test("[ -d $(echo $source) ]")) { |
||
98 | run("rsync -{$config['flags']} {{rsync_options}}{{rsync_excludes}}{{rsync_includes}}{{rsync_filter}} $source/ $destination/"); |
||
99 | } else { |
||
100 | writeln("<comment>No way to warmup rsync.</comment>"); |
||
101 | } |
||
102 | }); |
||
103 | |||
104 | |||
105 | desc('Rsync local->remote'); |
||
106 | task('rsync', function() { |
||
107 | $config = get('rsync'); |
||
108 | |||
109 | $src = get('rsync_src'); |
||
110 | while (is_callable($src)) { |
||
111 | $src = $src(); |
||
112 | } |
||
113 | |||
114 | if (!trim($src)) { |
||
115 | // if $src is not set here rsync is going to do a directory listing |
||
116 | // exiting with code 0, since only doing a directory listing clearly |
||
117 | // is not what we want to achieve we need to throw an exception |
||
118 | throw new \RuntimeException('You need to specify a source path.'); |
||
119 | } |
||
120 | |||
121 | $dst = get('rsync_dest'); |
||
122 | while (is_callable($dst)) { |
||
123 | $dst = $dst(); |
||
124 | } |
||
125 | |||
126 | if (!trim($dst)) { |
||
127 | // if $dst is not set here we are going to sync to root |
||
128 | // and even worse - depending on rsync flags and permission - |
||
129 | // might end up deleting everything we have write permission to |
||
130 | throw new \RuntimeException('You need to specify a destination path.'); |
||
131 | } |
||
132 | |||
133 | $server = \Deployer\Task\Context::get()->getHost(); |
||
134 | if ($server instanceof \Deployer\Host\Localhost) { |
||
135 | runLocally("rsync -{$config['flags']} {{rsync_options}}{{rsync_includes}}{{rsync_excludes}}{{rsync_filter}} '$src/' '$dst/'", $config); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
136 | return; |
||
137 | } |
||
138 | |||
139 | $host = $server->getHostname(); |
||
140 | $port = $server->getPort() ? ' -p' . $server->getPort() : ''; |
||
141 | $sshArguments = $server->getSshArguments(); |
||
142 | $user = !$server->getRemoteUser() ? '' : $server->getRemoteUser() . '@'; |
||
143 | |||
144 | runLocally("rsync -{$config['flags']} -e 'ssh$port $sshArguments' {{rsync_options}}{{rsync_includes}}{{rsync_excludes}}{{rsync_filter}} '$src/' '$user$host:$dst/'", $config); |
||
145 | }); |
||
146 |