问题描述
我有一个问题非常像这样:(不能附上图片..)
通过 OpenShift Cartridge Guide(OpenShift磁带盒指南):
磁带盒和永久存储:每当您推送时,远程磁带库目录中的所有内容都将被重新创建。一个sqlite数据库)在OpenShift数据目录中,这个数据目录之间会一直存在您可以通过环境变量$ OPENSHIFT_DATA_DIR找到OpenShift数据目录。
您可以保留原有的项目结构,只需使用deploy钩子将数据库移动到持久性存储中。
$ b
创建一个deploy动作钩子(可执行文件) .openshift / action_hooks / deploy
:
$ b $ $ $ $ $ $ $ $ $
$ b $这个部署钩子在依赖关系之后被执行解决了,并且
#build hook已经运行,但是在应用程序再次启动
#之前。
#如果这是初始安装,将数据库从repo复制到永久存储目录
if [! -f $ {OPENSHIFT_DATA_DIR} database.db];然后
cp -rf $ {OPENSHIFT_REPO_DIR} database.db $ {OPENSHIFT_DATA_DIR} /database.db 2> / dev / null
fi
#在回购期间从数据库中删除数据库如果[-d $ {OPENSHIFT_REPO_DIR} database.db]都将部署
;然后
rm -rf $ {OPENSHIFT_REPO_DIR} database.db
fi
#从repo目录创建符号链接到持久存储中的新数据库位置
ln -sf $ { OPENSHIFT_DATA_DIR} database.db $ {OPENSHIFT_REPO_DIR} database.db
另一位人士指出,你实际上正在提交/推送你的数据库(确保你的数据库不包含在你的.gitignore中)。
I have a problem pretty much exactly like this:How to preserve a SQLite database from being reverted after deploying to OpenShift?
I don't understand his answer fully and clearly not enough to apply it to my own app and since I can't comment his answer (not enough rep) I figured I had to make ask my own question.
Problem is that when pushing my local files (not including the database file) my database on openshift becomes the one I have locally (all changes made through the server are reverted).
I've googled alot and pretty much understand the problem being that the database should be located somewhere else but I can't grasp fully where to place it and how to deploy it if it's outside the repo.
EDIT: Quick solution: If you have this problem, try connecting to your openshift app with rhc ssh appname
and then cp app-root/repo/database.db app-root/data/database.db
if you have the openshift data dir as reference to SQLALCHEMY_DATABASE_URI. I recommend the accepted answer below though!
I've attached my filestructure and here's some related code:
config.py
import os
basedir = os.path.abspath(os.path.dirname(__file__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'database.db')
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')
app/__ init.py__
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
#so that flask doesn't swallow error messages
app.config['PROPAGATE_EXCEPTIONS'] = True
app.config.from_object('config')
db = SQLAlchemy(app)
from app import rest_api, models
wsgi.py:
#!/usr/bin/env python
import os
virtenv = os.path.join(os.environ.get('OPENSHIFT_PYTHON_DIR', '.'), 'virtenv')
#
# IMPORTANT: Put any additional includes below this line. If placed above this
# line, it's possible required libraries won't be in your searchable path
#
from app import app as application
## runs server locally
if __name__ == '__main__':
from wsgiref.simple_server import make_server
httpd = make_server('localhost', 4599, application)
httpd.serve_forever()
filestructure: http://sv.tinypic.com/r/121xseh/8 (can't attach image..)
Via the note at the top of the OpenShift Cartridge Guide:
"Cartridges and Persistent Storage: Every time you push, everything in your remote repo directory is recreated. Store long term items (like an sqlite database) in the OpenShift data directory, which will persist between pushes of your repo. The OpenShift data directory can be found via the environment variable $OPENSHIFT_DATA_DIR."
You can keep your existing project structure as-is and just use a deploy hook to move your database to persistent storage.
Create a deploy action hook (executable file) .openshift/action_hooks/deploy
:
#!/bin/bash
# This deploy hook gets executed after dependencies are resolved and the
# build hook has been run but before the application has been started back
# up again.
# if this is the initial install, copy DB from repo to persistent storage directory
if [ ! -f ${OPENSHIFT_DATA_DIR}database.db ]; then
cp -rf ${OPENSHIFT_REPO_DIR}database.db ${OPENSHIFT_DATA_DIR}/database.db 2>/dev/null
fi
# remove the database from the repo during all deploys
if [ -d ${OPENSHIFT_REPO_DIR}database.db ]; then
rm -rf ${OPENSHIFT_REPO_DIR}database.db
fi
# create symlink from repo directory to new database location in persistent storage
ln -sf ${OPENSHIFT_DATA_DIR}database.db ${OPENSHIFT_REPO_DIR}database.db
As another person pointed out, also make sure you are actually committing/pushing your database (make sure your database isn't included in your .gitignore).
这篇关于与瓶,sqlalchemy和sqlite的Openshift应用程序 - 与数据库还原的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!