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\Node; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Execute a shell command on either the current node or all nodes |
20
|
|
|
* |
21
|
|
|
* @category Netresearch |
22
|
|
|
* @package Netresearch\Kite |
23
|
|
|
* @subpackage Task |
24
|
|
|
* @author Christian Opitz <[email protected]> |
25
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
26
|
|
|
* @link http://www.netresearch.de |
27
|
|
|
*/ |
28
|
|
|
class RemoteShellTask extends ShellTask |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $cwd; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Hide the cwd from ShellTask |
37
|
|
|
* |
38
|
|
|
* @param string $option Option |
39
|
|
|
* @param mixed $value Value |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
public function offsetSet($option, $value) |
44
|
|
|
{ |
45
|
|
|
if ($option === 'cwd') { |
46
|
|
|
$this->cwd = $value; |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
parent::offsetSet($option, $value); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Execute the task for each of the available nodes or the current node if set |
54
|
|
|
* |
55
|
|
|
* @return array|mixed Return value(s) of the command on the node(s) |
56
|
|
|
*/ |
57
|
|
|
public function execute() |
58
|
|
|
{ |
59
|
|
|
if ($this->has('node')) { |
60
|
|
|
return $this->executeCommand(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/* @var \Netresearch\Kite\Node[] $nodes */ |
64
|
|
|
$nodes = $this->get('nodes'); |
65
|
|
|
if (!$nodes) { |
|
|
|
|
66
|
|
|
throw new \Netresearch\Kite\Exception('No nodes to work on'); |
67
|
|
|
} |
68
|
|
|
$returnValues = array(); |
69
|
|
|
foreach ($nodes as $node) { |
70
|
|
|
$this->set('node', $node); |
71
|
|
|
try { |
72
|
|
|
$returnValues[$node->get('id')] = $this->executeCommand(); |
73
|
|
|
} catch (\Exception $e) { |
74
|
|
|
$this->remove('node'); |
75
|
|
|
throw $e; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
$this->remove('node'); |
79
|
|
|
return $returnValues; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the command to execute |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected function getCommand() |
88
|
|
|
{ |
89
|
|
|
return $this->getSshCommand( |
90
|
|
|
parent::getCommand(), |
91
|
|
|
$this->get('node'), $this->expand($this->cwd) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Wrap the command in a ssh command |
97
|
|
|
* |
98
|
|
|
* @param string $command The command |
99
|
|
|
* @param Node $node The node |
100
|
|
|
* @param string $cwd The dir to change into (on the node) |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
protected function getSshCommand($command, Node $node, $cwd = null) |
105
|
|
|
{ |
106
|
|
|
if ($cwd && !preg_match('#^cd\s+[\'"]?/#', $command)) { |
|
|
|
|
107
|
|
|
$command = 'cd ' . escapeshellarg($cwd) . '; ' . $command; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$sshCommand |
111
|
|
|
= rtrim('ssh' . $node->get('sshOptions')) |
112
|
|
|
. ' ' . escapeshellarg($node->get('url')) |
113
|
|
|
. ' ' . escapeshellarg($command); |
114
|
|
|
|
115
|
|
|
$this->addExpect($sshCommand, $node); |
116
|
|
|
|
117
|
|
|
return $sshCommand; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Wrap the command in an expect command |
122
|
|
|
* |
123
|
|
|
* @param string $sshCommand The command |
124
|
|
|
* @param Node $node The node |
125
|
|
|
* |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
protected function addExpect(&$sshCommand, Node $node) |
129
|
|
|
{ |
130
|
|
|
$pass = $node->get('pass'); |
131
|
|
|
if ($pass) { |
132
|
|
|
$expectFile = dirname(dirname(__DIR__)); |
133
|
|
|
$expectFile .= '/res/script/password.expect'; |
134
|
|
|
$sshCommand = sprintf('expect %s %s %s', escapeshellarg($expectFile), escapeshellarg($pass), $sshCommand); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
?> |
139
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.