2013-03-06 90 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