Esempio n. 1
0
def getRuleObject(type=None, id=None):
    """ Open and read data from type name passed by user, then create Rule Object from data filtered by id.

        :return: Rule Object created from data read in heimdall/conf/rules/type.yml.
        :rtype: Rule Object

        ..seealso:: heimdall.core.rule.Rule
    """
    from core.rule import Rule
    from core.exceptions import RuleDoesNotExist
    from core.yml import open_yml

    filepath = path.join(__tdir__, type + '.yml')
    type_data = open_yml(path=filepath)

    try:
        if type_data:
            for r in type_data['rules']:
                if str(id) == str(r.get('id')):
                    r['type'] = type_data['name']
                    return Rule(**r)
            else:
                raise RuleDoesNotExist(
                    'Rule from type %s with id %s Does not exist!' %
                    (type_data.get('name'), id))
    except RuleDoesNotExist as rde:
        print rde
        exit(rde.code)
Esempio n. 2
0
def getTypeObject(name=None, id=None):
    """ Open and read data from type name passed by user, then create Type Object from data
        Rule Objects are also created and added to Type Object.

        :return: Type Object created from data read in heimdall/conf/rules/type.yml.
        :rtype: Type Object

        ..seealso:: heimdall.core.type.Type, heimdall.core.rule.Rule
    """
    from core.type import Type
    from core.rule import Rule
    from core.yml import open_yml

    filepath = path.join(__tdir__, name + '.yml')
    type_data = open_yml(path=filepath)
    if type_data:
        type_data['path'] = filepath
        type = Type(**type_data)
        for r in type_data['rules']:
            r['type'] = type.name
            if id and id != -1:
                for i in id:
                    if str(i) == str(r['id']):
                        type.rules.append(Rule(**r))
                        type.all = False
            else:
                type.rules.append(Rule(**r))
                type.all = True
        return type
Esempio n. 3
0
def getHostObject(plateform=None, name=None):
    """ Open and read data from plateform name passed by user, then create Host Object from data filtered by name.

        :return: Host Object created from data read in heimdall/conf/host/plateform.yml.
        :rtype: Host Object

        ..seealso:: heimdall.core.host.Host
    """
    from core.host import Host
    from core.yml import open_yml
    from core.exceptions import HostDoesNotExist

    filepath = path.join(__pdir__, plateform + '.yml')
    plateform_data = open_yml(path=filepath)
    try:
        if plateform_data:
            for e in plateform_data['environment']:
                for h in plateform_data['environment'][e]:
                    if name == h.get('name'):
                        h['plateform'] = plateform_data['name']
                        h['environment'] = e
                        return Host(**h)
                else:
                    raise HostDoesNotExist(
                        "Host %s in plateform %s doesnt exists" %
                        (name, plateform))
    except HostDoesNotExist as hde:
        print hde
        exit(hde.code)
Esempio n. 4
0
def getAllTypesObjects():
    """ Open and read data from heimdall/conf/rules/type.yaml, then create Type Objects from data.
        Rule Objects are also created and added to Type Objects.

        :return: list of all Type Objects available.
        :rtype: list of Type Objects

        ..seealso:: heimdall.core.type.Type, heimdall.core.rule.Rule
    """
    from core.type import Type
    from core.rule import Rule
    from core.yml import open_yml

    type_availables = []
    for type in __typesfiles__:
        filepath = path.join(__tdir__, type)
        type_data = open_yml(path=filepath)
        if type_data:
            type_data['path'] = filepath
            type = Type(**type_data)
            for r in type_data['rules']:
                r['type'] = type.name
                type.rules.append(Rule(**r))
                type.all = True
            type_availables.append(type)
    return type_availables
Esempio n. 5
0
def getAllPlateformObjects():
    """ Open and read data from heimdall/conf/host/plateform.yaml, then create Plateform Objects from data.
        Host Objects are also created and added to Plateform Objects.

        :return: list of all Plateform Objects available.
        :rtype: list of Plateform Objects

        ..seealso:: heimdall.core.plateform.Plateform, heimdall.core.host.Host
    """
    from core.plateform import Plateform
    from core.host import Host
    from core.yml import open_yml

    plateform_availables = []
    for plateform in __plateformsfiles__:
        filepath = path.join(__pdir__, plateform)
        plateform_data = open_yml(path=filepath)
        if plateform_data:
            plateform_data['path'] = filepath
            for env, hosts in plateform_data['environment'].iteritems():
                environment = []
                for h in hosts:
                    h['environment'] = env
                    environment.append(Host(**h))
                plateform_data['environment'][env] = environment
            plateform = Plateform(**plateform_data)
            plateform_availables.append(plateform)
    return plateform_availables
