在本地运行结构脚本

发布于 2024-11-24 17:56:22 字数 184 浏览 1 评论 0原文

我有一个 django 应用程序,我编写了一个结构脚本,用于在部署服务器(Cent OS 5)上安装我的应用程序。

现在我想在部署服务器上本地运行相同的结构脚本。

有没有办法在不提供 ssh 用户和密码的情况下完成此操作?

我的意思是只用“-H localhost”?

谢谢,亚历克斯·A.

I have a django app and I wrote a fabric script that installs my app on deployment server (Cent OS 5).

Now I want to run the same fabric script locally on the deployment server.

Is there a way to do it without supplying ssh user and password?

I mean just with "-H localhost"?

Thanks, Alex A.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心房敞 2024-12-01 17:56:22

是的,您可以使用方法local而不是run在本地运行fab。我通常做的是设置环境的方法,并在调用实际任务之前先调用这些方法。让我用一个针对您的具体问题的示例来说明这一点

fabfile.py

    from fabric.operations import local as lrun, run
    from fabric.api import task
    from fabric.state import env

    @task
    def localhost():
        env.run = lrun
        env.hosts = ['localhost']

    @task
    def remote():
        env.run = run
        env.hosts = ['some.remote.host']

    @task
    def install():
        env.run('deploymentcmd')

并且根据环境,您可以执行以下操作

在本地主机上安装:

    fab localhost install

在远程计算机上安装:

    fab remote install

Yes, you can run fab locally by using method local instead of run. What I do typically is have methods for setting up the environment, and call these methods first before calling the actual task. Let me illustrate this with an example for your specific question

fabfile.py

    from fabric.operations import local as lrun, run
    from fabric.api import task
    from fabric.state import env

    @task
    def localhost():
        env.run = lrun
        env.hosts = ['localhost']

    @task
    def remote():
        env.run = run
        env.hosts = ['some.remote.host']

    @task
    def install():
        env.run('deploymentcmd')

And based on the environment, you can do the following

Install on localhost:

    fab localhost install

Install on remote machine:

    fab remote install
不一样的天空 2024-12-01 17:56:22

我正在使用另一个技巧在本地执行远程任务:

from fabric.api import run, sudo, local
from contextlib import contextmanager

@contextmanager
def locally():
    global run
    global sudo
    global local
    _run, _sudo = run, sudo
    run = sudo = local
    yield
    run, sudo = _run, _sudo

def local_task():
    with locally():
        run("ls -la")

I am using another trick for executing remote task locally:

from fabric.api import run, sudo, local
from contextlib import contextmanager

@contextmanager
def locally():
    global run
    global sudo
    global local
    _run, _sudo = run, sudo
    run = sudo = local
    yield
    run, sudo = _run, _sudo

def local_task():
    with locally():
        run("ls -la")
小耗子 2024-12-01 17:56:22

Varun 的答案 稍微不那么优雅,但默认在本地计算机上运行可能更实用,除非给出另一个环境选择器。

from fabric.api import *

# default to running on localhost
env.hosts = ["localhost"]

DEPLOYMENT_PATH = "/some/path/{}/"

def local_or_remote(*args, **kwargs):
    func = local if env.host == "localhost" else run
    return func(*args, **kwargs)


@task
def live():
    """
    Select live environment
    """
    env.hosts = ["host1", "host2"]
    env.path = DEPLOYMENT_PATH.format("live")


@task
def beta():
    """
    Select beta environment
    """
    env.hosts = ["host1", "host2"]
    env.path = DEPLOYMENT_PATH.format("beta")


@task
def host_info():
    local_or_remote("uname -a")

然后在本地运行为:

fab host_info

或远程运行为:

fab live host_info

PS。以下是关于此主题的 Github 问题

Slightly less elegant than Varun's answer, but maybe more practical by defaulting to run on the local machine unless another environment selector is given.

from fabric.api import *

# default to running on localhost
env.hosts = ["localhost"]

DEPLOYMENT_PATH = "/some/path/{}/"

def local_or_remote(*args, **kwargs):
    func = local if env.host == "localhost" else run
    return func(*args, **kwargs)


@task
def live():
    """
    Select live environment
    """
    env.hosts = ["host1", "host2"]
    env.path = DEPLOYMENT_PATH.format("live")


@task
def beta():
    """
    Select beta environment
    """
    env.hosts = ["host1", "host2"]
    env.path = DEPLOYMENT_PATH.format("beta")


@task
def host_info():
    local_or_remote("uname -a")

Then run locally as:

fab host_info

or remotely with:

fab live host_info

PS. Here is the Github issue on this topic.

断肠人 2024-12-01 17:56:22

首先,确保您可以在没有密码的情况下 ssh 进入本地主机:

$ ssh-copy-id localhost

然后按照您所说的使用 -H localhost 命令运行它线路选项

First, make sure you can ssh into your localhost without a password:

$ ssh-copy-id localhost

And then just run it as you said, with the -H localhost command line option

森林迷了鹿 2024-12-01 17:56:22

您可以在本地计算机上运行 ssh 服务器,以便能够 ssh 到本地主机。然后只需使用“-H localhost”运行脚本即可。非常适合我。

You can run ssh server on your local machine to be able to ssh to localhost. And then just run scripts with "-H localhost". Works perfectly for me.

枫林﹌晚霞¤ 2024-12-01 17:56:22

Varun 的答案的修改版本,考虑到本地不捕获 stdout/stderr。如果不指定 capture=True 您将无法从本地获取结果。

from fabric.operations import local, run
from fabric.api import task
from fabric.state import env

def local_run(*args):
    return local(args[0], capture=True)

@task
def localhost():
    env.run = local_run
    env.hosts = ['localhost']

@task
def remote():
    env.run = run
    env.hosts = ['some.remote.host']

@task
def install():
    env.run('deploymentcmd')

A modified version of Varun's answer that takes into account local not capturing stdout/stderr. Without specifying capture=True you would not be able to get results from local.

from fabric.operations import local, run
from fabric.api import task
from fabric.state import env

def local_run(*args):
    return local(args[0], capture=True)

@task
def localhost():
    env.run = local_run
    env.hosts = ['localhost']

@task
def remote():
    env.run = run
    env.hosts = ['some.remote.host']

@task
def install():
    env.run('deploymentcmd')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文