Example #1
0
def remove_plateform(name=None):
    """ Remove plateform file from plateform name passed by user.

        :param: name: plateform's name (same as plateform yaml file) passed by user
        :type: name: list of one str element
        :return: True if remove succeed
        :rtype: bool

        .. seealso:: heimdall.conf.hosts.getPlateformObject(), heimdall.core.yml.remove_yml()
    """
    from core.exceptions import PlateformTypeError
    from core.yml import remove_yml
    from core.plateform import Plateform
    from conf.hosts import getPlateformObject

    p = getPlateformObject(name[0])
    try:
        if isinstance(p, Plateform):
            remove_yml(p.path)
            print "[*] - %s successfully removed!" % name
            return True
        else:
            raise PlateformTypeError("Argument passed to %s must be a Platform object." % update_plateform.__name__)
    except PlateformTypeError as pte:
        print pte
        exit(pte.code)
Example #2
0
def update_host(**kwargs):
    """ Create a Host Object from information passed by user, and return a Platform Object who contain the new Host Object.

        Keyword Args:
            plateform (list of one str): host's plateform (same as plateform yaml file)
            name (list of one str): host's name passed by user
            desc (list of one str): host's description passed by user
            ip (list of one str): host's ip passed by user
            account (list of one str): host's superuser account used for configuration passed by user
            distribution (list of one str): host's distribution passed by user
            kernel_version (list of one str): host's kernel_version passed by user

        :return: updated Plateform Object with the new Host Object
        :rtype: Plateform Object

        ..seealso: heimdall.core.host.Host, heimdall.core.plateform.Plateform, heimdall.conf.hosts.getPlateformObject()
    """
    from conf.hosts import getPlateformObject
    from core.exceptions import EnvironmentDoesNotExist
    p = getPlateformObject(kwargs.get('plateform')[0])

    if not p.check_environment(kwargs.get('environment')[0]):
        raise EnvironmentDoesNotExist(
            'Environment %s in plateform %s does not exists!' %
            (kwargs.get('environment')[0], p.name), p.name)
    for host in p.environment[kwargs.get('environment')[0]]:
        if host.name == kwargs.get('name')[0]:
            if kwargs.get('newname'):
                host.name = kwargs.get('newname')[0]
            [
                host.__setattr__(k, v[0]) for k, v in kwargs.iteritems()
                if v and k != 'name' and k != 'newname'
            ]
    return p
Example #3
0
def move_host(oldplateform=None,
              environment=None,
              name=None,
              newplateform=None,
              newenvironment=None):
    """ Move host from one plateform to another.
        Retrieve old Platform Object and new Platform Object and update both with the Host Object filter by name.

        :param: oldplateform: old host's plateform (same as plateform yaml file).
        :param: name: host's name passed by user.
        :param: newplateform: new host's plateform (same as plateform yaml file).
        :type: oldplateform: list of one str
        :type: name: list of one str
        :type: newplateform: list of one str
        :return: Updated old Platform and new Platform
        :rtype: Old Platform Object, New Platform Object

        .. seealso:: heimdall.conf.hosts.getPlateformObject(), heimdall.core.plateform.Plateform
    """
    from conf.hosts import getPlateformObject
    from utils.plateform import get_new_host_id
    from core.exceptions import EnvironmentDoesNotExist
    old = getPlateformObject(oldplateform[0])
    new = getPlateformObject(newplateform[0])

    # Check if new environment exist
    try:
        if not new.check_environment(environment[0]):
            raise EnvironmentDoesNotExist(
                'Environment %s in plateform %s does not exists!' %
                (newenvironment[0], new.name), new.name)
    except EnvironmentDoesNotExist as ede:
        print ede
        exit(ede.code)

    for env, hosts in old.environment.iteritems():
        if environment[0] == env:  # found our environment
            for h in hosts:
                if h.name == name[0] or name[0] == -1:
                    old.remove_host(h)
                    h.plateform = new.name  # Setting new plateform name and environement
                    h.environment = newenvironment[0]
                    h.id = get_new_host_id(new.name, newenvironment[0])
                    new.add_host(h)

    return old, new
Example #4
0
def get_new_host_id(plateform=None, environment=None):
    """ Return a new id available in plateform for one environment

        :return: new id if none host exist, 1 otherwise
        :rtype: int

        .. seealso:: heimdall.conf.hosts.getAllPlateformObjects()
    """

    from conf.hosts import getPlateformObject

    p = getPlateformObject(plateform)

    if not p:
        return 1

    for env, hosts in p.environment.iteritems():
        if environment == env:  # found our environment
            return len(hosts) + 1
    else:
        return 1
Example #5
0
def add_host(**kwargs):
    """ Create a Host Object from information passed by user, and return a Platform Object who contain the new Host Object.

        Keyword Args:
            plateform (list of one str): host's plateform (same as plateform yaml file)
            environment (list of one str): host's environment
            name (list of one str): host's name passed by user
            desc (list of one str): host's description passed by user
            ip (list of one str): host's ip passed by user
            account (list of one str): host's superuser account used for configuration passed by user
            distribution (list of one str): host's distribution passed by user
            kernel_version (list of one str): host's kernel_version passed by user

        :return: updated Plateform Object with the new Host Object
        :rtype: Plateform Object

        ..seealso: heimdall.core.host.Host, heimdall.core.plateform.Plateform, heimdall.conf.hosts.getPlateformObject()
    """
    from core.host import Host
    from conf.hosts import getPlateformObject
    from core.exceptions import EnvironmentDoesNotExist

    newhost = dict(
        (k, ''.join(v)) for k, v in kwargs.iteritems() if v and len(v) == 1)
    host = Host(**newhost)
    p = getPlateformObject(host.plateform)

    try:
        if not p.check_environment(kwargs.get('environment')[0]):
            raise EnvironmentDoesNotExist(
                'Environment %s in plateform %s does not exists!' %
                (kwargs.get('environment')[0], p.name), p.name)
    except EnvironmentDoesNotExist as ede:
        print ede
        exit(ede.code)

    host.id = p.get_new_id(host.environment)
    p.add_host(host)

    return p
