最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】使用Fabric把代码从Mac本地部署到CentOS服务器

Mac crifan 745浏览 0评论
之前知道项目发布时,代码部署可以用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
Fabfile 文件的结构和使用 — Fabric 文档
fabric/fabric: Simple, Pythonic remote execution and deployment.
“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 文档
“Fabric 是一个 Python (2.5-2.7) 的库和命令行工具,用来提高基于 SSH 的应用部署和系统管理效率。”
先去mac中安装Fabric:
【已解决】Mac中安装并试用Fabric
然后继续研究Fabric如何去部署代码。
Getting started — Fabric documentation
有关于文件传输的,就可以用于此处代码部署
然后用:
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去实现同步代码以实现项目代码部署

转载请注明:在路上 » 【已解决】使用Fabric把代码从Mac本地部署到CentOS服务器

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.242 seconds, using 22.19MB memory