Пример #1
0
def add_flavors_internal(session, exception_when_existing=True):
    configs = util.load_configs(setting.ADAPTER_FLAVOR_DIR)
    for config in configs:
        logging.info('add config %s to flavor', config)
        adapter = utils.get_db_object(
            session, models.Adapter,
            name=config['ADAPTER_NAME']
        )
        for flavor_dict in config['FLAVORS']:
            flavor = utils.add_db_object(
                session, models.AdapterFlavor,
                exception_when_existing, flavor_dict['flavor'], adapter.id,
                display_name=flavor_dict.get('display_name', None),
                template=flavor_dict.get('template', None)
            )
            role_names = flavor_dict.get('roles', [])
            for role_name in role_names:
                role = utils.get_db_object(
                    session, models.AdapterRole,
                    name=role_name, adapter_id=adapter.id
                )
                utils.add_db_object(
                    session, models.AdapterFlavorRole,
                    exception_when_existing, flavor.id, role.id
                )
                utils.update_db_object(
                    session, flavor,
                    patched_ordered_flavor_roles=[role_name]
                )
Пример #2
0
def _update_machine_if_necessary(
    machine, session=None, **kwargs
):
    """Update machine is there is something to update."""
    utils.update_db_object(
        session, machine, **kwargs
    )
Пример #3
0
def _update_machine_if_necessary(
    machine, session=None, **kwargs
):
    """Update machine is there is something to update."""
    utils.update_db_object(
        session, machine, **kwargs
    )
Пример #4
0
def update_host_state(host_id, user=None, session=None, **kwargs):
    """Update a host state."""
    host = utils.get_db_object(
        session, models.Host, id=host_id
    )
    utils.update_db_object(session, host.state, **kwargs)
    return host.state_dict()
Пример #5
0
 def test_update_db_obj_none_exist(self):
     with self.assertRaises(exception.DatabaseException):
         with database.session() as session:
             db_obj = utils.get_db_object(session,
                                          models.Permission,
                                          id=1000)
             utils.update_db_object(session, db_obj, name='dummy')
Пример #6
0
def add_os_adapters_internal(session):
    parents = {}
    configs = util.load_configs(setting.OS_ADAPTER_DIR)
    with session.begin(subtransactions=True):
        for config in configs:
            if 'OS' in config:
                os = utils.get_db_object(
                    session, models.OperatingSystem,
                    name=config['OS']
                )
            else:
                os = None
            if 'INSTALLER' in config:
                installer = utils.get_db_object(
                    session, models.OSInstaller,
                    name=config['INSTALLER']
                )
            else:
                installer = None
            object = utils.add_db_object(
                session, models.OSAdapter,
                True, config['NAME'], os=os, installer=installer
            )
            parents[config['NAME']] = (object, config.get('PARENT', None))
        for name, (object, parent_name) in parents.items():
            if parent_name:
                parent, _ = parents[parent_name]
            else:
                parent = None
            utils.update_db_object(
                session, object, parent=parent
            )

    _complement_os_adapters(session)
Пример #7
0
def update_machine(updater, machine_id, **kwargs):
    """Update a machine."""
    with database.session() as session:
         user_api.check_user_permission_internal(
            session, updater, permission.PERMISSION_UPDATE_MACHINE)
         machine = utils.get_db_object(session, models.Machine, id=machine_id)
         utils.update_db_object(session, machine, **kwargs)
         return machine.to_dict()
