diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e8242ce --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM python:2 +RUN apt-get install -y default-libmysqlclient-dev +COPY ./requirements.txt /app/ +WORKDIR /app +RUN pip install -r requirements.txt +COPY . /app +CMD ["gunicorn", "manage:app", "-c", "etc/gunicorn.conf.py"] diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..51b580b --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ +ifeq ($(REGISTRY),) + REGISTRY := whypro/ibati +endif + +ifeq ($(VERSION),) + VERSION := latest +endif + +IMAGE = $(REGISTRY):$(VERSION) +IMAGE_LATEST = $(REGISTRY):latest + +BUILD_ARGS=--build-arg http_proxy=http://10.0.0.12:8118 --build-arg https_proxy=https://10.0.0.12:8118 + +image: + docker build ${BUILD_ARGS} -t ${IMAGE} . + docker tag ${IMAGE} ${IMAGE_LATEST} +.PHONY: build + +image-push: + docker push ${IMAGE} +.PHONY: push + +image-push-latest: + docker push ${IMAGE_LATEST} +.PHONY: push-latest diff --git a/README.md b/README.md index ceaf475..72f5cc6 100755 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ * 安装 PIL 依赖的组件 - 请检查 libjpeg-devel 是否安装,否则 PIL 的某些苟能将不可用。 + 请检查 libjpeg-devel 是否安装,否则 PIL 的某些功能将不可用。 * 安装 virtualenv diff --git a/etc/gunicorn.conf.py b/etc/gunicorn.conf.py old mode 100644 new mode 100755 index 05414eb..616b0c3 --- a/etc/gunicorn.conf.py +++ b/etc/gunicorn.conf.py @@ -4,13 +4,14 @@ gevent.monkey.patch_all() import multiprocessing -bind = 'unix:/var/run/ibati.sock' +#bind = 'unix:/var/run/ibati.sock' #bind = '127.0.0.1:8000' +bind = '0.0.0.0:8000' max_requests = 10 keepalive = 5 -#workers = multiprocessing.cpu_count() * 2 + 1 -workers = 1 +workers = multiprocessing.cpu_count() * 2 + 1 +#workers = 1 worker_class = 'gunicorn.workers.ggevent.GeventWorker' loglevel = 'info' diff --git a/ibati/__init__.py b/ibati/__init__.py index c264275..0b1a892 100755 --- a/ibati/__init__.py +++ b/ibati/__init__.py @@ -46,7 +46,7 @@ def create_app(config=None): def init_app_logger(app): - file_handler = FileHandler('flask.log') + file_handler = FileHandler('./log/flask.log') file_handler.setFormatter(Formatter( '%(asctime)s|%(levelname)s|%(pathname)s:%(lineno)d|%(message)s' )) diff --git a/ibati/config.py b/ibati/config.py index 992cf7a..58f16f9 100755 --- a/ibati/config.py +++ b/ibati/config.py @@ -9,11 +9,11 @@ class Config(object): # JSONIFY_PRETTYPRINT_REGULAR = False # 数据库配置 - DB_HOST = 'localhost' - DB_DATABASE = 'ibati' - DB_USERNAME = 'whypro' - DB_PASSWORD = 'whypro' - DB_PORT = 3306 + DB_HOST = os.getenv('DB_HOST', '127.0.0.1') + DB_DATABASE = os.getenv('DB_DATABASE', 'ibati') + DB_USERNAME = os.getenv('DB_USERNAME', '') + DB_PASSWORD = os.getenv('DB_PASSWORD', '') + DB_PORT = os.getenv('DB_PORT', 3306) # FLASK-SQLALCHEMY SQLALCHEMY_DATABASE_URI = 'mysql://{username}:{password}@{host}:{port}/{database}?charset=utf8'.format( @@ -35,3 +35,4 @@ class Config(object): INDEX_AREA_NUM = 8 # 首页研究方向个数 APP_DIR = os.path.realpath('.') + diff --git a/manage.py b/manage.py index d6d2e67..dec1b08 100755 --- a/manage.py +++ b/manage.py @@ -13,6 +13,9 @@ import os import datetime import shutil +import glob +from sqlalchemy import create_engine +from sqlalchemy_utils import database_exists, create_database from flask.ext.script import Manager, Server from flask.ext.migrate import Migrate, MigrateCommand @@ -35,7 +38,6 @@ def debug(): app.run(host='0.0.0.0', port=5000, debug=True, processes=1) - @manager.command def backup(): # 备份上传文件 @@ -59,20 +61,28 @@ def restore(date_str=None): @manager.command def init(): # 创建数据库 - create_db_sql = 'CREATE DATABASE IF NOT EXISTS {0} DEFAULT CHARACTER SET utf8'.format(config.Config.DB_DATABASE) - # print create_db_sql - ret = subprocess.call( - [ - 'mysql', '-u', config.Config.DB_USERNAME, - '-p{0}'.format(config.Config.DB_PASSWORD), - '-e', create_db_sql, - ] - ) - if not ret: + engine = create_engine(config.Config.SQLALCHEMY_DATABASE_URI) + if not database_exists(engine.url): + create_database(engine.url, encoding='utf8') + if database_exists(engine.url): print '数据库创建成功' else: print '数据库创建失败' - return + return + + #ret = subprocess.call( + # [ + # 'mysql', '-u', config.Config.DB_USERNAME, + # '-p{0}'.format(config.Config.DB_PASSWORD), + # '-e', create_db_sql, + # ], + # shell=True, + #) + #if not ret: + # print '数据库创建成功' + #else: + # print '数据库创建失败' + # return db.drop_all() db.create_all() @@ -81,9 +91,18 @@ def init(): init_post(db.session) print '数据初始化成功' + def clean_dir(dirname): + for filename in os.listdir(dirname): + path = os.path.join(dirname, filename) + if os.path.isfile(path) or os.path.islink(path): + os.unlink(path) + elif os.path.isdir(path): + shutil.rmtree(path) + uploads_dir = config.Config.UPLOADS_DEFAULT_DEST - if os.path.exists(uploads_dir): - shutil.rmtree(uploads_dir) + # if os.path.exists(uploads_dir): + # shutil.rmtree(uploads_dir) + clean_dir(uploads_dir) print '目录初始化成功' diff --git a/requirements.txt b/requirements.txt old mode 100644 new mode 100755 index a076856..444cbe3 --- a/requirements.txt +++ b/requirements.txt @@ -12,8 +12,10 @@ itsdangerous==0.24 Jinja2==2.7.3 Mako==1.0.1 MarkupSafe==0.23 -MySQL-python==1.2.5 +#MySQL-python==1.2.5 Pillow==2.9.0 SQLAlchemy==1.0.6 Werkzeug==0.10.4 wheel==0.24.0 +mysqlclient==1.4.6 +SQLAlchemy-Utils==0.32.11