def main(): if len(sys.argv) == 1: print('Error: Config file must be specified.') print('nlbaas2octavia --config-file <filename>') return 1 logging.register_options(cfg.CONF) cfg.CONF(args=sys.argv[1:], project='nlbaas2octavia', version='nlbaas2octavia 1.0') logging.set_defaults() logging.setup(cfg.CONF, 'nlbaas2octavia') LOG = logging.getLogger('nlbaas2octavia') CONF.log_opt_values(LOG, logging.DEBUG) if not CONF.all and not CONF.lb_id and not CONF.project_id: print('Error: One of --all, --lb_id, --project_id must be specified.') return 1 if ((CONF.all and (CONF.lb_id or CONF.project_id)) or (CONF.lb_id and CONF.project_id)): print('Error: Only one of --all, --lb_id, --project_id allowed.') return 1 neutron_context_manager = enginefacade.transaction_context() neutron_context_manager.configure( connection=CONF.migration.neutron_db_connection) n_session_maker = neutron_context_manager.writer.get_sessionmaker() octavia_context_manager = enginefacade.transaction_context() octavia_context_manager.configure( connection=CONF.migration.octavia_db_connection) o_session_maker = octavia_context_manager.writer.get_sessionmaker() LOG.info('Starting migration.') n_session = n_session_maker(autocommit=True) lb_id_list = [] if CONF.lb_id: lb_id_list = [[CONF.lb_id]] elif CONF.project_id: lb_id_list = n_session.execute( "SELECT id FROM neutron.lbaas_loadbalancers WHERE " "project_id = :id AND provisioning_status = 'ACTIVE';", { 'id': CONF.project_id }).fetchall() else: # CONF.ALL lb_id_list = n_session.execute( "SELECT id FROM neutron.lbaas_loadbalancers WHERE " "provisioning_status = 'ACTIVE';").fetchall() failure_count = 0 for lb in lb_id_list: failure_count += migrate_lb(LOG, n_session_maker, o_session_maker, lb[0]) if failure_count: sys.exit(1)
def _get_main_context_manager(): global _main_context_manager if not _main_context_manager: _main_context_manager = enginefacade.transaction_context() return _main_context_manager
def find(cls, resource_type, resource_id): context = enginefacade.transaction_context() with enginefacade.reader.using(context) as session: mapping = session.query(ResourceMapping).filter_by( resource_type=resource_type, resource_id=resource_id.replace("-", "")).first() return mapping
def _create_context_manager(): global _CTX_MANAGER if _CTX_MANAGER is None: _CTX_MANAGER = enginefacade.transaction_context() _CTX_MANAGER.configure(sqlite_fk=True, flush_on_subtransaction=True) return _CTX_MANAGER
def _create_context_manager(): _ctx_mgr = enginefacade.transaction_context() # TODO(aarefiev): enable foreign keys for SQLite once all unit # tests with failed constraint will be fixed. _ctx_mgr.configure(sqlite_fk=False) return _ctx_mgr
def test_db_fixture(self): normal_mgr = enginefacade.transaction_context() normal_mgr.configure(connection="sqlite://", sqlite_fk=True, mysql_sql_mode="FOOBAR", max_overflow=38) class MyFixture(test_fixtures.OpportunisticDbFixture): def get_enginefacade(self): return normal_mgr test = mock.Mock(SCHEMA_SCOPE=None) fixture = MyFixture(test=test) resources = fixture._get_resources() testresources.setUpResources(test, resources, None) self.addCleanup(testresources.tearDownResources, test, resources, None) fixture.setUp() self.addCleanup(fixture.cleanUp) self.assertTrue(normal_mgr._factory._started) test.engine = normal_mgr.writer.get_engine() self.assertEqual("sqlite://", str(test.engine.url)) self.assertIs(test.engine, normal_mgr._factory._writer_engine) engine_args = normal_mgr._factory._engine_args_for_conf(None) self.assertTrue(engine_args['sqlite_fk']) self.assertEqual("FOOBAR", engine_args["mysql_sql_mode"]) self.assertEqual(38, engine_args["max_overflow"]) fixture.cleanUp() fixture._clear_cleanups() # so the real cleanUp works self.assertFalse(normal_mgr._factory._started)
def create_context_manager(connection=None): """Create a database context manager object. : param connection: The database connection string """ ctxt_mgr = enginefacade.transaction_context() ctxt_mgr.configure(**_get_db_conf(CONF.database, connection=connection)) return ctxt_mgr
def create_context_manager(connection=None): """Create a database context manager object. :param connection: The database connection string """ ctxt_mgr = enginefacade.transaction_context() ctxt_mgr.configure(**_get_db_conf(CONF.database, connection=connection)) return ctxt_mgr
def _setup_db_sessions(): neutron_context_manager = enginefacade.transaction_context() neutron_context_manager.configure( connection=CONF.migration.neutron_db_connection) n_session_maker = neutron_context_manager.writer.get_sessionmaker() n_session = n_session_maker(autocommit=False) octavia_context_manager = enginefacade.transaction_context() octavia_context_manager.configure( connection=CONF.migration.octavia_db_connection) o_session_maker = octavia_context_manager.writer.get_sessionmaker() o_session = o_session_maker(autocommit=False) db_sessions = {'n_session': n_session, 'o_session': o_session} if CONF.migration.keystone_db_connection: keystone_context_manager = enginefacade.transaction_context() keystone_context_manager.configure( connection=CONF.migration.keystone_db_connection) k_session_maker = keystone_context_manager.writer.get_sessionmaker() k_session = k_session_maker(autocommit=False) db_sessions['k_session'] = k_session if CONF.migration.a10_nlbaas_db_connection: a10_nlbaas_context_manager = enginefacade.transaction_context() a10_nlbaas_context_manager.configure( connection=CONF.migration.a10_nlbaas_db_connection) a10_nlbaas_session_maker = a10_nlbaas_context_manager.writer.get_sessionmaker( ) a10_nlbaas_session = a10_nlbaas_session_maker(autocommit=False) else: a10_nlbaas_session = n_session db_sessions['a10_nlbaas_session'] = a10_nlbaas_session if CONF.migration.a10_oct_db_connection: a10_oct_context_manager = enginefacade.transaction_context() a10_oct_context_manager.configure( connection=CONF.migration.a10_oct_db_connection) a10_oct_session_maker = a10_oct_context_manager.writer.get_sessionmaker( ) a10_oct_session = a10_oct_session_maker(autocommit=False) else: a10_oct_session = o_session db_sessions['a10_oct_session'] = a10_oct_session return db_sessions
def find(cls, resource_type, resource_id): context = enginefacade.transaction_context() with enginefacade.reader.using(context) as session: mapping = session.query(ResourceMapping).filter_by( resource_type=resource_type, resource_id=resource_id.replace("-", "") ).first() return mapping
def _get_main_context_manager(): global _main_context_manager if not _main_context_manager: _main_context_manager = enginefacade.transaction_context() cfg.CONF.import_group('profiler', 'senlin.common.config') if cfg.CONF.profiler.enabled: if cfg.CONF.profiler.trace_sqlalchemy: eng = _main_context_manager.get_legacy_facade().get_engine() osprofiler.sqlalchemy.add_tracing(sqlalchemy, eng, "db") return _main_context_manager
def _get_main_context_manager(): # TODO(DinaBelova): add DB profiling # this requires oslo.db modification for proper format and functionality # will be done in Newton timeframe global _main_context_manager if not _main_context_manager: _main_context_manager = enginefacade.transaction_context() return _main_context_manager
def main(): # Initialize the cli options setup_conf() config.setup_logging() # Get the neutron DB session neutron_context_manager = enginefacade.transaction_context() neutron_context_manager.configure( connection=cfg.CONF.neutron_db_connection) n_session_maker = neutron_context_manager.writer.get_sessionmaker() n_session = n_session_maker(autocommit=True) # Run DB migration migrate_fwaas_v1_to_v2(n_session) LOG.info("DB migration done.")
def main(): logging.register_options(cfg.CONF) cfg.CONF(args=sys.argv[1:], project='stresshm', version='stresshm 1.0') logging.set_defaults() logging.setup(cfg.CONF, 'stresshm') LOG = logging.getLogger(__name__) octavia_context_manager = enginefacade.transaction_context() octavia_context_manager.configure( connection=CONF.test_params.octavia_db_connection) o_session_maker = octavia_context_manager.writer.get_sessionmaker() if CONF.db_create_only: LOG.info('Your run prefix ID is: %s' % PREFIX) setup_db(o_session_maker, PREFIX) return if CONF.clean_db: cleanup_db(o_session_maker, CONF.clean_db) return LOG.info('Your run prefix ID is: %s' % PREFIX) lbs = setup_db(o_session_maker, PREFIX) exit_event = multiprocessing.Event() processes = [] for i in range(CONF.test_params.load_balancers): for amp_id in lbs[i]['amphorae']: amp = multiprocessing.Process(name='amp' + str(i), target=amp_sim, args=(exit_event, amp_id, lbs[i])) processes.append(amp) amp.start() time.sleep(CONF.test_params.test_runtime_secs) exit_event.set() for process in processes: process.join() cleanup_db(o_session_maker, PREFIX) return
def test_db_fixture(self): normal_mgr = enginefacade.transaction_context() normal_mgr.configure( connection="sqlite://", sqlite_fk=True, mysql_sql_mode="FOOBAR", max_overflow=38 ) class MyFixture(test_fixtures.OpportunisticDbFixture): def get_enginefacade(self): return normal_mgr test = mock.Mock(SCHEMA_SCOPE=None) fixture = MyFixture(test=test) resources = fixture._get_resources() testresources.setUpResources(test, resources, None) self.addCleanup( testresources.tearDownResources, test, resources, None ) fixture.setUp() self.addCleanup(fixture.cleanUp) self.assertTrue(normal_mgr._factory._started) test.engine = normal_mgr.writer.get_engine() self.assertEqual("sqlite://", str(test.engine.url)) self.assertIs(test.engine, normal_mgr._factory._writer_engine) engine_args = normal_mgr._factory._engine_args_for_conf(None) self.assertTrue(engine_args['sqlite_fk']) self.assertEqual("FOOBAR", engine_args["mysql_sql_mode"]) self.assertEqual(38, engine_args["max_overflow"]) fixture.cleanUp() fixture._clear_cleanups() # so the real cleanUp works self.assertFalse(normal_mgr._factory._started)
def delete(entity): context = enginefacade.transaction_context() with enginefacade.writer.using(context) as session: session.delete(entity)
def insert(entity): context = enginefacade.transaction_context() with enginefacade.writer.using(context) as session: session.add(entity)
def __init__(self, context, conf): self._transaction = enginefacade.transaction_context() self._transaction.configure( **dict(conf.database.items()) ) self._context = context
import contextlib from debtcollector import moves from oslo_config import cfg from oslo_db import api as oslo_db_api from oslo_db import exception as db_exc from oslo_db.sqlalchemy import enginefacade from oslo_utils import excutils import osprofiler.sqlalchemy import sqlalchemy from sqlalchemy.orm import exc from neutron.common import exceptions from neutron.common import profiler # noqa context_manager = enginefacade.transaction_context() _FACADE = None MAX_RETRIES = 10 def is_retriable(e): if _is_nested_instance(e, (db_exc.DBDeadlock, exc.StaleDataError)): return True # looking savepoints mangled by deadlocks. see bug/1590298 for details. return _is_nested_instance(e, db_exc.DBError) and '1305' in str(e) is_deadlock = moves.moved_function(is_retriable, 'is_deadlock', __name__, message='use "is_retriable" instead',
def enable_sqlite_foreign_key(): global _main_context_manager if not _main_context_manager: _main_context_manager = enginefacade.transaction_context() _main_context_manager.configure(sqlite_fk=True)
def create_context_manager(connection=None): print('called nova.db.sqlalchemy.api.create_context_manager') ctxt_mgr = enginefacade.transaction_context() ctxt_mgr.configure(**_get_db_conf(CONF.database, connection=connection)) return ctxt_mgr
def __init__(self, conf): self.trans = enginefacade.transaction_context() self.trans.configure(**dict(conf.database.items())) self._context = threading.local()
from oslo_db.sqlalchemy import enginefacade # The maximum value a signed INT type may have MAX_INT = 0x7FFFFFFF # NOTE(dosaboy): This is supposed to represent the maximum value that we can # place into a SQL single precision float so that we can check whether values # are oversize. Postgres and MySQL both define this as their max whereas Sqlite # uses dynamic typing so this would not apply. Different dbs react in different # ways to oversize values e.g. postgres will raise an exception while mysql # will round off the value. Nevertheless we may still want to know prior to # insert whether the value is oversize or not. SQL_SP_FLOAT_MAX = 3.40282e+38 main_context_manager = enginefacade.transaction_context() def _context_manager_from_context(context): if context: try: return context.db_connection except AttributeError: pass def get_context_manager(context): """Get a database context manager object. :param context: The request context that can contain a context manager """
def __init__(self, conf): self.trans = enginefacade.transaction_context() self.trans.configure( **dict(conf.database.items()) ) self._context = threading.local()
# a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. """Database context manager for placement database connection, kept in its own file so the nova db_api (which has cascading imports) is not imported. """ from oslo_db.sqlalchemy import enginefacade placement_context_manager = enginefacade.transaction_context() def _get_db_conf(conf_group): return dict(conf_group.items()) def configure(conf): # If [placement_database]/connection is not set in conf, then placement # data will be stored in the nova_api database. if conf.placement_database.connection is None: placement_context_manager.configure(**_get_db_conf(conf.api_database)) else: placement_context_manager.configure( **_get_db_conf(conf.placement_database))