Пример #8
0
def delete_cluster_config(updater, cluster_id):
    """Update a cluster config."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, updater, permission.PERMISSION_DEL_CLUSTER)
        cluster = utils.get_db_object(
            session, models.Cluster, id=cluster_id
        )
        utils.update_db_object(
            session, cluster, os_config={}, package_config={}
        )
        return cluster.to_dict()
Пример #9
0
def patch_machine(updater, machine_id, **kwargs):
    """Update a switch."""
    with database.session() as session:
         user_api.check_user_permission_internal(
            session, updater, permission.PERMISSION_UDPATE_MACHINE)
         machine = utils.get_db_object(session, models.Machine, id=machine_id)
         utils.update_db_object(session, machine, **kwargs)
         machine_dict = machine.to_dict()
         utils.validates(
             [], {'ipmi_credentials': _check_ipmi_credentials},
             **machine_dict
         )
         return machine_dict
Пример #10
0
 def test_update_db_obj_none_exist(self):
     with self.assertRaises(exception.DatabaseException):
         with database.session() as session:
             db_obj = utils.get_db_object(
                 session,
                 models.Permission,
                 id=1000
             )
             utils.update_db_object(
                 session,
                 db_obj,
                 name='dummy'
             )
Пример #11
0
def del_host_config(host_id, user=None, session=None):
    """delete a host config."""
    host = _get_host(host_id, session=session)
    check_host_editable(host, user=user)
    return utils.update_db_object(
        session, host, os_config={}, config_validated=False
    )
Пример #12
0
 def test_update_db_object(self):
     with database.session() as session:
         db_obj = utils.get_db_object(session, models.Permission, id=1)
         updated_obj = utils.update_db_object(session,
                                              db_obj,
                                              alias='updated')
         self.assertEqual('updated', updated_obj.alias)
Пример #13
0
def _update_host_network(
    session, updater, host_network, **kwargs
):
    if 'interface' in kwargs:
        interface = kwargs['interface']
        host_network_by_interface = utils.get_db_object(
            session, models.HostNetwork, False,
            host_id=host_network.host_id,
            interface=interface
        )
        if (
            host_network_by_interface and
            host_network_by_interface.id != host_network.id
        ):
            raise exception.InvalidParameter(
                'interface %s exists in host network %s' % (
                    interface, host_network_by_interface.id
                )
            )
    if 'ip' in kwargs:
        ip = kwargs['ip']
        ip_int = long(netaddr.IPAddress(ip))
        host_network_by_ip = utils.get_db_object(
            session, models.HostNetwork, False,
            ip_int=ip_int
        )
        if host_network_by_ip and host_network_by_ip.id != host_network.id:
            raise exception.InvalidParameter(
                'ip %s exist in host network %s' % (
                    ip, host_network_by_ip.id
                )
            )
    is_host_editable(session, host_network.host, updater)
    return utils.update_db_object(session, host_network, **kwargs)
Пример #14
0
def _update_host(host_id, session=None, user=None, **kwargs):
    """Update a host internal."""
    host = _get_host(host_id, session=session)
    check_host_editable(host,
                        user=user,
                        check_in_installing=kwargs.get('reinstall_os', False))
    return utils.update_db_object(session, host, **kwargs)
Пример #15
0
def update_report(cluster_id, name, session=None, **kwargs):
    """Update health check report."""
    report = _get_report(cluster_id, name, session=session)
    if report.state == 'finished':
        err_msg = 'Report cannot be updated if state is in "finished"'
        raise exception.Forbidden(err_msg)

    return utils.update_db_object(session, report, **kwargs)
Пример #16
0
def update_host_deployed_config(host_id, user=None, session=None, **kwargs):
    """Update host deployed config."""
    host = utils.get_db_object(
        session, models.Host, id=host_id
    )
    is_host_editable(session, host, user)
    is_host_validated(session, host)
    return utils.update_db_object(session, host, **kwargs)
Пример #17
0
def update_report(cluster_id, name, session=None, **kwargs):
    """Update health check report."""
    report = _get_report(cluster_id, name, session=session)
    if report.state == 'finished':
        err_msg = 'Report cannot be updated if state is in "finished"'
        raise exception.Forbidden(err_msg)

    return utils.update_db_object(session, report, **kwargs)
Пример #18
0
def _update_host_network(
    host_network, session=None, user=None, **kwargs
):
    """Update host network."""
    check_host_editable(host_network.host, user=user)
    subnet = network.get_subnet_internal(host_network.subnet_id, session=session)
    check_ip_available(subnet, ip)
    return utils.update_db_object(session, host_network, **kwargs)
Пример #19
0
def _add_system(session, model, configs):
    parents = {}
    for config in configs:
        object = utils.add_db_object(
            session, model,
            True, config['NAME'],
            deployable=config.get('DEPLOYABLE', False)
        )
        parents[config['NAME']] = (
            object, config.get('PARENT', None)
        )
    for name, (object, parent_name) in parents.items():
        if parent_name:
            parent, _ = parents[parent_name]
        else:
            parent = None
        utils.update_db_object(session, object, parent=parent)
Пример #20
0
def update_host_log_history(
    host_id, filename, user=None,
    session=None, **kwargs
):
    """Update a host log history."""
    host_log_history = utils.get_db_object(
        session, models.HostLogHistory, id=host_id, filename=filename
    )
    return utils.update_db_object(session, host_log_history, **kwargs)
Пример #21
0
def del_host_config(host_id, user=None, session=None):
    """delete a host config."""
    host = utils.get_db_object(
        session, models.Host, id=host_id
    )
    is_host_editable(session, host, user)
    return utils.update_db_object(
        session, host, os_config={}, config_validated=False
    )
Пример #22
0
def update_switch_machine_internal(
    session, switch_machine, switch_machines_fields, **kwargs
):
    """Update switch machine internal."""
    switch_machine_dict = {}
    machine_dict = {}
    for key, value in kwargs.items():
        if key in switch_machines_fields:
            switch_machine_dict[key] = value
        else:
            machine_dict[key] = value
    if machine_dict:
        utils.update_db_object(
            session, switch_machine.machine, **machine_dict
        )
    return utils.update_db_object(
        session, switch_machine, **switch_machine_dict
    )
Пример #23
0
def update_host_log_history(
    host_id, filename, user=None,
    session=None, **kwargs
):
    """Update a host log history."""
    host_log_history = _get_host_log_history(
        host_id, filename, session=session
    )
    return utils.update_db_object(session, host_log_history, **kwargs)
Пример #24
0
def _update_host(host_id, session=None, user=None, **kwargs):
    """Update a host internal."""
    host = _get_host(host_id, session=session)
    if host.state.state == "SUCCESSFUL" and not host.reinstall_os:
        logging.info("ignoring successful host: %s", host_id)
        return {}
    check_host_editable(host,
                        user=user,
                        check_in_installing=kwargs.get('reinstall_os', False))
    return utils.update_db_object(session, host, **kwargs)
Пример #25
0
def _update_host(session, user, host_id, **kwargs):
    """Update a host internal."""
    host = utils.get_db_object(
        session, models.Host, id=host_id
    )
    is_host_editable(
        session, host, user,
        reinstall_os_set=kwargs.get('reinstall_os', False)
    )
    return utils.update_db_object(session, host, **kwargs)
Пример #26
0
def update_cluster_config(updater, cluster_id, **kwargs):
    """Update a cluster config."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, updater, permission.PERMISSION_ADD_CLUSTER)
        cluster = utils.get_db_object(
            session, models.Cluster, id=cluster_id
        )
        utils.update_db_object(session, cluster, **kwargs)
        os_config = cluster.os_config
        if os_config:
            metadata_api.validate_os_config(
                os_config, cluster.adapter_id
            )
        package_config = cluster.package_config
        if package_config:
            metadata_api.validate_package_config(
                package_config, cluster.adapter_id
            )
        return cluster.to_dict()
