Exemplo n.º 1
0
    def exec_update_sql_dict(self, query, args=None):

        self.get_cursor()
        try:

            self.cursor.execute(query=query, args=args)

            self.conn.commit()
            self.cursor.close()
            self.conn.close()
            log.debug('exec sql success, sql=%s' % (str(query)))
            return
        except Exception, e:
            try:
                self.cursor.execute(query=query, args=args)
                self.conn.commit()
                self.cursor.close()
                self.conn.close()
                log.debug('exec sql success, sql=%s' % (str(query)))
                return
            except Exception, e:
                log.error('exec sql error, sql=%s, reason=%s' %
                          (str(query), e))
                self.cursor.close()
                self.conn.close()
                raise
 def templet_delete(self, context, templet_uuid):
     log.debug('delete the templet, the context is: %s' % context)
     try:
         parameter_check(templet_uuid, exist='yes')
     except Exception, e:
         log.error('parameters error, reason is: %s' % e)
         return request_result(101)
Exemplo n.º 3
0
    def exec_update_sql(self, *sql):

        self.get_cursor()
        try:
            for v_sql in sql:
                self.cursor.execute(v_sql)

            self.conn.commit()
            self.cursor.close()
            self.conn.close()
            log.debug('exec sql success, sql=%s' % (str(sql)))
            return
        except Exception, e:
            try:
                for v_sql in sql:
                    self.cursor.execute(v_sql)

                self.conn.commit()
                self.cursor.close()
                self.conn.close()
                log.debug('exec sql success, sql=%s' % (str(sql)))
                return
            except Exception, e:
                log.error('exec sql error, sql=%s, reason=%s' % (str(sql), e))
                self.cursor.close()
                self.conn.close()
                raise
    def snap_create(self, context, parameters):
        try:
            token = context['token']
            source_ip = context.get('source_ip')
            user_info = token_auth(context['token'])['result']
            user_uuid = user_info.get('user_uuid')
            team_uuid = user_info.get('team_uuid')
            project_uuid = user_info.get('project_uuid')

            log.debug('the token is: %s, source_ip is: %s, user_uuid is: %s,'
                      'team_uuid is: %s, project_uuid is: %s' % (token,
                                                                 source_ip,
                                                                 user_uuid,
                                                                 team_uuid,
                                                                 project_uuid))

            name = parameters.get('name')
            description = parameters.get('description')
            metadata = parameters.get('metadata')
            volume_uuid = parameters.get('volume_uuid')
            vm_uuid = parameters.get('vm_uuid')
            parameter_check(name, ptype='pnam')
        except Exception, e:
            log.error('parameters error, reason is: %s' % e)
            return request_result(101)
Exemplo n.º 5
0
    def mq_connect(self, mq_server01=conf.mq_server01,
                   mq_server02=conf.mq_server02,
                   mq_port=conf.mq_port,
                   heartbeat_time=30):

        log.debug('Connecting to rabbitmq server, server01=%s, server02=%s'
                  % (mq_server01, mq_server02))
        try:
            self.connection = pika.BlockingConnection(
                              pika.ConnectionParameters(
                                   host=mq_server01, port=mq_port,
                                   heartbeat_interval=heartbeat_time))
            self.channel = self.connection.channel()
        except Exception, e:
            log.error('RabbitMQ server %s connection error: reason=%s'
                      % (mq_server01, e))
            try:
                self.connection = pika.BlockingConnection(
                                  pika.ConnectionParameters(
                                       host=mq_server02, port=mq_port,
                                       heartbeat_interval=heartbeat_time))
                self.channel = self.connection.channel()
            except Exception, e:
                log.error('RabbitMQ server %s connection error: reason=%s'
                          % (mq_server02, e))
                raise
    def snap_delete(self, context, snapshot_uuid, logic=0):
        log.debug('delete the snapshot, context is: %s' % context)
        if logic == 0:
            return self.snap_manager.delete(snapshot_uuid)
        if logic == 1:
            return self.snap_manager.logic_delete(snapshot_uuid)

        return request_result(1003)
 def volume_delete(self, context, parameters):
     log.debug('delete context is: %s' % context)
     try:
         volume_uuid = parameters.get('volume_uuid')
         logic = parameters.get('logic')
         if logic is None:
             logic = 1
     except Exception, e:
         log.error('parameters check error, reason is: %s' % e)
         return request_result(101)
    def rc_status_info(self, token=None):

        try:
            r = requests.get(self.get_pod_status_url,
                             headers=self.headers,
                             verify=False, timeout=5)
            log.debug('rc_status=%s' % r.text)
            return request_result(0, r.text)
        except Exception, e:
            log.error('requests k8s api error, reason=%s' % e)
            return request_result(103)
