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