def import_plugins(path="/etc/pyconfd/conf.d/"): """ Import plugins from the plugin path. Each of the plugin filenames should be unique within the namespace of whatever python modules are available (like the default standard library, and anything else installed). """ loaded_modules = [] abspath = os.path.abspath(path) # make plugins available for importing sys.path.append(abspath) # get a list of available plugins filenames = os.listdir(abspath) log.debug("Found {} files.".format(len(filenames))) # Remove ".py" from the name, use filename as module name Assume that all # filenames are in the form of "xyz.py". module_names = [filename.split(".")[0] for filename in filenames] # filter out dupes module_names = list(set(module_names)) # import each plugin for module_name in module_names: log.debug("Importing {}".format(module_name)) newmodule = importlib.import_module(module_name) loaded_modules.append(newmodule) return loaded_modules
def find_plugins(): """ Finds all subclasses of Plugin. Use this after loading the plugin files. """ log.debug("Searching for loaded plugin subclasses.") subclasses = Plugin.__subclasses__() subclasses = [subclass for subclass in subclasses if subclass is not Plugin] log.debug("Found {} subclasses.".format(len(subclasses))) return subclasses
def reload_process(self): """ Graceful reload if haproxy is already running. """ try: output = subprocess.check_output(["pidof", "haproxy"]) pids = output.strip().split(" ") except Exception as exc: command = "/usr/sbin/haproxy -f {{ dest }} -p /var/run/haproxy.pid" else: command = "/usr/sbin/haproxy -f {{ dest }} -p /var/run/haproxy.pid -sf xyz" command = command.replace("xyz", " ".join(pids)) command = command.replace("{{ dest }}", self.dest) log.debug("Running reload_cmd: {}".format(command)) args = shlex.split(command) process = subprocess.Popen(args)
def launch_plugins(plugins): """ Starts looping each plugin. Note that blocking code will block other plugins from executing. """ # gevent jobs = [] # plugins is just a list of classes for plugin in plugins: log.debug("Starting plugin: {}".format(plugin)) # make an instance of the class instance = plugin() # async start plugin loop newjob = gevent.spawn(instance.loop) jobs.append(newjob) return jobs