2011-03-17 10 views
30

Ben böyle bir fabfile var şu:Bir Python Fabric görevi diğer görevleri çağırıp ana bilgisayar listelerine saygı gösterebilir mi?

@hosts('host1') 
def host1_deploy(): 
    """Some logic that is specific to deploying to host1""" 

@hosts('host2') 
def host2_deploy(): 
    """Some logic that is specific to deploying to host2""" 

def deploy(): 
    """"Deploy to both hosts, each using its own logic""" 
    host1_deploy() 
    host2_deploy() 

ben her koşmak,

fab deploy 

yapmak ister ve onu başka deyişle

fab host1_deploy host2_deploy 

eşdeğer olurdu alt görevler ve her biri için belirttiği ana bilgisayarların listesini kullanın. Ancak, bu işe yaramıyor. Bunun yerine, deploy() görevi, kendi alt görevlerinin tümüne yayılacak kendi ana bilgisayar listesini ister.

Deploy() görevini burada güncellemenin bir yolu var mı, böylece tek tek çalıştırılabilmeleri için alt görevleri tek başına bırakırken istediğimi yapacak mı?

cevap

1

muhtemelen işlemek için daha iyi bir yolu var, ama host1_deploy sonra) (dağıtmak için her iki ana geçmesi ve olabilir() ve host2_deploy() env.host atın:

def host1_deploy(): 
    if env.host in ['host1']: 
     run(whatever1) 

def host2_deploy(): 
    if env.host in ['host2']: 
     run(whatever2) 

@hosts('host1','host2') 
def deploy(): 
    host1_deploy() 
    host2_deploy() 
1

Bunu deneyin. Açıkçası yerel run veya sudo ile değiştirmek istiyorsunuz. Anahtar Bu topal deploy

from fabric.api import local 
from fabric.decorators import hosts 

@hosts('host1') 
def host1_deploy(): 
    """Some logic that is specific to deploying to host1""" 
    local('echo foo') 

@hosts('host2') 
def host2_deploy(): 
    """Some logic that is specific to deploying to host2""" 
    local('echo bar') 

@hosts('') 
def deploy(): 
    """"Deploy to both hosts, each using its own logic""" 
    host1_deploy() 
    host2_deploy() 
+0

Çalışmıyor ... –

3

için boş @hosts dekoratör ama 1.1.2

def host1_deploy(): 
    """Some logic that is specific to deploying to host1""" 
    if env.host in ["host1"]: 
     pass #this is only on host2 

def host2_deploy(): 
    """Some logic that is specific to deploying to host2""" 
    if env.host in ["host2"]: 
     pass #this is only on host2 

def deploy(): 
    """"Deploy to both hosts, each using its own logic""" 
    host1_deploy() 
    host2_deploy() 

burada benim test kodu Kumaş gibi çalışır: Fabric 1.3 yana

@task 
@roles(["prod_web","prod_workers"]) 
def test_multi(): 
    test_multi_a() 
    test_multi_b() 

def test_multi_a(): 
    if env.host in env.roledefs["prod_web"]: 
     run('uname -a') 

def test_multi_b(): 
    if env.host in env.roledefs["prod_workers"]: 
     run('uname -a') 
30

execute yardımcısı şimdi sadece bunu yapmak için kullanılabilir. Belgelere buradan ulaşabilirsiniz: Intelligently executing tasks with execute. Sonra

from fabric.api import run, roles 

env.roledefs = { 
    'db': ['db1', 'db2'], 
    'web': ['web1', 'web2', 'web3'], 
} 

@roles('db') 
def migrate(): 
    # Database stuff here. 
    pass 

@roles('web') 
def update(): 
    # Code updates here. 
    pass 

Ve başka bir görev deploy hem migrate ve web çalıştırmak için: Burada

kullandıkları örnektir

def deploy(): 
    execute(migrate) 
    execute(update) 

Ve bu rolleri ve ana saygı göstereceğiz bu görevler olduğunu listeler var.

+0

Rol, bir sınıf yöntemine eklemek mümkün mü? Kumaş görevleri için sınıfları kullanıyorum –

+1

Dokümantasyon bağlantısı kesildi. En son: http://docs.fabfile.org/en/1.8/usage/execution.html#intelligently-executing-tasks-with-execute – krd

İlgili konular