亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

Fabric - 動(dòng)態(tài)生成主機(jī)列表和角色列表

Lsnsh / 974人閱讀

摘要:在使用的過程中,一般我們常用的方式是手工填寫主機(jī)列表或者是角色列表,但是這樣當(dāng)服務(wù)器數(shù)量超級(jí)多的時(shí)候,你會(huì)有想死的感覺的。

在使用 Fabric 的過程中,一般我們常用的方式是手工填寫主機(jī)列表或者是角色列表,但是這樣當(dāng)服務(wù)器數(shù)量超級(jí)多的時(shí)候,你會(huì)有想死的感覺的。正好公司有 cmdb 的話,就可以結(jié)合 CMDB 來(lái)做。

  

PS:如果公司沒有開發(fā) CMDB 系統(tǒng),也自己盡量弄個(gè)簡(jiǎn)單的系統(tǒng)錄入服務(wù)器數(shù)據(jù)吧,不要那么復(fù)雜,提供 API,能獲取主機(jī)列表即可。

動(dòng)態(tài)生成主機(jī)列表

通過參考 Fabric 的官方文檔的 Using execute with dynamically-set host lists,其中有這么一段示例代碼:

from fabric.api import run, execute, task

# For example, code talking to an HTTP API, or a database, or ...
from mylib import external_datastore

# This is the actual algorithm involved. It does not care about host
# lists at all.
def do_work():
    run("something interesting on a host")

# This is the user-facing task invoked on the command line.
@task
def deploy(lookup_param):
    # This is the magic you don"t get with @hosts or @roles.
    # Even lazy-loading roles require you to declare available roles
    # beforehand. Here, the sky is the limit.
    host_list = external_datastore.query(lookup_param)
    # Put this dynamically generated host list together with the work to be
    # done.
    execute(do_work, hosts=host_list)

然后執(zhí)行命令:

$ fab deploy:app
$ fab deploy:db

其生成主機(jī)列表的格式如下:

["10.2.5.1", "10.2.5.2", "10.2.5.3", "10.2.5.4", "10.2.5.5", "10.2.5.6", "10.2.5.7", "10.2.5.8", "10.2.5.9", "10.2.5.10"]

現(xiàn)在我們就可以根據(jù) CMDB 接口來(lái)動(dòng)態(tài)生成主機(jī)列表了。具體見代碼吧

方法一:

#!/usr/bin/env python
#encoding=utf-8
import sys
import os
import requests


from fabric.api import env
from fabric.api import run
from fabric.api import put
from fabric.api import execute
from fabric.api import roles
from fabric.api import parallel
from fabric.api import cd
from fabric.api import task

env.user = "test"
env.password = "test"



cmdburl = "http://cmdb.test.com/test/listServer.do"


def find_ips_by_domain(domain_name):
     ips=[]
     payload={"domain":domain_name}
     res = requests.get(cmdburl, params=payload)
     hosts=res.json()["object"][0]["servers"]
     for host in hosts:
         host_ip=host["ip"]
         ips.append(host_ip)
     return ips 




def do_work():
    run("echo "Running stress test..."")

@task
def set_hosts(domain):
    # Update env.hosts instead of calling execute()
    host_list = find_ips_by_domain(domain)
    execute(do_work, hosts=host_list)
# 調(diào)用
fab set_hosts:app
fab set_hosts:db

方法二:

#!/usr/bin/env python
#encoding=utf-8
import sys
import os
import requests

from fabric.api import env
from fabric.api import run
from fabric.api import put
from fabric.api import execute
from fabric.api import roles
from fabric.api import parallel
from fabric.api import cd
from fabric.api import task

env.user = "test"
env.password = "test"



cmdburl = "http://cmdb.test.com/test/listServer.do"


def find_ips_by_domain(domain_name):
     ips=[]
     payload={"domain":domain_name}
     res = requests.get(cmdburl, params=payload)
     hosts=res.json()["object"][0]["servers"]
     for host in hosts:
         host_ip=host["ip"]
         ips.append(host_ip)
     return ips 



@task
def do_work():
    run("echo "Running stress test..."")

@task
def set_hosts(domain):
    # Update env.hosts instead of calling execute()
    env.hosts = find_ips_by_domain(domain)
#調(diào)用
fab set_hosts:test.com do_work

上面兩種方法的區(qū)別是,第二種方法更容易替換執(zhí)行其他任務(wù)

動(dòng)態(tài)生成角色列表

#!/usr/bin/env python #encoding=utf-8 import sys import os import requests from fabric.api import env from fabric.api import run from fabric.api import put from fabric.api import execute from fabric.api import roles from fabric.api import parallel from fabric.api import cd from fabric.api import task env.user = "test" env.password = "test" cmdburl = "http://cmdb.test.com/test/listServer.do" ## 根據(jù)域名(服務(wù)名)查詢?cè)撚虻乃蟹?wù)器列表 def find_ips_by_domain(domain_name): ips=[] payload={"domain":domain_name} res = requests.get(cmdburl, params=payload) hosts=res.json()["object"][0]["servers"] for host in hosts: host_ip=host["ip"] ips.append(host_ip) return ips @task def gener_roles(domain_name): ips = find_ips_by_domain(domain_name) ### 動(dòng)態(tài)生成角色列表 **env.roledefs["ips"] = map(lambda x: x, ips)** ### 根據(jù)生成的角色列表處理任務(wù) execute(do_work) @roles("ips") def do_work(): run("echo "Running stress test..."")