Пример #27
0
def _update_host(host_id, session=None, user=None, **kwargs):
    """Update a host internal."""
    host = _get_host(host_id, session=session)
    if host.state.state == "SUCCESSFUL" and not host.reinstall_os:
        logging.info("ignoring successful host: %s", host_id)
        return {}
    check_host_editable(
        host, user=user,
        check_in_installing=kwargs.get('reinstall_os', False)
    )
    return utils.update_db_object(session, host, **kwargs)
Пример #28
0
def _add_system(session, model, configs, exception_when_existing=True):
    parents = {}
    for config in configs:
        logging.info(
            'add config %s to %s',
            config, model
        )
        object = utils.add_db_object(
            session, model,
            exception_when_existing, config['NAME'],
            deployable=config.get('DEPLOYABLE', False)
        )
        parents[config['NAME']] = (
            object, config.get('PARENT', None)
        )
    for name, (object, parent_name) in parents.items():
        if parent_name:
            parent, _ = parents[parent_name]
        else:
            parent = None
        utils.update_db_object(session, object, parent=parent)
Пример #29
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)
    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:
        from compass.tasks import client as celery_client
        celery_client.celery.send_task(
            'compass.tasks.os_installed',
            (host.id, clusterhosts_ready, clusters_os_ready))
        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}
Пример #30
0
def add_package_adapters_internal(session):
    parents = {}
    configs = util.load_configs(setting.PACKAGE_ADAPTER_DIR)
    with session.begin(subtransactions=True):
        for config in configs:
            if 'DISTRIBUTED_SYSTEM' in config:
                distributed_system = utils.get_db_object(
                    session, models.DistributedSystem,
                    name=config['DISTRIBUTED_SYSTEM']
                )
            else:
                distributed_system = None
            if 'INSTALLER' in config:
                installer = utils.get_db_object(
                    session, models.PackageInstaller,
                    name=config['INSTALLER']
                )
            else:
                installer = None
            object = utils.add_db_object(
                session, models.PackageAdapter,
                True,
                config['NAME'],
                distributed_system=distributed_system,
                installer=installer,
                support_os_patterns=config.get('SUPPORT_OS_PATTERNS', [])
            )
            parents[config['NAME']] = (object, config.get('PARENT', None))
        for name, (object, parent_name) in parents.items():
            if parent_name:
                parent, _ = parents[parent_name]
            else:
                parent = None
            utils.update_db_object(session, object, parent=parent)

    _complement_distributed_system_adapters(session)
