Пример #1
0
    def do_execute(self):
        self.use_cache = asbool(os.environ.get("USE_CACHE", False))

        # Create new publication in admin database
        LOG.info("Log event to publication table in admin database")
        with self.engine.begin() as db:
            dbsession = get_session_factory(db)()
            Publication.new(dbsession)
            dbsession.flush()

        admin_env = {
            "PGHOST": os.environ['PGHOST'],
            "PGUSER": os.environ['PGUSER_ADMIN'],
            "PGPASSWORD": os.environ['PGPASSWORD_ADMIN'],
            "PGDATABASE": os.environ['PGDATABASE_ADMIN'],
        }

        if not self.use_cache or not os.path.isfile(LOCAL_BACKUP_PATH):
            LOG.info("Backup admin database to {}".format(LOCAL_BACKUP_PATH))
            if os.path.isfile(LOCAL_BACKUP_PATH):
                os.unlink(LOCAL_BACKUP_PATH)
            run(
                "pg_dump --no-owner -Fc -f '{}'".format(LOCAL_BACKUP_PATH),
                env=admin_env,
                shell=True,
            )

        LOG.info("Load backup to S3 bucket")
        s3_helper = S3Helper(self.settings)
        s3_helper.upload_file(
            LOCAL_BACKUP_PATH,
            "backups/thinkhazard.{}.backup".format(datetime.utcnow().isoformat())
        )

        public_env = {
            "PGHOST": os.environ['PGHOST'],
            "PGUSER": os.environ['PGUSER_PUBLIC'],
            "PGPASSWORD": os.environ['PGPASSWORD_PUBLIC'],
            "PGDATABASE": os.environ['PGDATABASE_PUBLIC'],
        }

        LOG.info("Restore backup into public database")
        run(
            "pg_restore --exit-on-error --no-owner -n datamart -n processing -f - '{backup}'"
            " | {reset_schemas} 'datamart processing'"
            " | psql --single-transaction -d {database}".format(
                backup=LOCAL_BACKUP_PATH,
                reset_schemas=resource_filename("thinkhazard", "scripts/reset-schemas"),
                database=public_env["PGDATABASE"],
            ),
            env=public_env,
            shell=True,
        )

        if not self.use_cache:
            LOG.info("Delete backup file from filesystem")
            os.remove(LOCAL_BACKUP_PATH)
Пример #2
0
    def run(cls, argv=sys.argv):
        parser = cls.argument_parser()
        args = vars(parser.parse_args(argv[1:]))

        config_uri = args.pop("config_uri")
        name = args.pop("name")
        settings = load_full_settings(config_uri, name=name)

        processor = cls()
        processor.settings = settings
        processor.engine = get_engine(settings, "sqlalchemy.")
        processor.dbsession = get_session_factory(processor.engine)()
        processor.execute(**args)
        processor.dbsession.commit()
Пример #3
0
def initdb(connection, drop_all=False):
    if drop_all:
        if schema_exists(connection, "processing"):
            connection.execute("DROP SCHEMA processing CASCADE;")
        if schema_exists(connection, "datamart"):
            connection.execute("DROP SCHEMA datamart CASCADE;")

    if not schema_exists(connection, "datamart"):
        connection.execute("CREATE SCHEMA datamart;")

    if not schema_exists(connection, "processing"):
        connection.execute("CREATE SCHEMA processing;")

    Base.metadata.create_all(connection)

    dbsession = get_session_factory(connection)()
    populate_datamart(dbsession)
    dbsession.commit()
Пример #4
0
    def __call__(self, filename, options):

        # FIXME find a better way to load settings
        settings = load_full_settings("c2c://development.ini")

        engine = engine_from_config(settings, "sqlalchemy.")
        dbsession = get_session_factory(engine)()

        messages = []

        for rec in dbsession.query(HazardLevel):
            messages.append((rec.title, type(rec).__name__))

        for rec in dbsession.query(HazardType):
            messages.append((rec.title, type(rec).__name__))

        return [
            Message(None, text, None, [], class_name, "", (filename, 1))
            for text, class_name in messages if text != "" and text is not None
        ]
Пример #5
0
    def __call__(self, filename, options):

        # FIXME find a better way to load settings
        settings = load_full_settings("c2c://development.ini")

        engine = engine_from_config(settings, "sqlalchemy.")
        dbsession = get_session_factory(engine)()

        messages = []

        for rec in dbsession.query(ClimateChangeRecommendation):
            messages.append((rec.text, type(rec).__name__))

        for rec in dbsession.query(HazardCategory):
            messages.append((rec.general_recommendation, type(rec).__name__))

        for rec in dbsession.query(TechnicalRecommendation):
            messages.append((rec.text, type(rec).__name__))
            messages.append((rec.detail, type(rec).__name__))

        return [
            Message(None, text, None, [], class_name, "", (filename, 1))
            for text, class_name in messages if text != "" and text is not None
        ]
Пример #6
0
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# ThinkHazard 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 General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# ThinkHazard.  If not, see <http://www.gnu.org/licenses/>.

import transaction
from thinkhazard.session import get_engine, get_session_factory, get_tm_session
from thinkhazard.settings import load_full_settings
from thinkhazard.scripts import wait_for_db
from thinkhazard.scripts.initializedb import initdb

settings = load_full_settings("c2c://tests.ini", name="admin")
engine = get_engine(settings)
session_factory = get_session_factory(engine)
DBSession = get_tm_session(session_factory, transaction.manager)


def populatedb():
    wait_for_db(engine)
    with engine.begin() as connection:
        initdb(connection, True)


populatedb()