Example #1
0
def del_machine(deleter, machine_id, **kwargs):
    """Delete a machine."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, deleter, permission.PERMISSION_DEL_MACHINE)
        machine = utils.get_db_object(session, models.Switch, id=machine_id)
        utils.del_db_object(session, machine)
        return machine.to_dict()
Example #2
0
def del_cluster(deleter, cluster_id, **kwargs):
    """Delete a cluster."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, deleter, permission.PERMISSION_DEL_CLUSTER)
        cluster = utils.get_db_object(
            session, models.Cluster, id=cluster_id
        )
        utils.del_db_object(session, cluster)
        return cluster.to_dict()
Example #3
0
def del_network(deleter, subnet_id, **kwargs):
    """Delete a subnet."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, deleter, permission.PERMISSION_DEL_NETWORK)
        network = utils.get_db_object(
            session, models.Network, id=subnet_id
        )
        utils.del_db_object(session, network)
        return network.to_dict()
Example #4
0
def del_machine(machine_id, user=None, session=None, **kwargs):
    """Delete a machine."""
    machine = utils.get_db_object(session, models.Machine, id=machine_id)
    if machine.host:
        host = machine.host
        raise exception.NotAcceptable("machine %s has host %s on it" % (machine.mac, host.name))
    return utils.del_db_object(session, machine)
Example #5
0
def del_switch(switch_id, user=None, session=None, **kwargs):
    """Delete a switch.

    If switch is not the default switch, and the machine under this switch
    is only connected to this switch, the machine will be moved to connect
    to default switch. Otherwise we can only simply delete the switch
    machine. The purpose here to make sure there is no machine not
    connecting to any switch.
    """
    # TODO(xicheng): Simplify the logic if the default switch feature
    # can be deprecated.
    switch = _get_switch(switch_id, session=session)
    default_switch = _get_switch_by_ip(
        setting.DEFAULT_SWITCH_IP, session=session
    )
    if switch.id != default_switch.id:
        for switch_machine in switch.switch_machines:
            machine = switch_machine.machine
            if len(machine.switch_machines) <= 1:
                utils.add_db_object(
                    session, models.SwitchMachine,
                    False,
                    default_switch.id, machine.id,
                    port=switch_machine.port
                )
    return utils.del_db_object(session, switch)
Example #6
0
def del_permission(user_id, permission_id, user=None, session=None, **kwargs):
    """Delete a permission from a user."""
    user_permission = _get_permission(user_id,
                                      permission_id,
                                      session=session,
                                      **kwargs)
    return utils.del_db_object(session, user_permission)
Example #7
0
def del_hostnetwork(host_network_id, user=None, session=None, **kwargs):
    """Delete a host network."""
    host_network = utils.get_db_object(
        session, models.HostNetwork, id=host_network_id
    )
    is_host_editable(session, host_network.host, user)
    return utils.del_db_object(session, host_network)
Example #8
0
def del_switchmachine(session, deleter, switch_machine_id, **kwargs):
    """Delete switch machines."""
    switch_machine = utils.get_db_object(
        session, models.SwitchMachine,
        switch_machine_id=switch_machine_id
    )
    return utils.del_db_object(session, switch_machine)
Example #9
0
def del_hostnetwork(host_network_id, user=None, session=None, **kwargs):
    """Delete a host network by host network id."""
    host_network = _get_hostnetwork(
        host_network_id, session=session
    )
    check_host_editable(host_network.host, user=user)
    return utils.del_db_object(session, host_network)
Example #10
0
def del_subnet(subnet_id, user=None, session=None, **kwargs):
    """Delete a subnet."""
    subnet = _get_subnet(
        subnet_id, session=session
    )
    _check_subnet_deletable(subnet)
    return utils.del_db_object(session, subnet)
Example #11
0
def del_permission(user_id, permission_id, user=None, session=None, **kwargs):
    """Delete a permission from a user."""
    user_permission = _get_permission(
        user_id, permission_id,
        session=session, **kwargs
    )
    return utils.del_db_object(session, user_permission)
Example #12
0
def del_switch(switch_id, user=None, session=None, **kwargs):
    """Delete a switch.

    If switch is not the default switch, and the machine under this switch
    is only connected to this switch, the machine will be moved to connect
    to default switch. Otherwise we can only simply delete the switch
    machine. The purpose here to make sure there is no machine not
    connecting to any switch.
    """
    # TODO(xicheng): Simplify the logic if the default switch feature
    # can be deprecated.
    switch = _get_switch(switch_id, session=session)
    default_switch = _get_switch_by_ip(setting.DEFAULT_SWITCH_IP,
                                       session=session)
    if switch.id != default_switch.id:
        for switch_machine in switch.switch_machines:
            machine = switch_machine.machine
            if len(machine.switch_machines) <= 1:
                utils.add_db_object(session,
                                    models.SwitchMachine,
                                    False,
                                    default_switch.id,
                                    machine.id,
                                    port=switch_machine.port)
    return utils.del_db_object(session, switch)
Example #13
0
 def test_del_db_object(self):
     with self.assertRaises(exception.RecordNotExists):
         with database.session() as session:
             db_obj = utils.get_db_object(
                 session,
                 models.Permission,
                 id=1
             )
             utils.del_db_object(
                 session,
                 db_obj
             )
             utils.get_db_object(
                 session,
                 models.Permission,
                 id=1
             )
Example #14
0
def del_permission(user_id, permission_id, user=None, session=None, **kwargs):
    """Delete a specific user permission."""
    user_permission = utils.get_db_object(
        session, models.UserPermission,
        user_id=user_id, permission_id=permission_id,
        **kwargs
    )
    return utils.del_db_object(session, user_permission)
Example #15
0
def del_host(session, deleter, host_id, **kwargs):
    """Delete a host."""
    host = utils.get_db_object(
        session, models.Host, id=host_id
    )
    is_host_editable(
        session, host, deleter,
        reinstall_os_set=True
    )
    return utils.del_db_object(session, host)
def delete_reports(user, cluster_id, name=None, session=None):
    if not name:
        report = utils.get_db_object(
            session, models.HealthCheckReport, cluster_id=cluster_id, name=name
        )
        return utils.del_db_object(session, report)

    return utils.del_db_objects(
        session, models.HealthCheckReport, cluster_id=cluster_id
    )
Example #17
0
def del_host(host_id,
             force=False,
             from_database_only=False,
             user=None,
             session=None,
             **kwargs):
    """Delete a host.

    If force, we delete the host anyway.
    If from_database_only, we only delete the host record in databaes.
    Otherwise we send to del host task to celery to delete the host
    record in os installer and package installer, clean installation logs
    and at last clean database record.
    The backend will call this function again after it deletes the record
    in os installer and package installer with from_database_only set.
    """
    from compass.db.api import cluster as cluster_api
    host = _get_host(host_id, session=session)
    # force set host state to ERROR when we want to delete the
    # host anyway even the host is in installing or already
    # installed. It let the api know the deleting is in doing when backend
    # is doing the real deleting. In future we may import a new state like
    # INDELETE to indicate the deleting is processing.
    # We need discuss about if we can delete a host when it is already
    # installed by api.
    if host.state.state != 'UNINITIALIZED' and force:
        host.state.state = 'ERROR'
    check_host_editable(host, user=user, check_in_installing=True)
    cluster_ids = []
    for clusterhost in host.clusterhosts:
        if clusterhost.state.state != 'UNINITIALIZED' and force:
            clusterhost.state.state = 'ERROR'
        # TODO(grace): here we check all clusters which use this host editable.
        # Because in backend we do not have functions to delete host without
        # reference its cluster. After deleting pure host supported in backend,
        # we should change code here to is_cluster_editable.
        # Here delete a host may fail even we set force flag.
        cluster_api.check_cluster_editable(clusterhost.cluster,
                                           user=user,
                                           check_in_installing=True)
        cluster_ids.append(clusterhost.cluster_id)

    # Delete host record directly if there is no need to delete it
    # in backend or from_database_only is set.
    if host.state.state == 'UNINITIALIZED' or from_database_only:
        return utils.del_db_object(session, host)
    else:
        logging.info('send del host %s task to celery', host_id)
        from compass.tasks import client as celery_client
        celery_client.celery.send_task('compass.tasks.delete_host',
                                       (user.email, host.id, cluster_ids))
        return {
            'status': 'delete action sent',
            'host': host,
        }
def delete_reports(cluster_id, name=None, user=None, session=None):
    # TODO(grace): better to separate this function into two.
    # One is to delete a report of a cluster, the other to delete all
    # reports under a cluster.
    if name:
        report = _get_report(cluster_id, name, session=session)
        return utils.del_db_object(session, report)
    else:
        cluster = cluster_api.get_cluster_internal(cluster_id, session=session)
        return utils.del_db_objects(session,
                                    models.HealthCheckReport,
                                    cluster_id=cluster.id)
Example #19
0
def del_host_network(session, deleter, host_id, host_network_id, **kwargs):
    """Delete a host network."""
    host_network = utils.get_db_object(
        session, models.HostNetwork,
        id=host_network_id
    )
    if host_network.host_id != host_id:
        raise exception.RecordNotExists(
            'host %s does not own host network %s' % (
                host_id, host_network_id
            )
        )
    is_host_editable(session, host_network.host, deleter)
    return utils.del_db_object(session, host_network)
def delete_reports(cluster_id, name=None, user=None, session=None):
    # TODO(grace): better to separate this function into two.
    # One is to delete a report of a cluster, the other to delete all
    # reports under a cluster.
    if name:
        report = _get_report(cluster_id, name, session=session)
        return utils.del_db_object(session, report)
    else:
        cluster = cluster_api.get_cluster_internal(
            cluster_id, session=session
        )
        return utils.del_db_objects(
            session, models.HealthCheckReport, cluster_id=cluster.id
        )
Example #21
0
def _del_switch_machine(switch_machine, session=None):
    """Delete switch machine.

    If this is the last switch machine associated to underlying machine,
    add a switch machine record to default switch to make the machine
    searchable.
    """
    default_switch = _get_switch_by_ip(setting.DEFAULT_SWITCH_IP,
                                       session=session)
    machine = switch_machine.machine
    if len(machine.switch_machines) <= 1:
        utils.add_db_object(session,
                            models.SwitchMachine,
                            False,
                            default_switch.id,
                            machine.id,
                            port=switch_machine.port)
    return utils.del_db_object(session, switch_machine)
Example #22
0
def del_switch(switch_id, user=None, session=None, **kwargs):
    """Delete a switch."""
    switch = utils.get_db_object(session, models.Switch, id=switch_id)
    default_switch_ip_int = long(netaddr.IPAddress(setting.DEFAULT_SWITCH_IP))
    default_switch = utils.get_db_object(
        session, models.Switch,
        ip_int=default_switch_ip_int
    )
    for switch_machine in switch.switch_machines:
        machine = switch_machine.machine
        if len(machine.switch_machines) <= 1:
            utils.add_db_object(
                session, models.SwitchMachine,
                False,
                default_switch.id, machine.id,
                port=switch_machine.port
            )
    return utils.del_db_object(session, switch)
Example #23
0
def del_host(
    host_id, force=False, from_database_only=False,
    user=None, session=None, **kwargs
):
    """Delete a host."""
    from compass.db.api import cluster as cluster_api
    host = utils.get_db_object(
        session, models.Host, id=host_id
    )
    if host.state.state != 'UNINITIALIZED' and force:
        host.state.state = 'ERROR'
    is_host_editable(
        session, host, user,
        reinstall_os_set=True
    )
    cluster_ids = []
    for clusterhost in host.clusterhosts:
        if clusterhost.state.state != 'UNINITIALIZED' and force:
            clusterhost.state.state = 'ERROR'
        cluster_api.is_cluster_editable(
            session, clusterhost.cluster, user,
            reinstall_distributed_system_set=True
        )
        cluster_ids.append(clusterhost.cluster_id)

    if host.state.state == 'UNINITIALIZED' or from_database_only:
        return utils.del_db_object(session, host)
    else:
        logging.info(
            'send del host %s task to celery', host_id
        )
        from compass.tasks import client as celery_client
        celery_client.celery.send_task(
            'compass.tasks.delete_host',
            (
                user.email, host_id, cluster_ids
            )
        )
        return {
            'status': 'delete action sent',
            'host': host,
        }
Example #24
0
def del_subnet(subnet_id, user=None, session=None, **kwargs):
    """Delete a subnet."""
    subnet = utils.get_db_object(
        session, models.Subnet, id=subnet_id
    )
    if subnet.host_networks:
        host_networks = [
            '%s:%s=%s' % (
                host_network.host.name, host_network.interface,
                host_network.ip
            )
            for host_network in subnet.host_networks
        ]
        raise exception.NotAcceptable(
            'subnet %s contains host networks %s' % (
                subnet.subnet, host_networks
            )
        )

    return utils.del_db_object(session, subnet)
Example #25
0
def _del_switch_machine(
    switch_machine, session=None
):
    """Delete switch machine.

    If this is the last switch machine associated to underlying machine,
    add a switch machine record to default switch to make the machine
    searchable.
    """
    default_switch = _get_switch_by_ip(
        setting.DEFAULT_SWITCH_IP, session=session
    )
    machine = switch_machine.machine
    if len(machine.switch_machines) <= 1:
        utils.add_db_object(
            session, models.SwitchMachine,
            False,
            default_switch.id, machine.id,
            port=switch_machine.port
        )
    return utils.del_db_object(session, switch_machine)
Example #26
0
def del_machine(machine_id, user=None, session=None, **kwargs):
    """Delete a machine."""
    machine = _get_machine(machine_id, session=session)
    _check_machine_deletable(machine)
    return utils.del_db_object(session, machine)
Example #27
0
def del_machine(session, deleter, machine_id, **kwargs):
    """Delete a machine."""
    machine = utils.get_db_object(session, models.Machine, id=machine_id)
    return utils.del_db_object(session, machine)
Example #28
0
def del_host_from_database(session, deleter, host_id):
    host = utils.get_db_object(
        session, models.Host, id=host_id
    )
    return utils.del_db_object(session, host)
Example #29
0
def del_subnet(subnet_id, user=None, session=None, **kwargs):
    """Delete a subnet."""
    subnet = _get_subnet(subnet_id, session=session)
    _check_subnet_deletable(subnet)
    return utils.del_db_object(session, subnet)
Example #30
0
def del_subnet(session, deleter, subnet_id, **kwargs):
    """Delete a subnet."""
    subnet = utils.get_db_object(
        session, models.Subnet, id=subnet_id
    )
    return utils.del_db_object(session, subnet)
Example #31
0
def del_switch(session, deleter, switch_id, **kwargs):
    """Delete a switch."""
    switch = utils.get_db_object(session, models.Switch, id=switch_id)
    return utils.del_db_object(session, switch)
Example #32
0
def del_user(user_id, user=None, session=None, **kwargs):
    """delete a user and return the deleted user object."""
    del_user = _get_user(user_id, session=session)
    return utils.del_db_object(session, del_user)
Example #33
0
def del_user(user_id, user=None, session=None, **kwargs):
    """delete a user and return the deleted user object."""
    del_user = _get_user(user_id, session=session)
    return utils.del_db_object(session, del_user)
Example #34
0
def del_host(
    host_id, force=False, from_database_only=False,
    user=None, session=None, **kwargs
):
    """Delete a host.

    If force, we delete the host anyway.
    If from_database_only, we only delete the host record in databaes.
    Otherwise we send to del host task to celery to delete the host
    record in os installer and package installer, clean installation logs
    and at last clean database record.
    The backend will call this function again after it deletes the record
    in os installer and package installer with from_database_only set.
    """
    from compass.db.api import cluster as cluster_api
    host = _get_host(host_id, session=session)
    # force set host state to ERROR when we want to delete the
    # host anyway even the host is in installing or already
    # installed. It let the api know the deleting is in doing when backend
    # is doing the real deleting. In future we may import a new state like
    # INDELETE to indicate the deleting is processing.
    # We need discuss about if we can delete a host when it is already
    # installed by api.
    if host.state.state != 'UNINITIALIZED' and force:
        host.state.state = 'ERROR'
    check_host_editable(
        host, user=user,
        check_in_installing=True
    )
    cluster_ids = []
    for clusterhost in host.clusterhosts:
        if clusterhost.state.state != 'UNINITIALIZED' and force:
            clusterhost.state.state = 'ERROR'
        # TODO(grace): here we check all clusters which use this host editable.
        # Because in backend we do not have functions to delete host without
        # reference its cluster. After deleting pure host supported in backend,
        # we should change code here to is_cluster_editable.
        # Here delete a host may fail even we set force flag.
        cluster_api.check_cluster_editable(
            clusterhost.cluster, user=user,
            check_in_installing=True
        )
        cluster_ids.append(clusterhost.cluster_id)

    # Delete host record directly if there is no need to delete it
    # in backend or from_database_only is set.
    if host.state.state == 'UNINITIALIZED' or from_database_only:
        return utils.del_db_object(session, host)
    else:
        logging.info(
            'send del host %s task to celery', host_id
        )
        from compass.tasks import client as celery_client
        celery_client.celery.send_task(
            'compass.tasks.delete_host',
            (
                user.email, host.id, cluster_ids
            )
        )
        return {
            'status': 'delete action sent',
            'host': host,
        }
Example #35
0
 def test_del_db_object(self):
     with self.assertRaises(exception.RecordNotExists):
         with database.session() as session:
             db_obj = utils.get_db_object(session, models.Permission, id=1)
             utils.del_db_object(session, db_obj)
             utils.get_db_object(session, models.Permission, id=1)
Example #36
0
def del_user(user_id, user=None, session=None, **kwargs):
    """delete a user and return the deleted user object."""
    user = utils.get_db_object(session, models.User, id=user_id)
    return utils.del_db_object(session, user)
Example #37
0
def del_machine(machine_id, user=None, session=None, **kwargs):
    """Delete a machine."""
    machine = _get_machine(machine_id, session=session)
    _check_machine_deletable(machine)
    return utils.del_db_object(session, machine)