deploy script

This commit is contained in:
virusdefender 2017-11-25 03:37:40 +08:00
parent 2d038c7bcc
commit 6d08011e2d
13 changed files with 127 additions and 76 deletions

23
.gitignore vendored
View File

@ -54,21 +54,18 @@ db.db
#*.out #*.out
*.sqlite3 *.sqlite3
.DS_Store .DS_Store
log/
static/release/css
static/release/js
static/release/img
static/src/upload_image/*
build.txt build.txt
tmp/ tmp/
test_case/
release/
upload/
custom_settings.py custom_settings.py
docker-compose.yml
*.zip *.zip
rsyncd.passwd
node_modules/ data/log/*
update.sh !data/log/.gitkeep
ssh.sh data/testcase/*
!data/testcase/.gitkeep
data/ssl/*
!data/ssl/.gitkeep
data/static/upload/*
!data/static/upload/.gitkeep
data/static/avatar/*
!data/static/avatar/default.png

0
data/log/.gitkeep Normal file
View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

0
data/ssl/.gitkeep Normal file
View File

0
data/testcase/.gitkeep Normal file
View File

View File

@ -3,11 +3,10 @@ FROM python:3.6-alpine3.6
ENV OJ_ENV production ENV OJ_ENV production
RUN apk add --no-cache supervisor jpeg-dev zlib-dev postgresql-dev freetype-dev RUN apk add --no-cache supervisor jpeg-dev zlib-dev postgresql-dev freetype-dev
ADD requirements.txt /tmp ADD deploy/requirements.txt /tmp
RUN apk add --no-cache build-base && \ RUN apk add --update --no-cache build-base nginx openssl && \
pip install --no-cache-dir -r /tmp/requirements.txt -i https://pypi.doubanio.com/simple && \ pip install --no-cache-dir -r /tmp/requirements.txt -i https://pypi.doubanio.com/simple && \
apk del build-base --purge apk del build-base --purge
VOLUME [ "/app" ] ADD . /app
CMD sh /app/deploy/run.sh CMD sh /app/deploy/run.sh

62
deploy/oj.conf Normal file
View File

@ -0,0 +1,62 @@
user nobody;
daemon off;
pid /tmp/nginx.pid;
worker_processes auto;
pcre_jit on;
error_log /data/log/nginx_error.log warn;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server_tokens off;
keepalive_timeout 65;
sendfile on;
tcp_nodelay on;
gzip on;
gzip_vary on;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /app/data/log/nginx_access.log main;
upstream backend {
server 127.0.0.1:8080;
keepalive 32;
}
server {
listen 8000 default_server;
server_name _;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
client_max_body_size 200M;
location /public {
root /app/data;
}
location /api {
proxy_pass http://backend;
proxy_set_header Host $host;
}
location /admin {
root /app/dist/admin;
try_files $uri $uri/ /index.html =404;
}
location / {
root /app/dist;
try_files $uri $uri/ /index.html =404;
}
}
}

View File

@ -1,39 +1,28 @@
#!/bin/bash #!/bin/bash
BASE=/app BASE=/app
DATA=$BASE/data
if [ ! -f "$BASE/custom_settings.py" ]; then if [ ! -f "$BASE/oj/custom_settings.py" ]; then
echo SECRET_KEY=\"$(cat /dev/urandom | head -1 | md5sum | head -c 32)\" >> /app/oj/custom_settings.py echo SECRET_KEY=\"$(cat /dev/urandom | head -1 | md5sum | head -c 32)\" >> $BASE/oj/custom_settings.py
fi fi
if [ ! -d "$BASE/log" ]; then mkdir -p $DATA/log $DATA/testcase $DATA/public/upload
mkdir -p $BASE/log
fi
cd $BASE cd $BASE
find . -name "*.pyc" -delete
# wait for postgresql start
sleep 6
n=0 n=0
while [ $n -lt 3 ] while [ $n -lt 5 ]
do do
python manage.py migrate python manage.py migrate --no-input &&
if [ $? -ne 0 ]; then python manage.py initinstall &&
echo "Can't start server, try again in 3 seconds.." break
sleep 3 n=$(($n+1))
let "n+=1" echo "Failed to migrate, going to retry..."
continue sleep 8
fi
python manage.py initinstall
break
done done
if [ $n -eq 3 ]; then cp $BASE/deploy/oj.conf /etc/nginx/conf.d/default.conf
echo "Can't start server, please check log file for details."
exit 1
fi
chown -R nobody:nogroup /data/log /data/test_case /data/avatar /data/upload chown -R nobody:nogroup $DATA $BASE/dist
exec supervisord -c /app/deploy/supervisor.conf exec supervisord -c /app/deploy/supervisor.conf

View File

@ -1,20 +1,32 @@
[supervisord] [supervisord]
logfile=/app/log/supervisord.log logfile=/app/data/log/supervisord.log
logfile_maxbytes=10MB logfile_maxbytes=10MB
logfile_backups=10 logfile_backups=10
loglevel=info loglevel=info
pidfile=/tmp/supervisord.pid pidfile=/tmp/supervisord.pid
nodaemon=true nodaemon=true
childlogdir=/data/log/ childlogdir=/app/data/log/
[supervisorctl] [supervisorctl]
serverurl=unix:///tmp/supervisor.sock serverurl=unix:///tmp/supervisor.sock
[program:gunicorn] [program:nginx]
command=sh -c "gunicorn oj.wsgi --user nobody -b 0.0.0.0:8080 --reload -w `grep -c ^processor /proc/cpuinfo`" command=nginx -c /app/deploy/oj.conf
directory=/app/ directory=/app/
stdout_logfile=/data/log/gunicorn.log stdout_logfile=/app/data/log/nginx.log
stderr_logfile=/data/log/gunicorn.log stderr_logfile=/app/data/log/nginx.log
autostart=true
autorestart=true
startsecs=5
stopwaitsecs = 5
killasgroup=true
[program:gunicorn]
command=sh -c "gunicorn oj.wsgi --user nobody -b 127.0.0.1:8080 --reload -w `grep -c ^processor /proc/cpuinfo`"
directory=/app/
user=nobody
stdout_logfile=/app/data/log/gunicorn.log
stderr_logfile=/app/data/log/gunicorn.log
autostart=true autostart=true
autorestart=true autorestart=true
startsecs=5 startsecs=5
@ -25,8 +37,8 @@ killasgroup=true
command=celery -A oj worker -l warning command=celery -A oj worker -l warning
directory=/app/ directory=/app/
user=nobody user=nobody
stdout_logfile=/data/log/celery.log stdout_logfile=/app/data/log/celery.log
stderr_logfile=/data/log/celery.log stderr_logfile=/app/data/log/celery.log
autostart=true autostart=true
autorestart=true autorestart=true
startsecs=5 startsecs=5

View File

@ -7,7 +7,7 @@ DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', 'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': '127.0.0.1', 'HOST': '127.0.0.1',
'PORT': 5432, 'PORT': 5433,
'NAME': "onlinejudge", 'NAME': "onlinejudge",
'USER': "onlinejudge", 'USER': "onlinejudge",
'PASSWORD': 'onlinejudge' 'PASSWORD': 'onlinejudge'
@ -24,16 +24,4 @@ DEBUG = True
ALLOWED_HOSTS = ["*"] ALLOWED_HOSTS = ["*"]
TEST_CASE_DIR = "/tmp" DATA_DIR = f"{BASE_DIR}/data"
LOG_PATH = f"{BASE_DIR}/log/"
AVATAR_URI_PREFIX = "/static/avatar"
AVATAR_UPLOAD_DIR = f"{BASE_DIR}{AVATAR_URI_PREFIX}"
UPLOAD_PREFIX = "/static/upload"
UPLOAD_DIR = f"{BASE_DIR}{UPLOAD_PREFIX}"
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]

View File

@ -8,8 +8,8 @@ def get_env(name, default=""):
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', 'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': get_env("POSTGRES_HOST", "postgres"), 'HOST': get_env("POSTGRES_HOST", "oj-postgres"),
'PORT': get_env("POSTGRES_PORT", "5433"), 'PORT': get_env("POSTGRES_PORT", "5432"),
'NAME': get_env("POSTGRES_DB"), 'NAME': get_env("POSTGRES_DB"),
'USER': get_env("POSTGRES_USER"), 'USER': get_env("POSTGRES_USER"),
'PASSWORD': get_env("POSTGRES_PASSWORD") 'PASSWORD': get_env("POSTGRES_PASSWORD")
@ -17,7 +17,7 @@ DATABASES = {
} }
REDIS_CONF = { REDIS_CONF = {
"host": get_env("REDIS_HOST", "redis"), "host": get_env("REDIS_HOST", "oj-redis"),
"port": get_env("REDIS_PORT", "6379") "port": get_env("REDIS_PORT", "6379")
} }
@ -25,12 +25,4 @@ DEBUG = False
ALLOWED_HOSTS = ['*'] ALLOWED_HOSTS = ['*']
AVATAR_URI_PREFIX = "/static/avatar" DATA_DIR = "/data"
AVATAR_UPLOAD_DIR = "/data/avatar"
UPLOAD_PREFIX = "/static/upload"
UPLOAD_DIR = "/data/upload"
TEST_CASE_DIR = "/data/test_case"
LOG_PATH = "/data/log"
DEFAULT_JUDGE_SERVER_SERVICE_URL = "http://judge-server:8080/"

View File

@ -109,10 +109,22 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/ # https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/' STATIC_URL = '/storage/'
AUTH_USER_MODEL = 'account.User' AUTH_USER_MODEL = 'account.User'
TEST_CASE_DIR = os.path.join(DATA_DIR, "testcase")
LOG_PATH = os.path.join(DATA_DIR, "log")
AVATAR_URI_PREFIX = "/public/avatar"
AVATAR_UPLOAD_DIR = f"{DATA_DIR}{AVATAR_URI_PREFIX}"
UPLOAD_PREFIX = "/public/upload"
UPLOAD_DIR = f"{DATA_DIR}{UPLOAD_PREFIX}"
STATICFILES_DIRS = [os.path.join(DATA_DIR, "public")]
LOGGING = { LOGGING = {
'version': 1, 'version': 1,
'disable_existing_loggers': False, 'disable_existing_loggers': False,