Skip to content

Commit

Permalink
Add docker support.
Browse files Browse the repository at this point in the history
  • Loading branch information
whypro committed Sep 18, 2020
1 parent e79f3b1 commit b3cc7cd
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 25 deletions.
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

* 安装 PIL 依赖的组件

请检查 libjpeg-devel 是否安装,否则 PIL 的某些苟能将不可用
请检查 libjpeg-devel 是否安装,否则 PIL 的某些功能将不可用

* 安装 virtualenv

Expand Down
7 changes: 4 additions & 3 deletions etc/gunicorn.conf.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion ibati/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
))
Expand Down
11 changes: 6 additions & 5 deletions ibati/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -35,3 +35,4 @@ class Config(object):
INDEX_AREA_NUM = 8 # 首页研究方向个数

APP_DIR = os.path.realpath('.')

47 changes: 33 additions & 14 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,7 +38,6 @@ def debug():
app.run(host='0.0.0.0', port=5000, debug=True, processes=1)



@manager.command
def backup():
# 备份上传文件
Expand All @@ -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()
Expand 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 '目录初始化成功'


Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b3cc7cd

Please sign in to comment.