執(zhí)行任務(wù)的方式為:

 fab gener_roles:test.com
參考資料

Using execute with dynamically-set host lists

http://my.oschina.net/indestiny/blog/290239

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/37474.html

相關(guān)文章

  • SSH連接與自動(dòng)化部署工具paramiko與Fabric

    摘要:是基于實(shí)現(xiàn)的遠(yuǎn)程安全連接,支持認(rèn)證及密鑰方法。利用函數(shù)發(fā)送到,通過函數(shù)獲取回顯。如下全局屬性設(shè)定對(duì)象的作用是定義的全局設(shè)定,支持多個(gè)屬性及自定義屬性。相比確實(shí)簡(jiǎn)化了不少。出現(xiàn)異常時(shí),發(fā)出警告,繼續(xù)執(zhí)行,不要終止。 paramiko paramiko是基于Python實(shí)現(xiàn)的SSH2遠(yuǎn)程安全連接,支持認(rèn)證及密鑰方法??梢詫?shí)現(xiàn)遠(yuǎn)程命令執(zhí)行,文件傳輸,中間SSH代理等功能,相對(duì)于Pexpect...

    ermaoL 評(píng)論0 收藏0
  • Fabric 實(shí)踐:local 并發(fā)執(zhí)行

    摘要:環(huán)境服務(wù)器目標(biāo)服務(wù)器組一共臺(tái)服務(wù)器需求我需要把我服務(wù)器上的某些文件同步到集群,但是我又需要并發(fā)執(zhí)行,而不是通過循環(huán)或者是串行的方式。 環(huán)境: fabric 服務(wù)器:10.10.1.1 目標(biāo)服務(wù)器組:test.com (10.10.1.2-21)一共 20 臺(tái)服務(wù)器 需求: 我需要把我 fabric 服務(wù)器上的某些文件同步到 test.com 集群,但是我又需要并發(fā)執(zhí)行,而不是通過 ...

    kviccn 評(píng)論0 收藏0
  • Hyperledger Fabric(術(shù)語(yǔ)表)

    摘要:區(qū)塊鏈接到區(qū)塊,區(qū)塊鏈接到區(qū)塊。共識(shí)整個(gè)交易流的更廣泛的術(shù)語(yǔ),用于生成順序協(xié)議并確認(rèn)構(gòu)成區(qū)塊的交易集合的正確性。策略策略是由數(shù)字身份的屬性組成的表達(dá)式,例如。在中,智能合約被稱為鏈碼,智能合約鏈碼安裝在對(duì)等節(jié)點(diǎn)上并實(shí)例化為一個(gè)或多個(gè)通道。 術(shù)語(yǔ)表 術(shù)語(yǔ)很重要,以便所有Hyperledger Fabric用戶和開發(fā)人員都同意每個(gè)特定術(shù)語(yǔ)的含義,例如,什么是智能合約。文檔將根據(jù)需要引用術(shù)語(yǔ)...

    wind3110991 評(píng)論0 收藏0
  • Hyperledger Fabric(功能)

    摘要:私有通道是受限制的消息傳遞路徑,可用于為網(wǎng)絡(luò)成員的特定子集提供交易隱私和機(jī)密性。所有數(shù)據(jù),包括交易,成員和通道信息,在通道上是不可見的,并且任何未明確授予對(duì)通頻道的訪問權(quán)限的網(wǎng)絡(luò)成員都無(wú)法訪問。 Hyperledger Fabric功能 Hyperledger Fabric是分布式分類賬技術(shù)(DLT)的一種實(shí)現(xiàn),可在模塊化區(qū)塊鏈架構(gòu)中提供企業(yè)級(jí)網(wǎng)絡(luò)安全性,可擴(kuò)展性,機(jī)密性和性能,Hyp...

    Ashin 評(píng)論0 收藏0
  • Hyperledger Fabric(身份)

    摘要:的證書撤銷列表構(gòu)成不再有效的證書的參考,證書的撤銷可能由于多種原因而發(fā)生,例如,因?yàn)榕c證書關(guān)聯(lián)的加密私有材料已被公開導(dǎo)致證書可能會(huì)被撤銷。描述一個(gè)名為的當(dāng)事人的數(shù)字證書,是證書的,高亮的文本顯示了關(guān)于的關(guān)鍵事實(shí)。 身份 什么是身份? 區(qū)塊鏈網(wǎng)絡(luò)中的不同參與者包括對(duì)等點(diǎn)、排序者、客戶端應(yīng)用程序,管理員等。這些參與者中的每一個(gè) — 網(wǎng)絡(luò)內(nèi)部或外部能夠使用服務(wù)的活動(dòng)元素 — 都具有封裝在X....

    ConardLi 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

Lsnsh

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<