Commit 1dea6942 authored by Nelso Jost's avatar Nelso Jost

FIX: mysql deployment

parent 8566df61
......@@ -4,6 +4,7 @@ PORT := 8000
SERVER := nginx-gunicorn
HOST := localhost
VENV := .venv
VENVPY := ${VENV}/bin/python
all: help
......@@ -62,32 +63,38 @@ clean-venv:
initdb:
@ echo "-------------------------------------------------------"
${VENV}/bin/python manage.py initdb
${VENVPY} manage.py initdb
run:
@ echo "-------------------------------------------------------"
export EMMCONFIG=dev && ${VENV}/bin/python manage.py runserver --host ${HOST} --port 5000
${VENVPY} manage.py runserver --host ${HOST} --port 5000
shell:
${VENV}/bin/ipython manage.py shell
fakelog:
${VENV}/bin/python tests/fakemeteorolog.py ${BOARD_ID}
${VENVPY} tests/fakemeteorolog.py ${BOARD_ID}
tree:
tree . -I "${VENV}|__pycache__|*.pyc"
mysql-client:
mysql -u root -p
mysql-chroot:
${VENVPY} production/mysqlsetup.py chroot
mysql-setup:
mysql -u root -p < production/MYSQL_SETUP
${VENVPY} production/mysqlsetup.py setup
mysql-info:
mysql -u root -p < production/MYSQL_FETCH_INFO
${VENVPY} production/mysqlsetup.py info
deploy:
sudo ${VENV}/bin/python production/nginx-gunicorn/deploy.py --port=${PORT}
sudo ${VENVPY} production/nginx-gunicorn/deploy.py --port=${PORT}
undeploy:
sudo ${VENV}/bin/python production/nginx-gunicorn/deploy.py -u
sudo ${VENVPY} production/nginx-gunicorn/deploy.py -u
clean-logs-nginx:
rm -rf production/nginx-gunicorn/logs
......
......@@ -47,8 +47,8 @@ def create_app(config_name):
app.config.from_object(config[config_name])
config[config_name].init_app(app)
with open(path_here('../data/DB_INIT.json')) as f:
app.config['DB_INIT'] = json.load(f)
with open(path_here('../data/DB_INIT.json'), encoding='utf-8') as f:
app.config['DB_INIT'] = json.loads(f.read())
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
......
......@@ -53,10 +53,17 @@ class ProductionConfig(Config):
DEBUG = False
# format: mysql+DRIVER://USER:PASSWORD@localhost/DATABASE
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://ctaemm:ctaemm@localhost/emmdb'
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://ctaemm:{}@localhost/emmdb'\
.format(os.environ.get('EMMPASS'))
class TestingConfig(ProductionConfig):
# activat es auto reload on file changes and werkzeug's debugger
DEBUG = True
config = {'dev': DevelopmentConfig,
'test': TestingConfig,
'prod': ProductionConfig,
'default': DevelopmentConfig}
......
......@@ -9,7 +9,7 @@ class Unity(db.Model, DBUtils):
__tablename__ = 'unities'
id = db.Column(db.Integer, primary_key=True)
label = db.Column(db.String(10), unique=True, nullable=False)
label = db.Column(db.String(30), unique=True, nullable=False)
latex_label = db.Column(db.String(150), unique=True)
unity_measurements = db.relationship('UnityMeasurement', backref='unity',
......@@ -23,7 +23,7 @@ class Measurement(db.Model, DBUtils):
__tablename__ = 'measurements'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20), unique=True, nullable=False)
name = db.Column(db.String(50), unique=True, nullable=False)
label_br = db.Column(db.String(50), unique=True, nullable=False)
label_en = db.Column(db.String(50), unique=True, nullable=False)
......@@ -45,10 +45,11 @@ class Measurement(db.Model, DBUtils):
if unities:
db.session.flush()
for u in unities:
UnityMeasurement.add(measurement_id=measurement.id,
um = UnityMeasurement.add(measurement_id=measurement.id,
unity_id=Unity.get(label=u).id)
return measurement
class UnityMeasurement(db.Model, DBUtils):
__tablename__ = 'unitymeasurements'
......
......@@ -130,8 +130,6 @@ def initdb():
Unity.import_from_json(*json_db['UNITIES'])
Measurement.import_from_json(*json_db['MEASUREMENTS'])
db.session.commit()
for board_nickname in ('Pezzi', 'CAP'):
Board.add(nickname=board_nickname, user=user_admin,
sensors=json_db['SUPPORTED_SENSORS'])
......
SELECT table_schema emmdb,
sum( data_length + index_length ) / 1024 / 1024 "Data Base Size in MB",
sum( data_free )/ 1024 / 1024 "Free Space in MB"
FROM information_schema.TABLES
GROUP BY table_schema ;
#DROP DATABASE emmdb;
CREATE DATABASE emmdb CHARACTER SET utf8 COLLATE utf8_bin;
# DROP USER 'ctaemm'@'localhost';
CREATE USER 'ctaemm'@'localhost' identified by 'ctaemm';
GRANT ALL PRIVILEGES on emmdb.* to 'ctaemm'@'localhost';
FLUSH PRIVILEGES;
import sys
import os
if not 'EMMPASS' in os.environ:
os.environ['EMMPASS'] = input('EMMPASS=')
DATABASE_NAME = 'emmdb'
DATABASE_USER = 'ctaemm'
EMMPASS = os.environ['EMMPASS']
SCRIPT_CHANGE_ROOT_PASSWORD =\
"""\
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('{EMMPASS}')
""".format(**globals())
SCRIPT_CREATE_ALL =\
"""\
CREATE DATABASE IF NOT EXISTS {DATABASE_NAME}
CHARACTER SET utf8 COLLATE utf8_bin;
CREATE USER '{DATABASE_USER}'@'localhost' IDENTIFIED BY '{EMMPASS}';
GRANT ALL PRIVILEGES on {DATABASE_NAME}.* to '{DATABASE_USER}'@'localhost';
FLUSH PRIVILEGES;
""".format(**globals())
SCRIPT_FETCH_INFO =\
"""\
SELECT table_schema {DATABASE_NAME},
sum( data_length + index_length ) / 1024 / 1024 "Data Base Size in MB",
sum( data_free )/ 1024 / 1024 "Free Space in MB"
FROM information_schema.TABLES
GROUP BY table_schema ;
""".format(**globals())
def mysql_chroot():
with open(SCRIPT_CHANGE_ROOT_PASSWORD_FILENAME, 'w') as f:
f.write(SCRIPT_CHANGE_ROOT_PASSWORD)
os.system("sudo mysqld_safe --init-file=mysql-init.temp")
os.remove(SCRIPT_CHANGE_ROOT_PASSWORD_FILENAME)
def mysql_exec_script(script):
'''
Execute the MYSQL script by creating a temp file and
'''
filename = '.mysqlscript.temp'
with open(filename, 'w') as f:
f.write(script)
os.system("sudo mysql -u root -p < {}".format(filename))
os.remove(filename)
if len(sys.argv) > 1:
if sys.argv[1] == 'chroot':
mysql_chroot()
elif sys.argv[1] == 'setup':
mysql_exec_script(SCRIPT_CREATE_ALL)
elif sys.argv[1] == 'info':
mysql_exec_script(SCRIPT_FETCH_INFO)
......@@ -5,6 +5,10 @@ import sys
import subprocess
import time
if not 'EMMPASS' in os.environ:
os.environ['EMMPASS'] = input('EMMPASS=')
EMMPASS = os.environ['EMMPASS']
parser = argparse.ArgumentParser(
description="Deploy the web app onto the production server")
parser.add_argument('-u', dest='undeploy', action='store_const', const=True,
......@@ -17,17 +21,6 @@ args = vars(parser.parse_args())
def make_path_here(filename):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), filename)
EMMPASS = None
if not args['undeploy']:
try:
EMMPASS = os.environ['EMMPASS']
print('EMMPASS=', EMMPASS, sep='')
if not EMMPASS:
raise Exception
except:
EMMPASS = input('EMMPASS=')
print('EMMPASS=', EMMPASS, sep='')
# this assumes that the deploy script was launched from the root directory
# of the project
......
......@@ -36,7 +36,7 @@ class ReverseProxied(object):
return self.app(environ, start_response)
app = create_app(config_name='prod')
app = create_app(config_name='test')
app.wsgi_app = ReverseProxied(app.wsgi_app)
if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment