Example #1
0
def search_by_ip(ip):
    if request.method == 'GET':
        try:
            manage_paths = MangePathAssignment()
            logger.info("get object from MangePathAssignment.")
            show_list = manage_paths.search_by_ip(ip)
            logger.info("call search by ip function.")
            return show_list.write_json()

        except Exception as e:
            logger.error(e)
            return -1
Example #2
0
def auto_assign_paths(type):
    if request.method == 'POST':
        try:
            manage_path_assignment = MangePathAssignment()
            logger.info("get object from MangePathAssignment.")
            manage_path_assignment.auto(int(type))
            logger.info("call auto function.")
            # session['success'] = "ui_admin_disks_paths_assignment_list_success"
            logger.info("set success message and will redirect to paths list.")
            return redirect(url_for('disk_controller.paths_list'))

        except Exception as e:
            # session['err'] = "ui_admin_disks_paths_assignment_list_fail"
            logger.error(e)
def path_host(args):
    logger.info("Reassignment paths script invoked to run clean action.")
    if MangePathAssignment().clean_source_node(args.ip, args.disk_id):
        print "0"
        return

    print "-1"
Example #4
0
def get_assignments_status():
    """
    DOCSTRING : this function is called to delete a certain pool , then redirect to the page :
    'admin/disk/path_assignment_list.html'.
    Args : none
    Returns : redirect to the page : list of all disks status
    """
    if request.method == 'GET':
        try:
            manage_paths = MangePathAssignment()
            logger.info("get object from MangePathAssignment.")
            show_list = manage_paths.get_assignments_stats()
            logger.info("call get assignments stats function.")
            return show_list.write_json()

        except Exception as e:
            logger.error(e)
            return -1
def server(args):

    try:
        logger.info("Reassignment paths script invoked to run process action.")
        MangePathAssignment().process()
    except Exception as ex:
        logger.error("error process reassignments actions.")
        logger.exception(ex.message)
        print(-1)
        sys.exit(-1)
Example #6
0
def manual_assign_paths():
    if request.method == 'POST':
        try:
            selected_paths = request.form.getlist('option_path[]')
            selected_paths_lst = [str(x) for x in selected_paths]
            paths_assign_info_lst = []
            logger.info("User starts manual assignments.")
            for path in selected_paths_lst:
                selected_paths_assign_info = path.split('##')

                paths_assign_info = PathAssignmentInfo()
                paths_assign_info.node = selected_paths_assign_info[0]
                paths_assign_info.disk_name = selected_paths_assign_info[1]
                paths_assign_info.ip = selected_paths_assign_info[2]
                paths_assign_info.disk_id = selected_paths_assign_info[3]

                paths_assign_info_lst.append(paths_assign_info)
                logger.info("User selected path {} {} {}.".format(
                    paths_assign_info.disk_id, paths_assign_info.disk_name,
                    paths_assign_info.node))

            assign_to = request.form.get('assign_to')
            manage_path_assignment = MangePathAssignment()
            if assign_to == "auto":
                logger.info("User selected auto option in manual assignment.")
                manage_path_assignment.manual(paths_assign_info_lst)
                logger.info("System started auto assignments.")
                # pass
            else:
                logger.info("User selected manual option in assignment.")
                manage_path_assignment.manual(paths_assign_info_lst, assign_to)
                logger.info("System started manual assignments.")

            session['success'] = "ui_admin_disks_paths_assignment_list_success"
            return redirect(url_for('disk_controller.paths_list'))

        except Exception as e:
            session['err'] = "ui_admin_disks_paths_assignment_list_fail"
            logger.error(e)
