Fabric - 项目路径环境

时间:2012-03-29 15:08:29

标签: python django deployment fabric

让我们考虑一下 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 path'的最佳方法是什么?

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

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

那样的东西?

2 个答案:

答案 0 :(得分:2)

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

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()

答案 1 :(得分:0)

不确定这是一个好习惯,但它似乎可以完成这项工作

env.remote_path = '/var/www/project'

def colstat():
  with cd(env.remote_path):

#...

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