def write_pgpass(self): # Only save password if using password authentication if self.auth_type != PASSWORD_AUTHENTICATION: return write_pg_pass(self.settings.dbname, self.settings.address, self.settings.port, self.settings.username, self.settings.password)
def _try_connect(self): from stoqlib.lib.message import error try: store_uri = self._config.get_settings().get_store_uri() except: type, value, trace = sys.exc_info() error( _("Could not open the database config file"), _("Invalid config file settings, got error '%s', " "of type '%s'") % (value, type)) from stoqlib.database.exceptions import PostgreSQLError from stoqlib.database.runtime import get_default_store from stoqlib.exceptions import DatabaseError from stoqlib.lib.pgpass import write_pg_pass from stoq.lib.startup import setup # XXX: progress dialog for connecting (if it takes more than # 2 seconds) or creating the database log.debug('calling setup()') try: setup(self._config, self._options, register_station=False, check_schema=False, load_plugins=False) # the setup call above is not really trying to connect (since # register_station, check_schema and load_plugins are all False). # Try to really connect here. get_default_store() except (StoqlibError, PostgreSQLError) as e: log.debug('Connection failed.') error(_('Could not connect to the database'), 'error=%s uri=%s' % (str(e), store_uri)) except DatabaseError: log.debug('Connection failed. Tring to setup .pgpass') # This is probably a missing password configuration. Setup the # pgpass file and try again. try: password = self._get_password() if not password: # There is no password stored in data file. Abort raise from stoqlib.database.settings import db_settings write_pg_pass(db_settings.dbname, db_settings.address, db_settings.port, db_settings.username, password) # Now that there is a pg_pass file, try to connect again get_default_store() except DatabaseError as e: log.debug('Connection failed again.') error(_('Could not connect to the database'), 'error=%s uri=%s' % (str(e), store_uri))
def _try_connect(self): from stoqlib.lib.message import error try: store_dsn = self._config.get_settings().get_store_dsn() except: type, value, trace = sys.exc_info() error(_("Could not open the database config file"), _("Invalid config file settings, got error '%s', " "of type '%s'") % (value, type)) from stoqlib.database.exceptions import PostgreSQLError from stoqlib.database.runtime import get_default_store from stoqlib.exceptions import DatabaseError from stoqlib.lib.pgpass import write_pg_pass from stoq.lib.startup import setup # XXX: progress dialog for connecting (if it takes more than # 2 seconds) or creating the database log.debug('calling setup()') try: setup(self._config, self._options, register_station=False, check_schema=False, load_plugins=False) # the setup call above is not really trying to connect (since # register_station, check_schema and load_plugins are all False). # Try to really connect here. get_default_store() except (StoqlibError, PostgreSQLError) as e: log.debug('Connection failed.') error(_('Could not connect to the database'), 'error=%s uri=%s' % (str(e), store_dsn)) except DatabaseError: log.debug('Connection failed. Tring to setup .pgpass') # This is probably a missing password configuration. Setup the # pgpass file and try again. password = self._get_password() if not password: # There is no password stored in data file. Abort raise from stoqlib.database.settings import db_settings write_pg_pass(db_settings.dbname, db_settings.address, db_settings.port, db_settings.username, password) # Now that there is a pg_pass file, try to connect again try: get_default_store() except DatabaseError as e: log.debug('Connection failed again.') error(_('Could not connect to the database'), 'error=%s uri=%s' % (str(e), store_dsn))
def cmd_init(self, options): """Creates and initializes a database""" # Create a database user before trying to connect if options.create_dbuser: if not options.username: raise SystemExit("This option requires a --username set") retval = self._create_dbuser(options.username) if retval != 0: return retval config = self._read_config(options, register_station=False, check_schema=False, load_plugins=False) from stoqlib.database.admin import initialize_system from stoqlib.database.runtime import set_default_store from stoqlib.database.settings import db_settings from stoqlib.lib.pgpass import write_pg_pass from stoqlib.net.server import ServerProxy if options.dbname: db_settings.dbname = options.dbname if options.address: db_settings.address = options.address if options.port: db_settings.port = options.port if options.username: db_settings.username = options.username if options.password: db_settings.password = options.password # a password was sent via command line. Make sure we can run psql by # setting up pgpass write_pg_pass(db_settings.dbname, db_settings.address, db_settings.port, db_settings.username, db_settings.password) server = ServerProxy() running = server.check_running() if running: server.call('pause_tasks') # ServerProxy may have opened a store set_default_store(None) try: initialize_system(password='', force=options.force, empty=options.empty) except ValueError as e: # Database server is missing pg_trgm if 'pg_trgm' in str(e): return 31 else: raise if options.create_examples or options.demo: from stoqlib.importers.stoqlibexamples import create create(utilities=True) if options.register_station and not options.empty: self._register_station() if options.pre_plugins: self._register_plugins(str(options.pre_plugins).split(',')) if options.plugins: self._enable_plugins(str(options.plugins).split(',')) if options.demo: self._enable_demo() config.flush() # The schema was upgraded. If it was running before, # restart it so it can load the new code if running: server.call('restart') return 0
def cmd_init(self, options): """Creates and initializes a database""" # Create a database user before trying to connect if options.create_dbuser: if not options.username: raise SystemExit( "This option requires a --username set") retval = self._create_dbuser(options.username) if retval != 0: return retval config = self._read_config(options, register_station=False, check_schema=False, load_plugins=False) from stoqlib.database.admin import initialize_system from stoqlib.database.runtime import set_default_store from stoqlib.database.settings import db_settings from stoqlib.lib.pgpass import write_pg_pass from stoqlib.net.server import ServerProxy if options.dbname: db_settings.dbname = options.dbname if options.address: db_settings.address = options.address if options.port: db_settings.port = options.port if options.username: db_settings.username = options.username if options.password: db_settings.password = options.password # a password was sent via command line. Make sure we can run psql by # setting up pgpass write_pg_pass(db_settings.dbname, db_settings.address, db_settings.port, db_settings.username, db_settings.password) server = ServerProxy() running = server.check_running() if running: server.call('pause_tasks') # ServerProxy may have opened a store set_default_store(None) try: initialize_system(password='', force=options.force, empty=options.empty) except ValueError as e: # Database server is missing pg_trgm if 'pg_trgm' in str(e): return 31 else: raise if options.create_examples or options.demo: from stoqlib.importers.stoqlibexamples import create create(utilities=True) if options.register_station and not options.empty: self._register_station() if options.pre_plugins: self._register_plugins(str(options.pre_plugins).split(',')) if options.plugins: self._enable_plugins(str(options.plugins).split(',')) if options.demo: self._enable_demo() config.flush() # The schema was upgraded. If it was running before, # restart it so it can load the new code if running: server.call('restart') return 0