Compare commits

...

2 Commits

Author SHA1 Message Date
simon 635983f347
implement maintenance mode env var 2022-09-19 21:00:10 +07:00
simon 9285a1ea32
deploy to prod and testing, sync db 2022-09-19 20:59:26 +07:00
4 changed files with 128 additions and 30 deletions

View File

@ -1,20 +1,68 @@
#!/bin/bash
# upload project to vps2
host="vps2"
remote_host="vps2"
local_host="lpb-air.local"
rsync --progress -a docker-compose.yml "$host":docker/
rsync --progress -a env "$host":docker/
rsync --progress -a --delete-after helper_scripts "$host":docker/
rsync --progress -a --delete-after nginx "$host":docker/
rsync --progress -a --delete-after \
--exclude config.json.sample \
--exclude __pycache__ \
--exclude static/dyn \
web "$host":docker/
# rebuild production
function sync_docker {
rsync --progress --ignore-existing -a docker-compose_prod.yml "$remote_host":docker/docker-compose.yml
rsync --progress -a env "$remote_host":docker/
rsync --progress -a --delete-after helper_scripts "$remote_host":docker/
rsync --progress -a --delete-after nginx "$remote_host":docker/
rsync --progress -a --delete-after \
--exclude config.json.sample \
--exclude __pycache__ \
--exclude static/dyn \
web "$remote_host":docker/
ssh "$host" 'docker build -t bbilly1/lpb-air:latest docker/web'
ssh "$host" 'docker-compose -f docker/docker-compose.yml up -d'
ssh "$remote_host" 'docker build -t bbilly1/lpb-air:latest docker/web'
ssh "$remote_host" 'docker-compose -f docker/docker-compose.yml up -d'
}
# rebuild testing
function sync_test {
ssh "$local_host" "mkdir -p docker"
rsync --progress --ignore-existing -a docker-compose_testing.yml "$local_host":docker/docker-compose.yml
rsync --progress -a env "$local_host":docker/
rsync --progress -a --delete-after helper_scripts "$local_host":docker/
rsync --progress -a --delete-after nginx "$local_host":docker/
rsync --progress -a --delete-after \
--exclude config.json.sample \
--exclude __pycache__ \
--exclude static/dyn \
web "$local_host":docker/
ssh "$local_host" 'docker build -t bbilly1/lpb-air:latest docker/web'
ssh "$local_host" 'docker compose -f docker/docker-compose.yml up -d'
}
# sync prod db to testing
function sync_db {
newest_backup=$(ssh $remote_host 'find backup -type f -name "pg_*.gz" | sort | tail -n 1')
rsync --progress -e ssh $remote_host:"$newest_backup" .
rsync --progress -e ssh "$(basename "$newest_backup")" $local_host:backup/backup.gz
ssh $local_host "gzip -d backup/backup.gz"
ssh $local_host "docker exec -i postgres psql -U aqi -d aqi < backup/backup"
ssh $local_host "docker compose -f docker/docker-compose.yml restart"
}
if [[ $1 == "test" ]]; then
sync_test "$2"
elif [[ $1 == "docker" ]]; then
sync_docker
elif [[ $1 == "db" ]]; then
sync_db
else
echo "valid options are: test | docker | db"
fi
##
exit 0

View File

@ -300,6 +300,18 @@ a {
}
/* about end */
/* maintenance start */
.maintenance-wrap {
background-color: rgb(219, 228, 209);
min-height: 100vh;
display: grid;
justify-content: center;
align-items: center;
text-align: center;
padding: 10px;
}
/* maintenance end */
/* responsiv start */
@media screen and (max-width: 1000px) {
.content,

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style.css') }}">
<title>AQI - Under Maintenance</title>
</head>
<body>
<div class="maintenance-wrap">
<div>
<h1>Under Maintenance</h1>
<p>Air Quality is taking a break and will be back soon.</p>
</div>
</div>
</body>
</html>

View File

@ -21,6 +21,24 @@ matplotlib.use('Agg')
# start up
app = Flask(__name__)
# maintenance mode
is_maintenance_mode = bool(os.environ.get("MAINTENANCE"))
@app.before_request
def check_for_maintenance():
print(request.path)
ALLOW = ["/static/css/style.css", "/static/favicon.ico", "/static/font/Rubik-Bold.ttf", "/static/font/Rubik-Regular.ttf"]
if is_maintenance_mode and request.path not in ALLOW:
# return redirect(url_for('maintenance'))
# Or alternatively, dont redirect
# return 'Sorry, off for maintenance!', 503
return render_template('maintenance.html'), 503
# @app.route('/maintenance')
# def maintenance():
# return 'Sorry, off for maintenance!', 503
CONFIG = get_config()
auth = HTTPBasicAuth()
aqi_user = CONFIG['aqi_monitor']
@ -28,25 +46,26 @@ USER_DATA = {
aqi_user['authUsername']: aqi_user['authPassword']
}
# initial export
print('initial export')
current_graph()
nightly_graph()
monthly_graph()
if not is_maintenance_mode:
# initial export
print('initial export')
current_graph()
nightly_graph()
monthly_graph()
# start scheduler
timezone = os.environ.get("TZ")
scheduler = BackgroundScheduler(timezone=timezone)
scheduler.add_job(
current_graph, trigger="cron", minute='*/5', name='current_graph'
)
scheduler.add_job(
nightly_graph, trigger="cron", day='*', hour='1', minute='1', name='night'
)
scheduler.add_job(
monthly_graph, trigger="cron", day='*', hour='1', minute='2', name='month'
)
scheduler.start()
# start scheduler
timezone = os.environ.get("TZ")
scheduler = BackgroundScheduler(timezone=timezone)
scheduler.add_job(
current_graph, trigger="cron", minute='*/5', name='current_graph'
)
scheduler.add_job(
nightly_graph, trigger="cron", day='*', hour='1', minute='1', name='night'
)
scheduler.add_job(
monthly_graph, trigger="cron", day='*', hour='1', minute='2', name='month'
)
scheduler.start()
@auth.verify_password