Example #6
0
def update_self_plateform(name=None, desc=None, newname=None):
    """ Update a Plateform Object from information passed by user, and return the new Plateform Object created.
        New object is write in corresponding yaml file.

        :param: name: plateform's name (same as plateform yaml file) passed by user
        :param: desc: plateform's description passed by user
        :param: newname: plateform's new name (plateform yaml file will be rename) passed by user
        :type: name: list of one str element
        :type: desc: list of one str element
        :type: newname: list of one str element
        :return: updated new plateform object
        :rtype: plateform object

        .. seealso:: heimdall.conf.hosts.getPlateformObject(), heimdall.core.yml.rename_yml(), update_plateform()
    """
    from core.exceptions import PlateformTypeError
    from core.plateform import Plateform
    from core.yml import rename_yml
    from conf.hosts import getPlateformObject

    p = getPlateformObject(name[0])
    oldpath = p.path

    if desc:
        p.desc = desc[0]
    if newname:
        p.name = newname[0]
        p.path = p.path.replace(name[0], newname[0])

    try:
        if isinstance(p, Plateform):
            rename_yml(oldpath=oldpath, newpath=p.path)
        else:
            raise PlateformTypeError("Argument passed to %s must be a Platform object." % update_plateform.__name__)
    except PlateformTypeError as pte:
        print pte
        exit(pte.code)
    update_plateform(p)
    return p
Example #7
0
def check_host(plateform=None, name=None, environment=None, check_exist=False):
    """ Check if host exist

        :param: plateform: host's plateform passed by user.
        :param: name: host's name passed by user.
        :param: environment: host's environment passed by user.
        :type: plateform: list of one str
        :type: name: list of one str
        :type: environment: list of one str
        :return: True if rule exist, raise exception otherwise.
        :rtype: bool

        .. seealso:: heimdall.conf.hosts.getPlateformObject()
    """
    from conf.hosts import getPlateformObject
    from core.exceptions import HostDoesNotExist, HostAlreadyExist

    if not plateform and not name:
        return False

    try:
        for plat in plateform:
            p = getPlateformObject(plat)
            if check_exist and p.check_host_exist(name[0], environment[0]):
                raise HostAlreadyExist("Host %s from %s already exist !" %
                                       (name[0], plat))
            if not check_exist and not p.check_host_exist(
                    name[0], environment[0]):
                raise HostDoesNotExist(
                    "Host %s from %s in env %s doesnt exist !" %
                    (name[0], plat, environment[0]))
        else:
            return True
    except HostDoesNotExist as hde:
        print hde
        exit(hde.code)
    except HostAlreadyExist as hae:
        print hae
        exit(hae.code)
Example #8
0
def get_plateform(plateform=None, environment=None, id=None):
    """ Take plateform information passed by user, and return list of plateforms filtered

        :param: plateform: plateform's name (same as plateform yaml file) passed by user
        :param: environment: plateform's environment passed by user
        :param: id: plateform's id passed by user
        :type: plateform: list of str
        :type: environment: list of str
        :type: id: list of int
        :return: list of plateform objects filtered
        :rtype: list of plateform objects

        .. seealso::: heimdall.conf.hosts.getPlateformObject(), heimdall.conf.hosts.getAllPlateformObjects()
    """

    plateforms_availables = []
    all_plateform = []

    from conf.hosts import getAllPlateformObjects, getPlateformObject
    if plateform:
        [all_plateform.append(getPlateformObject(p)) for p in plateform]
    else:
        all_plateform = getAllPlateformObjects()

    for p in all_plateform:
        if environment:  # if environment is set
            for env in environment:
                if id:  # if id is set, filter with environment AND id
                    [[plateforms_availables.append(p) for i in id if i == p.id] for e in p.environment if e == env]
                else:  # only filter with environment
                    [plateforms_availables.append(p) for e in p.environment if e == env]
        elif id:  # if environment is not set, but id is set filter only with id
            [plateforms_availables.append(p) for i in id if i == p.id]
        else:  # environment and id are not set, just return all plateforms
            plateforms_availables = all_plateform
    return plateforms_availables
Example #9
0
def remove_host(plateform=None, name=None, environment=None):
    """ Remove Host Object from Platform Object attribute hosts and return updated Platform Object.

        :param: plateform: host's plateform (same as type yaml file) passed by user
        :param: name: host's name passed by user
        :param: name: host's environment passed by user
        :type: plateform: list of one str
        :type: name: list of one str
        :type: environment: list of one str
        :return: Updated Plateform
        :rtype: Plateform Object

        .. seealso:: heimdall.conf.hosts.getPlateformObject(), heimdall.core.plateform.Plateform
    """
    from conf.hosts import getPlateformObject
    from core.exceptions import EnvironmentDoesNotExist

    p = getPlateformObject(plateform[0])
    try:
        if not p.check_environment(environment[0]):
            raise EnvironmentDoesNotExist(
                'Environment %s in plateform %s does not exists!' %
                (environment[0], p.name), p.name)
    except EnvironmentDoesNotExist as ede:
        print ede
        exit(ede.code)

    if name[0] == -1:  # remove all
        p.environment[environment[0]] = []
    else:
        [
            p.remove_host(host) for host in p.environment[environment[0]]
            for n in name if host.name == n
        ]

    return p