示例#1
0
    def addGroup(self, group_name, priority=999):
        """
        Adds a new process group to the supervisor configuration.
        
        Args:
            group_name (str): The name of the group to add.
            priority (int, optional): The group priority. Defaults to 999.
            
        Returns:
            (boolean) True, unless an error is raised.
        """
        self._update('addGroup')

        # make sure the group doesn't already exist
        if self.supervisord.process_groups.get(group_name) is not None:
            raise RPCError(Faults.ALREADY_ADDED, group_name)
        
        options = self.supervisord.options
        group_config = ProcessGroupConfig(
            options, group_name, priority, process_configs=[])
        self.supervisord.options.process_group_configs.append(group_config)
        group_config.after_setuid()
        self.supervisord.process_groups[group_name] = group_config.make_group()

        return True
    def addGroup(self, name, priority=999):
        """ Add a new process group.
        
        @param string  group_name       Name of group to create
        @param int     priority         Group start priority
        @return boolean                 True unless error
        """
        self._update('addGroup')

        options = self.supervisord.options
        group_config = None
        for config in options.process_group_configs:
            if name == config.name:
                group_config = config
                break

        if not group_config:
            group_config = ProcessGroupConfig(options, name, priority, process_configs=[])
            self.supervisord.options.process_group_configs.append(group_config)

        if name not in self.supervisord.process_groups:
            group_config.after_setuid()
            self.supervisord.process_groups[name] = group_config.make_group()
        else:
            raise RPCError(SupervisorFaults.ALREADY_ADDED, name)

        return True
 def make_gconfig(name, pconfigs):
     return ProcessGroupConfig(options, name, 25, pconfigs)
 def make_gconfig(self, name, pconfigs, options):
     """
     Make process group config
     """
     return ProcessGroupConfig(options, name, 25, pconfigs)
class TwiddlerNamespaceRPCInterface:
    """ A supervisor rpc interface that facilitates manipulation of
    supervisor's configuration and state in ways that are not
    normally accessible at runtime.
    """

    def __init__(self, supervisord, whitelist=[]):
        self.supervisord = supervisord
        self._whitelist = list_of_strings(whitelist)

    def _update(self, func_name):
        self.update_text = func_name

        state = self.supervisord.get_state()
        if state == SupervisorStates.SHUTDOWN:
            raise RPCError(SupervisorFaults.SHUTDOWN_STATE)

        if len(self._whitelist):
            if func_name not in self._whitelist:
                raise RPCError(Faults.NOT_IN_WHITELIST, func_name)

    # RPC API methods

    def getAPIVersion(self):
        """ Return the version of the RPC API used by supervisor_twiddler

        @return int version version id
        """
        self._update('getAPIVersion')
        return API_VERSION

    def getGroupNames(self):
        """ Return an array with the names of the process groups.

        @return array                Process group names
        """
        self._update('getGroupNames')
        return list(self.supervisord.process_groups.keys())

    def log(self, message, level=supervisor.loggers.LevelsByName.INFO):
        """ Write an arbitrary message to the main supervisord log.  This is
            useful for recording information about your twiddling.

        @param  string      message      Message to write to the log
        @param  string|int  level        Log level name (INFO) or code (20)
        @return boolean                  Always True unless error
        """
        self._update('log')

        if isinstance(level, str):
            level = getattr(supervisor.loggers.LevelsByName,
                            level.upper(), None)

        if supervisor.loggers.LOG_LEVELS_BY_NUM.get(level, None) is None:
            raise RPCError(SupervisorFaults.INCORRECT_PARAMETERS)

        self.supervisord.options.logger.log(level, message)
        return True

    def addProgramToGroup(self, group_name, program_name, program_options):
        """ Add a new program to an existing process group.  Depending on the
            numprocs option, this will result in one or more processes being
            added to the group.

        @param string  group_name       Name of an existing process group
        @param string  program_name     Name of the new process in the process table
        @param struct  program_options  Program options, same as in supervisord.conf
        @return boolean                 Always True unless error
        """
        self._update('addProgramToGroup')

        group = self._getProcessGroup(group_name)

        # make configparser instance for program options
        section_name = 'program:%s' % program_name
        parser = self._makeConfigParser(section_name, program_options)

        # make process configs from parser instance
        options = self.supervisord.options
        try:
            new_configs = options.processes_from_section(parser, section_name, group_name)
        except ValueError as e:
            raise RPCError(SupervisorFaults.INCORRECT_PARAMETERS, e)

        # check new process names don't already exist in the config
        for new_config in new_configs:
            for existing_config in group.config.process_configs:
                if new_config.name == existing_config.name:
                    raise RPCError(SupervisorFaults.BAD_NAME, new_config.name)

        # add process configs to group
        group.config.process_configs.extend(new_configs)

        for new_config in new_configs:
            # the process group config already exists and its after_setuid hook
            # will not be called again to make the auto child logs for this process.
            new_config.create_autochildlogs()

            # add process instance
            group.processes[new_config.name] = new_config.make_process(group)

        return True

    def removeProcessFromGroup(self, group_name, process_name):
        """ Remove a process from a process group.  When a program is added with
            addProgramToGroup(), one or more processes for that program is added
            to the group.  This method removes individual processes (named by the
            numprocs and process_name options), not programs.

        @param string group_name    Name of an existing process group
        @param string process_name  Name of the process to remove from group
        @return boolean             Always return True unless error
        """
        self._update('removeProcessFromGroup')

        group = self._getProcessGroup(group_name)

        # check process exists and is running
        process = group.processes.get(process_name)
        if process is None:
            raise RPCError(SupervisorFaults.BAD_NAME, process_name)
        if process.pid or process.state not in STOPPED_STATES:
            raise RPCError(SupervisorFaults.STILL_RUNNING, process_name)

        group.transition()

        # del process config from group, then del process
        for index, config in enumerate(group.config.process_configs):
            if config.name == process_name:
                del group.config.process_configs[index]

        del group.processes[process_name]
        return True

    def addGroup(self, name, priority=999):
        """ Add a new, empty process group.

        @param string   name         Name for the new process group
        @param integer  priority     Group priority (same as supervisord.conf)
        @return boolean              Always True unless error
        """
        self._update('addGroup')

        # check group_name does not already exist
        if self.supervisord.process_groups.get(name) is not None:
            raise RPCError(SupervisorFaults.BAD_NAME, name)

        # check priority is sane
        try:
            int(priority)
        except ValueError, why:
            raise RPCError(SupervisorFaults.INCORRECT_PARAMETERS, why[0])

            # make a new group with no process configs
        options = self.supervisord.options
        config = ProcessGroupConfig(options, name, priority, [])
        group = config.make_group()

        # add new process group
        self.supervisord.process_groups[name] = group
        return True