Beispiel #1
0
    def detach(self, connection_info):
        """Detach a volume from a guest
        """
        fcp = connection_info['zvm_fcp']
        target_wwpns = connection_info['target_wwpn']
        target_lun = connection_info['target_lun']
        assigner_id = connection_info['assigner_id']
        assigner_id = assigner_id.upper()
        multipath = connection_info['multipath']
        os_version = connection_info['os_version']
        mount_point = connection_info['mount_point']
        multipath = multipath.lower()
        if multipath == 'true':
            multipath = True
        else:
            multipath = False

        is_root_volume = connection_info.get('is_root_volume', False)
        if is_root_volume is False and \
                not zvmutils.check_userid_exist(assigner_id):
            LOG.error("Guest '%s' does not exist" % assigner_id)
            raise exception.SDKObjectNotExistError(obj_desc=("Guest '%s'" %
                                                             assigner_id),
                                                   modID='volume')
        else:
            # TODO: now we only support 2 paths
            path_count = len(fcp)
            for i in range(path_count):
                self._detach(fcp[i].lower(), assigner_id, target_wwpns,
                             target_lun, multipath, os_version, mount_point,
                             is_root_volume)
Beispiel #2
0
    def image_query_record(self, imagename=None):
        """Query the image record from database, if imagename is None, all
        of the image records will be returned, otherwise only the specified
        image record will be returned."""

        if imagename:
            with get_image_conn() as conn:
                result = conn.execute(
                    "SELECT * FROM image WHERE "
                    "imagename=?", (imagename, ))
                image_list = result.fetchall()
            if not image_list:
                obj_desc = "Image with name: %s" % imagename
                raise exception.SDKObjectNotExistError(obj_desc=obj_desc,
                                                       modID=self._module_id)
        else:
            with get_image_conn() as conn:
                result = conn.execute("SELECT * FROM image")
                image_list = result.fetchall()

        # Map each image record to be a dict, with the key is the field name in
        # image DB
        image_keys_list = [
            'imagename', 'imageosdistro', 'md5sum', 'disk_size_units',
            'image_size_in_bytes', 'type', 'comments'
        ]

        image_result = []
        for item in image_list:
            image_item = dict(zip(image_keys_list, item))
            image_result.append(image_item)
        return image_result
Beispiel #3
0
    def attach(self, connection_info):
        """Attach a volume to a guest

        connection_info contains info from host and storage side
        this mostly includes
        host side FCP: this can get host side wwpn
        storage side wwpn
        storage side lun

        all the above assume the storage side info is given by caller
        """
        fcp = connection_info['zvm_fcp']
        fcp = fcp.lower()
        target_wwpn = connection_info['target_wwpn']
        target_lun = connection_info['target_lun']
        assigner_id = connection_info['assigner_id']
        assigner_id = assigner_id.upper()
        multipath = connection_info['multipath']
        multipath = multipath.lower()
        if multipath == 'true':
            multipath = True
        else:
            multipath = False
        os_version = connection_info['os_version']
        mount_point = connection_info['mount_point']

        # TODO: check exist in db?
        if not zvmutils.check_userid_exist(assigner_id):
            LOG.error("User directory '%s' does not exist." % assigner_id)
            raise exception.SDKObjectNotExistError(obj_desc=("Guest '%s'" %
                                                             assigner_id),
                                                   modID='volume')
        else:
            self._attach(fcp, assigner_id, target_wwpn, target_lun, multipath,
                         os_version, mount_point)
Beispiel #4
0
    def switch_update_record_with_switch(self, userid, interface, switch=None):
        """Update information in switch table."""
        if not self._get_switch_by_user_interface(userid, interface):
            msg = "User %s with nic %s does not exist in DB" % (userid,
                                                                interface)
            LOG.error(msg)
            obj_desc = ('User %s with nic %s' % (userid, interface))
            raise exception.SDKObjectNotExistError(obj_desc,
                                                   modID=self._module_id)

        if switch is not None:
            with get_network_conn() as conn:
                conn.execute(
                    "UPDATE switch SET switch=? "
                    "WHERE userid=? and interface=?",
                    (switch, userid, interface))
                LOG.debug("Set switch to %s for user %s with nic %s "
                          "in switch table" % (switch, userid, interface))
        else:
            with get_network_conn() as conn:
                conn.execute(
                    "UPDATE switch SET switch=NULL "
                    "WHERE userid=? and interface=?", (userid, interface))
                LOG.debug("Set switch to None for user %s with nic %s "
                          "in switch table" % (userid, interface))
