2020-01-05 01:00:06 +01:00
|
|
|
from subprocess import run
|
2020-01-04 17:44:51 +01:00
|
|
|
from sys import argv
|
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
from flask import Flask
|
|
|
|
from fields import Glacier, Annual_Data, Annual_Change, User
|
2020-01-02 21:36:35 +01:00
|
|
|
|
2020-01-05 01:00:06 +01:00
|
|
|
global db
|
|
|
|
|
2020-01-02 04:27:45 +01:00
|
|
|
|
2020-01-04 17:44:51 +01:00
|
|
|
def create_database(db_name, db_user, db_pw):
|
|
|
|
script = "mariadb_setup.sh"
|
2020-01-05 01:00:06 +01:00
|
|
|
output = run([script + db_name + db_user + db_pw])
|
|
|
|
if output.returncode != 0:
|
2020-01-04 17:44:51 +01:00
|
|
|
print("Error: couldn't create database")
|
|
|
|
exit()
|
2020-01-02 04:27:45 +01:00
|
|
|
|
2020-01-04 17:44:51 +01:00
|
|
|
|
|
|
|
def create_connection(db_name, db_user, db_pw):
|
|
|
|
host = "localhost:3306"
|
|
|
|
connection_uri = "mysql+pymysql://{user}:{pw}@{url}/{db}".format(
|
|
|
|
user=db_user, pw=db_pw, url=host, db=db_name
|
2020-01-02 04:27:45 +01:00
|
|
|
)
|
2020-01-04 17:44:51 +01:00
|
|
|
app = Flask(__name__)
|
|
|
|
app.config["SQLALCHEMY_DATABASE_URI"] = connection_uri
|
|
|
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
|
|
|
global db
|
|
|
|
db = SQLAlchemy(app)
|
|
|
|
|
|
|
|
|
|
|
|
def create_tables(db):
|
|
|
|
db.create_all()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
if len(argv) != 3:
|
2020-01-05 01:00:06 +01:00
|
|
|
print("Usage: " + argv[0] + " <db name> <db user> <db password>")
|
2020-01-04 17:44:51 +01:00
|
|
|
exit()
|
2020-01-02 21:36:35 +01:00
|
|
|
|
2020-01-04 17:44:51 +01:00
|
|
|
db_name = str(argv[1])
|
|
|
|
db_user = str(argv[2])
|
|
|
|
db_password = str(argv[3])
|
2020-01-02 21:36:35 +01:00
|
|
|
|
2020-01-04 17:44:51 +01:00
|
|
|
create_database(db_name, db_user, db_password)
|
|
|
|
create_connection(db_name, db_user, db_password)
|
|
|
|
create_tables(db)
|
2020-01-02 04:27:45 +01:00
|
|
|
|
|
|
|
|
2020-01-04 17:44:51 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|