Пример #31
0
 def test_update_db_object(self):
     with database.session() as session:
         db_obj = utils.get_db_object(
             session,
             models.Permission,
             id=1
         )
         updated_obj = utils.update_db_object(
             session,
             db_obj,
             alias='updated'
         )
         self.assertEqual(
             'updated',
             updated_obj.alias
         )
Пример #32
0
def update_user(user_id, user=None, session=None, **kwargs):
    """Update a user and return the updated user object."""
    update_user = _get_user(
        user_id,
        session=session,
    )
    allowed_fields = set()
    if user.is_admin:
        allowed_fields |= set(ADMIN_UPDATED_FIELDS)
    if user.id == update_user.id:
        allowed_fields |= set(SELF_UPDATED_FIELDS)
    unsupported_fields = set(kwargs) - allowed_fields
    if unsupported_fields:
        # The user is not allowed to update a user.
        raise exception.Forbidden(
            'User %s has no permission to update user %s fields %s.' %
            (user.email, user.email, unsupported_fields))
    return utils.update_db_object(session, update_user, **kwargs)
Пример #33
0
def record_user_token(
    token, expire_timestamp, user=None, session=None
):
    """record user token in database."""
    user_token = utils.get_db_object(
        session, models.UserToken, False,
        user_id=user.id, token=token
    )
    if not user_token:
        return utils.add_db_object(
            session, models.UserToken, True,
            token, user_id=user.id,
            expire_timestamp=expire_timestamp
        )
    elif expire_timestamp > user_token.expire_timestamp:
        return utils.update_db_object(
            session, user_token, expire_timestamp=expire_timestamp
        )
    return user_token
Пример #34
0
def update_user(user_id, user=None, session=None, **kwargs):
    """Update a user and return the updated user object."""
    update_user = _get_user(
        user_id, session=session,
    )
    allowed_fields = set()
    if user.is_admin:
        allowed_fields |= set(ADMIN_UPDATED_FIELDS)
    if user.id == update_user.id:
        allowed_fields |= set(SELF_UPDATED_FIELDS)
    unsupported_fields = set(kwargs) - allowed_fields
    if unsupported_fields:
            # The user is not allowed to update a user.
        raise exception.Forbidden(
            'User %s has no permission to update user %s fields %s.' % (
                user.email, user.email, unsupported_fields
            )
        )
    return utils.update_db_object(session, update_user, **kwargs)
Пример #35
0
def record_user_token(token, expire_timestamp, user=None, session=None):
    """record user token in database."""
    user_token = utils.get_db_object(session,
                                     models.UserToken,
                                     False,
                                     user_id=user.id,
                                     token=token)
    if not user_token:
        return utils.add_db_object(session,
                                   models.UserToken,
                                   True,
                                   token,
                                   user_id=user.id,
                                   expire_timestamp=expire_timestamp)
    elif expire_timestamp > user_token.expire_timestamp:
        return utils.update_db_object(session,
                                      user_token,
                                      expire_timestamp=expire_timestamp)
    return user_token
Пример #36
0
def _update_host(session, updater, host_id, **kwargs):
    """Update a host internal."""
    host = utils.get_db_object(
        session, models.Host, id=host_id
    )
    is_host_editable(
        session, host, updater,
        reinstall_os_set=kwargs.get('reinstall_os', False)
    )
    if 'name' in kwargs:
        hostname = kwargs['name']
        host_by_name = utils.get_db_object(
            session, models.Host, False, name=hostname
        )
        if host_by_name and host_by_name.id != host.id:
            raise exception.InvalidParameter(
                'hostname %s is already exists in host %s' % (
                    hostname, host_by_name.id
                )
            )
    return utils.update_db_object(session, host, **kwargs)
Пример #37
0
def _update_machine(machine_id, session=None, **kwargs):
    """Update a machine."""
    machine = _get_machine(machine_id, session=session)
    return utils.update_db_object(session, machine, **kwargs)
Пример #38
0
def update_host_state(host_id, user=None, session=None, **kwargs):
    """Update a host state."""
    host = _get_host(host_id, session=session)
    utils.update_db_object(session, host.state, **kwargs)
    return host.state
Пример #39
0
def _update_host_network(host_network, session=None, user=None, **kwargs):
    """Update host network."""
    check_host_editable(host_network.host, user=user)
    return utils.update_db_object(session, host_network, **kwargs)
Пример #40
0
def _update_host_config(host, session=None, user=None, **kwargs):
    """Update host config."""
    check_host_editable(host, user=user)
    return utils.update_db_object(session, host, **kwargs)