Beispiel #5
0
    def detach(self, connection_info):
        """Detach a volume from a guest
        """
        fcp = connection_info['zvm_fcp']
        target_wwpns = connection_info['target_wwpn']
        target_lun = connection_info['target_lun']
        assigner_id = connection_info['assigner_id']
        assigner_id = assigner_id.upper()
        multipath = connection_info['multipath']
        os_version = connection_info['os_version']
        mount_point = connection_info['mount_point']
        multipath = multipath.lower()
        if multipath == 'true':
            multipath = True
        else:
            multipath = False

        is_root_volume = connection_info.get('is_root_volume', False)
        if is_root_volume is False and \
                not zvmutils.check_userid_exist(assigner_id):
            LOG.error("Guest '%s' does not exist" % assigner_id)
            raise exception.SDKObjectNotExistError(
                    obj_desc=("Guest '%s'" % assigner_id), modID='volume')
        else:
            path_count = len(fcp)
            need_restart = False
            for i in range(path_count):
                # To avoid restart zvmguestconfigure service too often,
                # only restart it when the last fcp is detached
                if i == path_count - 1:
                    need_restart = True
                self._detach(fcp[i].lower(), assigner_id, target_wwpns,
                             target_lun, multipath, os_version, mount_point,
                             is_root_volume, need_restart)
Beispiel #6
0
    def check_guests_exist_in_db(self, userids, raise_exc=True):
        if not isinstance(userids, list):
            # convert userid string to list
            userids = [userids]

        all_userids = self.guest_list()

        userids_not_in_db = list(set(userids) - set(all_userids))
        if userids_not_in_db:
            if raise_exc:
                # log and raise exception
                userids_not_in_db = ' '.join(userids_not_in_db)
                LOG.error("Guest '%s' does not exist in guests database" %
                          userids_not_in_db)
                raise exception.SDKObjectNotExistError(
                    obj_desc=("Guest '%s'" % userids_not_in_db), modID='guest')
            else:
                return False
        else:
            userids_migrated = self._GuestDbOperator.get_migrated_guest_list()
            userids_in_migrated = list(set(userids) & set(userids_migrated))

            # case1 userid has been migrated.
            if userids_in_migrated:
                if raise_exc:
                    migrated_userids = ' '.join(userids_in_migrated)
                    LOG.error("Guest(s) '%s' has been migrated." %
                              migrated_userids)
                    raise exception.SDKObjectNotExistError(
                        obj_desc=("Guest(s) '%s'" % migrated_userids),
                        modID='guest')
                else:
                    return False

            flag = True
            for uid in userids:
                # case2 userid has been shudown and started on other host.
                if zvmutils.check_userid_on_others(uid):
                    flag = False
                    comment = self._GuestDbOperator.get_comments_by_userid(uid)
                    comment['migrated'] = 1
                    action = "update guest '%s' in database" % uid
                    with zvmutils.log_and_reraise_sdkbase_error(action):
                        self._GuestDbOperator.update_guest_by_userid(
                            uid, comments=comment)
            return flag
Beispiel #7
0
    def attach(self, connection_info):
        """Attach a volume to a guest

        connection_info contains info from host and storage side
        this mostly includes
        host side FCP: this can get host side wwpn
        storage side wwpn
        storage side lun

        all the above assume the storage side info is given by caller
        """
        fcp = connection_info['zvm_fcp']
        target_wwpns = connection_info['target_wwpn']
        target_lun = connection_info['target_lun']
        assigner_id = connection_info['assigner_id']
        assigner_id = assigner_id.upper()
        multipath = connection_info['multipath']
        multipath = multipath.lower()
        if multipath == 'true':
            multipath = True
        else:
            multipath = False
        os_version = connection_info['os_version']
        mount_point = connection_info['mount_point']
        is_root_volume = connection_info.get('is_root_volume', False)

        # TODO: check exist in db?
        if is_root_volume is False and \
                not zvmutils.check_userid_exist(assigner_id):
            LOG.error("User directory '%s' does not exist." % assigner_id)
            raise exception.SDKObjectNotExistError(
                    obj_desc=("Guest '%s'" % assigner_id), modID='volume')
        else:
            # TODO: the length of fcp is the count of paths in multipath
            path_count = len(fcp)
            dedicated_fcp = []
            need_restart = False
            for i in range(path_count):
                # To avoid restart zvmguestconfigure service too often,
                # only restart it when the last fcp is attached
                if i == path_count - 1:
                    need_restart = True
                try:
                    new = self._attach(fcp[i].lower(), assigner_id,
                                       target_wwpns, target_lun, multipath,
                                       os_version, mount_point, path_count,
                                       is_root_volume, need_restart)
                    if new and is_root_volume is False:
                        dedicated_fcp.append(fcp[i])
                except exception.SDKBaseException:
                    self._rollback_dedicated_fcp(dedicated_fcp, assigner_id,
                        all_fcp_list=fcp)
                    raise
