发布系统(2)

伪代码

views.py

def pubilsh(request):
    if request.method == 'GET':
        env = models.Use_Env.object.all()
        return render(request,'fabu.html', locals())
    else:
        env = request.POST.get('env')
        app = request.POST.get('app')
        obj_list = models.App.objects.filter(name=app,environment__name=env)  # 跨表查询

        # 拿到对应的主机组   代码 -> 地址
        # 循环 主机组  推送代码
        app_name = ORM 查表
        host_list = [{'id':'salt-id','path':'/data/app/www/abc'},]   # 这里是通过数据库取到的
        package = 'svn://xxxx'   # svn 地址

template

fabu.html

# name -> key ,框是 -> values

自动化管理平台 -> 必须是 和salt-master 安装在同一台机上 ,使用salt原生的API

第一步 在 自动化管理平台 里面下载代码 (可打包) 通过 subporcsess 执行命令
# from subprocess import Popen, PIPE
import os
path = os.getcwd() + r'/project_path/'
subprocsess -> 执行命令
# cd path
# mkdir app_name
# svn co $package
# tar 打包

create.sh

#!/bin/bash
cd path
mkdir $app_name && cd $app_name
svn co $package
tar 打包



subprocess.call(['cd',  '-l'])


from subprocess import Popen, PIPE 

p = subprocess.Popen('sh create.sh', stdout=PIPE, shell=True)   

第二步 推送 salt stack -> state.sls # 状态管理
写 state.sls 规则的yml文件
通过 Python 代码 salt-api 调用 state 触发推送

第三部 执行远程端代码 -> cmd.run cd 路径 python xxx

django celery 被封装成了 djcelery

就要学会如何使用

celery.py
form __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTIONG_MODULE', '项目名称.settiongs')
app = Celery('项目名称')

app.config_from_object('django.conf:settings')

app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

@app.task(bind=True)
def debug_task(self):
    print ('Request:  {0!x}'.format(self.request))

settings.py

# 文件最后添加

import djcelery
from celery.schedules improt crontab
from datetime import timedelta
djcelery.setup_loader()

CELERY_TIMEZONE = TIME_ZONE
BROKER_URL='redis://:'             # redis 地址 发送端口
CELERY_RESULT_BACKEND = 'redis://:'      # redis 接收端口
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Africa/Nairobi'
CELERY_IMPORTS = ['应用名目录下的.task',]   # 应用名目录下的.task ,主要看有没有task.py文件
CELERY_MAX_TASKS_PRR_CHILD = 3
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'

task.py

form __future__ import absolute_import, unicode_literals
import time
import requests
from celery import shared_task
from django.views.decorators.csrf import csrf_exempt, csrf_protect
from django.shortcuts import render, HttpResponse,redirect



@shared_task:
def add(x, y):
    return x+y   # 定义自己的推送代码


@shared_task:
def add(x, y):
    return x*y


@shared_task:
def xsun(numbers):
    print (sum(numbers))
    return sum(numbers)    

urls.py

urlpatterns = [
           url(r'^celery/', views.celery_status),   # 必须要写的路由      
          ]

views.py

def celery_status(request):
    import datetime
    import json
    if request.method == 'GET':
        if request.GET.get('x') and request.GET.get('y'):
            if request.GET.get('after'):
                ctime = datetime.datetime.now()
                utc_ctime = datetime.datetime.utcfromtimestamp(ctime.timestamp())
                s1 = datetime.timedelta(seconds=int(request.GET.get('after'))*60)
                ctime_x = utc_ctime + s1
            
            year = request.GET.get('year')
            mouth = request.GET.get('month')
            day = reuqest.GET.get('day')
            hour = request.GET.get('hour')
            minute = request.GET.get('minute')

            if year and mouth and day and hour and minute:
                ctime = datetime.datetime(year=int(year), month=int(mouth)),
                                           day=int(day), hour=int(hour), minute=int(minute))
                # 把当前的本地时间转换成 UTC 时间
                ctime_x = datetime.datetime.utcfromtimestamp(ctime.timestamp)
            
            if ctime_x:
                #  最核心的代码
                ret = add.apply_async(args=[int(request.GET.get('x')), int(request.GET.get('y'))], eta=ctime_x)
                num = ret.id
 
            if request.GET.get('cancel'):
                async = AsyncResult(id=request.GET.get('cancel'), app=app)
                async.revoke(terminate=True)
                cancel_tag = '取消成功'
 
            if request.GET.get('stop'):
                async = AsyncResult(id=request.GET.get('stop'), app=app)       
                async.revoke()
                stop_tag='中止成功'
            return render(request, 'celery.html', locals())
        else:
            ret = request.POST.get('id','')
            data = ""
            if ret:
                async = AsyncResult(id=ret,app=app)
                if async.successful():
                    data = "执行成功,数据是: " + str( async.get() ) 
                    async.forget()
                elif async.failed():
                    data='执行失败'
                elif async.status == 'PBNDING':
                    data = "等待被执行"
                elif async.status == 'RBTPY' :
                    data = '任务异常正常重试'
                elif async.status == 'STARTBD':
                    data = "任务正在执行"
                else:
                    data = "未知"
        retrun render(request, 'celery.html', locals())

celery 需要在命令行里单独启动 terminal

celery worker -A 发布 -l debug

templates
celery.html

{% csrf_token %} id: 结果:

# 空行
# 分割线
x: + y:
年: 月: 日: 时: 分:
几分钟后:
取消这个任务: 结果:
中止这个任务: 结果:

结果:
结果:

你可能感兴趣的:(发布系统(2))