fabric 强大的运维工具包,python 实现自动化部署。
from fabric import Connection
c = Connection(host=host,user=user,connect_kwargs={"password":"ubuntu"})
c.run("uname -a")
c.sudo("ls",password="")
with c.cd("../"):
c.run("pwd")
shell上下文的或者环境变量
with c.prefix('workon myvenv'):
c.run('./manage.py migrate')
实用小例:
def upload_and_unpack(c):
"""上传解压"""
c.put("fabric_01.zip", "/home/ubuntu/")
c.run('unzip -o /home/ubuntu/fabric_01.zip -d /home/ubuntu/data')
c.run("ls")
with c.cd("/home/ubuntu/data"):
res = c.run("ls")
print("======")
print(res.stdout)
print(res.exited)
def pack_and_download(c):
"""打包下载"""
"zip -r test.zip /home/ubuntu/"
c.run("ls") # 打包
c.run("pwd")
c.run("zip -r test.zip /home/ubuntu/test")
with c.cd("/home/ubuntu"):
c.get("test.zip", r"D:\\")
参考:[https://docs.fabfile.org/en/2.6/getting-started.html]