2012-03-29 54 views
0

讓我們考慮這個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() 

這樣的事情?

回答

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

作業可能無法適用於織物,但... – enderskill 2012-03-29 15:13:20

+0

這應該工作在織物上就好了。如果您仍然想用'fab command'調用它們而不需要指定參數,只需給它們一個默認值即可。 – Wilduck 2012-03-29 15:59:13

+0

工作,但問題是我不能獨立調用'syncdb','colstat' – 2012-03-30 11:26:01

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