Esempio n. 1
0
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/')
Esempio n. 2
0
def deleteDomain(req):
    if req.method == 'POST':
        ids = req.POST.get('domain_ids')
        id_list = ids.split(',')
        username = req.COOKIES.get('username')
        for i in id_list:
            if i:
                id_obj = Domain.objects.filter(id=i)
                domain_name = id_obj[0].domain_name
                delete_obj = DiLianManager()
                cname_obj = CName()
                status, reason, resp = delete_obj.delete(id_obj[0].distribution_id, id_obj[0].etag)
                if status == 200:
                    update_time = datetime.datetime.now()
                    id_obj.update(domain_status='Deleted', update_time=update_time)
                    cname_obj.del_cname(domain_name)
                    LOG.info('User %s delete domain %s' % (username, domain_name))
                    result = 1
                else:
                    result = Etree.fromstring(resp).find("Message").text
    return HttpResponse(result)
Esempio n. 3
0
def commonResult(cdnAct, domain=None, domain_id=None):
    # domain 和 domain_id 肯定会有一个传进来,根据这个去 DB 获取 cdn_clients
    cdn_clients = ''
    if domain:
        cdn_clients = getCdnClientsFromDB(domain=domain)
    if domain_id:
        cdn_clients = getCdnClientsFromDB(domain_id=domain_id)
    if not cdn_clients:
        return jsonify({"error": "sp can not found"}), 404
    all_cdn_len = len(cdn_clients)
    success_cdn = 0
    res = {}
    for i in cdn_clients:
        aaa = ''
        exec "import ztcdn.api." + i + ".api.domainApi as aaa"
        DomainApi = getattr(aaa, 'DomainApi')
        api = DomainApi()
        result = ''
        has_success = 0
        if cdnAct == "find":
            # 传入了 domain_id, 这个 id 是 zt_domain_id
            _id = getSpDomainId(domain_id, i)
            result = api.find(_id)
            if result.getRet() == 0:
                success_cdn += 1
                has_success = 1
                saveFindDomainToDB(result.domain, i, domain_id)

        if cdnAct == "modify":
            # 传入了 domain 对象
            domain_id = domain.domainId
            _id = getSpDomainId(domain_id, i)
            c = copy.deepcopy(domain)
            c.domainId = _id
            result = api.modify(c)
            if result.getRet() == 0:
                success_cdn += 1
                # 来一遍 find 以更新库
                find_result = api.find(_id)
                if find_result.getRet() == 0:
                    saveFindDomainToDB(find_result.domain, i, domain_id, is_updated=True)

        if cdnAct == "delete":
            _id = getSpDomainId(domain_id, i)
            result = api.delete(_id)
            if result.getRet() == 0:
                success_cdn += 1
                saveDeleteSpDomainToDB(_id)

        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": domain_id,
    }
    res["successRate"] = success_cdn / all_cdn_len * 100
    if cdnAct == "delete" and success_cdn == all_cdn_len:
        # 各厂商CDN都删除成功,设置主表状态为Delete
        # 并且删除 CName
        cname = CName()
        cname.del_cname(domain_id)
        saveDeleteDomainToDB(domain_id)
    if success_cdn != all_cdn_len:
        res_error = {"error": result.getMsg()}
        return jsonify(res_error), result.getRet()
    else:
        return jsonify(res)
Esempio n. 4
0
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