美文网首页
Fabric--几个简单的API

Fabric--几个简单的API

作者: 涵仔睡觉 | 来源:发表于2017-09-18 22:24 被阅读0次

    Fabric

    fabric允许通过命令行执行任意Python函数,是用来在SSH和python上执行shell命令的子程序库。

    工具fab导入fabfile.py并执行指定的Python函数。如

    # filename:fabfile.py
    def hello():
        print("Hello world!")
    

    命令行执行语句:

    $ fab hello
    
    输出:hello world!
    

    执行带参数Python函数:

    # filename:fabfile.py
    def hello(name="world"):
        print("Hello %s!" % name)
    
    $ fab hello:name=Jeff
    
    输出:Hello Jeff!
    

    定义连接

    # env.host确定要连接的远程服务器
    # filename:fabfile.py
    env.hosts = ['my_server_ip']
    env.user = 'my_server_name'
    env.sudo_user = 'root'
    
    def test():
        do_test_stuff()
    

    fabric APIs

    • local(string of command)

    在本地执行shell命令,命令以字符串的形式作为参数,如:

    # filename:fabfile.py
    # 执行命令行,添加、提交git仓库的修改,并push到远程仓库
    from fabric.api import local
    
    def prepare_deploy():
        local("git add -p && git commit")
        local("git push")
    
    • run(string of command)

    在远程服务器上'my_server'执行shell命令,命令以字符串的形式作为参数,如:

    # filename:fabfile.py
    # 在远程服务器上执行命令行,进入目录code_dir,git获取仓库
    from fabric.api import local
    
    def deploy():
        code_dir = '/srv/django/myproject'
        with cd(code_dir):
            run("git pull")
    
    • lcd(string of directory)

    在本地执行目录切换操作,路径以字符串形式作为参数

    • cd(string of directory)

    在远程服务器'my_server'上执行目录切换操作,路径以字符串形式作为参数

    • put(file, remote_directory)

    把本地文件file上传到远程服务器的remote_directory目录下

    综合应用链接

    相关文章

      网友评论

          本文标题:Fabric--几个简单的API

          本文链接:https://www.haomeiwen.com/subject/btdisxtx.html