在Fabric中切换用户 [英] Switching user in Fabric

查看:230
本文介绍了在Fabric中切换用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Fabric模仿SSH工作流来部署Web应用程序时出现问题.

I have a problem when using Fabric to mimic my SSH workflow to deploy my web application.

当我通过SSH连接到服务器时,这是我通常的命令流:

Here's my usual flow of commands when I SSH to a server:

  1. 使用root用户的SSH. ssh root@1.2.3.4
  2. 切换到Web用户:su-web
  3. 更改目录:cd/srv/web/prod/abc_project
  4. 启动virtualenv:workon abc_env
  5. 执行git pull:git pull原始主机
  6. 运行脚本:build_stuff -m build
  7. 运行另一个脚本:./run

我试图将其编写为Fabric中的部署脚本,并且在输入su-web时得到了shell输出.我必须按Ctrl-D才能继续执行脚本.我也无法激活我的virtualenv....因为:su-web成功将用户切换到web,但是由于Ctrl-d(以便我可以继续使用Fabric脚本),它注销了该用户并回到根.

I tried to write this as a deploy script in Fabric and I get a shell output when su - web is entered. I have to hit Ctrl-D to continue the script. I am also unable to activate my virtualenv....because: su - web successfully switches the user to web but because of the Ctrl-d (so that I can continue the Fabric script), it logs out of that user and back to root.

这是我的剧本:

env.user = 'root'

@roles('web')
def deploy():
    dev_path = '/srv/web/prod'
    app_path = '/srv/web/prod/rhino'
    workon = 'workon rhino_env'
    with prefix('su - web'):
        puts('Switched to `web` user')
        with settings(warn_only=True):
            run('kill -9 `cat /srv/web/run/rhino/rhino.pid`')
            puts('Stopped rhino...')
        with cd(app_path):
            run('git reset --hard HEAD')
            puts('Discarded all untracked and modified files')
            run('git checkout master')
            run('git pull origin master')
            users = run('users')
            puts('Output from `users` command: %s' % users)
            run(workon)
            run('build_assets -m build')
        run('cd %(dev_path)s; chown -R web:ebalu rhino' % {'dev_path': dev_path})
        run('cd %(app_path)s; ./run' % {'app_path': app_path})
        pid = run('cat /srv/web/run/rhino/rhino.pid')
        puts('Rhino started again with pid: %s.' % pid)

...还有一件事:不,我最初不能以Web身份登录,我必须以root用户身份登录.具有virtualenv的Web用户而不是root用户.

...there's one more thing: No, I can't login as web initially, I have to login as root. It is the web user that has the virtualenv not the root user.

推荐答案

首先,在由另一个用户执行命令时,应使用sudo.其次,workon设置当前shell的环境变量.由于结构会为每个命令调用新的Shell,因此您应该在每个需要virtualenv(即作为前缀)的命令中运行workon rhino_env.进行此修改后,您的代码应如下所示:

First of all, you should use sudo when executing commands under another user. Second, workon sets environment variables for current shell. Since fabric invokes new shell for every command, you should run workon rhino_env in every command, where you need virtualenv (i.e. as prefix). With this edits yor code should look like this:

env.user = 'root'

@roles('web')
def deploy():
    dev_path = '/srv/web/prod'
    app_path = '/srv/web/prod/rhino'
    workon = 'workon rhino_env; '
    with settings(warn_only=True):
        run('kill -9 `cat /srv/web/run/rhino/rhino.pid`')
        puts('Stopped rhino...')
    with cd(app_path):
        sudo('git reset --hard HEAD', user='web')
        puts('Discarded all untracked and modified files')
        sudo('git checkout master', user='web')
        sudo('git pull origin master', user='web')
        users = run('users')
        puts('Output from `users` command: %s' % users)

        with prefix(workon):
            sudo('build_assets -m build', user='web')
    with cd(dev_path):
        run('chown -R web:ebalu rhino')

    with cd(app_path):
        sudo('./run', user='web')

    pid = run('cat /srv/web/run/rhino/rhino.pid')
    puts('Rhino started again with pid: %s.' % pid)

这篇关于在Fabric中切换用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