资讯专栏INFORMATION COLUMN

Fabric - 动态生成主机列表和角色列表

Lsnsh / 746人阅读

摘要:在使用的过程中,一般我们常用的方式是手工填写主机列表或者是角色列表,但是这样当服务器数量超级多的时候,你会有想死的感觉的。

在使用 Fabric 的过程中,一般我们常用的方式是手工填写主机列表或者是角色列表,但是这样当服务器数量超级多的时候,你会有想死的感觉的。正好公司有 cmdb 的话,就可以结合 CMDB 来做。

  

PS:如果公司没有开发 CMDB 系统,也自己尽量弄个简单的系统录入服务器数据吧,不要那么复杂,提供 API,能获取主机列表即可。

动态生成主机列表

通过参考 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)

然后执行命令:

$ fab deploy:app
$ fab deploy:db

其生成主机列表的格式如下:

["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"]

现在我们就可以根据 CMDB 接口来动态生成主机列表了。具体见代码吧

方法一:

#!/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)
# 调用
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)
#调用
fab set_hosts:test.com do_work

上面两种方法的区别是,第二种方法更容易替换执行其他任务

动态生成角色列表

#!/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 gener_roles(domain_name): ips = find_ips_by_domain(domain_name) ### 动态生成角色列表 **env.roledefs["ips"] = map(lambda x: x, ips)** ### 根据生成的角色列表处理任务 execute(do_work) @roles("ips") def do_work(): run("echo "Running stress test..."")

执行任务的方式为:

 fab gener_roles:test.com
参考资料

Using execute with dynamically-set host lists

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/37474.html

相关文章

  • SSH连接与自动化部署工具paramiko与Fabric

    摘要:是基于实现的远程安全连接,支持认证及密钥方法。利用函数发送到,通过函数获取回显。如下全局属性设定对象的作用是定义的全局设定,支持多个属性及自定义属性。相比确实简化了不少。出现异常时,发出警告,继续执行,不要终止。 paramiko paramiko是基于Python实现的SSH2远程安全连接,支持认证及密钥方法。可以实现远程命令执行,文件传输,中间SSH代理等功能,相对于Pexpect...

    ermaoL 评论0 收藏0
  • Fabric 实践:local 并发执行

    摘要:环境服务器目标服务器组一共台服务器需求我需要把我服务器上的某些文件同步到集群,但是我又需要并发执行,而不是通过循环或者是串行的方式。 环境: fabric 服务器:10.10.1.1 目标服务器组:test.com (10.10.1.2-21)一共 20 台服务器 需求: 我需要把我 fabric 服务器上的某些文件同步到 test.com 集群,但是我又需要并发执行,而不是通过 ...

    kviccn 评论0 收藏0
  • Hyperledger Fabric(术语表)

    摘要:区块链接到区块,区块链接到区块。共识整个交易流的更广泛的术语,用于生成顺序协议并确认构成区块的交易集合的正确性。策略策略是由数字身份的属性组成的表达式,例如。在中,智能合约被称为链码,智能合约链码安装在对等节点上并实例化为一个或多个通道。 术语表 术语很重要,以便所有Hyperledger Fabric用户和开发人员都同意每个特定术语的含义,例如,什么是智能合约。文档将根据需要引用术语...

    wind3110991 评论0 收藏0
  • Hyperledger Fabric(功能)

    摘要:私有通道是受限制的消息传递路径,可用于为网络成员的特定子集提供交易隐私和机密性。所有数据,包括交易,成员和通道信息,在通道上是不可见的,并且任何未明确授予对通频道的访问权限的网络成员都无法访问。 Hyperledger Fabric功能 Hyperledger Fabric是分布式分类账技术(DLT)的一种实现,可在模块化区块链架构中提供企业级网络安全性,可扩展性,机密性和性能,Hyp...

    Ashin 评论0 收藏0
  • Hyperledger Fabric(身份)

    摘要:的证书撤销列表构成不再有效的证书的参考,证书的撤销可能由于多种原因而发生,例如,因为与证书关联的加密私有材料已被公开导致证书可能会被撤销。描述一个名为的当事人的数字证书,是证书的,高亮的文本显示了关于的关键事实。 身份 什么是身份? 区块链网络中的不同参与者包括对等点、排序者、客户端应用程序,管理员等。这些参与者中的每一个 — 网络内部或外部能够使用服务的活动元素 — 都具有封装在X....

    ConardLi 评论0 收藏0

发表评论

0条评论

Lsnsh

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<