因为在学习salt-gitfs过程中。需要安装salt-api。这里面好多坑,其实步骤很简单的!

把这个过程记录一下。

系统环境:centos6.5x86_64

          python 2.6+

一、安装salt和依赖

cat /etc/redhat-release


安装epel源

yum install epel-release


安装依赖

yum -y install kernel-firmware kernel-headers perf e2fsprogs

yum  install libyaml

yum install PyYAML

 yum install salt-master salt-api


二、安装pip

在安装pip前,先安装setuptools

否则会报错:

Traceback (most recent call last):

  File "setup.py", line 6, in <module>

    from setuptools import setup, find_packages

ImportError: No module named setuptools

安装setuptools

wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-12.0.3.tar.gz#md5=f07e4b0f4c1c9368fcd980d888b29a65 

cd setuptools-12.3

python setup.py install

接下来可以安装pip了。

wget https://pypi.python.org/packages/source/p/pip/pip-1.5.6.tar.gz#md5=01026f87978932060cc86c1dc527903e --no-check-certificate

tar -zxvf pip-*
cd pip-
python setup.py build && python setup.py install && pip freeze
#使用pip安装cherrypy:
pip install cherrypy==3.2.3

三、配置openssl证书

1、

cd /etc/pki/tls/certs


umask 77 ; \

/usr/bin/openssl genrsa -aes128 2048 > /etc/pki/tls/private/localhost.key

Generating RSA private key, 2048 bit long modulus

........................................+++

...............+++

e is 65537 (0x10001)

Enter pass phrase:(此处输入一个test)

Verifying - Enter pass phrase:(再次确认你输入的)


/usr/bin/openssl req -utf8 -new -key /etc/pki/tls/private/localhost.key 也输入你上次输入那个

后面的提示都是可选择填的。。

此时你的../private/的文件夹中多了个localhost.key


openssl rsa -in localhost.key -out localhost_nopass.key
Enter pass phrase for localhost.key:此处输入上次输入那个
writing RSA key

创建一个用户为api访问。但是不能登陆。
useradd -M -s /sbin/nologin test
重设密码:
passwd test

四、配置salt-api

配置salt-master 修改/etc/salt/master

将default_include:选项去掉注释

mkdir -p /etc/salt/master.d


cd /etc/salt/master.d


vim api.conf

rest_cherrypy:
  port: 8000
  ssl_crt: /etc/pki/tls/certs/localhost.crt
  ssl_key: /etc/pki/tls/private/localhost_nopass.key

vim eauth.conf

external_auth:
  pam:
    test:
      - .*
      - '@wheel'
      - '@runner'
      
 重新启动salt-master和salt-api服务:
 
 [root@Gitlab-test ~]# service salt-master restart
Stopping salt-master daemon:                               [FAILED]
Starting salt-master daemon:                               [  OK  ]
[root@Gitlab-test ~]# service salt-api restart
Stopping salt-api daemon:                                  [FAILED]
Starting salt-api daemon:                                  [  OK  ]
此时可以查看端口:
[root@Gitlab-test ~]# netstat -an |grep :8000
tcp        0      0 0.0.0.0:8000                0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:46591             127.0.0.1:8000              TIME_WAIT  
说明salt-api服务正常启动。。
一般你可以查看/var/log/salt/master 日志去看salt-api的运行情况。

五、简单使用salt-api
(1)首先登陆获取Token

[root@Master master.d]# curl  -k https://192.168.100.129:8001/login -H  "Accept: application/json"  -d username='test' -d password='test' -d eauth='pam' -d tgt='*' -d fun='status.diskusage' |jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
258   194  194   194    0    64   1529    504 --:--:-- --:--:-- --:--:--  1805
{
  "return": [
    {
      "eauth": "pam",
      "user": "test",
      "expire": 1454339116.828233,
      "token": "e573084a3bb6d39489644ee73a3274e7bc063a34",
      "start": 1454295916.828232,
      "perms": [
        ".*",
        "@wheel",
        "@runner"
      ]
    }
  ]
}
自此后登陆就不用使用用户和密码了。
(2)[root@Master master.d]# curl -k https://192.168.100.129:8001/ -H "Accpet:application/json" -H "X-Auth-Token:e573084a3bb6d39489644ee73a3274e7bc063a34" -d client='local' -d tgt='*' -d fun='cmd.run' -d arg='free -m'|jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
109   507  101   507    0    42   1446    119 --:--:-- --:--:-- --:--:--  1560
{
  "return": [
    {
      "Master": "             total       used       free     shared    buffers     cached\nMem:           490        485          5          0         18         43\n-/+ buffers/cache:        423         67\nSwap:         2047         45       2002",
      "minion": "             total       used       free     shared    buffers     cached\nMem:          2006       1788        217          0         58        151\n-/+ buffers/cache:       1578        427 \nSwap:         2047          0       2047"
    }
  ]
}
(3)运行runner可以查看你的minion运行情况
[root@master ~]#  curl -k https://192.168.100.129:8000/jobs -H "Accpet:application/json" -H "X-Auth-Token:8abe3367cd9fd93ba5773f7f559f697195712ee3" -d client='runner' -d fun='manage.status'|jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0    52    0    52    0    31      3      1 --:--:--  0:00:15 --:--:--     4
{
  "return": [
    {
      "up": [
        "Master"
      ],
      "down": [
        "minion"
      ]
    }
  ]
}