Beispiel #8
0
 def _check_existence_by_userid(self, userid, ignore=False):
     guest = self.get_guest_by_userid(userid)
     if guest is None:
         msg = 'Guest with userid: %s does not exist in DB.' % userid
         if ignore:
             # Just print a warning message
             LOG.info(msg)
         else:
             LOG.error(msg)
             obj_desc = "Guest with userid: %s" % userid
             raise exception.SDKObjectNotExistError(obj_desc=obj_desc,
                                                    modID=self._module_id)
     return guest
Beispiel #9
0
    def get_connections_from_fcp(self, fcp):
        connections = 0
        with get_fcp_conn() as conn:
            result = conn.execute("SELECT connections FROM fcp WHERE "
                                  "fcp_id=?", (fcp,))
            fcp_info = result.fetchall()
            if not fcp_info:
                msg = 'FCP with id: %s does not exist in DB.' % fcp
                LOG.error(msg)
                obj_desc = "FCP with id: %s" % fcp
                raise exception.SDKObjectNotExistError(obj_desc=obj_desc,
                                                       modID=self._module_id)
            connections = fcp_info[0][0]

        return connections
Beispiel #10
0
 def check_fcp_exist_in_db(self, fcp, raise_exec=True):
     all_fcps_raw = self.db.get_all()
     all_fcps = []
     for item in all_fcps_raw:
         all_fcps.append(item[0].lower())
     if fcp not in all_fcps:
         if raise_exec:
             LOG.error("fcp %s not exist in db!", fcp)
             raise exception.SDKObjectNotExistError(
                 obj_desc=("FCP '%s'" % fcp), modID='volume')
         else:
             LOG.warning("fcp %s not exist in db!", fcp)
             return False
     else:
         return True
Beispiel #11
0
 def get_all_fcps_of_assigner(self, assigner_id):
     ret = {}
     with get_fcp_conn() as conn:
         result = conn.execute("SELECT fcp_id, reserved, connections FROM "
                               "fcp WHERE assigner_id=?", (assigner_id,))
         fcp_info = result.fetchall()
         if not fcp_info:
             msg = 'No FCPs found belongs to userid %s.' % assigner_id
             LOG.error(msg)
             obj_desc = "FCPs belongs to %s" % assigner_id
             raise exception.SDKObjectNotExistError(obj_desc=obj_desc,
                                                    modID=self._module_id)
         for item in fcp_info:
             # 'fcp_id': (userid, reserved, connections)
             ret[item[0]] = (assigner_id, item[1], item[2])
     return ret
Beispiel #12
0
    def get_all_fcps(self):
        ret = {}
        with get_fcp_conn() as conn:
            result = conn.execute("SELECT fcp_id, assigner_id, reserved, "
                                  "connections FROM fcp")
            fcp_info = result.fetchall()

            if not fcp_info:
                msg = 'No FCPs found in database.'
                LOG.error(msg)
                obj_desc = "FCPs in database"
                raise exception.SDKObjectNotExistError(obj_desc=obj_desc,
                                                       modID=self._module_id)
            for item in fcp_info:
                # 'fcp_id': (reserved, connections)
                ret[item[0]] = (item[1], item[2], item[3])
        return ret
