Saltstack 新特性測試之proxy minion

salt 目前主要的應用場景是Linux OS下,另外還有Windows Client(Win下沒用過,但是看官方issue,應該……),最近關注到官方的一個小模塊提到了Proxy minion,羣裏也多有提及,便想着看看到底是啥存在。


任何設備均可被salt託管

salt proxy minion的出現,使得網管設備或者啞設備(比如sms gateway)均可被salt統一管理,而實際的管理模塊或通訊接口均由用戶自行編寫好,具體操作內容請參見salt 官網對應的 Proxy Minion 介紹


開發中的新特性

首先需要注意一點,Proxy minion是2014.1.x版本引進的新特性,並且到目前爲止仍處於開發階段,僅可用作測試用途。


測試環境的準備

本人的PC環境:

  • vbox下ubuntu 14.04 LTS Server

  • Salt master & minion 2014.1.7(配置test.ping通,不多說了)

準備好基礎環境之後,需要從github下載salt官方開發人員用於測試Proxy Minion的一個小程序(Rest Server,模擬網管設備的管理接口),名字是 salt-proxy-rest


這個程序可能依賴兩個python庫,bottle和requests(其實就是web server需要的組件……),安裝一下即可。


使用 python rest.py 運行該程序,可以將此作爲一個網管設備的Web管理接口:

wKioL1PNwe-DFXmzAADvoe1QHMw550.jpg


嘗試運行!

至此,準備工作算是完成。在當前環境下,salt-master和salt-minion穩定運行,並且有一個提供REST接口的網管設備在獨立工作,我們需要做的便是將其拉進Salt的陣營。


  • 配置pillar


鄙人的minion id是docker,對應的pillar的top.sls內容配置爲:

root@docker:/srv/pillar# cat top.sls
base:
  docker:
    - proxyminion


而proxyminion.sls內容則是對應網管設備的描述:

root@docker:/srv/pillar# cat proxyminion.sls
proxy:
  rest_sample:
    proxytype: rest_sample    
    url: http://127.0.0.1:8080/
    id: proxy_docker


這裏需要注意的是,proxytype必須是在salt/proxy下已經預先定義好的,而其他的一些參數則是自己網管設備通信需要的一些數據,不一定相同。


定義好pillar數據之後,需要爲之添加對應的proxy conn class和grains數據,這裏鄙人使用官方sample,就偷個懶:


root@docker:/srv/pillar# cat /usr/lib/python2.7/dist-packages/salt/proxy/rest_sample.py
# -*- coding: utf-8 -*-'''
This is a simple proxy-minion designed to connect to and communicate with
the bottle-based web service contained in salt/tests/rest.py.

Note this example needs the 'requests' library.
Requests is not a hard dependency for Salt
'''
……


放心,2014.1.7版本已經默認有這個sample代碼。 接下來,直接test.ping試試吧!

root@docker:/srv/pillar# salt '*' test.ping -v
Executing job with jid 20140720110315049478
-------------------------------------------
docker:
    True
rest_sample-localhost:
    True


誒,等一下,爲什麼多出來個key?爲什麼還能test.ping通?沒錯!這個就是ProxyMinion,而salt默認已經配置了test.ping方法兼容proxy minion了,只要寫好對應的ping模塊,就可以使用常規的test.ping來探測!(本例的ping代碼如下)


def ping(self):
    '''
    Is the REST server up?
    '''
    r = requests.get(self.url+'ping')
    try:        
        if r.status_code == 200:
            return True
        else:
            return False
    except Exception:        
        return False


rest_sample還提供很多function,比如鄙人測試的一個service_status,修改對應的模塊代碼即可使之兼容proxy minion(代碼路徑爲/usr/lib/python2.7/dist-packages/salt/modules/service.py):


def status(name, sig=None):
    '''
    Return the status for a service, 
    returns the PID or an empty string if the
    service is running or not, pass a signature
    to use to find the service via ps

    CLI Example:

    .. code-block:: bash

    salt '*' service.status <service name> [service signature]
    '''
    #wjx add, denote it to work!!
    #if 'proxyobject' in __opts__:
    #    return __opts['proxyobject'].service_status(sig if sig else name)
    return __salt__['status.pid'](sig if sig else name)


那麼這時候再看看當前proxy minion管理的服務狀態咋樣了:


root@docker:/srv/pillar# salt '*' service.status apache
rest_sample-localhost:
    ----------    
    comment:
        stopped    
    ret:
        True
docker:
    False


完全和普通minion兼容!!rest_sample本身還配置了grain數據,代碼位於/usr/lib/python2.7/dist-packages/salt/grains/rest_sample.py,直接敲命令看看:


root@docker:/srv/pillar# salt 'rest_sample-localhost' grains.items
rest_sample-localhost:
    housecat: Are you kidding?    
    kernel: 0.0000001
    location: In this darn virtual machine.  Let me out!    
    os: RestExampleOS
    os_family: proxy


Awesome!!這樣一來,一個基本的salt proxy minion就算是配置完成,Proxy Minion 的類定義代碼位於/usr/lib/python2.7/dist-packages/salt/minion.py,有興趣可以看看。


可能的bug

鄙人在本機測試時,Minion Docker在嘗試fork出一個ProxyMinion過程中間報錯,說_running參數沒有配置,在添加代碼後通過(即位於minion.py代碼裏)


class ProxyMinion(Minion):
'''
This class instantiates a 'proxy' minion--a minion that does not manipulate
the host it runs on, but instead manipulates a device that cannot run a minion.
'''
def __init__(self, opts, timeout=60, safe=True):  
# pylint: disable=W0231
    '''
    Pass in the options dict
    '''
    #wjx add, maybe a bug
    self._running = None

    # Warn if ZMQ < 3.2
    if HAS_ZMQ and (not(hasattr(zmq, 'zmq_version_info')) or
                    zmq.zmq_version_info() < (3, 2)):
    ……


聊聊Proxy Minion

Proxy minion使得salt針對網管設備的配置管理成爲可能,不過想要實現一個ProxyType的ProxyMinion的完全管理,可能需要編寫很多額外的module去支持它的運行。


在大公司複雜的網絡環境下,完全可以針對此編寫對應SNMP管理模塊或者針對OVS編寫對應的管理模塊,爾後通過salt統一託管,畢竟Salt有一套完善的配置管理體系啊!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章