Exemplo n.º 9
0
        def __aclauth(*args, **kwargs):

            func_args = inspect.getcallargs(func, *args, **kwargs)
            context = func_args.get('context')

            token = context['token']
            resources_uuid = context['resource_uuid']
            action = context['action']

            user_info = token_auth(token)['result']
            user_uuid = user_info['user_uuid']
            team_uuid = user_info['team_uuid']
            team_priv = user_info['team_priv']
            project_uuid = user_info['project_uuid']
            project_priv = user_info['project_priv']

            context = "%s%s%s%s%s%s%s" % (user_uuid, team_uuid, team_priv,
                                          project_uuid, project_priv,
                                          resources_uuid, action)

            log.debug('start ack check, context=%s' % (context))
            acl_info = caches.get(context)
            for resource_uuid in resources_uuid:
                if (acl_info is LocalCache.notFound):
                    log.debug('Cache acl not hit, context=%s' % (context))
                    auth_manager = AuthManager(service_name)
                    ret = auth_manager.resource_acl_check(
                        user_uuid, team_uuid, team_priv, project_uuid,
                        project_priv, resource_uuid, action)
                    expire = int(time.time()) + 300
                    caches.set(context, {"acl_check": ret, "expire": expire})
                    log.debug('Cached acl check, context=%s' % (context))
                else:
                    log.debug('Cache acl hit, context=%s' % (context))
                    ret = acl_info['acl_check']

                log.debug('ack check result=%s' % (ret))

                if ret == 0:
                    try:
                        return func(*args, **kwargs)
                    except Exception, e:
                        log.error('function(%s) exec error, reason = %s' %
                                  (func.__name__, e))
                        return request_result(999)
                else:
                    log.warning('Resource acl auth denied: user_uuid = %s, \
                                 team_uuid=%s, team_priv=%s, project_uuid=%s, \
                                 project_priv=%s, resource_uuid=%s, action=%s'
                                %
                                (user_uuid, team_uuid, team_priv, project_uuid,
                                 project_priv, resource_uuid, action))

                    return request_result(202)
Exemplo n.º 10
0
    def app_events_es(self, project_uuid, rc_name):
        event = []
        try:

            ns_events_info = self.k8s_driver.app_events_info(project_uuid)
            if ns_events_info.get('status') != 0:
                log.debug('Get events from k8s error')
            events_info = json.loads(ns_events_info['result'])
            events_list = events_info['items']
        except Exception, e:
            log.error('get the events error, reason is: %s' % e)
            raise Exception('get the events error')
Exemplo n.º 11
0
    def exec_select_sql(self, sql):

        self.get_cursor()
        try:
            self.cursor.execute(sql)
            result = self.cursor.fetchall()
            self.cursor.close()
            self.conn.close()
            log.debug('exec sql success, sql=%s' % (sql))
            return result
        except Exception, e:
            log.error('exec sql error, sql=%s, reason=%s' % (sql, e))
            raise
def appstatus_service():

    appstatus = AppStatusManager()

    rc_status_cache = {}

    while True:
        try:
            log.debug('rc_status_cache=%s' % (rc_status_cache))
            rc_status_cache = appstatus.rc_status_update(rc_status_cache)
        except Exception, e:
            rc_status_cache = {}
            log.warning('Appstatus Service running error, reason=%s' % e)
        sleep(15)
    def rc_status_update(self, rc_status_cache):

        rc_status_info = self.k8s_driver.rc_status_info()
        status = rc_status_info['status']
        if status != 0:
            log.debug('Get pods status from k8s error')
            return rc_status_cache

        try:
            rc_status_info = json.loads(rc_status_info['result'])
            log.debug('rc_status_list_type=%s' % (type(rc_status_info)))
            rc_status_list = rc_status_info['items']
        except Exception, e:
            log.error('Get rc status list error, reason=%s' % (e))
            return rc_status_cache
