之前知道项目发布时,代码部署可以用Fabric这种工具。
一直没用过。
这次别人建议用,所以去试试,其中现有一个配置如下:

from fabric.api import *
from fabric.contrib.project import rsync_project
from fabric.contrib.console import confirm
env.hosts = ['root@xxx-general-01']
def rsync():
rsync_project(
remote_dir="/root/xxx_20180101/",
local_dir="../processData",
exclude=(".*", "*.pyc", 'logs', '*.log', 'tmp', 'cach', 'model', 'models'),
delete=('processData/')
)要搞清楚怎么操作。
而此处环境是:
本地是Mac开发,代码调试完毕后,git上传到仓库
而之前部署最新代码的方式是:
要么去服务器上 git pull
要么Mac本地用PyCharm的deployment中用sftp上传代码
而要搞清楚,后续如何改为Fabric去部署
fabric 部署
fabric python
“Fabric is a high level Python (2.7, 3.4+) library designed to execute shell commands remotely over SSH, yielding useful Python objects in return. It builds on top of Invoke (subprocess command execution and command-line features) and Paramiko (SSH protocol implementation), extending their APIs to complement one another and provide additional functionality.”
“Fabric 是一个 Python (2.5-2.7) 的库和命令行工具,用来提高基于 SSH 的应用部署和系统管理效率。”
先去mac中安装Fabric:
【已解决】Mac中安装并试用Fabric
然后继续研究Fabric如何去部署代码。
有关于文件传输的,就可以用于此处代码部署
然后用:
from invoke import task
from fabric import Connection
RemoteHost = 'xxx'
RemoteUser = 'xxx'
RemotePathRoot = '/root/xxx_20180101/nlp/test_fabric'
def seperatorLine(seperatorChar='-', count=80):
print(seperatorChar * count)
@task
def deploy(context):
remoteConn = Connection(host=RemoteHost, user=RemoteUser)
# print(remoteConn)
seperatorLine()
remoteConn.run('uname -a')
remoteConn.run('pwd')
# remoteConn.run('ls -lha')
remoteConn.run('cd %s' % RemotePathRoot)
remoteConn.run('pwd')
# remoteConn.run('ls -lha')
putFileResult = remoteConn.put('fabfile.py', remote=RemotePathRoot)
print("Uploaded {0.local} to {0.remote}".format(putFileResult))可以实现上次单个文件:
[root@xxx-general-01 test_fabric]# pwd /root/xxx_20180101/nlp/test_fabric [root@xxx-general-01 test_fabric]# ls -lh total 4.0K -rw-r--r-- 1 root root 2.5K Aug 17 13:56 fabfile.py
【已解决】Fabric 2中如何实现Fabric 1中的rsync_project去实现同步代码以实现项目代码部署