示例#1
0
文件: views.py 项目: JerryDog/cdn
def updateDomain(req, domain_id):
    if req.method == "POST":
        s_id = req.POST.get('edit_session_id')
        if not req.session.has_key("edit_session_id"):
            req.session['current_js'] = JS_DICT["fail_update"] % 'Null edit_session_id'
            return HttpResponseRedirect('/domain_manage/')
        if req.session["edit_session_id"] != s_id:
            req.session['current_js'] = JS_DICT["fail_update"] % '请勿重复提交'
            return HttpResponseRedirect('/domain_manage/')
        else:
            del req.session["edit_session_id"]
        cache_rules = req.POST.get('cache_rules', None)
        acl = req.POST.get('acl', None)
        domain_status = 'InProgress'
        ip_str = req.POST.get('ip_list').strip()
        ignore_param_req = req.POST.get('ignore_param_req', False)

        d = Domain.objects.get(id=domain_id)
        domain_name = d.domain_name
        test_url = d.test_url
        disId = d.distribution_id
        etag = d.etag
        if ignore_param_req:
            noUse = 'True'
        else:
            noUse = 'False'

        json_str = utils.getJson4Xml(cache_rules, acl)
        json_str = json.dumps(json_str)
        random = uuid.uuid1()
        xml_name = '%s%s' % (domain_name, random)
        LOG.info('Update 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:
            req.session['current_js'] = JS_DICT["fail_update"] % e
            return HttpResponseRedirect('/domain_manage/')
        update_obj = DiLianManager(domain_name, ip_str, test_url, xml_name)
        status, reason, resp, ETag = update_obj.config(disId, etag)
        if status == 200:
            req.session['current_js'] = JS_DICT["succ_update"]
            update_time = datetime.datetime.now()
            domain = Domain.objects.filter(id=domain_id)
            domain.update(domain_status=domain_status,
                          ip_list=ip_str,
                          ignore_param_req=ignore_param_req,
                          etag=ETag,
                          update_time=update_time)
            #delete acl and cache rules of this domain
            CacheRules.objects.filter(domain_id=domain_id).delete()
            AccessControl.objects.filter(domain_id=domain_id).delete()
            utils.saveCacheRulesAndAcl(domain_id, cache_rules, acl)
            os.remove(settings.XML_PATH % xml_name)
        else:
            req.session['current_js'] = JS_DICT["fail_update"] % Etree.fromstring(resp).find("Message").text
        session_id = '%s' % uuid.uuid1()
        req.session['edit_session_id'] = session_id
        return HttpResponseRedirect('/domain_manage/')
示例#2
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/')