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.")
def get_list(): api = ConsulAPI() x = api.get_disk_kvs() for i in x: print i.Key
def _filter_assignments_stats(self, filter_type=0, filter_text=None, set_session=False): __disk_consul_stopped = set() running_paths = dict() ceph_api = CephAPI() consul_api = ConsulAPI() disk_kvs = consul_api.get_disk_kvs() # Step 1 get all running paths. for consul_kv_obj in disk_kvs: path_key = str(consul_kv_obj.Key).replace( self.__app_conf.get_consul_disks_path(), "") disk_id = str(path_key).split('/')[0] if disk_id in __disk_consul_stopped: continue if consul_kv_obj.Value == "disk": disk_id = str(path_key).split('/')[0] # Step 2 avoid stopping disks if str(consul_kv_obj.Flags) == "1": __disk_consul_stopped.add(disk_id) continue running_paths[path_key] = consul_kv_obj if len(running_paths) == 0: return AssignmentStats() # Step 3 get all images metadata images = ceph_api.get_disks_meta() assignment_stats = AssignmentStats() # Step 4 get current reassignments current_running_assignments = self.get_current_reassignment() if current_running_assignments is not None: assignment_stats.is_reassign_busy = True filter_type = 0 # we will stop any filter and get all data if here is running reassignment # Step 5 fill paths assignment info for path_key, consul_kv_obj in running_paths.iteritems(): disk_id = str(path_key).split('/')[0] disk = next((img for img in images if img.id == disk_id), None) if disk is None: continue disk_path = Path() path_index = int(str(path_key).split(disk_id + "/")[1]) path_str = disk.paths[path_index - 1] disk_path.load_json(json.dumps(path_str)) path_assignment_info = PathAssignmentInfo() path_assignment_info.interface = disk_path.eth if disk_path.vlan_id: path_assignment_info.interface = disk_path.eth + "." + disk_path.vlan_id path_assignment_info.ip = disk_path.ip path_assignment_info.disk_name = disk.disk_name path_assignment_info.disk_id = disk_id path_assignment_info.index = path_index current_path = None if current_running_assignments is not None: current_path = current_running_assignments.get(disk_path.ip) if hasattr(consul_kv_obj, "Session") and self.__session_dict.has_key( consul_kv_obj.Session): # Fill status and node name for started paths path_assignment_info.node = self.__session_dict.get( consul_kv_obj.Session).Node if current_running_assignments is not None: if current_path is not None and current_path.status != -1: path_assignment_info.status = current_path.status path_assignment_info.target_node = current_path.target_node if set_session: # session refers to the node that lock this path assignment,This property helps to know the # status of path and the node will handle this path path_assignment_info.session = current_path.session elif current_path: path_assignment_info.node = current_path.node path_assignment_info.target_node = current_path.target_node path_assignment_info.status = current_path.status if set_session: path_assignment_info.session = current_path.session # Step 6 search or get all if filter_type == 1 and filter_text is not None and len( str(filter_text).strip()) > 0: # by disk name if filter_text.strip().lower( ) in path_assignment_info.disk_name.lower(): assignment_stats.paths.append(path_assignment_info) elif filter_type == 2 and filter_text is not None and len( str(filter_text).strip()) > 0: # by ip if filter_text.strip() == path_assignment_info.ip.strip(): assignment_stats.paths.append(path_assignment_info) break else: assignment_stats.paths.append(path_assignment_info) # Step 7 set all online nodes assignment_stats.nodes = self._get_nodes() return assignment_stats