Example #7
0
    def __acquire_path(self, path, consul_kv):
        if self.__ignored_acquire_paths.get(path):
            logger.info("Ignore forced path {}".format(path))
            return
        logger.debug("Start acquire path {} by node session {}.".format(
            path, self.__session))
        consul_api = ConsulAPI()
        ceph_api = CephAPI()
        lio_api = LioAPI()
        network_api = NetworkAPI()
        config = configuration()
        try:
            disk_id, path_index = str(path).split("/")
            pool = self._get_pool_by_disk(disk_id)
            if not pool:
                logger.error('Could not find pool for disk ' + disk_id)
                return
            image_name = self.__image_name_prefix + disk_id
            logger.debug(
                "Start read image meta for acquire path {}.".format(path))
            all_image_meta = ceph_api.read_image_metadata(image_name, pool)
            petasan_meta = all_image_meta.get(
                self.__app_conf.get_image_meta_key())
            disk_meta = DiskMeta()
            disk_meta.load_json(petasan_meta)
            logger.debug(
                "End read image meta for acquire path {}.".format(path))

            logger.debug("Try to acquire path {}.".format(path))
            node_name = config.get_node_name()
            result = consul_api.lock_disk_path(
                self.__app_conf.get_consul_disks_path() + path, self.__session,
                node_name, str(consul_kv.CreateIndex))
            if not result:
                logger.info("Could not lock path {} with session {}.".format(
                    path, self.__session))
            elif result:
                if consul_kv.Value != None and len(str(
                        consul_kv.Value)) > 0 and node_name != str(
                            consul_kv.Value):
                    logger.info("The path {} was locked by {}.".format(
                        path, str(consul_kv.Value)))
                    logger.debug("Node {} will kill node {}.".format(
                        config.get_node_name(), str(consul_kv.Value)))
                    self.__fencing(str(consul_kv.Value))

                # we locked it
                if disk_meta.paths:
                    # if lio has the image name in its backstore already, do not perform rbd mapping
                    if image_name not in self.__backstore:
                        status = ceph_api.map_iamge(image_name, pool)
                    else:
                        status = Status.done
                    if Status.done == status:
                        # Get path info from metadata
                        path_obj = disk_meta.get_paths()[int(path_index) - 1]
                        # add path ips to our network interfaces
                        network_api.add_ip(path_obj.ip, path_obj.subnet_mask,
                                           path_obj.eth, path_obj.vlan_id)
                        #update neighbors arp table
                        network_api.update_neighbors_arp(
                            path_obj.ip, path_obj.eth)
                        # add new target in lio if not there already
                        if not lio_api.is_backstore_image_found(image_name):
                            # Give ceph map image complete it job
                            sleep(3)
                            # Add rbd backstores and target
                            status = lio_api.add_target(
                                disk_meta, disk_meta.pool)
                            """
                            wwn = self.calculate_disk_wwn(disk_meta)
                            status = lio_api.add_target(disk_meta, wwn, disk_meta.pool)
                            """
                        if Status.done == status:
                            # enable the path we locked to true
                            self.__last_acquire_succeeded = True
                            lio_api.enable_path(disk_meta.iqn, path_index,
                                                True)
                            logger.info("Path %s acquired successfully" % path)

                            if self.__acquire_warning_counter > 2:
                                logger.info(
                                    "PetaSAN finally succeeded to acquire path after retrying {} times."
                                    .format(self.__acquire_warning_counter))
                                self.__acquire_warning_counter = 0
                            path_assignment_info = self.__force_acquire_paths.get(
                                path)

                            if path_assignment_info:
                                MangePathAssignment().update_path(
                                    path_obj.ip, ReassignPathStatus.succeeded)
                        else:
                            path_assignment_info = self.__force_acquire_paths.get(
                                path)
                            if path_assignment_info:
                                logger.info(
                                    "Acquired forced path {}".format(path))
                                MangePathAssignment().update_path(
                                    path_obj.ip, ReassignPathStatus.failed)
                            self.__last_acquire_succeeded = False
                            if self.__acquire_warning_counter > 2:
                                logger.warning(
                                    "PetaSAN failed to acquire path after {} times."
                                    .format(self.__acquire_warning_counter))
                                self.__acquire_warning_counter += 1
                            logger.error("Error could not acquire path %s" %
                                         path)

                    else:
                        self.__unlock_consul_path(path)

        except Exception as e:
            logger.info("---------------------------------")
            logger.error(str(e.message) + "\n")
            logger.exception(e)

            if str(e.message).find("invalid session") > -1:
                logger.error("Session is invalid")
                try:
                    logger.info("Trying to create new session id")
                    self.__session = ConsulAPI().get_new_session_ID(
                        self.__session_name, self.__node_info.name)
                    logger.info("New session id is {}".format(self.__session))
                    logger.info("Cleaning all mapped disks from old session")
                    self.__clean()
                except Exception as ex:
                    logger.exception(ex)
            logger.exception("Could not acquire path %s" % path)
            raise e
        logger.debug("End acquire path {}.".format(path))
        return
Example #8
0
    def __read_resources_consul(self):
        logger.debug("Start read resources consul.")
        self.__paths_per_session = {}
        self.__total_cluster_paths = 0
        unlock_kvs = set()
        consul_api = ConsulAPI()
        try:
            disk_kvs = consul_api.get_disk_kvs()
            for kv in disk_kvs:
                key = str(kv.Key).replace(
                    self.__app_conf.get_consul_disks_path(), "")
                disk_id = str(key).split('/')[0]
                if disk_id in self.__disk_consul_stopped:
                    continue
                if kv.Value == "disk":
                    disk_id = str(key).split('/')[0]
                    self.__paths_per_disk_local[disk_id] = 0
                    if str(kv.Flags) == "1":
                        self.__disk_consul_stopped.add(disk_id)
                    continue
                # Count paths in the cluster.
                self.__total_cluster_paths += 1

                if hasattr(kv, "Session"):
                    # locked paths
                    if kv.Session == self.__session:
                        self.__paths_consul_locked_node.add(key)
                        disk_paths_count = self.__paths_per_disk_local.get(
                            disk_id, 0) + 1
                        self.__paths_per_disk_local[disk_id] = disk_paths_count
                    # Total count of paths for each session
                    if self.__paths_per_session.has_key(kv.Session):
                        count = self.__paths_per_session.get(kv.Session)
                        self.__paths_per_session[kv.Session] = count + 1
                    else:
                        self.__paths_per_session[kv.Session] = 1
                # unlocked paths
                elif not hasattr(kv, "Session"):
                    unlock_kvs.add(kv)
            # Filter unlocked paths
            reassignments = None
            if len(unlock_kvs) > 0:
                reassignments = MangePathAssignment().get_forced_paths()
            for kv in unlock_kvs:
                key = str(kv.Key).replace(
                    self.__app_conf.get_consul_disks_path(), "")
                if reassignments:
                    path_assignment_info = reassignments.get(key)
                    if path_assignment_info and path_assignment_info.target_node == self.__node_info.name:
                        self.__force_acquire_paths[key] = kv
                        continue
                    else:
                        self.__ignored_acquire_paths[key] = kv
                        continue

                disk_id = str(key).split('/')[0]
                if self.__paths_per_disk_local.get(disk_id, 0) > 0:
                    self.__paths_consul_unlocked_siblings[key] = kv
                else:
                    self.__paths_consul_unlocked_firstborn[key] = kv
        except Exception as e:
            logger.error("Could not read consul resources.")
            logger.exception(e)
            raise e
        logger.debug("End read resources consul.")