Esempio n. 1
0
 def test_get_user(self):
     get_user = user_api.get_user(
         self.user_object.id,
         user=self.user_object
     )
     self.assertIsNotNone(get_user)
     self.assertEqual(get_user['email'], setting.COMPASS_ADMIN_EMAIL)
Esempio n. 2
0
def list_user_actions(user_id, user=None, session=None, **filters):
    """list user actions of a user."""
    list_user = user_api.get_user(user_id, user=user, session=session)
    return utils.list_db_objects(
        session, models.UserLog, order_by=['timestamp'],
        user_id=list_user['id'], **filters
    )
Esempio n. 3
0
def reset_machine(machine_id, reset={}, user=None, session=None, **kwargs):
    """reset machine."""
    from compass.tasks import client as celery_client
    machine = _get_machine(machine_id, session=session)
    if not user:
        user_id = machine.owner_id
        user_dict = user_api.get_user(user_id, session=session)
        user_email = user_dict['email']
    else:
        user_email = user.email
    celery_client.celery.send_task('compass.tasks.reset_machine',
                                   (machine_id, ),
                                   queue=user_email,
                                   exchange=user_email,
                                   routing_key=user_email)
    return {'status': 'reset %s action sent' % machine.mac, 'machine': machine}
Esempio n. 4
0
def reset_host(
    host_id, reset={}, user=None, session=None, **kwargs
):
    """reset host."""
    from compass.tasks import client as celery_client
    host = _get_host(host_id, session=session)
    check_host_validated(host)
    if not user:
        user_id = host.creator_id
        user_dict = user_api.get_user(user_id, session=session)
        user_email = user_dict['email']
    else:
        user_email = user.email
    celery_client.celery.send_task(
        'compass.tasks.reset_host',
        (host.id,),
        queue=user_email,
        exchange=user_email,
        routing_key=user_email
    )
    return {
        'status': 'reset %s action sent' % host.name,
        'host': host
    }
Esempio n. 5
0
def show_user(user_id):
    """Get user."""
    data = _get_request_args()
    return utils.make_json_response(
        200, user_api.get_user(current_user, user_id, **data)
    )
Esempio n. 6
0
def update_host_state_internal(host_id,
                               from_database_only=False,
                               user=None,
                               session=None,
                               **kwargs):
    """Update a host state.

    This function is called when host os is installed.
    If from_database_only, the state is updated in database.
    Otherwise a celery task sent to os installer and package installer
    to do some future actions.
    """
    # TODO(xicheng): should be merged into update_host_state
    host = _get_host(host_id, session=session)
    logging.info("======host state: %s", host.state)
    if 'ready' in kwargs and kwargs['ready'] and not host.state.ready:
        ready_triggered = True
    else:
        ready_triggered = False
    clusterhosts_ready = {}
    clusters_os_ready = {}
    if ready_triggered:
        for clusterhost in host.clusterhosts:
            cluster = clusterhost.cluster
            if cluster.flavor_name:
                clusterhosts_ready[cluster.id] = False
            else:
                clusterhosts_ready[cluster.id] = True
            all_os_ready = True
            for clusterhost_in_cluster in cluster.clusterhosts:
                host_in_cluster = clusterhost_in_cluster.host
                if host_in_cluster.id == host.id:
                    continue
                if not host_in_cluster.state.ready:
                    all_os_ready = False
            clusters_os_ready[cluster.id] = all_os_ready
    logging.debug('host %s ready: %s', host_id, ready_triggered)
    logging.debug("clusterhosts_ready is: %s", clusterhosts_ready)
    logging.debug("clusters_os_ready is %s", clusters_os_ready)

    if not ready_triggered or from_database_only:
        logging.debug('%s state is set to %s', host.name, kwargs)
        utils.update_db_object(session, host.state, **kwargs)
        if not host.state.ready:
            for clusterhost in host.clusterhosts:
                utils.update_db_object(session, clusterhost.state, ready=False)
                utils.update_db_object(session,
                                       clusterhost.cluster.state,
                                       ready=False)
        status = '%s state is updated' % host.name
    else:
        if not user:
            user_id = host.creator_id
            user_dict = user_api.get_user(user_id, session=session)
            user_email = user_dict['email']
        else:
            user_email = user.email
        from compass.tasks import client as celery_client
        celery_client.celery.send_task(
            'compass.tasks.os_installed',
            (host.id, clusterhosts_ready, clusters_os_ready),
            queue=user_email,
            exchange=user_email,
            routing_key=user_email)
        status = '%s: clusterhosts ready %s clusters os ready %s' % (
            host.name, clusterhosts_ready, clusters_os_ready)
        logging.info('action status: %s', status)
    return {'status': status, 'host': host.state}
Esempio n. 7
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)
        if not user:
            user_id = host.creator_id
            user_dict = user_api.get_user(user_id, session=session)
            user_email = user_dict['email']
        else:
            user_email = user.email
        from compass.tasks import client as celery_client
        celery_client.celery.send_task('compass.tasks.delete_host',
                                       (user.email, host.id, cluster_ids),
                                       queue=user_email,
                                       exchange=user_email,
                                       routing_key=user_email)
        return {
            'status': 'delete action sent',
            'host': host,
        }
Esempio n. 8
0
def del_user_actions(user_id, user=None, session=None, **filters):
    """delete actions of a user."""
    del_user = user_api.get_user(user_id, user=user, session=session)
    return utils.del_db_objects(
        session, models.UserLog, user_id=del_user['id'], **filters
    )
Esempio n. 9
0
 def test_get_user(self):
     get_user = user_api.get_user(self.user_object.id,
                                  user=self.user_object)
     self.assertIsNotNone(get_user)
     self.assertEqual(get_user['email'], setting.COMPASS_ADMIN_EMAIL)