Example #1
0
    def get_domain(self, I):

        self.d['ERROR'] = []

        sub, top = get_default_domain(self.db2, I.id)

        if top:
            sub = I.subdomain if I.subdomain else sub

            if not I.access_ip:
                self.d['ERROR'].append( self.trans(_('Can not get access_ip, please configure instance network or run instance')) )

        else:
            self.d['ERROR'].append( self.trans(_('can not get domain, domain may not have been configured in Administration Console.')) )

        self.d['subdomain'] = sub
        self.d['topdomain'] = top

        self.render('myun/instance/edit_domain.html', **self.d)
Example #2
0
    def post_domain(self, I):

        d = self.d
        d['ERROR'] = []

        sub, d['topdomain'] = get_default_domain(self.db2, I.id)

        if d['topdomain']:
            oldsub = I.subdomain if I.subdomain else sub

        subdomain = self.get_argument('subdomain', None)
        if not subdomain:
            d['ERROR'].append( self.trans(_('Domain is not configured!')) )

        if subdomain != oldsub:
            d['subdomain'] = subdomain
            if subdomain.isalpha():
                exist_domain = self.db2.query(Instance.id).filter_by(
                    subdomain = subdomain ).first()
                if exist_domain:
                    d['ERROR'].append( self.trans(_('Domain name is taken!')) )
                if len(subdomain) > 16:
                    d['ERROR'].append( self.trans(_("Domain name is too long.")) )
            else:
                d['ERROR'].append( self.trans(_('Please use alpha characters in domain name!')) )

        if d['ERROR']:
            return self.render('myun/instance/edit_domain.html', **d)

        # Updated instance subdomain value
        I.subdomain = subdomain
        self.db2.commit()

        fulldomain = '.'.join([subdomain, d['topdomain']])

        # Binding in nginx
        from tool.domain import binding_domain_in_nginx
        ret, reason = binding_domain_in_nginx(
            self.db2, I.id, domain = fulldomain )
        if not ret:
            d['ERROR'].append(_('binding domain error: %s') % reason )

        if not I.config:
            I.init_config()

        config = json.loads(I.config)
            
        if 'domain' in config.keys():
            domain = config['domain']
        else:
            domain = {}

        domain['name'] = fulldomain
        domain['ip'] = I.access_ip
        config['domain'] = domain

        I.config = json.dumps( config )

        I.updated = datetime.now()
        if I.is_running:
            I.ischanged = True
        self.db2.commit()

        if d['ERROR']:
            self.render('myun/instance/edit_domain.html', **d)
        else:
            url = self.reverse_url('myun:instance:view', I.id)
            url += '?tab=domain'
            self.redirect( url )