【问题标题】:Fabric - Project path environmentFabric - 项目路径环境
【发布时间】:2012-03-29 15:08:28
【问题描述】:

让我们考虑一下这个fabfile

def syncdb():
  print(green("Database Synchronization ..."))
  with cd('/var/www/project'):
    sudo('python manage.py syncdb', user='www-data')

def colstat():
  print(green("Collecting Static Files..."))
  with cd('/var/www/project'):
    sudo('python manage.py collectstatic --noinput', user='www-data')

def httpdrst():
  print(green("Restarting Apache..."))
  sudo('apachectl restart')

def srefresh():
  colstat()
  syncdb()
  httpdrst()

srefresh 指令调用所有其他指令,其中某些 with cd(...)

在变量中包含这个“cd 路径”的最佳方法是什么?

def colstat():
  with cd(env.remote['path']):

def srefresh():
  env.remote['path'] = '/var/www/project'
  colstat()
  syncdb()
  httpdrst()

类似的东西?

【问题讨论】:

    标签: python django deployment fabric


    【解决方案1】:

    我只是将变量作为参数传递给函数。

    def syncdb(path):
      print(green("Database Synchronization ..."))
      with cd(path):
        sudo('python manage.py syncdb', user='www-data')
    
    def colstat(path):
      print(green("Collecting Static Files..."))
      with cd(path):
        sudo('python manage.py collectstatic --noinput', user='www-data')
    
    def httpdrst():
      print(green("Restarting Apache..."))
      sudo('apachectl restart')
    
    def srefresh():
      path = '/var/www/project'
      colstat(path)
      syncdb(path)
      httpdrst()
    

    【讨论】:

    • 这在 Fabric 上应该可以正常工作。如果您仍然想用fab command 调用它们而不需要指定参数,只需给它们一个默认值。
    • 工作,但问题是我不能再单独打电话给syncdbcolstat
    【解决方案2】:

    不确定这是一个好习惯,但它似乎可以做到这一点

    env.remote_path = '/var/www/project'
    
    def colstat():
      with cd(env.remote_path):
    
    #...
    
    def srefresh():
      env.remote_path = '/var/www/other_project'
      pushpull()
      colstat()
    #...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-02
      • 2011-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      相关资源
      最近更新 更多