2013-03-06 118 views
0

現在我正在使用芹菜來處理我的部署腳本(結構)。所以我需要將每個項目部署日誌保存到數據庫,然後我可以檢查部署是否完美運行。但是我仍然沒有找到保存每個任務日誌的正確方法,並且對使用芹菜記錄器有點困惑。 我用芹菜2.5.1獲取芹菜每個任務日誌

views.py

def deploy(request, project_slug): 
    project = get_object_or_404(Project, slug=project_slug) 
    deploy_settings = simplejson.loads(project.result) #settings in json format 
    tasks.deploy.delay(deploy_settings) 

tasks.py

from celery.task import task 
from deployer import fabfile 

@task 
def deploy(deploy_settings): 
    logger = deploy.get_logger() 
    fabfile.deploy_settings = deploy_settings 
    fabfile.clone() #run the deployment script, I need to save all the output message 

    return True 

fabfile.py

env.host_string = '[email protected]' 
env.password = 'example' 
deploy_settings = {} 

def clone(): 
    with cd(deploy_settings['PATH_TO_APPS']): 
     run('mkdir %s' % deploy_settings['PROJECT_SLUG']) 
     run('git clone %s %s' % (deploy_settings['URL'], deploy_settings['PROJECT_SLUG'])) 
     with cd('%s/example/' % deploy_settings['PROJECT_SLUG']): 
      run('git checkout %s' % deploy_settings['BRANCH']) 

回答

0
class LogEntry(models.Model): 
    timestamp = models.DateTimeField(auto_now_add=True) 
    severity = models.CharField(blank=False, max_length=10) 
    message = models.TextField(blank=False) 

@task 
def deploy(deploy_settings): 
    log = LogEntry(severity="INFO", message="Deploying....") 
    log.save() 
    fabfile.deploy_settings = deploy_settings 
    fabfile.run_all() #run the deployment script, I need to save all the output message 

    return True 
+0

好感謝的代碼。但是如果我使用該代碼,我只能將消息字段保存爲「正在部署...」,不是嗎?我想保存結構任務中的所有芹菜輸出(例如,從git克隆,創建數據庫等) – Agus 2013-03-06 08:34:47

+0

我可以問你如何在視圖中實現deploy(deploy_settings)嗎?你可以發佈你的代碼嗎 – catherine 2013-03-06 08:40:22

+0

我已經更新了代碼 – Agus 2013-03-06 08:52:02