Beispiel #13
0
    def get_usage_of_fcp(self, fcp):
        connections = 0
        reserved = 0
        with get_fcp_conn() as conn:
            result = conn.execute("SELECT assigner_id, reserved, connections "
                                  "FROM fcp WHERE fcp_id=?", (fcp,))
            fcp_info = result.fetchall()
            if not fcp_info:
                msg = 'FCP with id: %s does not exist in DB.' % fcp
                LOG.error(msg)
                obj_desc = "FCP with id: %s" % fcp
                raise exception.SDKObjectNotExistError(obj_desc=obj_desc,
                                                       modID=self._module_id)
            assigner_id = fcp_info[0][0]
            reserved = fcp_info[0][1]
            connections = fcp_info[0][2]

        return assigner_id, reserved, connections
Beispiel #14
0
    def check_guests_exist_in_db(self, userids, raise_exc=True):
        if not isinstance(userids, list):
            # convert userid string to list
            userids = [userids]

        all_userids = self.guest_list()
        userids_not_in_db = list(set(userids) - set(all_userids))
        if userids_not_in_db:
            if raise_exc:
                # log and raise exception
                userids_not_in_db = ' '.join(userids_not_in_db)
                LOG.error("Guest '%s' does not exist in guests database" %
                          userids_not_in_db)
                raise exception.SDKObjectNotExistError(
                    obj_desc=("Guest '%s'" % userids_not_in_db), modID='guest')
            else:
                return False
        else:
            return True
Beispiel #15
0
    def attach(self, connection_info):
        """Attach a volume to a guest

        connection_info contains info from host and storage side
        this mostly includes
        host side FCP: this can get host side wwpn
        storage side wwpn
        storage side lun

        all the above assume the storage side info is given by caller
        """
        fcp = connection_info['zvm_fcp']
        target_wwpns = connection_info['target_wwpn']
        target_lun = connection_info['target_lun']
        assigner_id = connection_info['assigner_id']
        assigner_id = assigner_id.upper()
        multipath = connection_info['multipath']
        multipath = multipath.lower()
        if multipath == 'true':
            multipath = True
        else:
            multipath = False
        os_version = connection_info['os_version']
        mount_point = connection_info['mount_point']
        is_root_volume = connection_info.get('is_root_volume', False)

        # TODO: check exist in db?
        if is_root_volume is False and \
                not zvmutils.check_userid_exist(assigner_id):
            LOG.error("User directory '%s' does not exist." % assigner_id)
            raise exception.SDKObjectNotExistError(
                    obj_desc=("Guest '%s'" % assigner_id), modID='volume')
        else:
            # TODO: the length of fcp is the count of paths in multipath
            path_count = len(fcp)
            # transfer to lower cases
            fcp_list = [x.lower() for x in fcp]
            self._attach(fcp_list, assigner_id,
                         target_wwpns, target_lun,
                         multipath, os_version,
                         mount_point, path_count,
                         is_root_volume)
Beispiel #16
0
    def detach(self, connection_info):
        """Detach a volume from a guest
        """
        fcp = connection_info['zvm_fcp']
        fcp = fcp.lower()
        target_wwpn = connection_info['target_wwpn']
        target_lun = connection_info['target_lun']
        assigner_id = connection_info['assigner_id']
        assigner_id = assigner_id.upper()
        multipath = connection_info['multipath']
        os_version = connection_info['os_version']
        mount_point = connection_info['mount_point']

        if not zvmutils.check_userid_exist(assigner_id):
            LOG.error("Guest '%s' does not exist" % assigner_id)
            raise exception.SDKObjectNotExistError(obj_desc=("Guest '%s'" %
                                                             assigner_id),
                                                   modID='volume')
        else:
            self._detach(fcp, assigner_id, target_wwpn, target_lun, multipath,
                         os_version, mount_point)
Beispiel #17
0
    def decrease_usage(self, fcp):
        with get_fcp_conn() as conn:

            result = conn.execute("SELECT * FROM fcp WHERE "
                                  "fcp_id=?", (fcp, ))
            fcp_list = result.fetchall()
            connections = fcp_list[0][2]
            if connections == 0:
                msg = 'FCP with id: %s no connections in DB.' % fcp
                LOG.error(msg)
                obj_desc = "FCP with id: %s" % fcp
                raise exception.SDKObjectNotExistError(obj_desc=obj_desc,
                                                       modID=self._module_id)
            else:
                connections -= 1
            if connections < 0:
                connections = 0
                LOG.warning("Warning: connections of fcp is negative", fcp)

            conn.execute("UPDATE fcp SET connections=? "
                         "WHERE fcp_id=?", (connections, fcp))
            return connections