Пример #41
0
def update_host_deployed_config(host_id, user=None, session=None, **kwargs):
    """Update host deployed config."""
    host = _get_host(host_id, session=session)
    check_host_editable(host, user=user)
    check_host_validated(host)
    return utils.update_db_object(session, host, **kwargs)
Пример #42
0
def update_switch_filters(switch_id, user=None, session=None, **kwargs):
    """Update filters of a switch."""
    switch = _get_switch(switch_id, session=session)
    return utils.update_db_object(session, switch, **kwargs)
Пример #43
0
def add_adapters_internal(session, exception_when_existing=True):
    parents = {}
    configs = util.load_configs(setting.ADAPTER_DIR)
    for config in configs:
        logging.info('add config %s to adapter', config)
        if 'DISTRIBUTED_SYSTEM' in config:
            distributed_system = utils.get_db_object(
                session, models.DistributedSystem,
                name=config['DISTRIBUTED_SYSTEM']
            )
        else:
            distributed_system = None
        if 'OS_INSTALLER' in config:
            os_installer = utils.get_db_object(
                session, models.OSInstaller,
                alias=config['OS_INSTALLER']
            )
        else:
            os_installer = None
        if 'PACKAGE_INSTALLER' in config:
            package_installer = utils.get_db_object(
                session, models.PackageInstaller,
                alias=config['PACKAGE_INSTALLER']
            )
        else:
            package_installer = None
        adapter = utils.add_db_object(
            session, models.Adapter,
            exception_when_existing,
            config['NAME'],
            display_name=config.get('DISPLAY_NAME', None),
            distributed_system=distributed_system,
            os_installer=os_installer,
            package_installer=package_installer,
            deployable=config.get('DEPLOYABLE', False)
        )
        supported_os_patterns = [
            re.compile(supported_os_pattern)
            for supported_os_pattern in config.get('SUPPORTED_OS_PATTERNS', [])
        ]
        oses = utils.list_db_objects(
            session, models.OperatingSystem
        )
        for os in oses:
            if not os.deployable:
                continue
            os_name = os.name
            for supported_os_pattern in supported_os_patterns:
                if supported_os_pattern.match(os_name):
                    utils.add_db_object(
                        session, models.AdapterOS,
                        exception_when_existing,
                        os.id, adapter.id
                    )
                    break
            parents[config['NAME']] = (adapter, config.get('PARENT', None))

    for name, (adapter, parent_name) in parents.items():
            if parent_name:
                parent, _ = parents[parent_name]
            else:
                parent = None
            utils.update_db_object(session, adapter, parent=parent)
Пример #44
0
def patch_switch_filter(switch_id, user=None, session=None, **kwargs):
    """Patch filters to a switch."""
    switch = _get_switch(switch_id, session=session)
    return utils.update_db_object(session, switch, **kwargs)
Пример #45
0
def update_subnet(subnet_id, user=None, session=None, **kwargs):
    """Update a subnet."""
    subnet = _get_subnet(subnet_id, session=session)
    return utils.update_db_object(session, subnet, **kwargs)
Пример #46
0
def _update_machine(session, updater, machine_id, **kwargs):
    """Update a machine."""
    machine = utils.get_db_object(session, models.Machine, id=machine_id)
    return utils.update_db_object(session, machine, **kwargs)
Пример #47
0
        )
        return network.to_dict()


@utils.wrap_to_dict(RESP_FIELDS)
@utils.input_validates(subnet=_check_network)
@utils.supported_filters(UPDATED_FIELDS)
def update_subnet(updater, subnet_id, **kwargs):
     """Update a subnet."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, updater, permission.PERMISSION_ADD_NETWORK)
        network = utils.get_db_object(
            session, models.Network, id=subnet_id
        )
        utils.update_db_object(session, network, **kwargs)
        return network.to_dict()


@utils.wrap_to_dict(RESP_FIELDS)
@utils.supported_filters([])
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()
Пример #48
0
def _update_switch_machine_only(switch_machine, session=None, **kwargs):
    """Update switch machine."""
    return utils.update_db_object(session, switch_machine, **kwargs)
Пример #49
0
def update_subnet(subnet_id, user=None, session=None, **kwargs):
    """Update a subnet."""
    subnet = utils.get_db_object(
        session, models.Subnet, id=subnet_id
    )
    return utils.update_db_object(session, subnet, **kwargs)
Пример #50
0
def _update_switch(switch_id, session=None, **kwargs):
    """Update a switch."""
    switch = _get_switch(switch_id, session=session)
    return utils.update_db_object(session, switch, **kwargs)