当前位置: 代码迷 >> python >> 本地和远程的Python Fabric部署
  详细解决方案

本地和远程的Python Fabric部署

热度:61   发布时间:2023-06-13 15:18:07.0

我使用fabric来自动执行在本地计算机或远程服务器上运行的许多任务。

所有任务都是单独的。 但我想要的是有一个新的任务,在本地机器上做一些工作(构建一个图像,上传)和一些在服务器上工作(重新加载服务器)

如果我手动运行命令,它们可以工作:

fab build_file
fab remote deploy_file

这是一个输出:

> fab remote deploy_file
[localhost] Executing task 'remote'
[docker.me] Executing task 'deploy_file'
[docker.me] run: echo 'reload image'
[docker.me] out: reload image
[docker.me] out:


Done.
Disconnecting from docker.me... done.

但如果我尝试结合本地和远程任务,这就是我得到的:

> fab do_all
[localhost] Executing task 'do_all'
[localhost] local: echo 'make image'
make image
[localhost] local: echo 'upload image'
upload image
[localhost] run: echo 'reload image'

Fatal error: Low level socket error connecting to host localhost on port 22: Connection refused (tried 1 time)

Underlying exception:
    Connection refused

Aborting.

有没有办法在单个优步任务中执行本地和远程任务?

我的fabfile.py:

from fabric.api import *

env.run = local
env.hosts = ['localhost']

@task
def remote():
    env.run = run
    env.hosts = ["docker.me"]

@task
def build_file():
    env.run("echo 'make image'")
    env.run("echo 'upload image'")


@task
def deploy_file():
    env.run("echo 'reload image'")


@task
def do_all():
    build_file()
    remote()
    deploy_file()

我可能会忽略这一点,但你为什么不简单地做这样的事情

from fabric.api import *

env.hosts = ['docker.me']

@task
def all():
    local("echo 'make image'")
    local("echo 'upload image'")
    run("echo 'reload image'")

编辑:

我懂了。 你的代码基本上没问题。 只有一个细节。 使用以下代码,您应该没问题:

from fabric.api import *

env.run = local
env.hosts = ["docker.me"] # change this!

@task
def remote():
    env.run = run

@task
def build_file():
    env.run("echo 'make image'")
    env.run("echo 'upload image'")

@task
def deploy_file():
    env.run("echo 'reload image'")

@task
def do_all():
    build_file()
    remote()
    deploy_file()