class OpenstackDriver(object):

    def __init__(self):
        self.conn = connection()

    @staticmethod
    def get_token(user_name, password):
        header = {"Content-Type": "application/json",
                  "Accept": "application/json"}
        user_msg = {"auth": {"tenantName": "cloud",
                             "passwordCredentials": {"username": user_name,
                                                     "password": password}}}
        try:
            ret = requests.post(url=conf.token_url, json=user_msg,
                                headers=header,
                                timeout=5)
        except Exception, e:
            log.error('get the token error, reason is: %s' % e)
            return request_result(501)
        if ret.status_code != 200:
            return request_result(501)
        log.debug('get the projectID and token(op) result is: %s' % ret.text)
        try:
            token = json.loads(ret.text).get('access').get('token').get('id')
            user_uuid = json.loads(ret.text).get('access').get('user').get(
                'id')
        except Exception, e:
            log.error('get the token from openstack error, reason is: %s' % e)
            return request_result(203)
Exemplo n.º 15
0
def token_auth(token):

    log.debug('start token check, token=%s' % (token))
    token_info = caches.get(token)
    if (token_info is LocalCache.notFound):
        log.debug('Cache token auth not hit, token=%s' % (token))
        try:
            headers = {'token': token}
            ret = requests.get(url, headers=headers, timeout=5).json()
            status = ret['status']
            if status != 0:
                raise
        except Exception, e:
            log.error('Token ucenter auth error: reason=%s' % (e))
            raise

        expire = int(time.time()) + 300
        caches.set(token, {"token_info": ret, "expire": expire})
Exemplo n.º 16
0
    def token_check(self, context, parameters=None):

        token = context['token']

        log.debug('Ucenter start token check, token=%s' % (token))
        token_info = caches.get(token)
        if (token_info is LocalCache.notFound):
            log.debug('Cache token auth not hit, token=%s' % (token))
            try:
                rpc_body = rpc_data("uct_tkn_tkn_chk", context, parameters)
                ret = self.rbtmq.rpc_call_client(self.queue, self.timeout,
                                                 rpc_body)
            except Exception, e:
                log.error('Rpc client exec error, reason=%s' % (e))
                return request_result(598)

            expire = int(time.time()) + 300
            caches.set(token, {"token_info": ret, "expire": expire})
def token_auth(token):

    log.debug('start token check, token=%s' % (token))
    token_info = caches.get(token)
    if (token_info is LocalCache.notFound):
        log.debug('Cache token auth not hit, token=%s' % (token))
        try:
            context = {"token": token}
            ret = rpc_client.UcenterRpcClient().token_check(context)
            status = ret['status']
            if status != 0:
                raise(Exception('Token auth denied'))
        except Exception, e:
            log.error('Token local auth error: reason=%s' % (e))
            raise(Exception('Token auth error'))

        expire = int(time.time()) + 300
        caches.set(token, {"token_info": ret, "expire": expire})
    def __init__(self,
                 mq_server01=conf.mq_server01,
                 mq_server02=conf.mq_server02,
                 mq_port=conf.mq_port):

        log.debug('Connecting to rabbitmq server, server01=%s, server02=%s' %
                  (mq_server01, mq_server02))

        # self.params = pika.URLParameters(
        #    'amqp://*****:*****@127.0.0.1:5672/?'
        #    'socket_timeout=10&'
        #    'connection_attempts=2')

        self.params01 = pika.ConnectionParameters(host=mq_server01,
                                                  port=mq_port)
        self.params02 = pika.ConnectionParameters(host=mq_server02,
                                                  port=mq_port)

        try:
            self.pool = pika_pool.QueuedPool(
                create=lambda: pika.BlockingConnection(parameters=self.params01
                                                       ),
                max_size=10,
                max_overflow=10,
                timeout=10,
                recycle=3600,
                stale=45)
        except Exception, e:
            log.error('rabbitmq server %s connection error: reason=%s' %
                      (mq_server01, e))
            try:
                self.pool = pika_pool.QueuedPool(
                    create=lambda: pika.BlockingConnection(parameters=self.
                                                           params02),
                    max_size=10,
                    max_overflow=10,
                    timeout=10,
                    recycle=3600,
                    stale=45)
            except Exception, e:
                log.error('rabbitmq server %s connection error: reason=%s' %
                          (mq_server02, e))
                raise
Exemplo n.º 19
0
        def __reslmt(*args, **kwargs):

            try:
                func_args = inspect.getcallargs(func, *args, **kwargs)
                token = func_args.get('token')
                cost = func_args.get('cost')

                user_info = token_auth(token)['result']
                team_uuid = user_info.get('team_uuid')
                project_uuid = user_info.get('project_uuid')
                user_uuid = user_info.get('user_uuid')

                if user_uuid != 'sysadmin':
                    limit_info = billing_limit_check(
                                 token, resource_type, cost)
                    balance_check = limit_info['result']['balance_check']
                    if int(balance_check) != 0:
                        log.warning('Limit check denied, not enough balance')
                        return request_result(302)

                    limit_check = limit_info['result']['limit_check']
                    res_db = resources_db.ResourcesDB()
                    resource_count = res_db.resource_count(
                                            resource_type, team_uuid,
                                            project_uuid, user_uuid)
                    log.debug('billing_limit_check=%s, resource_count=%s'
                              % (limit_check, resource_count))
                    if int(resource_count) >= int(limit_check):
                        log.warning('Limit check denied, Team(%s) resource(%s) '
                                    'reach upper limit'
                                    % (team_uuid, resource_type))
                        return request_result(303)

                try:
                    return func(*args, **kwargs)
                except Exception, e:
                    log.error('function(%s) exec error, reason = %s'
                              % (func.__name__, e))
                    return request_result(601)
            except Exception, e:
                log.error('Limit check error, reason=%s' % (e))
                return request_result(303)
Exemplo n.º 20
0
def billing_limit_check(token, resource_type, cost):

    try:
        log.debug('Start billing limit check, '
                  'token=%s, resource_type=%s, cost=%s'
                  % (token, resource_type, cost))

        context = context_data(token, "bil_lmt_lmt_chk", "create")
        parameters = {"resource_type": resource_type, "cost": cost}
        limit = billing_rpcapi.BillingRpcApi().limit_check(context, parameters)

        log.debug('Billing limit result=%s' % (limit))

        status = limit['status']
        if status != 0:
            raise(Exception('Billing limit check error, '
                            'request code not equal 0'))
    except Exception, e:
        log.error('Billing limit local check error: reason=%s' % (e))
        raise(Exception('Billing limit check error'))
    def attachment_create(self, context, server_uuid, volume_uuid):
        try:
            token = context['token']
            source_ip = context.get('source_ip')
            user_info = token_auth(context['token'])['result']
            user_uuid = user_info.get('user_uuid')
            team_uuid = user_info.get('team_uuid')
            project_uuid = user_info.get('project_uuid')

            log.debug('the token is: %s, source_ip is: %s, user_uuid is: %s,'
                      'team_uuid is: %s, project_uuid is: %s' % (token,
                                                                 source_ip,
                                                                 user_uuid,
                                                                 team_uuid,
                                                                 project_uuid))
            parameter_check(server_uuid, exist='yes')
            parameter_check(volume_uuid, exist='yes')
        except Exception, e:
            log.error('parameters error, reason is: %s' % e)
            return request_result(101)
Exemplo n.º 22
0
def token_auth(token):

    log.debug('start token check, token=%s' % token)
    token_info = caches.get(token)
    if token_info is LocalCache.notFound:
        log.debug('Cache token auth not hit, token=%s' % token)
        try:
            headers = {'token': token}
            ret = requests.get(url, headers=headers, timeout=5).text
            log.info("token confirm result is: %s(%s)" % (ret, type(ret)))
            ret = json.loads(ret)
            status = ret['status']
            if status != 0:
                raise
        except Exception, e:
            log.error('Token ucenter auth error: reason=%s' % e)
            raise

        expire = int(time.time()) + 300
        caches.set(token, {"token_info": ret, "expire": expire})
Exemplo n.º 23
0
 def osdisk_create(self,
                   name,
                   volume_uuid,
                   v_type,
                   image_uuid,
                   size,
                   conn_to,
                   user_uuid,
                   project_uuid,
                   team_uuid,
                   description='os_volume',
                   source_volume_uuid=None,
                   snapshot_uuid=None,
                   is_use_domain=None,
                   is_start=1,
                   is_secret=0):
     log.debug(size)
     try:
         size = self.op_driver.image_size(image_uuid)
     except Exception, e:
         log.error('get the image size error, reason is: %s' % e)
         return request_result(1204)
    def _tokenauth(*args, **kwargs):

        try:
            func_args = inspect.getcallargs(func, *args, **kwargs)
            token = func_args.get('token')
            if token is None:
                context = func_args.get('context')
                if context is None:
                    for context in func_args.get('args'):
                        if isinstance(context, dict):
                            break

            token = context.get('token')
            log.debug('token=%s' % (token))

            userinfo_auth = token_auth(token)['status']
            if userinfo_auth == 0:
                try:
                    result = func(*args, **kwargs)
                except Exception, e:
                    log.error('function(%s) exec error, reason = %s'
                              % (func.__name__, e))
                    return request_result(601)
            else:
Exemplo n.º 25
0
def billing_limit_check(token, resource_type, cost):

    try:
        log.debug('Start billing limit check, '
                  'token=%s, resource_type=%s, cost=%s' %
                  (token, resource_type, cost))

        headers = {'token': token}
        body = {"resource_type": resource_type, "cost": cost}

        limit = requests.post(limit_url,
                              headers=headers,
                              data=json.dumps(body),
                              timeout=5).json()

        log.debug('Billing limit result=%s' % (limit))

        status = limit['status']
        if status != 0:
            raise (Exception('Billing limit check error, '
                             'request code not equal 0'))
    except Exception, e:
        log.error('Billing limit local check error: reason=%s' % (e))
        raise (Exception('Billing limit check error'))
Exemplo n.º 26
0
 def db_check_floatingip_bind(self, floatingip_uuid):
     sql = "select count(*) from floating_ip where uuid='%s' " \
           "and vm_uuid is not NULL and vm_uuid != 'None'" % floatingip_uuid
     log.debug('check the floatingip bind sql is: %s' % sql)
     return super(NetworkDB, self).exec_select_sql(sql)
    def callback_create(self):

        log.debug('Create callback queue')
        self.callback_queue = self.channel.queue_declare(
            exclusive=True).method.queue
        self.channel.basic_consume(self.on_response, queue=self.callback_queue)
    def mq_disconnect(self):

        log.debug('Disconnect from rabbitmq server')
        self.channel.close()
        self.connection.close()
Exemplo n.º 29
0
        except Exception, e:
            log.error('add the floating ip to server(op) error, reason is: '
                      '%s' % e)
            return request_result(631)
        return request_result(0, op_result)

    def floatip_unbind(self, token, vm_uuid, floatip):
        try:
            op_result = self.conn.compute.\
                remove_floating_ip_from_server(server=vm_uuid,
                                               address=floatip)
        except Exception, e:
            log.error('remove the floating ip from server(op) error, '
                      'reason is: %s' % e)
            return request_result(632)
        log.debug('unbind floatip result(op) is: %s' % op_result)
        return request_result(0, 'removed')

    def vm_pwd_reset(self, token, server_uuid, new_password):
        try:
            op_result = self.conn.compute.\
                change_server_password(server_uuid, new_password)
        except Exception, e:
            log.error('reset the password of the vm error, reason is: %s' % e)
            return request_result(641)

        log.info('pwd reset the result(op) is %s' % op_result)  # None
        return request_result(0, {'server_uuid': server_uuid})


# test-code
    if (token_info is LocalCache.notFound):
        log.debug('Cache token auth not hit, token=%s' % (token))
        try:
            context = {"token": token}
            ret = rpc_client.UcenterRpcClient().token_check(context)
            status = ret['status']
            if status != 0:
                raise(Exception('Token auth denied'))
        except Exception, e:
            log.error('Token local auth error: reason=%s' % (e))
            raise(Exception('Token auth error'))

        expire = int(time.time()) + 300
        caches.set(token, {"token_info": ret, "expire": expire})
    else:
        log.debug('Cache token auth hit, token=%s' % (token))
        ret = token_info['token_info']

    log.debug('token_info = %s' % (ret))

    return ret


def token_check(func):

    def _tokenauth(*args, **kwargs):

        try:
            func_args = inspect.getcallargs(func, *args, **kwargs)
            token = func_args.get('token')
            if token is None: