Beispiel #1
0
def get_metadata(plugin):
    """
    Gets metadata for plugin
    :param plugin: plugin object
    :return: metadata dict for that plugin
    """

    path = citellus.regexpfile(filename=plugin['plugin'],
                               regexp='\A# path:')[7:].strip()
    citellus.LOG.debug('IRANZO')
    citellus.LOG.debug(path)
    path = path.replace('${CITELLUS_ROOT}', '')
    citellus.LOG.debug(path)

    metadata = {
        'description':
        citellus.regexpfile(filename=plugin['plugin'],
                            regexp='\A# description:')[14:].strip(),
        'long_name':
        citellus.regexpfile(filename=plugin['plugin'],
                            regexp='\A# long_name:')[12:].strip(),
        'bugzilla':
        citellus.regexpfile(filename=plugin['plugin'],
                            regexp='\A# bugzilla:')[11:].strip(),
        'priority':
        int(
            citellus.regexpfile(filename=plugin['plugin'],
                                regexp='\A# priority:')[11:].strip() or 0),
        'path':
        path
    }
    return metadata
Beispiel #2
0
    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 citellus.regexpfile(
                filename=plugin["plugin"], regexp=".*common-functions"
            ):
                nonsourcing.append(plugin["plugin"])

        commonfunctions = []

        for script in citellus.findplugins(
            folders=[os.path.join(citellus.citellusdir, "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 citellus.regexpfile(filename=plugin, regexp=".*%s" % func):
                    usingcf.append(plugin)

        assert sorted(set(usingcf)) == []
Beispiel #3
0
def get_metadata(plugin):
    """
    Gets meadata for plugin
    :param plugin: plugin object
    :return: metadata dict for that plugin
    """

    with open(plugin['plugin'], 'r') as stream:
        try:
            doc = (yaml.load(stream))
        except:
            doc = ""

    try:
        description = doc[0]['vars']['metadata']['description']
    except:
        description = ""

    metadata = {
        'description':
        description,
        'long_name':
        citellus.regexpfile(filename=plugin['plugin'],
                            regexp='\A# long_name:')[12:].strip(),
        'bugzilla':
        citellus.regexpfile(filename=plugin['plugin'],
                            regexp='\A# bugzilla:')[11:].strip(),
        'priority':
        int(
            citellus.regexpfile(filename=plugin['plugin'],
                                regexp='\A# priority:')[11:].strip() or 0)
    }

    return metadata
Beispiel #4
0
    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 citellus.regexpfile(filename=plugin['plugin'],
                                       regexp='.*common-functions'):
                nonsourcing.append(plugin['plugin'])

        commonfunctions = []

        for script in citellus.findplugins(
                folders=[os.path.join(citellus.citellusdir, '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 citellus.regexpfile(filename=plugin, regexp='.*%s' % func):
                    usingcf.append(plugin)

        assert sorted(set(usingcf)) == []
Beispiel #5
0
def get_metadata(plugin):
    """
    Gets metadata for plugin
    :param plugin: plugin object
    :return: metadata dict for that plugin
    """

    metadata = {
        'description':
        citellus.regexpfile(filename=plugin['plugin'],
                            regexp='\A# description:')[14:].strip(),
        'long_name':
        citellus.regexpfile(filename=plugin['plugin'],
                            regexp='\A# long_name:')[12:].strip(),
        'bugzilla':
        citellus.regexpfile(filename=plugin['plugin'],
                            regexp='\A# bugzilla:')[11:].strip(),
        'priority':
        int(
            citellus.regexpfile(filename=plugin['plugin'],
                                regexp='\A# priority:')[11:].strip() or 0),
        'path':
        citellus.regexpfile(filename=plugin['plugin'],
                            regexp='\A# path:')[8:].strip() or 0
    }
    return metadata
Beispiel #6
0
def run(plugin):  # do not edit this line
    """
    Executes plugin
    :param plugin: plugin dictionary
    :return: returncode, out, err
    """

    ansible = citellus.which("ansible-playbook")
    if not ansible:
        return citellus.RC_SKIPPED, '', _('ansible-playbook support not found')

    if citellus.CITELLUS_LIVE == 0 and citellus.regexpfile(
            filename=plugin['plugin'], regexp="CITELLUS_ROOT"):
        # We're running in snapshoot and playbook has CITELLUS_ROOT
        skipped = 0
    elif citellus.CITELLUS_LIVE == 1:
        if citellus.regexpfile(
                filename=plugin['plugin'],
                regexp="CITELLUS_HYBRID") or not citellus.regexpfile(
                    filename=plugin['plugin'], regexp="CITELLUS_ROOT"):
            # We're running in Live mode and either plugin supports HYBRID or has no CITELLUS_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 citellus.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 = citellus.execonshell(filename=command)

    # Do formatting of results to remove ansible-playbook -i localhost, and adjust return codes to citellus standards
    if returncode == 2:
        returncode = citellus.RC_FAILED
    elif returncode == 0:
        returncode = citellus.RC_OKAY

    # Convert stdout to stderr for citellus 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
Beispiel #7
0
def run(data, quiet=False):  # 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)

    # Find available profile definitions
    profiles = citellus.findplugins(folders=[pluginsdir], executables=False, fileextension='.txt')
    for item in profiles:
        uid = citellus.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": int(os.environ['RC_OKAY']),
                                "err": "",
                                "out": ""},
                     "time": 0,
                     "backend": "profile",
                     "id": uid,
                     "subcategory": subcategory}

        metadata = {'description': citellus.regexpfile(filename=plugin['plugin'], regexp='\A# description:')[14:].strip(),
                    'long_name': citellus.regexpfile(filename=plugin['plugin'], regexp='\A# long_name:')[12:].strip(),
                    'bugzilla': citellus.regexpfile(filename=plugin['plugin'], regexp='\A# bugzilla:')[11:].strip(),
                    'priority': int(citellus.regexpfile(filename=plugin['plugin'], regexp='\A# priority:')[11:].strip() or 0)}
        data[uid].update(metadata)

        # Start asembling data for the plugins relevant for profile
        data[uid]['result']['err'] = ''
        ids = plugidsforprofile(profile=profile, plugins=plugins)
        for id in ids:
            data[uid]['result']['err'] = data[uid]['result']['err'] + "\n" + "%s" % {'plugin': data[id]['plugin'].replace(os.path.join(citellus.citellusdir, 'plugins'), ''), 'err': data[id]['result']['err'].strip(), 'rc': data[id]['result']['rc']}

        data[uid]['components'] = ids

    return data
def run(plugin):  # do not edit this line
    """
    Executes plugin
    : return: returncode, out, err
    """

    filename = plugin['path']

    if '${CITELLUS_ROOT}' in filename:
        filename = filename.replace('${CITELLUS_ROOT}',
                                    os.environ['CITELLUS_ROOT'])

    pattern = plugin['pattern']
    reason = plugin['reason']

    out = ''
    err = ''
    returncode = citellus.RC_FAILED

    if os.access(filename, os.R_OK) and os.path.isfile(filename):
        if citellus.regexpfile(filename=filename, regexp=pattern):
            err = reason
            returncode = citellus.RC_FAILED
        else:
            returncode = citellus.RC_OKAY
    else:
        returncode = citellus.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"]

    if "${CITELLUS_ROOT}" in filename:
        filename = filename.replace("${CITELLUS_ROOT}", os.environ["CITELLUS_ROOT"])

    pattern = plugin["pattern"]
    reason = plugin["reason"]

    out = ""
    err = ""
    returncode = citellus.RC_FAILED

    if os.access(filename, os.R_OK) and os.path.isfile(filename):
        if citellus.regexpfile(filename=filename, regexp=pattern):
            err = reason
            returncode = citellus.RC_FAILED
        else:
            returncode = citellus.RC_OKAY
    else:
        returncode = citellus.RC_SKIPPED
        err = "File %s is not accessible in read mode" % filename

    return returncode, out, err
Beispiel #10
0
 def test_plugins_no_echo_RC(self):
     for plugin in citplugs:
         result = citellus.regexpfile(filename=plugin["plugin"],
                                      regexp=r".*echo \$RC_.*")
         if result == "":
             print(plugin["plugin"])
             assert result == ""
Beispiel #11
0
 def test_plugins_no_echo_RC(self):
     for plugin in citplugs:
         result = citellus.regexpfile(filename=plugin['plugin'],
                                      regexp='.*echo \$RC_.*')
         if result == '':
             print(plugin['plugin'])
             assert result == ''
Beispiel #12
0
def run(plugin):  # do not edit this line
    """
    Executes plugin
    :return: returncode, out, err
    """
    filename = plugin["path"]

    skipped = 0
    if os.environ["CITELLUS_LIVE"] == 0 and citellus.regexpfile(
            filename=filename, regexp="CITELLUS_ROOT"):
        # We're running in snapshoot and faraday file has CITELLUS_ROOT
        skipped = 0
    else:
        if os.environ["CITELLUS_LIVE"] == 1:
            if citellus.regexpfile(
                    filename=plugin["plugin"],
                    regexp="CITELLUS_HYBRID") or not citellus.regexpfile(
                        filename=filename, regexp="CITELLUS_ROOT"):
                # We're running in Live mode and either plugin supports HYBRID or has no CITELLUS_ROOT
                skipped = 0
            else:
                # We do not satisfy conditions, exit early
                skipped = 1

    if skipped == 1:
        return (
            citellus.RC_SKIPPED,
            "",
            _("Plugin does not satisfy conditions for running"),
        )

    if "${CITELLUS_ROOT}" in filename:
        filename = filename.replace("${CITELLUS_ROOT}",
                                    os.environ["CITELLUS_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 = citellus.RC_OKAY
    else:
        returncode = citellus.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(citellus.citellusdir, 'plugins', 'core')]
        pluginscit = []
        for plugin in citellus.findplugins(folders=pluginpath):
            filename = plugin['plugin']
            regexp = 'if \( '
            if citellus.regexpfile(filename=filename, regexp=regexp):
                pluginscit.append(filename)

        assert len(pluginscit) == 0
Beispiel #14
0
def run(plugin):  # do not edit this line
    """
    Executes plugin
    :return: returncode, out, err
    """
    filename = plugin['path']

    skipped = 0
    if os.environ['CITELLUS_LIVE'] == 0 and citellus.regexpfile(
            filename=filename, regexp="CITELLUS_ROOT"):
        # We're running in snapshoot and faraday file has CITELLUS_ROOT
        skipped = 0
    else:
        if os.environ['CITELLUS_LIVE'] == 1:
            if citellus.regexpfile(
                    filename=plugin['plugin'],
                    regexp="CITELLUS_HYBRID") or not citellus.regexpfile(
                        filename=filename, regexp="CITELLUS_ROOT"):
                # We're running in Live mode and either plugin supports HYBRID or has no CITELLUS_ROOT
                skipped = 0
            else:
                # We do not satisfy conditions, exit early
                skipped = 1

    if skipped == 1:
        return citellus.RC_SKIPPED, '', _(
            'Plugin does not satisfy conditions for running')

    if '${CITELLUS_ROOT}' in filename:
        filename = filename.replace('${CITELLUS_ROOT}',
                                    os.environ['CITELLUS_ROOT'])

    if os.access(filename, os.R_OK):
        # We can read the file, so let's calculate md5sum
        out = ''
        err = open(filename, 'rb').read()
        returncode = citellus.RC_OKAY
    else:
        returncode = citellus.RC_SKIPPED
        out = ''
        err = 'File %s is not accessible in read mode' % filename

    return returncode, out, err
Beispiel #15
0
def get_metadata(plugin):
    """
    Gets metadata for plugin
    :param plugin: plugin object
    :return: metadata dict for that plugin
    """

    path = citellus.regexpfile(filename=plugin['plugin'], regexp='\A# path:')[7:].strip()
    path = path.replace('${CITELLUS_ROOT}', '')

    subcategory = os.path.split(plugin['plugin'])[0].replace(pluginsdir, '')[1:]
    metadata = {'description': citellus.regexpfile(filename=plugin['plugin'], regexp='\A# description:')[14:].strip(),
                'long_name': citellus.regexpfile(filename=plugin['plugin'], regexp='\A# long_name:')[12:].strip(),
                'bugzilla': citellus.regexpfile(filename=plugin['plugin'], regexp='\A# bugzilla:')[11:].strip(),
                'priority': int(citellus.regexpfile(filename=plugin['plugin'], regexp='\A# priority:')[11:].strip() or 0),
                'path': path,
                'subcategory': subcategory,
                'category': os.path.normpath(subcategory).split(os.sep)[1] or ''}
    return metadata
    def test_plugins_have_dual_brackets_for_if(self):
        pluginpath = [os.path.join(citellus.citellusdir, "plugins", "core")]
        pluginscit = []
        for plugin in citellus.findplugins(folders=pluginpath):
            filename = plugin["plugin"]
            regexp = r"if \[ "
            if citellus.regexpfile(filename=filename, regexp=regexp):
                pluginscit.append(filename)

        assert len(pluginscit) == 0
Beispiel #17
0
def run(data, quiet=False):  # 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)

    # Find available profile definitions
    profiles = citellus.findplugins(folders=[pluginsdir],
                                    executables=False,
                                    fileextension=".txt")
    for item in profiles:
        uid = citellus.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":
            citellus.regexpfile(filename=plugin["plugin"],
                                regexp=r"\A# description:")[14:].strip(),
            "long_name":
            citellus.regexpfile(filename=plugin["plugin"],
                                regexp=r"\A# long_name:")[12:].strip(),
            "bugzilla":
            citellus.regexpfile(filename=plugin["plugin"],
                                regexp=r"\A# bugzilla:")[11:].strip(),
            "priority":
            int(
                citellus.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(citellus.citellusdir, "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