# -*- coding: utf-8 -*-# vim: tabstop=4 shiftwidth=4 softtabstop=4## Copyright (C) 2016-2023 GEM Foundation## OpenQuake is free software: you can redistribute it and/or modify it# under the terms of the GNU Affero General Public License as published# by the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## OpenQuake is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with OpenQuake. If not, see <http://www.gnu.org/licenses/>.importosimportsysimportsignalimportgetpassfromopenquake.baselibimportconfigfromopenquake.commonlibimportlogs,dbapifromopenquake.serverimportdbserver,db
[docs]defmain(cmd,dbhostport=None,foreground=False,*,loglevel='INFO'):""" start/stop/restart the database server, or return its status """ifconfig.multi_user:user=getpass.getuser()ifuser!='openquake':sys.exit(f'Only user openquake can start the dbserver 'f'but you are {user}')ifcmd=='upgrade':applied=db.actions.upgrade_db(dbapi.db)ifapplied:print('Applied upgrades',applied)else:print('Already upgraded')dbapi.db.close()returnifos.environ.get('OQ_DATABASE',config.dbserver.host)=='local':print('Doing nothing since OQ_DATABASE=local')returnstatus=dbserver.get_status()ifcmd=='status':print('dbserver '+status)elifcmd=='stop':ifstatus=='running':pid=logs.dbcmd('getpid')os.kill(pid,signal.SIGINT)# this is trapped by the DbServerelse:print('dbserver already stopped')elifcmd=='start':ifstatus=='not-running':dbserver.run_server(dbhostport,loglevel,foreground)else:print('dbserver already running')
main.cmd=dict(help='dbserver command',choices='start stop status upgrade'.split())main.dbhostport='dbhost:port'main.foreground='stay in foreground'main.loglevel='DEBUG|INFO|WARN'