|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* See class comment |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Netresearch |
|
8
|
|
|
* @package Netresearch\Kite |
|
9
|
|
|
* @subpackage Task |
|
10
|
|
|
* @author Christian Opitz <[email protected]> |
|
11
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
12
|
|
|
* @link http://www.netresearch.de |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Netresearch\Kite\Task; |
|
16
|
|
|
use Netresearch\Kite\Exception; |
|
17
|
|
|
use Netresearch\Kite\Node; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Up/download via SCP |
|
21
|
|
|
* |
|
22
|
|
|
* @category Netresearch |
|
23
|
|
|
* @package Netresearch\Kite |
|
24
|
|
|
* @subpackage Task |
|
25
|
|
|
* @author Christian Opitz <[email protected]> |
|
26
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
27
|
|
|
* @link http://www.netresearch.de |
|
28
|
|
|
*/ |
|
29
|
|
|
class ScpTask extends \Netresearch\Kite\Task\RemoteShellTask |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Configure the options |
|
33
|
|
|
* |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function configureVariables() |
|
37
|
|
|
{ |
|
38
|
|
|
return array( |
|
39
|
|
|
'from' => array( |
|
40
|
|
|
'type' => 'string', |
|
41
|
|
|
'required' => true, |
|
42
|
|
|
'label' => 'Path to the source (prefix with {node}: to download from a node)' |
|
43
|
|
|
), |
|
44
|
|
|
'to' => array( |
|
45
|
|
|
'type' => 'string', |
|
46
|
|
|
'required' => true, |
|
47
|
|
|
'label' => 'Path to the target (prefix with {node}: to upload to a node)' |
|
48
|
|
|
), |
|
49
|
|
|
'options' => null, |
|
50
|
|
|
'arguments' => null, |
|
51
|
|
|
'optArg' => null, |
|
52
|
|
|
'--' |
|
53
|
|
|
) + parent::configureVariables(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Create all necessary commands |
|
58
|
|
|
* |
|
59
|
|
|
* @return array|string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function getCommand() |
|
62
|
|
|
{ |
|
63
|
|
|
$to = $this->get('to'); |
|
64
|
|
|
$from = $this->get('from'); |
|
65
|
|
|
|
|
66
|
|
|
$toNode = $this->getNodeFromPath($to); |
|
67
|
|
|
$fromNode = $this->getNodeFromPath($from); |
|
68
|
|
|
$node = $toNode ?: $fromNode; |
|
69
|
|
|
|
|
70
|
|
|
if (!$node) { |
|
71
|
|
|
throw new Exception('This task is intended to be used with nodes - use shell task for other sources/targets'); |
|
72
|
|
|
} elseif ($toNode === $fromNode) { |
|
73
|
|
|
throw new Exception('Transfer on same node not supported'); |
|
74
|
|
|
} elseif ($toNode && $fromNode && $toNode !== $fromNode) { |
|
75
|
|
|
throw new Exception('Transfer between nodes not (yet) supported'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$cwd = $this->expand($this->cwd); |
|
79
|
|
|
$cwdCmd = $cwd ? 'cd ' . escapeshellarg($cwd) . '; ' : null; |
|
80
|
|
|
$toDirParent = dirname(array_pop(explode(':', $to, 2))); |
|
|
|
|
|
|
81
|
|
|
if ($toDirParent !== '/') { |
|
82
|
|
|
$prepareDirCmd = 'mkdir -p ' . escapeshellarg($toDirParent); |
|
83
|
|
|
if ($toNode) { |
|
84
|
|
|
$prepareDirCmd = $this->getSshCommand($prepareDirCmd, $toNode); |
|
85
|
|
|
} elseif ($cwd && $toDirParent[0] !== '/') { |
|
86
|
|
|
$prepareDirCmd = $cwdCmd . $prepareDirCmd; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$scpCommand = $this->getScpCommand($node) . ' ' . escapeshellarg($from) . ' ' . escapeshellarg($to); |
|
91
|
|
|
|
|
92
|
|
|
if ($node) { |
|
93
|
|
|
$this->addExpect($scpCommand, $node); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ($cwdCmd) { |
|
|
|
|
|
|
97
|
|
|
$scpCommand = $cwdCmd . $scpCommand; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return isset($prepareDirCmd) ? array($prepareDirCmd, $scpCommand) : $scpCommand; |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Create the scp command |
|
105
|
|
|
* |
|
106
|
|
|
* @param Node $node The node |
|
107
|
|
|
* |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function getScpCommand(Node $node) |
|
111
|
|
|
{ |
|
112
|
|
|
return rtrim('scp -r ' . trim($node->get('scpOptions'))); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get the node from a path (by it's url) |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $path The path |
|
119
|
|
|
* |
|
120
|
|
|
* @return Node |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function getNodeFromPath($path) |
|
123
|
|
|
{ |
|
124
|
|
|
if (strpos($path, ':')) { |
|
125
|
|
|
/* @var Node[] $nodes */ |
|
126
|
|
|
$nodes = $this->get('nodes', array()); |
|
127
|
|
|
$url = array_shift(explode(':', $path, 2)); |
|
|
|
|
|
|
128
|
|
|
foreach ($nodes as $node) { |
|
129
|
|
|
if ($node->get('url') === $url) { |
|
130
|
|
|
return $node; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
return null; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
?> |
|
138
|
|
|
|