用PHP实现salt-api调用

saltstack由于其强大的配置部署及管理功能,是越来越火,被很多运维人员所认可。由于最近要开发一个运维平台,底层用到了salt-api来实现。如果你是入门的用户可以参考saltstack安装的bolg 
http://blog.coocla.org/category/saltstack 
还有很多优秀的blog,可以百度神马的很多的,还有saltstack中文站也可以逛逛
http://www.saltstack.cn/
安装salt-api请参考
http://www.saltstack.cn/projects/cssug-kb/wiki/salt-api-deploy-and-use
经过saltsatck和salt-api的学习后,开始了苦逼的堆代码的工作,首先用PHP实现了对salt-api的访问,后来由于别的需求也实现了Python对salt-api的访问。
下面的代码是最初的比较原始的版本saltapi.php,不过功能够用了。

<?php
function get_token(){
global $token;
$url = 'https://ip:8000/login'; //ip是已经配好的api地址如 192.168.1.22
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,False);
curl_setopt($ch,CURLOPT_HTTPHEADER, Array('Accept: application/x-yaml'));
curl_setopt($ch,CURLOPT_POSTFIELDS, 'username=你的用户名&password=用户对应的密码&eauth=pam');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
$token = curl_exec($ch);
$token = strstr($token, 'token', false);  //取登录后返回字符串中的token
$token = strstr($token, 'user', true);
$token = explode(" ",$token)[1];
curl_close($ch);
$token = rtrim($token,"\n"); //删掉token字符串最后面的'\n',不然后面死活登不上去
}

//适用于带一个或不带参数的salt exc模块如 salt \* test.ping 或 salt \* test.echo "hello"这样的模块
function exc_salt($tgt, $fun, $arg, $arg_num){
global $token;
global $report;
$url = 'https://ip:8000/';
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,False);
curl_setopt($ch,CURLOPT_HTTPHEADER,Array('Accept: application/x-yaml',"X-Auth-Token: $token"));
if ($arg_num)
    curl_setopt($ch,CURLOPT_POSTFIELDS,"client=local&tgt=$tgt&fun=$fun&arg=$arg");
else
    curl_setopt($ch,CURLOPT_POSTFIELDS,"client=local&tgt=$tgt&fun=$fun");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
$report = curl_exec($ch);
curl_close($ch);
return $report;
}

//适用于带两个参数的salt exc模块
function exc_salt_2($tgt, $fun, $arg_1, $arg_2){
global $token;
global $report;
$url = 'https://ip:8000/';
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,False);
curl_setopt($ch,CURLOPT_HTTPHEADER,Array('Accept: application/x-yaml',"X-Auth-Token: $token"));
curl_setopt($ch,CURLOPT_POSTFIELDS,"client=local&tgt=$tgt&fun=$fun&arg=$arg_1&arg=$arg_2");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
$report = curl_exec($ch);
curl_close($ch);
return $report;
}

//以下是测试这个脚本功能的部分,正式上线是得注释掉
get_token();
print exc_salt('主机key', 'test.echo', 'hello', 1); //带一个参数的
print exc_salt('主机key', 'test.ping', '', 0);  //不带参数的
?>

运行结果如下:
phpsalt
这些函数还可以封装成一个类,方便调用。如果要求不高这样也可以满足要求了。
salt-api手册参考 http://salt-api.readthedocs.org/en/latest/
如果你的python还可以的话,直接看源代码,也是一样一样的。
转载请注明出处http://www.xiaomastack.com/2014/06/26/php-salt-api/ 谢谢!

2 Comments

 Add your comment

发表评论:

98 Trackbacks

  1. Best online casino reviews in New Zealand (Trackback)
  2. Paf Casino (Trackback)
  3. in vitro intestinal permeability (Trackback)
  4. buy marijuana online (Trackback)
  5. Actualite économique Sénégal (Trackback)
  6. Escort Roma (Trackback)
  7. verified sellers fixed matches (Trackback)
  8. Digital Worth Academy (Trackback)
  9. see here (Trackback)
  10. prepaid gift balance (Trackback)