示例#1
0
def _trait_sync(context):
    """Sync the os_traits symbols to the database.

    Reads all symbols from the os_traits library, checks if any of them do
    not exist in the database and bulk-inserts those that are not. This is
    done once per web-service process, at startup.

    :param context: `placement.context.RequestContext` that may be used to grab
                    a DB connection.
    """
    # Create a set of all traits in the os_traits library.
    std_traits = set(os_traits.get_traits())
    # Get the traits in the database
    trait_names = Trait.get_all_names(context)
    db_traits = set(name for name in trait_names
                    if not os_traits.is_custom(name))
    # Determine those traits which are in os_traits but not
    # currently in the database, and insert them.
    need_sync = std_traits - db_traits
    if not need_sync:
        return
    qlines = []
    for num, trait_name in enumerate(need_sync):
        qline = """
                CREATE (t%s:TRAIT {name: '%s', created_at: timestamp(),
                    updated_at: timestamp()})
        """ % (num, trait_name)
        qlines.append(qline)
    query = "\n".join(qlines) + "\nRETURN t0"

    try:
        result = context.tx.run(query).data()
    except db.ClientError:
        pass  # some other process sync'd, just ignore
示例#2
0
def _trait_sync(ctx):
    """Sync the os_traits symbols to the database.

    Reads all symbols from the os_traits library, checks if any of them do
    not exist in the database and bulk-inserts those that are not. This is
    done once per web-service process, at startup.

    :param ctx: `placement.context.RequestContext` that may be used to grab a
                 DB connection.
    """
    # Create a set of all traits in the os_traits library.
    std_traits = set(os_traits.get_traits())
    sel = sa.select([_TRAIT_TBL.c.name])
    res = ctx.session.execute(sel).fetchall()
    # Create a set of all traits in the db that are not custom
    # traits.
    db_traits = set(
        r[0] for r in res
        if not os_traits.is_custom(r[0])
    )
    # Determine those traits which are in os_traits but not
    # currently in the database, and insert them.
    need_sync = std_traits - db_traits
    ins = _TRAIT_TBL.insert()
    batch_args = [
        {'name': six.text_type(trait)}
        for trait in need_sync
    ]
    if batch_args:
        try:
            ctx.session.execute(ins, batch_args)
            LOG.debug("Synced traits from os_traits into API DB: %s",
                      need_sync)
        except db_exc.DBDuplicateEntry:
            pass  # some other process sync'd, just ignore
 def _validate_traits(provider):
     # Check that traits are custom
     additional_traits = set(
         provider.get("traits", {}).get("additional", []))
     trait_conflicts = [
         trait for trait in additional_traits
         if not os_traits.is_custom(trait)
     ]
     if trait_conflicts:
         # sort for more predictable message for testing
         message = _("Invalid traits, only custom traits are allowed: %s"
                     ) % sorted(trait_conflicts)
         raise nova_exc.ProviderConfigException(error=message)
     return additional_traits
 def test_is_custom(self):
     self.assertTrue(ot.is_custom('CUSTOM_FOO'))
     self.assertFalse(ot.is_custom('HW_CPU_X86_SSE42'))