-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRsyncFilesBetweenHostsHandler.php
278 lines (255 loc) · 9.62 KB
/
RsyncFilesBetweenHostsHandler.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
namespace DigipolisGent\Robo\Helpers\EventHandler\DefaultHandler;
use DigipolisGent\CommandBuilder\CommandBuilder;
use DigipolisGent\Robo\Helpers\EventHandler\AbstractTaskEventHandler;
use DigipolisGent\Robo\Helpers\Util\RemoteConfig;
use DigipolisGent\Robo\Task\Deploy\Ssh\Auth\AbstractAuth;
use DigipolisGent\Robo\Task\Deploy\Ssh\Auth\KeyFile;
use Symfony\Component\EventDispatcher\GenericEvent;
class RsyncFilesBetweenHostsHandler extends AbstractTaskEventHandler
{
use \DigipolisGent\Robo\Task\Deploy\Tasks;
use \Robo\Task\Base\Tasks;
use \Robo\Task\Remote\Tasks;
/**
* {@inheritDoc}
*/
public function handle(GenericEvent $event)
{
/** @var RemoteConfig $sourceRemoteConfig */
$sourceRemoteConfig = $event->getArgument('sourceRemoteConfig');
$destinationRemoteConfig = $event->getArgument('destinationRemoteConfig');
$fileBackupConfig = $event->getArgument('fileBackupConfig');
$timeouts = $event->getArgument('timeouts');
$tmpPrivateKeyFile = '~/.ssh/' . uniqid('robo_', true) . '.id_rsa';
$collection = $this->collectionBuilder();
// Generate a temporary key.
$collection->addTask(
$this->generateKeyPair($tmpPrivateKeyFile)
);
$collection->completion(
$this->removeKeyPair($tmpPrivateKeyFile)
);
// Install it on the destination host.
$collection->addTask(
$this->installPublicKeyOnDestination(
$tmpPrivateKeyFile,
$destinationRemoteConfig
)
);
// Remove it from the destination host when we're done.
$collection->completion(
$this->removePublicKeyFromDestination(
$tmpPrivateKeyFile,
$destinationRemoteConfig
)
);
// Install the private key on the source host.
$collection->addTask(
$this->installPrivateKeyOnSource(
$tmpPrivateKeyFile,
$sourceRemoteConfig
)
);
// Remove the private key from the source host.
$collection->completion(
$this->removePrivateKeyFromSource(
$tmpPrivateKeyFile,
$sourceRemoteConfig
)
);
$dirs = ($fileBackupConfig['file_backup_subdirs'] ? $fileBackupConfig['file_backup_subdirs'] : ['']);
foreach ($dirs as $dir) {
$dir .= ($dir !== '' ? '/' : '');
$collection->addTask(
$this->rsyncDirectory(
$dir,
$tmpPrivateKeyFile,
$sourceRemoteConfig,
$destinationRemoteConfig,
$fileBackupConfig,
$timeouts['synctask_rsync']
)
);
}
return $collection;
}
/**
* Generate an SSH key pair.
*
* @param string $privateKeyFile
* Path to store the private key file.
*
* @return \Robo\Contract\TaskInterface
*/
protected function generateKeyPair($privateKeyFile)
{
return $this->taskExec(
(string) CommandBuilder::create('ssh-keygen')
->addFlag('q')
->addFlag('t', 'rsa')
->addFlag('b', 4096)
->addRawFlag('N', '""')
->addRawFlag('f', $privateKeyFile)
->addFlag('C', 'robo:' . md5($privateKeyFile))
);
}
/**
* Remove an SSH key pair.
*
* @param string $privateKeyFile
* Path to store the private key file.
*
* @return \Robo\Contract\TaskInterface
*/
protected function removeKeyPair($privateKeyFile)
{
return $this->taskExecStack()
->exec(
(string) CommandBuilder::create('rm')
->addFlag('f')
->addRawArgument($privateKeyFile)
->addRawArgument($privateKeyFile . '.pub')
);
}
/**
* Install a public SSH key on a host.
*
* @param string $privateKeyFile
* Path to the private key file of the key pair to install.
* @param RemoteConfig $remoteConfig
* RemoteConfig object populated with data relevant to the host.
*
* @return \Robo\Contract\TaskInterface
*/
protected function installPublicKeyOnDestination($privateKeyFile, RemoteConfig $remoteConfig)
{
return $this->taskExec(
(string) CommandBuilder::create('cat')
->addRawArgument($privateKeyFile . '.pub')
->pipeOutputTo(
CommandBuilder::create('ssh')
->addArgument($remoteConfig->getUser() . '@' . $remoteConfig->getHost())
->addFlag('o', 'StrictHostKeyChecking=no')
->addRawFlag('i', $remoteConfig->getPrivateKeyFile())
)
->addArgument(
CommandBuilder::create('mkdir')
->addFlag('p')
->addRawArgument('~/.ssh')
->onSuccess(
CommandBuilder::create('cat')
->chain('~/.ssh/authorized_keys', '>>')
)
)
);
}
/**
* Remove a public key from a host.
*
* @param string $privateKeyFile
* Path to the private key file of the key pair to remove.
* @param RemoteConfig $remoteConfig
* RemoteConfig object populated with data relevant to the host.
*
* @return \Robo\Contract\TaskInterface
*/
protected function removePublicKeyFromDestination($privateKeyFile, RemoteConfig $remoteConfig)
{
return $this->taskSsh($remoteConfig->getHost(), new KeyFile($remoteConfig->getUser(), $remoteConfig->getPrivateKeyFile()))
->exec(
(string) CommandBuilder::create('sed')
->addFlag('i', '/robo:' . md5($privateKeyFile) . '/d')
->addRawArgument('~/.ssh/authorized_keys')
);
}
/**
* Install a private key on a host.
*
* @param string $privateKeyFile
* Private key to install.
* @param RemoteConfig $remoteConfig
* RemoteConfig object populated with data relevant to the host.
*
* @return \Robo\Contract\TaskInterface
*/
protected function installPrivateKeyOnSource($privateKeyFile, RemoteConfig $remoteConfig)
{
return $this->taskRsync()
->rawArg('--rsh "ssh -o StrictHostKeyChecking=no -i `vendor/bin/robo digipolis:realpath ' . $remoteConfig->getPrivateKeyFile() . '`"')
->fromPath($privateKeyFile)
->toHost($remoteConfig->getHost())
->toUser($remoteConfig->getUser())
->toPath('~/.ssh')
->archive()
->compress()
->checksum()
->wholeFile();
}
/**
* Remove a private key from a host.
*
* @param string $privateKeyFile
* Path to the private key file of the key pair to remove.
* @param RemoteConfig $remoteConfig
* RemoteConfig object populated with data relevant to the host.
*
* @return \Robo\Contract\TaskInterface
*/
protected function removePrivateKeyFromSource($privateKeyFile, RemoteConfig $remoteConfig)
{
return $this->taskSsh($remoteConfig->getHost(), new KeyFile($remoteConfig->getUser(), $remoteConfig->getPrivateKeyFile()))
->exec(
(string) CommandBuilder::create('rm')
->addFlag('f')
->addRawArgument($privateKeyFile)
);
}
/**
* Rsync a directory between hosts.
*
* @param string $directory
* The directory to sync.
* @param string $privateKeyFile
* The path the the private key of the keypair installed on src and dest.
* @param RemoteConfig $sourceRemoteConfig
* RemoteConfig object populated with data relevant to the source.
* @param RemoteConfig $destinationRemoteConfig
* RemoteConfig object populated with data relevant to the destination.
* @param array $fileBackupConfig
* File backup config.
* @param int $timeout
* Timeout setting for the sync.
*
* @return \Robo\Contract\TaskInterface
*/
protected function rsyncDirectory(
$directory,
$privateKeyFile,
RemoteConfig $sourceRemoteConfig,
RemoteConfig $destinationRemoteConfig,
$fileBackupConfig,
$timeout
) {
$sourceRemoteSettings = $sourceRemoteConfig->getRemoteSettings();
$destinationRemoteSettings = $destinationRemoteConfig->getRemoteSettings();
$rsync = $this->taskRsync()
->rawArg('--rsh "ssh -o StrictHostKeyChecking=no -i `cd -P ' . $sourceRemoteConfig->getCurrentProjectRoot() . ' && vendor/bin/robo digipolis:realpath ' . $privateKeyFile . '`"')
->fromPath($sourceRemoteSettings['filesdir'] . '/' . $directory)
->toHost($destinationRemoteConfig->getHost())
->toUser($destinationRemoteConfig->getUser())
->toPath($destinationRemoteSettings['filesdir'] . '/' . $directory)
->archive()
->delete()
->rawArg('--copy-links --keep-dirlinks')
->compress()
->checksum()
->wholeFile();
foreach ($fileBackupConfig['exclude_from_backup'] as $exclude) {
$rsync->exclude($exclude);
}
return $this->taskSsh($sourceRemoteConfig->getHost(), new KeyFile($sourceRemoteConfig->getUser(), $sourceRemoteConfig->getPrivateKeyFile()))
->timeout($timeout)
->exec($rsync);
}
}