def test_ut_sourced_if_used(self): # Check list of plugins for regexp sourcing common functions and skip them nonsourcing = [] for plugin in plugins: if not risu.regexpfile(filename=plugin["plugin"], regexp=".*common-functions"): nonsourcing.append(plugin["plugin"]) commonfunctions = [] for script in risu.findplugins( folders=[os.path.join(risu.risudir, "common.d")], fileextension=".sh", ): filename = script["plugin"] with open(filename, "r") as f: for line in f: find = re.match("^(([a-z]+_+)+[a-z]*)", line) if find and find.groups()[0] != "": commonfunctions.append(find.groups()[0]) usingcf = [] for plugin in nonsourcing: for func in commonfunctions: if risu.regexpfile(filename=plugin, regexp=".*%s" % func): usingcf.append(plugin) assert sorted(set(usingcf)) == []
def run(plugin): # do not edit this line """ Executes plugin : return: returncode, out, err """ filename = plugin["path"] if "${RISU_ROOT}" in filename: filename = filename.replace("${RISU_ROOT}", os.environ["RISU_ROOT"]) pattern = plugin["pattern"] reason = plugin["reason"] out = "" err = "" returncode = risu.RC_FAILED if os.access(filename, os.R_OK) and os.path.isfile(filename): if risu.regexpfile(filename=filename, regexp=pattern): err = reason returncode = risu.RC_FAILED else: returncode = risu.RC_OKAY else: returncode = risu.RC_SKIPPED err = "File %s is not accessible in read mode" % filename return returncode, out, err
def run(plugin): # do not edit this line """ Executes plugin :return: returncode, out, err """ filename = plugin["path"] skipped = 0 if os.environ["RISU_LIVE"] == 0 and risu.regexpfile( filename=filename, regexp="RISU_ROOT" ): # We're running in snapshoot and faraday file has RISU_ROOT skipped = 0 else: if os.environ["RISU_LIVE"] == 1: if risu.regexpfile( filename=plugin["plugin"], regexp="RISU_HYBRID" ) or not risu.regexpfile(filename=filename, regexp="RISU_ROOT"): # We're running in Live mode and either plugin supports HYBRID or has no RISU_ROOT skipped = 0 else: # We do not satisfy conditions, exit early skipped = 1 if skipped == 1: return ( risu.RC_SKIPPED, "", _("Plugin does not satisfy conditions for running"), ) if "${RISU_ROOT}" in filename: filename = filename.replace("${RISU_ROOT}", os.environ["RISU_ROOT"]) if os.access(filename, os.R_OK): # We can read the file, so let's calculate md5sum out = "" err = hashlib.sha512(open(filename, "rb").read()).hexdigest() returncode = risu.RC_OKAY else: returncode = risu.RC_SKIPPED out = "" err = "File %s is not accessible in read mode" % filename return returncode, out, err
def test_plugins_have_dual_parenthesis_for_if(self): pluginpath = [os.path.join(risu.risudir, "plugins", "core")] pluginscit = [] for plugin in risu.findplugins(folders=pluginpath): filename = plugin["plugin"] regexp = r"if \( " if risu.regexpfile(filename=filename, regexp=regexp): pluginscit.append(filename) assert len(pluginscit) == 0
def run(plugin): # do not edit this line """ Executes plugin :param plugin: plugin dictionary :return: returncode, out, err """ ansible = risu.which("ansible-playbook") if not ansible: return risu.RC_SKIPPED, "", _("ansible-playbook support not found") if risu.RISU_LIVE == 0 and risu.regexpfile(filename=plugin["plugin"], regexp="RISU_ROOT"): # We're running in snapshoot and playbook has RISU_ROOT skipped = 0 elif risu.RISU_LIVE == 1: if risu.regexpfile(filename=plugin["plugin"], regexp="RISU_HYBRID") or not risu.regexpfile( filename=plugin["plugin"], regexp="RISU_ROOT"): # We're running in Live mode and either plugin supports HYBRID or has no RISU_ROOT skipped = 0 else: # We do not satisfy conditions, exit early skipped = 1 else: # We do not satisfy conditions, exit early skipped = 1 if skipped == 1: return ( risu.RC_SKIPPED, "", _("Plugin does not satisfy conditions for running"), ) command = "%s -i localhost, --connection=local %s" % (ansible, plugin["plugin"]) # Disable Ansible retry files creation: os.environ["ANSIBLE_RETRY_FILES_ENABLED"] = "0" # Call exec to run playbook returncode, out, err = risu.execonshell(filename=command) # Do formatting of results to remove ansible-playbook -i localhost, and adjust return codes to risu standards if returncode == 2: returncode = risu.RC_FAILED elif returncode == 0: returncode = risu.RC_OKAY # Convert stdout to stderr for risu handling err = out out = "" # Rewrite error messages to not contain all playbook execution but just the actual error if "FAILED!" in err: start = err.find("FAILED!", 0) + 11 end = err.find("PLAY RECAP", 0) - 10 newtext = err[start:end] err = newtext return returncode, out, err
def run(data, quiet=False, options=None): # do not edit this line """ Executes plugin :param quiet: be more silent on returned information :param data: data to process :return: returncode, out, err """ # prefill plugins we had used: plugins = [] for item in data: plugin = {"plugin": data[item]["plugin"], "id": data[item]["id"]} plugins.append(plugin) if options and options.extraplugintree: folders = [ pluginsdir, os.path.join(options.extraplugintree, extension) ] else: folders = [pluginsdir] # Find available profile definitions try: include = options.include except: include = None try: exclude = options.exclude except: exclude = None profiles = risu.findplugins( folders=folders, executables=False, fileextension=".txt", include=include, exclude=exclude, ) for item in profiles: uid = risu.getids(plugins=[item])[0] profile = item["plugin"] plugin = dict(item) # Precreate storage for this profile name = "Profiles: %s" % os.path.basename( os.path.splitext(profile.replace(pluginsdir, ""))[0]) subcategory = "" category = name data[uid] = { "category": category, "hash": item["hash"], "plugin": item["plugin"], "name": name, "result": { "rc": 0, "err": "", "out": "" }, "time": 0, "backend": "profile", "id": uid, "subcategory": subcategory, } metadata = { "description": risu.regexpfile(filename=plugin["plugin"], regexp=r"\A# description:")[14:].strip(), "long_name": risu.regexpfile(filename=plugin["plugin"], regexp=r"\A# long_name:")[12:].strip(), "bugzilla": risu.regexpfile(filename=plugin["plugin"], regexp=r"\A# bugzilla:")[11:].strip(), "priority": int( risu.regexpfile(filename=plugin["plugin"], regexp=r"\A# priority:")[11:].strip() or 0), } data[uid].update(metadata) # start with OK status okay = int(os.environ["RC_OKAY"]) failed = int(os.environ["RC_FAILED"]) skipped = int(os.environ["RC_SKIPPED"]) info = int(os.environ["RC_INFO"]) # Start asembling data for the plugins relevant for profile data[uid]["result"]["err"] = "" ids = plugidsforprofile(profile=profile, plugins=plugins) new_results = [] overallitems = [] for id in ids: if id in data: if "sysinfo" in name and data[id]["result"]["rc"] == skipped: # Do nothing as we don't want to show skipped in sysinfo pass else: new_results.append({ "plugin_id": id, "plugin": data[id]["plugin"].replace( os.path.join(risu.risudir, "plugins"), ""), "err": data[id]["result"]["err"].strip(), "rc": data[id]["result"]["rc"], }) overallitems.append(data[id]["result"]["rc"]) if "sysinfo" in name: if okay in overallitems or failed in overallitems or info in overallitems: overall = info else: # No plugins matched, so skip it overall = skipped else: if failed in overallitems: overall = failed elif info in overallitems: overall = info elif skipped in overallitems: overall = skipped else: overall = okay data[uid]["result"]["err"] = json.dumps(new_results) data[uid]["components"] = ids data[uid]["result"]["rc"] = overall return data