def attachment_delete_wait(self, attachment_uuid, server_uuid):
     result = self.attachment_delete(attachment_uuid, server_uuid)
     if result.get('status') != 0:
         return result
     timeout = 0
     while True:
         timeout = timeout + 1
         if timeout >= 16:
             # 超时3秒
             return request_result(1012)
         log.info('volume detail start execute')
         op_status = self.volume_detail(attachment_uuid)
         log.info('volume detail end execute')
         if op_status.get('status') != 0:
             sleep(0.2)
             continue
         else:
             status = op_status.get('result')
             if status == 'available':
                 return result
             if status == 'detaching':
                 sleep(0.2)
                 continue
             else:
                 return request_result(1012)
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.º 3
0
    def floating_ip_unbind(self, floatingip):
        try:
            # get the float ip uuid
            floatingip_uuid = self.db.db_get_floatingip_uuid(floatingip)
            if len(floatingip_uuid[0]) == 0:
                return request_result(1058)
            else:
                floatingip_uuid = floatingip_uuid[0][0]

            # check the float ip if need unbind
            db_check = self.db.db_check_floatingip_bind(floatingip_uuid)
            if db_check[0][0] == 0:
                log.info('need not unbind!!!')
                return request_result(0)

            # get the vm_uuid the floatip binded
            db_message = self.db.db_get_floatingip_addr(floatingip_uuid)
            floatingip_addr = db_message[0][0]
            vm_uuid = db_message[0][1]
            fixed_ip_address = db_message[0][2]
            if fixed_ip_address == 'None':
                fixed_ip_address = None
        except Exception, e:
            log.error('check the floatingip if unbined(db) error, '
                      'reason is: %s' % e)
            return request_result(403)
    def network_create(self,
                       name,
                       description,
                       is_admin_state_up=1,
                       is_shared=0,
                       user_uuid=None,
                       project_uuid=None,
                       team_uuid=None):
        if is_admin_state_up == 1:
            is_admin_state_up_1 = True
        else:
            is_admin_state_up = 0
            is_admin_state_up_1 = False

        if is_shared == 1:
            is_shared_1 = True
        else:
            is_shared = 0
            is_shared_1 = False
        try:
            same_name = self.db.network_name_check(name, user_uuid,
                                                   project_uuid, team_uuid)
            if same_name[0][0] != 0:
                return request_result(302)
        except Exception, e:
            log.error('check the name is used(db) error, reason is: %s' % e)
            return request_result(999)
Exemplo n.º 5
0
    def router_create(self, name, description, is_admin_state_up,
                      out_network_uuid):
        if self.conn is False:
            return request_result(701)
        if out_network_uuid is None:
            external_gateway_info = {}
        else:
            external_gateway_info = {"network_id": out_network_uuid}

        try:
            if description is None:
                op_result = self.conn.network.create_router(
                    name=name,
                    # external_gateway_info=''
                    # availability_zones=['nova'],
                    is_admin_state_up=is_admin_state_up,
                    external_gateway_info=external_gateway_info)
            else:
                op_result = self.conn.network.create_router(
                    name=name,
                    description=description,
                    # external_gateway_info=''
                    # availability_zones=['nova'],
                    is_admin_state_up=is_admin_state_up,
                    external_gateway_info=external_gateway_info)
        except Exception, e:
            log.error('router create(op) error, reason is: %s' % e)
            return request_result(1041)
class CinderDriver(object):
    def __init__(self):
        pass

    def update_volume(self, token, up_dict):
        cinder_url = conf.cinder_url + \
                     'volumes/' + \
                     up_dict['volume_uuid']
        parameters_dict = {"volume": {}}

        if 'name' in up_dict.keys():
            parameters_dict['volume']['name'] = up_dict['name']
        elif 'description' in up_dict.keys():
            parameters_dict['volume']['description'] = up_dict['description']
        else:
            return request_result(0, 'should not update')
        headers = {"X-Auth-Token": token}
        try:
            result = requests.put(url=cinder_url,
                                  json=parameters_dict,
                                  headers=headers,
                                  timeout=10)
            # result = requests.get(cinder_url+'/metadata', headers=headers)

        except Exception, e:
            log.error('update the volume(op) error, reason is: %s' % e)
            return request_result(602)

        if result.status_code != 200:
            log.error('update the volume result(op) is: %s' % result.text)
            return request_result(602)

        return request_result(0, {'resource_uuid': up_dict['volume_uuid']})
Exemplo n.º 7
0
    def network_create(self,
                       name,
                       description,
                       is_admin_state_up=True,
                       is_shared=False):
        """

        :param name: 名称
        :param description: 描述
        :param is_admin_state_up: 是否管理员状态1/0
        :param is_shared: 是否外部共享
        :return:
        """
        if self.conn is False:
            return request_result(701)

        try:
            if description is None:
                op_result = self.conn.network.\
                    create_network(name=name,
                                   is_admin_state_up=is_admin_state_up,
                                   shared=is_shared
                                   )
            else:
                op_result = self.conn.network.\
                    create_network(name=name,
                                   description=description,
                                   is_admin_state_up=is_admin_state_up,
                                   shared=is_shared
                                   )
        except Exception, e:
            log.error('create the network(op) error, reason is: %s' % e)
            return request_result(1020)
 def volume_detail(self, volume_uuid):
     if self.conn is False:
         return request_result(701)
     try:
         op_result = self.conn.block_storage.get_volume(volume_uuid)
     except Exception, e:
         log.error('get the volume detail(op) error, reason is: %s' % e)
         return request_result(605)
 def volume_delete(self, volume_uuid):
     if self.conn is False:
         return request_result(701)
     try:
         op_result = self.conn.block_storage.delete_volume(volume_uuid)
     except Exception, e:
         log.error('delete the volume(op) error, reason is: %s' % e)
         return request_result(603)
Exemplo n.º 10
0
 def port_delete(self, port_uuid):
     if self.conn is False:
         return request_result(701)
     try:
         self.conn.network.delete_port(port_uuid)
     except Exception, e:
         log.error('delete the port error, reason is: %s' % e)
         return request_result(1062)
Exemplo n.º 11
0
 def router_delete(self, router_uuid):
     if self.conn is False:
         return request_result(701)
     try:
         op_result = self.conn.network.delete_router(router_uuid)
     except Exception, e:
         log.error('router delete(op) error, reason is: %s' % e)
         return request_result(1043)
Exemplo n.º 12
0
 def subnet_delete(self, subnet_uuid):
     if self.conn is False:
         return request_result(701)
     try:
         op_result = self.conn.network.delete_subnet(subnet_uuid)
     except Exception, e:
         log.error('delete the subnet error, reason is: %s' % e)
         return request_result(1032)
Exemplo n.º 13
0
 def add_ip_to_port(self, port_uuid, ip):
     if self.conn is False:
         return request_result(701)
     try:
         op_result = self.conn.network.add_ip_to_port(port=port_uuid, ip=ip)
     except Exception, e:
         log.error('add ip to port(op) error, reason is: %s' % e)
         return request_result(1063)
Exemplo n.º 14
0
 def remove_ip_from_port(self, ip):
     if self.conn is False:
         return request_result(701)
     try:
         op_result = self.conn.network.remove_ip_from_port(ip)
     except Exception, e:
         log.error('remove the ip from port(op) error, reason is: %s' % e)
         return request_result(1063)
Exemplo n.º 15
0
 def network_delete(self, network_uuid):
     if self.conn is False:
         return request_result(701)
     try:
         op_result = self.conn.network.\
             delete_network(network_uuid)
     except Exception, e:
         log.error('delete the network(op) error, reason is: %s' % e)
         return request_result(1022)
 def add_os_interface(self, vm_uuid, port_uuid):
     # check the vnic if is used
     try:
         db_check = self.db.db_port_attach_check(port_uuid)
         if db_check[0][0] is not None:
             return request_result(1065)
     except Exception, e:
         log.error('check the vnic if used(db) error, reason is: %s' % e)
         return request_result(403)
Exemplo n.º 17
0
    def network_get(self):
        if self.conn is False:
            return request_result(701)

        try:
            op_result = self.conn.network.get_subnet('testsubnet')
        except Exception, e:
            log.error('query the network message error, reason is: %s' % e)
            return request_result(1023)
 def volume_list(self):
     if self.conn is False:
         return request_result(701)
     try:
         op_result = self.conn.block_storage.volumes()
     except Exception, e:
         log.error('SERVICE(clean delete mark):GET VOLUME LIST ERROR, '
                   'REASON IS: %s' % e)
         return request_result(605)
