forked from meolu/walle-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.php
244 lines (206 loc) · 5.43 KB
/
Command.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
<?php
/* *****************************************************************
* @Author: wushuiyong
* @Created Time : 五 7/31 22:42:32 2015
*
* @File Name: command/Command.php
* @Description:
* *****************************************************************/
namespace app\components;
class Command
{
protected static $LOGDIR = '';
/**
* Handler to the current Log File.
*
* @var mixed
*/
protected static $logFile = null;
/**
* Config
*
* @var \walle\config\Config
*/
protected $config;
/**
* 命令运行返回值:0失败,1成功
*
* @var int
*/
protected $status = 1;
protected $command = '';
protected $log = null;
/**
* 加载配置
*
* @param $config
* @return $this
* @throws \Exception
*/
public function __construct($config)
{
if ($config) {
$this->config = $config;
} else {
throw new \Exception(\yii::t('walle', 'unknown config'));
}
}
/**
* 执行本地宿主机命令
*
* @param $command
* @return bool|int true 成功,false 失败
*/
final public function runLocalCommand($command)
{
$command = trim($command);
$this->log('---------------------------------');
$this->log('---- Executing: $ ' . $command);
$status = 1;
$log = '';
exec($command . ' 2>&1', $log, $status);
// 执行过的命令
$this->command = $command;
// 执行的状态
$this->status = !$status;
// 操作日志
$log = implode(PHP_EOL, $log);
$this->log = trim($log);
$this->log($log);
$this->log('---------------------------------');
return $this->status;
}
/**
* 执行远程目标机器命令
*
* @param string $command
* @param integer $delay 每台机器延迟执行post_release任务间隔, 不推荐使用, 仅当业务无法平滑重启时使用
* @return bool
*/
final public function runRemoteCommand($command, $delay = 0)
{
$this->log = '';
$needTTY = '-T';
foreach (GlobalHelper::str2arr($this->getConfig()->hosts) as $remoteHost) {
$localCommand = sprintf('ssh %s -p %d -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=false %s@%s %s',
$needTTY, $this->getHostPort($remoteHost), escapeshellarg($this->getConfig()->release_user),
escapeshellarg($this->getHostName($remoteHost)), escapeshellarg($command));
if ($delay > 0) {
// 每台机器延迟执行post_release任务间隔, 不推荐使用, 仅当业务无法平滑重启时使用
static::log(sprintf('Sleep: %d s', $delay));
sleep($delay);
}
static::log('Run remote command ' . $command);
$log = $this->log;
$this->status = $this->runLocalCommand($localCommand);
$this->log = $log . (($log ? PHP_EOL : '') . $remoteHost . ' : ' . $this->log);
if (!$this->status) {
return false;
}
}
return true;
}
/**
* 加载配置
*
* @param $config
* @return $this
* @throws \Exception
*/
public function setConfig($config)
{
if ($config) {
$this->config = $config;
} else {
throw new \Exception(\yii::t('walle', 'unknown config'));
}
return $this;
}
/**
* 获取配置
*
* @return \walle\config\Config
*/
protected function getConfig()
{
return $this->config;
}
public static function log($message)
{
if (empty(\Yii::$app->params['log.dir'])) {
return;
}
$logDir = \Yii::$app->params['log.dir'];
if (is_dir($logDir) === false && mkdir($logDir, 0777, true) === false) {
return;
}
$logFile = realpath($logDir) . '/walle-' . date('Ymd') . '.log';
if (self::$logFile === null) {
self::$logFile = fopen($logFile, 'a');
}
$message = date('Y-m-d H:i:s -- ') . $message;
fwrite(self::$logFile, $message . PHP_EOL);
}
/**
* 获取执行command
*
* @author wushuiyong
* @return string
*/
public function getExeCommand()
{
return $this->command;
}
/**
* 获取执行log
*
* @author wushuiyong
* @return string
*/
public function getExeLog()
{
return $this->log;
}
/**
* 获取执行log
*
* @author wushuiyong
* @return string
*/
public function getExeStatus()
{
return $this->status;
}
/**
* 获取耗时毫秒数
*
* @return int
*/
public static function getMs()
{
return intval(microtime(true) * 1000);
}
/**
* 获取目标机器的ip或别名
*
* @param $host
* @return mixed
*/
protected function getHostName($host)
{
list($hostName,) = explode(':', $host);
return $hostName;
}
/**
* 获取目标机器的ssh端口
*
* @param $host
* @param int $default
* @return int
*/
protected function getHostPort($host, $default = 22)
{
$hostInfo = explode(':', $host);
return !empty($hostInfo[1]) ? $hostInfo[1] : $default;
}
}