def _get_app_cgroups(self, path, username): # If the app path contains "application_home", then assume the app was deployed using capistrano # and use the parent directory name as the application name. Otherwise use the last path # component as the application name. path_components = split_path_components(path) try: app_home_idx = path_components.index('application_home') except: app_home_idx = 0 app_name = path_components[app_home_idx-1] # Return the cgroups that the process should be assigned to # (/<subsystem>/RoR/<username>/<app name>) return [(subsystem, self.base_cgroup, os.path.join(username, app_name)) for subsystem in self.subsystems]
def __init__(self, subsystems=[], base_cgroups=[], cgroups_perms=0o755): self.__logger = logging.getLogger(__name__) # Mount any subsystems that are not already mounted trees.bootstrap(root_path=cgroups_root_path, subsystems=subsystems) # This will create the specified base_cgroups under each subsystem if they don't already exist self.cgroups_tree = trees.Tree(root_path=cgroups_root_path, groups=subsystems, sub_groups=base_cgroups) self.subsystems = subsystems or [ node.name for node in self.cgroups_tree.children ] self.base_cgroups = base_cgroups self.cgroups_perms = cgroups_perms # Set up cleanup parameters # Extending classes can override this in their __init__() after calling super().__init__() self.cleanup_interval = 30 # minutes self.last_cleanup = datetime.min self.cleanup_nodes = [ self.cgroups_tree.get_node_by_path(os.path.join('/', subsystem, base_cgroup)) for base_cgroup in base_cgroups for subsystem in subsystems] self.initialized_cgroups = {} # Configure (or reconfigure) the subsystems for subsystem in subsystems: self.config_subsystem(self.cgroups_tree.get_node_by_path(os.path.join('/', subsystem))) # Configure (or reconfigure) the base_cgroups for subsystem in self.subsystems: for base_cgroup in base_cgroups: path = os.path.join('/', subsystem) depth = 1 for path_component in split_path_components(base_cgroup)[0:-1]: path = os.path.join(path, path_component) self.config_base_cgroup_parent(self.cgroups_tree.get_node_by_path(path), depth) self.initialized_cgroups[path] = True depth += 1 path = os.path.join('/', subsystem, base_cgroup) self.config_base_cgroup(self.cgroups_tree.get_node_by_path(path)) self.initialized_cgroups[path] = True # Register callbacks with cgroupsd_listener cgroupsd_listener.handlers['process'] += self.handle_process cgroupsd_listener.handlers['thread'] += self.handle_thread cgroupsd_listener.handlers['cleanup'] += self.handle_cleanup
def config_cgroups(self, cgroups, init_existing=False): if not init_existing: # This shouldn't be needed any more after we've reconfigured all of the existing cgroups self.initialized_cgroups = {} for cgroup in cgroups: path = os.path.join('/', cgroup[0], cgroup[1]) cgroup_node = self.cgroups_tree.get_node_by_path(path) depth = 1 for path_component in split_path_components(cgroup[2])[0:-1]: path = os.path.join(path, path_component) next_cgroup_node = self.cgroups_tree.get_node_by_path(path) if not next_cgroup_node: self.__logger.info('Creating cgroup {0}'.format(path)) cgroup_node = cgroup_node.create_cgroup(path_component) self.init_cgroup_parent(cgroup_node, depth, new_cgroup=True) elif init_existing and self.initialized_cgroups.get(path, None) == None: cgroup_node = next_cgroup_node self.init_cgroup_parent(cgroup_node, depth, new_cgroup=False) self.initialized_cgroups[path] = True else: cgroup_node = next_cgroup_node self.reconfig_cgroup_parent(cgroup_node, depth) depth += 1 path = os.path.join('/', *cgroup) next_cgroup_node = self.cgroups_tree.get_node_by_path(path) if not next_cgroup_node: self.__logger.info('Creating cgroup {0}'.format(path)) cgroup_node = cgroup_node.create_cgroup(os.path.basename(path)) self.init_cgroup(cgroup_node, new_cgroup=True) elif init_existing and self.initialized_cgroups.get(path, None) == None: cgroup_node = next_cgroup_node self.init_cgroup(cgroup_node, new_cgroup=False) self.initialized_cgroups[path] = True else: cgroup_node = next_cgroup_node self.reconfig_cgroup(cgroup_node)
def check__split_path_components_case(path, components): assert split_path_components(path) == components