Esempio n. 6
0
def getAllProfilesObjects():
    """ Open and read data from heimdall/conf/profiles/profile.yml, then create Profile Objects from data.
        Type Objects are also created and added to Profile Objects.

        :return: list of all Profile Objects available.
        :rtype: list of Profile Objects

        ..seealso:: heimdall.core.profile.Profile, heimdall.conf.rules.getTypeObject()
    """
    from core.profile import Profile
    from conf.rules import getTypeObject
    from core.yml import open_yml

    profile_availables = []
    for profilesfiles in __profilesfiles__:
        filepath = path.join(__pdir__, profilesfiles)
        profile_data = open_yml(path=filepath)

        if profile_data:
            profile_data['path'] = filepath
            profile = Profile(**profile_data)
            [
                profile.types.append(getTypeObject(t, i))
                for t, i in profile_data['types'].iteritems()
            ]
            profile_availables.append(profile)

    return profile_availables
Esempio n. 7
0
def getPlateformObject(name=None):
    from core.plateform import Plateform
    from core.host import Host
    from core.yml import open_yml

    filepath = path.join(__pdir__, name + '.yml')
    plateform_data = open_yml(path=filepath)
    plateform = []
    if plateform_data:
        plateform_data['path'] = filepath
        plateform = Plateform(**plateform_data)
        for env, hosts in plateform.environment.iteritems():
            environment = []
            for h in hosts:
                h['environment'] = env
                environment.append(Host(**h))
            plateform.environment[env] = environment
    return plateform
Esempio n. 8
0
def getAllRulesObjects():
    """ Open and read data from heimdall/conf/rules/type.yaml, then create Rule Objects from data.

        :return: list of all Rule Objects available.
        :rtype: list of Rule Objects

        ..seealso:: heimdall.core.rule.Rule
    """
    from core.rule import Rule
    from core.yml import open_yml

    rules_availables = []
    for type in __typesfiles__:
        filepath = path.join(__tdir__, type)
        type_data = open_yml(path=filepath)
        if type_data:
            for r in type_data['rules']:
                r['type'] = type_data['name']  # Adding type for format_output
                rules_availables.append(Rule(**r))
    return rules_availables
Esempio n. 9
0
def getAllHostsObjects():
    """ Open and read data from heimdall/conf/hosts/plateform.yaml, then create Host Objects from data.

        :return: list of all Host Objects available.
        :rtype: list of Host Objects

        ..seealso:: heimdall.core.host.Host
    """
    from core.host import Host
    from core.yml import open_yml

    hosts_availables = []
    for plateform in __plateformsfiles__:
        filepath = path.join(__pdir__, plateform)
        hosts_data = open_yml(path=filepath)
        if hosts_data:
            for env, hosts in hosts_data['environment'].iteritems():
                for h in hosts:
                    h['environment'] = env
                    h['plateform'] = hosts_data['name']
                    hosts_availables.append(Host(**h))
    return hosts_availables
Esempio n. 10
0
def getProfileObject(name=None):
    """ Open and read data from profile name passed by user, then create Profile Object from data
        Type Objects are also created and added to Profile Object.

        :return: Profile Object created from data read in heimdall/conf/profiles/profile.yml.
        :rtype: Profile Object

        ..seealso:: heimdall.core.profile.Profile, heimdall.conf.rules.getTypeObject()
    """
    from core.profile import Profile
    from conf.rules import getTypeObject
    from core.yml import open_yml

    filepath = path.join(__pdir__, name + '.yml')
    profile_data = open_yml(path=filepath)

    if profile_data:
        profile_data['path'] = filepath
        profile = Profile(**profile_data)
        [
            profile.types.append(getTypeObject(t, i))
            for t, i in profile_data['types'].iteritems()
        ]
        return profile