一、首先要讓salt-master找到我們自定義的腳本(/etc/salt/master):
使用ext_pillar 擴(kuò)展自定義腳本,gameInfo就是腳本的名字,腳本的目錄要放在/usr/lib/python2.6/site-packages/salt/pillar這個(gè)目錄下面
新版可以支持自定義目錄,使用參數(shù)extension_modules: /srv/salt/modules
二、自定義腳本需要返回字典數(shù)據(jù),gameInfo腳本如下:
#!/usr/bin/python #coding:utf-8 import urllib2 import json import logging # Set up logging log = logging.getLogger(__name__) def __virtual__(): return True def ext_pillar(minion_id, pillar, *args, **kwargs): try: ip = minion_id.split('-')[-1] url = 'http://xxx.xxx.com/public/api/SaltPillarInfo/{ip}/'.format(ip=ip) request = urllib2.urlopen(url=url, timeout=30) response = request.read() data = json.loads(response) logging.info("From URL:{url} get data:{data}".format(url=url, data=data)) except Exception as e: logging.error(str(e)) data = {} return {'gameInfo': data}
因?yàn)槲业膍inionid是'game-192.168.1.1' 這種用途-ip的形式命名,minion_id.split('-')[-1]獲取出minion的IP,然后請求django提供的接口,將django返回的數(shù)據(jù)轉(zhuǎn)換成python數(shù)據(jù)格式直接返回出來
三、django代碼就很簡單了
url配置
from django.conf.urls import patterns, include, url from views import * urlpatterns = patterns('', url(r'^api/SaltPillarInfo/(?P<ip>.*)/$', SaltPillarInfoApiView.as_view()), 以下省略。。。 )
views:
class SaltPillarInfoApiView(View): def get(self, request, ip): try: hostobj = GameHost.objects.using('res').get(Q(ip1=ip)|Q(ip2=ip), state=1) gamename = GameInfo.objects.using('res').get(id=hostobj.gameid).code plat = GamePlatform.objects.using('res').get(id=hostobj.platid).code serverlist = [ gsobj.serverid for gsobj in GameServer.objects.using('res').filter(hostid=hostobj.id, mergetarget__isnull=True) ] ret = { 'gamename': gamename, 'plat': plat, 'serverlist': serverlist } except Exception as e: print str(e) ret = {} return HttpResponse(json.dumps(ret))
直接請求就是這個(gè)效果啦:
四、測試:
妥妥地給機(jī)器打上游戲,平臺,區(qū)服的標(biāo)志了。。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前標(biāo)題:結(jié)合django動態(tài)生成salt的pillar數(shù)據(jù)-創(chuàng)新互聯(lián)
標(biāo)題來源:http://muchs.cn/article36/ioisg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、外貿(mào)建站、企業(yè)建站、建站公司、服務(wù)器托管、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容