-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPreSymlinkHandler.php
79 lines (70 loc) · 2.85 KB
/
PreSymlinkHandler.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
<?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 PreSymlinkHandler extends AbstractTaskEventHandler
{
use \DigipolisGent\Robo\Task\Deploy\Tasks;
/**
* {@inheritDoc}
*/
public function handle(GenericEvent $event)
{
/** @var RemoteConfig $remoteConfig */
$remoteConfig = $event->getArgument('remoteConfig');
$remoteSettings = $remoteConfig->getRemoteSettings();
$auth = new KeyFile($remoteConfig->getUser(), $remoteConfig->getPrivateKeyFile());
$timeouts = $event->getArgument('timeouts');
$collection = $this->collectionBuilder();
foreach ($remoteSettings['symlinks'] as $symlink) {
$preIndividualSymlinkTask = $this->preIndividualSymlinkTask($remoteConfig, $symlink, $timeouts['pre_symlink']);
if ($preIndividualSymlinkTask) {
$collection->addTask($preIndividualSymlinkTask);
}
}
return $collection;
}
/**
* Tasks to execute before creating an individual symlink.
*
* @param RemoteConfig $remoteConfig
* RemoteConfig object populated with data relevant to the host.
* @param string $symlink
* The symlink in format "target:link".
* @param int $timeout
* The SSH timeout in seconds.
*
* @return bool|\Robo\Contract\TaskInterface
* The presymlink task, false if no pre symlink task needs to run.
*/
public function preIndividualSymlinkTask(RemoteConfig $remoteConfig, $symlink, $timeout)
{
$remoteSettings = $remoteConfig->getRemoteSettings();
$projectRoot = $remoteSettings['rootdir'];
$task = $this->taskSsh($remoteConfig->getHost(), new KeyFile($remoteConfig->getUser(), $remoteConfig->getPrivateKeyFile()))
->remoteDirectory($projectRoot, true)
->timeout($timeout);
list($target, $link) = explode(':', $symlink);
if ($link === $remoteSettings['currentdir']) {
return false;
}
// If the link we're going to create is an existing directory,
// mirror that directory on the symlink target and then delete it
// before creating the symlink
$task->exec(
(string) CommandBuilder::create('vendor/bin/robo digipolis:mirror-dir')
->addArgument($link)
->addArgument($target)
);
$task->exec(
(string) CommandBuilder::create('rm')
->addFlag('rf')
->addArgument($link)
);
return $task;
}
}