Exemplo n.º 19
0
 def get_port(self, port_uuid):
     if self.conn is False:
         return request_result(701)
     try:
         op_result = self.conn.network.get_port(port_uuid)
     except Exception, e:
         log.error('get the detail of the port(op) error, '
                   'reason is: %s' % e)
         return request_result(1064)
Exemplo n.º 20
0
 def user_password_manager(self, username):
     try:
         db_result = self.db.select_user_password(username)
         log.info('get the database result is: %s' % db_result)
         password = db_result[0][0]
         log.info(password)
         return request_result(200, password)
     except Exception, e:
         log.error('get the password by username from db error, reason is: %s' % e)
         return request_result(403)
Exemplo n.º 21
0
 def change_sessionid(self, stype, username, sessionid):
     try:
         if stype == 'insert':
             self.db.insert_cookie(username, sessionid)
         if stype == 'update':
             self.db.update_cookie(username, sessionid)
         return request_result(200, 'ok')
     except Exception, e:
         log.error('update the sessionid error, reason is: %s' % e)
         return request_result(402)
 def port_operate_delete(self, port_uuid):
     # check: if can delete the port
     try:
         db_result = self.db.db_port_if_can_del(port_uuid)
         # if db_result[0][0].lower() != 'down':
         if db_result[0][0].lower() != 'available':
             return request_result(405)
     except Exception, e:
         log.error('check the port if can delete error, reason is: %s' % e)
         return request_result(404)
 def remove_os_interface(self, port_uuid):
     try:
         db_check = self.db.db_port_attach_check(port_uuid)
         if db_check[0][0] is None:
             return request_result(0)
         vm_uuid = db_check[0][0]
     except Exception, e:
         log.error('check the port if need unattach error, '
                   'reason is: %s' % e)
         return request_result(403)
Exemplo n.º 24
0
    def delete(self, volume_uuid):
        # 如果待删除的存储卷有快照依赖或者正在使用,禁止删除
        del_check = self.if_can_delete(volume_uuid)
        if del_check is False:
            return request_result(604)

        try:
            db_result = self.db.volume_delete(volume_uuid)
        except Exception, e:
            log.error('logic delete the volume(db) error, reason is: %s' % e)
            return request_result(402)
Exemplo n.º 25
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)
    def attachment_create(self, server_uuid, volume_uuid):
        if self.conn is False:
            return request_result(701)

        try:
            op_result = self.conn.compute.\
                create_volume_attachment(server=server_uuid,
                                         volume_id=volume_uuid)
        except Exception, e:
            log.error('create the attachment(op) error, reason is: %s' % e)
            return request_result(1010)
    def attachment_delete(self, attachment_uuid, server_uuid):
        if self.conn is False:
            return request_result(701)

        try:
            op_result = self.conn.compute.\
                delete_volume_attachment(volume_attachment=attachment_uuid,
                                         server=server_uuid)
        except Exception, e:
            log.error('delete the attachment(op) error, reason is: %s' % e)
            return request_result(1012)
 def templet_update(self, templet_uuid, name=None, description=None,
                    user_uuid=None, project_uuid=None, team_uuid=None):
     if name is None and description is None:
         return request_result(0, templet_uuid)
     if name is None and description is not None:
         try:
             self.db.templet_update(templet_uuid, name, description)
         except Exception, e:
             log.error('update the templet(db) error, reason is: %s' % e)
             return request_result(402)
         return request_result(0, templet_uuid)
 def port_random_create(self, network_uuid, name=None, description=None,
                        user_uuid=None, project_uuid=None, team_uuid=None):
     # check the network if bind subnet, if not, forbid to create port
     try:
         db_result = self.db.db_check_network_if_bind_subnet(network_uuid)
         if db_result[0][0] == 0:
             return request_result(1024)
     except Exception, e:
         log.error('check the network if connect to subnet error, '
                   'reason is: %s' % e)
         return request_result(403)
Exemplo n.º 30
0
    def logic_delete(self, volume_uuid):
        # check if can be delete
        del_check = self.if_can_delete(volume_uuid)
        if del_check is False:
            return request_result(604)

        # update the volume show status from database
        try:
            db_result = self.db.volume_logic_update(volume_uuid)
        except Exception, e:
            log.error('logic delete the volume(db) error, reason is: %s' % e)
            return request_result(402)