예제 #1
0
파일: views.py 프로젝트: JerryDog/cdn
def domainManage(req):
    if req.method == "POST":
        if not req.session.has_key("session_id"):
            req.session['current_js'] = JS_DICT["fail_create"] % 'Null session_id'
            return HttpResponseRedirect('/domain_manage/')
        s_id = req.POST.get('session_id')
        if req.session["session_id"] != s_id:
            req.session['current_js'] = JS_DICT["fail_create"] % '请勿重复提交'
            return HttpResponseRedirect('/domain_manage/')
        else:
            del req.session["session_id"]
        cache_rules = req.POST.get('cache_rules', None)
        acl = req.POST.get('acl', None)
        domain_name = req.POST.get('domain_name').strip()
        domain_type = 'dilian'
        domain_status = 'InProgress'
        ip_str = req.POST.get('ip_list').strip()
        test_url = req.POST.get('test_url').strip()
        ignore_param_req = req.POST.get('ignore_param_req', False)
        json_str = utils.getJson4Xml(cache_rules, acl)
        json_str = json.dumps(json_str)
        random = uuid.uuid1()
        xml_name = '%s%s' % (domain_name, random)

        project_id = req.session['project_id']
        username = req.COOKIES.get('username')
        if ignore_param_req:
            noUse = 'True'
        else:
            noUse = 'False'
        LOG.info('Create Domain: /usr/bin/python %s %s %s \'%s\'  %s %s -J \'%s\'' % (
            settings.CREATE_XML_PATH, xml_name, domain_name, ip_str, test_url, noUse, json_str))
        try:
            os.system('/usr/bin/python %s %s %s \'%s\' %s %s -J \'%s\'' % (
                settings.CREATE_XML_PATH, xml_name, domain_name, ip_str, test_url, noUse, json_str))
        except Exception,e:
            LOG.error('Create Domain Failed because of os.system')
            req.session['current_js'] = JS_DICT["fail_create"] % e
            return HttpResponseRedirect('/domain_manage/')
        create_obj = DiLianManager(domain_name, ip_str, test_url, xml_name)
        status, reason, resp = create_obj.create()
        cname_obj = CName()
        if status == 201:
            req.session['current_js'] = JS_DICT["succ_create"]
            disId = Etree.fromstring(resp).find("Id").text
            ETag = create_obj.md5_file(settings.XML_PATH % xml_name)
            domain_cname = domain_name + settings.DINON_CNAME
            domain_id = utils.saveDomainAndReturnId(domain_name, domain_cname, domain_type,
                                                    domain_status, disId, ETag, project_id, username,
                                                    ip_str,test_url,ignore_param_req)
            utils.saveCacheRulesAndAcl(domain_id, cache_rules, acl)
            cname_obj.insert_cname(domain_name, disId)
            os.remove(settings.XML_PATH % xml_name)
        else:
            req.session['current_js'] = JS_DICT["fail_create"] % Etree.fromstring(resp).find("Message").text
        return HttpResponseRedirect('/domain_manage/')
예제 #2
0
파일: apiUtil.py 프로젝트: JerryDog/ztcdn
def addResult(domain, project_id, username):
    # 逸云用户单独建站
    if username in YY_USER:
        cdn_clients = ['yy_api']
    else:
        cdn_clients = getCdnClients(domain=domain)
    all_cdn_len = len(cdn_clients)
    success_cdn = 0
    res = {}
    zt_domain_id = uuid.uuid1()
    for i in cdn_clients:
        aaa = ''
        exec "import ztcdn.api." + i + ".api.domainApi as aaa"
        DomainApi = getattr(aaa, 'DomainApi')
        api = DomainApi()
        has_success = 0
        result = api.add(domain)
        if result.getRet() == 0:
            success_cdn += 1
            saveAddDomainToDB(result.domain, i, zt_domain_id, project_id, username)
        if has_success == 1:
            res[i] = {
                "ret": 0,
                "msg": 'OK',
            }
        else:
            res[i] = {
                "ret": result.getRet(),
                "msg": result.getMsg(),
            }
        if result.domain:
            res[i]["domain"] = {
                "domain_name": result.domain.domainName,
                "domain_status": result.domain.status,
                "domain_cname": result.domain.cname,
                "domain_id": result.domain.domainId,
                "enabled": result.domain.enabled,
            }
            if hasattr(result.domain, 'testUrl'):
                res[i]["domain"]["testUrl"] = result.domain.testUrl
            if hasattr(result.domain, 'serviceType'):
                res[i]["domain"]["serviceType"] = result.domain.serviceType
            if hasattr(result.domain, 'comment'):
                res[i]["domain"]["comment"] = result.domain.comment
            if result.domain.originConfig != None:
                res[i]["domain"]["originIps"] = result.domain.originConfig.originIps
            if result.domain.etag != None:
                res[i]["domain"]["etag"] = result.domain.etag
            if result.domain.cacheBehaviors:
                cacheBehaviors = []
                for c in result.domain.cacheBehaviors:
                    cacheBehavior = {}
                    cacheBehavior["pathPattern"] = c.pathPattern
                    cacheBehavior["ignoreCacheControl"] = c.ignoreCacheControl
                    cacheBehavior["cacheTtl"] = c.cacheTtl
                    cacheBehaviors.append(cacheBehavior)
                res[i]["domain"]["cacheBehaviors"] = cacheBehaviors
    res["domain"] = {
        "domain_id": zt_domain_id,
    }
    res["successRate"] = success_cdn / all_cdn_len * 100
    if success_cdn == 0:
        res_error = {"error": result.getMsg()}
        return jsonify(res_error), result.getRet()
    else:
        # 插入 cname
        cname = CName()
        cname.insert_cname(zt_domain_id)
        return jsonify(res), 201