Example #1
0
    def ovs_run_module():
        module = AnsibleModule(
            argument_spec=argument_spec,
            supports_check_mode=supports_check_mode,
        )
        module._ovs_vars = {}

        state = module.params['state']
        op = ops_with_defaults[state]

        if not HAS_OVS:
            module.fail_json(
                msg='Python Open vSwitch library is not installed')

        try:
            schema_path = '{}/{}'.format(ovs.dirs.PKGDATADIR, schema_file)
            remote = 'unix:{}/{}'.format(ovs.dirs.RUNDIR, ctl)
            schema = SchemaHelper(location=schema_path)
            op['register_interest'](schema)
            idl = Idl(remote, schema)

            try:
                wait_for_db_change(idl)
            except Exception as e:
                module.fail_json(msg=str(e), exception=traceback.format_exc())

            op['prepare'](module, idl)

            if not module.check_mode:
                for i in range(3):
                    txn = Transaction(idl)
                    op['build_txn'](module, idl, txn)
                    status = txn.commit_block()
                    if status == Transaction.SUCCESS:
                        break
                    elif status != Transaction.TRY_AGAIN:
                        break

                if status == Transaction.SUCCESS:
                    changed = True
                elif status == Transaction.UNCHANGED:
                    changed = False
                else:
                    msg = op['txn_failure_msg'](module)
                    module.fail_json(msg='{}: {}'.format(msg, status))
            else:
                changed = True

            module.exit_json(changed=changed)
        except Exception as e:
            module.fail_json(msg=str(e), exception=traceback.format_exc())
        finally:
            if 'idl' in locals():
                idl.close()
Example #2
0
def register(extschema, ovsschema, ovsremote):
    """Register interest in all configuration and index
    columns for all tables in ovsschema.

    Args:
        extschema (opslib.RestSchema): This is the
            parsed extended-schema (vswitch.extschema) object.
        ovsschema: OVSDB schema file
        ovsremote: OVSDB remote socket

    Returns:
        ovs.db.idl.Idl instance
    """

    schema_helper = SchemaHelper(ovsschema)

    for tablename, tableschema in extschema.ovs_tables.iteritems():

        register_columns = []

        # configuration columns
        config_columns = [str(key) for key in tableschema.config.keys()]
        # reference columns
        reference_columns = [str(key) for key in tableschema.references.keys()]

        # index columns
        for item in tableschema.index_columns:
            if not item in config_columns:
                register_columns.append(str(item))

        register_columns += config_columns
        register_columns += reference_columns

        # dynamic columns
        if tableschema.dynamic:
            for key in tableschema.dynamic.keys():
                if key not in register_columns:
                    register_columns.append(key)

        # NOTE: remove this when we have a proper
        # solution for TG-1116
        if str(tablename) == 'VLAN':
            register_columns.append('internal_usage')

        schema_helper.register_columns(str(tablename), register_columns)

    idl = ops.opsidl.OpsIdl(ovsremote, schema_helper)
    return idl
Example #3
0
def connect():
    ovsschema = settings.get('cfg_db_schema')
    ovsremote = settings.get('ovs_remote')
    schema_helper = SchemaHelper(ovsschema)
    schema_helper.register_all()
    idl = Idl(ovsremote, schema_helper)

    change_seqno = idl.change_seqno
    while True:
        idl.run()
        if change_seqno != idl.change_seqno:
            break
        poller = ovs.poller.Poller()
        idl.wait(poller)
        poller.block()

    return idl
Example #4
0
    def start(self, register_tables=None, track_all=False):
        try:
            app_log.info("Starting Connection Manager!")

            # Ensure stopping of any existing connection
            self.stop()
            self.schema_helper = SchemaHelper(self.schema)

            # Store registration and tracking info in case initial
            # connection is unsuccessful. If initial connection is unsuccesful,
            # the timeout callback will cause register_tables and track_all
            # to be None.
            if register_tables is not None:
                self.register_tables = register_tables
            if track_all is not False:
                self.track_all = track_all

            if not self.register_tables:
                self.register_schema_helper_columns(self.schema_helper,
                                                    self.rest_schema)
            else:
                for table in self.register_tables:
                    self.schema_helper.register_table(str(table))

            self.idl = OpsIdl(self.remote, self.schema_helper)
            self.curr_seqno = self.idl.change_seqno

            if self.track_all:
                app_log.debug("Tracking all changes")
                self.idl.track_add_all()

            # We do not reset transactions when the DB connection goes down
            if self.transactions is None:
                self.transactions = OvsdbTransactionList()

            self.idl_init()

        except Exception as e:
            app_log.info("Connection Manager failed! Reason: %s" % e)
            self.timeout_handle = \
                IOLoop.current().add_timeout(time.time() + self.timeout,
                                             self.start)