forked from meolu/walle-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnsible.php
213 lines (178 loc) · 6.2 KB
/
Ansible.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
<?php
/* *****************************************************************
* @Author: wushuiyong
* @Created Time : 五 7/31 22:21:23 2015
*
* @File Name: command/Sync.php
* @Description:
* *****************************************************************/
namespace app\components;
use app\models\Project;
class Ansible extends Command
{
/**
* Ansible 并发数
*
* @var int
*/
public $ansibleFork = 10;
/**
* Ansible 超时时间
*
* @var int
*/
public $ansibleTimeout = 600;
/**
* Ansible 调用SSH的附加参数
*
* @var string
*/
public $ansibleSshArgs = 'ANSIBLE_SSH_ARGS=\'-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=false\'';
/**
* 测试 ansible 命令是否可用
*
* @return bool|int
*/
public function test()
{
$command = 'ansible --version';
return $this->runLocalCommand($command);
}
/**
* ping 测试 ansible 连接远程主机是否正确
*
* @param array $hosts
*/
public function ping($hosts = [])
{
$ansibleHosts = $this->_getAnsibleHosts($hosts);
$command = sprintf('%s ansible %s -u %s -m ping -i %s -f %d -T %d -vvv',
$this->ansibleSshArgs,
escapeshellarg($ansibleHosts),
escapeshellarg($this->getConfig()->release_user),
escapeshellarg(Project::getAnsibleHostsFile()),
$this->ansibleFork,
$this->ansibleTimeout);
return $this->runLocalCommand($command);
}
/**
* 根据传入的 remote hosts 数组 生成 ansible 命令需要的 hosts 参数
*
* @param array $remoteHosts 不能在remoteHosts中传入 :端口
* @return string
*/
protected function _getAnsibleHosts($remoteHosts = [])
{
if ($remoteHosts) {
// ansible 多个主机通过 : 间隔
$ansibleHosts = implode(':', $remoteHosts);
} else {
$ansibleHosts = 'all';
}
return $ansibleHosts;
}
/**
* 通过 ansible 并发执行目标机器命令
* RAW 模块, 不推荐使用, 仅用于首次安装 python 等场景
*
* @param string $command
* @param array $hosts
* @return bool|int
*/
public function runRemoteCommandByAnsibleRaw($remoteCommand, $hosts = [])
{
$ansibleHosts = $this->_getAnsibleHosts($hosts);
$localCommand = sprintf('%s ansible %s -u %s -m raw -a %s -i %s -f %d -T %d',
$this->ansibleSshArgs,
escapeshellarg($ansibleHosts),
escapeshellarg($this->getConfig()->release_user),
escapeshellarg($remoteCommand),
escapeshellarg(Project::getAnsibleHostsFile()),
$this->ansibleFork,
$this->ansibleTimeout);
return $this->runLocalCommand($localCommand);
}
/**
* 通过 ansible 并发执行目标机器命令
* Command 模块, 无法取得返回值, 不支持管道符, 命令中含有 $HOME, "<", ">", "|", and "&" 会返回失败
*
* @param string $command
* @param array $hosts
* @return bool|int
*/
public function runRemoteCommandByAnsibleCommand($remoteCommand, $hosts = [])
{
$ansibleHosts = $this->_getAnsibleHosts($hosts);
$localCommand = sprintf('%s ansible %s -u %s -m command -a %s -i %s -f %d -T %d',
$this->ansibleSshArgs,
escapeshellarg($ansibleHosts),
escapeshellarg($this->getConfig()->release_user),
escapeshellarg($remoteCommand),
escapeshellarg(Project::getAnsibleHostsFile()),
$this->ansibleFork,
$this->ansibleTimeout);
return $this->runLocalCommand($localCommand);
}
/**
* 通过 ansible 并发执行目标机器命令
* Shell 模块, 推荐使用
*
* @param string $command
* @param array $hosts
* @return bool|int
*/
public function runRemoteCommandByAnsibleShell($remoteCommand, $hosts = [])
{
$ansibleHosts = $this->_getAnsibleHosts($hosts);
$localCommand = sprintf('%s ansible %s -u %s -m shell -a %s -i %s -f %d -T %d',
$this->ansibleSshArgs,
escapeshellarg($ansibleHosts),
escapeshellarg($this->getConfig()->release_user),
escapeshellarg($remoteCommand),
escapeshellarg(Project::getAnsibleHostsFile()),
$this->ansibleFork,
$this->ansibleTimeout);
return $this->runLocalCommand($localCommand);
}
/**
* 通过 ansible 并发执行目标机器命令
* script 模块, 将宿主机的 .sh 文件推送到目标机上, 再执行这个文件
*
* @param string $command
* @param array $hosts
* @return bool|int
*/
public function runRemoteCommandByAnsibleScript($shellFile, $hosts = [])
{
$ansibleHosts = $this->_getAnsibleHosts($hosts);
$localCommand = sprintf('%s ansible %s -u %s -m script -a %s -i %s -f %d -T %d',
$this->ansibleSshArgs,
escapeshellarg($ansibleHosts),
escapeshellarg($this->getConfig()->release_user),
escapeshellarg($shellFile),
escapeshellarg(Project::getAnsibleHostsFile()),
$this->ansibleFork,
$this->ansibleTimeout);
return $this->runLocalCommand($localCommand);
}
/**
* 通过 ansible 复制文件模块, 并发传输文件
*
* @param string $src 宿主机文件路径
* @param string $dest 目标机文件路径
* @param array $hosts 可选的主机列表
* @return bool|int
*/
public function copyFilesByAnsibleCopy($src, $dest, $hosts = []) {
$ansibleHosts = $this->_getAnsibleHosts($hosts);
$localCommand = sprintf('%s ansible %s -u %s -m copy -a %s -i %s -f %d -T %d',
$this->ansibleSshArgs,
escapeshellarg($ansibleHosts),
escapeshellarg($this->getConfig()->release_user),
escapeshellarg('src=' . $src . ' dest=' . $dest),
escapeshellarg(Project::getAnsibleHostsFile()),
$this->ansibleFork,
$this->ansibleTimeout);
return $this->runLocalCommand($localCommand);
}
}