Skip to content
Snippets Groups Projects
admin_service.py 3.69 KiB
Newer Older
  • Learn to ignore specific revisions
  • thomaswoehlke's avatar
    thomaswoehlke committed
    import os
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    import subprocess
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    from database import app
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    
    
    class AdminService:
        def __init__(self, database):
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            app.logger.debug("------------------------------------------------------------")
            app.logger.debug(" Admin Service [init]")
            app.logger.debug("------------------------------------------------------------")
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            self.__database = database
            self.limit_nr = 20
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            app.logger.debug("------------------------------------------------------------")
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            app.logger.info(" Admin Service [ready]")
    
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        def task_database_drop_create(self):
            self.run_admin_database_dump()
            return self
    
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        def run_admin_database_dump(self):
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            app.logger.info(" run database dump [begin]")
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            app.logger.info("------------------------------------------------------------")
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            user = app.config['SQLALCHEMY_POSTGRES_USER']
            url = app.config['SQLALCHEMY_POSTGRES_URL']
            db = app.config['SQLALCHEMY_POSTGRES_DB']
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            cmd = 'pg_dump -U '+user+' -h '+url+' '+db\
                  +' --compress=9 --clean --if-exists --no-tablespaces '\
                  +' --on-conflict-do-nothing --rows-per-insert=200 --column-inserts '\
                  +' --quote-all-identifiers --no-privileges > '\
    
    thomaswoehlke's avatar
    thomaswoehlke committed
                  + '..'+os.sep+'data'+os.sep+'db'+os.sep+'covid19data.sql.gz'
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            app.logger.info(" start: "+str(cmd))
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            returncode = self.__run_ome_shell_command(cmd)
            app.logger.info(" result: " + str(returncode))
            app.logger.info(" run database dump [done]")
            app.logger.info("------------------------------------------------------------")
            return self
    
        @classmethod
        def __run_ome_shell_command(cls, cmd):
            args = [cmd]
            app.logger.info(" start: " + str(cmd))
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            returncode = 0
            try:
                result = subprocess.run(args, shell=True, check=True, capture_output=True, encoding='UTF-8')
                returncode = result.returncode
            except subprocess.CalledProcessError as error:
    
    thomaswoehlke's avatar
    thomaswoehlke committed
                app.logger.warning("WARN: AdminService.__run_ome_shell_command")
                app.logger.warning("cmd    :::" + cmd + ":::")
                app.logger.warning("error  :::" + str(error) + ":::")
                app.logger.warning("WARN: AdminService.__run_ome_shell_command")
            return returncode
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        def run_admin_database_dump_reimport(self):
            app.logger.info(" run database dump reimport [begin]")
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            app.logger.info("------------------------------------------------------------")
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            user = app.config['SQLALCHEMY_POSTGRES_USER']
            url = app.config['SQLALCHEMY_POSTGRES_URL']
            db = app.config['SQLALCHEMY_POSTGRES_DB']
            file_path = '..' + os.sep + '..' + os.sep + 'data' + os.sep + 'db' + os.sep + 'covid19data.sql'
            cmd_list = [
                'gunzip ' + file_path + '.gz',
                'pgsql -U ' + user + ' -h ' + url + ' ' + db + ' < ' + file_path,
                'gzip ' + file_path
            ]
            for cmd in cmd_list:
                returncode = self.__run_ome_shell_command(cmd)
                msg = '[ returncode: ' + str(returncode) + '] ' + cmd
                app.logger.info(msg)
            app.logger.info(" run database dump reimport [done]")
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            app.logger.info("------------------------------------------------------------")
            return self
    
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        def run_admin_database_drop(self):
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            app.logger.info(" run database drop and create [begin]")
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            app.logger.info("------------------------------------------------------------")
            self.__database.drop_all()
            self.__database.create_all()
            app.logger.info("")
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            app.logger.info(" run database drop and create [done]")
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            app.logger.info("------------------------------------------------------------")
            return self