From d8e535c89de6d7c977ffc12bf437aa3e450699c5 Mon Sep 17 00:00:00 2001
From: Heaven <djyesu@gmail.com>
Date: Sat, 13 Jun 2020 19:40:19 +0800
Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=89=93=E5=8C=85=E9=95=9C?=
 =?UTF-8?q?=E5=83=8F,=E5=B0=86=E7=8E=AF=E5=A2=83=E7=9A=84=E9=85=8D?=
 =?UTF-8?q?=E7=BD=AE=E5=85=A8=E9=83=A8=E8=BD=AC=E4=B8=BA=E5=AE=B9=E5=99=A8?=
 =?UTF-8?q?=E5=86=85=E6=9D=A5=E6=93=8D=E4=BD=9C=E5=AE=8C=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Dockerfile                               | 37 +++-----------
 doc/supervisor_ops.conf                  | 19 -------
 docker-compose.yml                       |  9 ----
 docker/docker-compose.yml                |  5 ++
 docker/nginx_default.conf                | 64 ++++++++++++++++++++++++
 docker/nginx_ops.conf                    | 18 +++++++
 docker/prestart.sh                       | 30 +++++++++++
 docker/supervisor_ops.conf               | 36 +++++++++++++
 doc/requirements.txt => requirements.txt |  0
 9 files changed, 159 insertions(+), 59 deletions(-)
 delete mode 100644 doc/supervisor_ops.conf
 delete mode 100644 docker-compose.yml
 create mode 100644 docker/docker-compose.yml
 create mode 100644 docker/nginx_default.conf
 create mode 100644 docker/nginx_ops.conf
 create mode 100644 docker/prestart.sh
 create mode 100644 docker/supervisor_ops.conf
 rename doc/requirements.txt => requirements.txt (100%)

diff --git a/Dockerfile b/Dockerfile
index 33dd5f2..76f0869 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,37 +1,12 @@
-FROM centos:7
-# 设置编码
-ENV LANG en_US.UTF-8
-# 同步时间
-ENV TZ=Asia/Shanghai
-RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
+FROM registry.cn-hangzhou.aliyuncs.com/sourcegarden/python:centos7-3.6
 
-# 1. 安装基本依赖
-RUN yum update -y && yum install epel-release -y && yum update -y && yum install wget unzip epel-release nginx  xz gcc automake zlib-devel openssl-devel supervisor  groupinstall development  libxslt-devel libxml2-devel libcurl-devel git -y
-#WORKDIR /var/www/
-
-# 2. 准备python
-RUN wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz
-RUN xz -d Python-3.6.6.tar.xz && tar xvf Python-3.6.6.tar && cd Python-3.6.6 && ./configure && make && make install
-
-# 3. 安装SDK依赖
-RUN pip3 install --upgrade pip
-RUN pip3 install -U git+https://github.com/ss1917/ops_sdk.git
-
-# 4. 复制代码
-RUN mkdir -p /var/www/
 ADD . /var/www/codo-cron/
 
-# 5. 安装pip依赖
-RUN pip3 install -r /var/www/codo-cron/doc/requirements.txt
-
-# 6. 初始化生成表结构
-# RUN python3 /var/www/codo-cron/db_sync.py
-
-# 7. 日志
-VOLUME /var/log/
+RUN pip3 install -r /var/www/codo-cron/requirements.txt
 
-# 8. 准备文件
-COPY doc/supervisor_ops.conf  /etc/supervisord.conf
+COPY docker/nginx_default.conf /etc/nginx/nginx.conf
+COPY docker/nginx_ops.conf /etc/nginx/conf.d/codo-cron.conf
+COPY docker/supervisor_ops.conf  /etc/supervisord.conf
 
-EXPOSE 9900
+EXPOSE 80
 CMD ["/usr/bin/supervisord"]
\ No newline at end of file
diff --git a/doc/supervisor_ops.conf b/doc/supervisor_ops.conf
deleted file mode 100644
index d0205d3..0000000
--- a/doc/supervisor_ops.conf
+++ /dev/null
@@ -1,19 +0,0 @@
-[supervisord]
-nodaemon=true
-
-[group:yunwei_cron]
-programs=cron
-
-[program:cron]
-command=python3 startup.py --service=cron --port=99%(process_num)02d
-process_name=%(program_name)s_%(process_num)02d
-numprocs=1
-directory=/var/www/codo-cron/
-user=root
-autostart = true
-autorestart=true
-redirect_stderr=true
-stdout_logfile=/var/log/supervisor/cron.log
-loglevel=info
-logfile_maxbytes=100MB
-logfile_backups=3
diff --git a/docker-compose.yml b/docker-compose.yml
deleted file mode 100644
index 63f068c..0000000
--- a/docker-compose.yml
+++ /dev/null
@@ -1,9 +0,0 @@
-codo_cron:
-  restart: unless-stopped
-  image: codo_cron_image
-  volumes:
-    - /var/log/supervisor/:/var/log/supervisor/
-    - /opt/ops_scripts:/opt/ops_scripts
-    - /sys/fs/cgroup:/sys/fs/cgroup
-  ports:
-    - "9900:9900"
diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml
new file mode 100644
index 0000000..2b00d9d
--- /dev/null
+++ b/docker/docker-compose.yml
@@ -0,0 +1,5 @@
+codo_cron:
+  restart: unless-stopped
+  image: codo_cron_image
+  ports:
+    - "9900:9900"
diff --git a/docker/nginx_default.conf b/docker/nginx_default.conf
new file mode 100644
index 0000000..e5709d3
--- /dev/null
+++ b/docker/nginx_default.conf
@@ -0,0 +1,64 @@
+user nginx;
+worker_processes auto;
+error_log /var/log/nginx/error.log;
+pid /run/nginx.pid;
+
+# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
+include /usr/share/nginx/modules/*.conf;
+
+events {
+    worker_connections 1024;
+}
+
+http {
+    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  /var/log/nginx/access.log  main;
+
+    sendfile            on;
+    tcp_nopush          on;
+    tcp_nodelay         on;
+    keepalive_timeout   65;
+    types_hash_max_size 2048;
+
+    include             /etc/nginx/mime.types;
+    default_type        application/octet-stream;
+
+    # Load modular configuration files from the /etc/nginx/conf.d directory.
+    # See http://nginx.org/en/docs/ngx_core_module.html#include
+    # for more information.
+    include /etc/nginx/conf.d/*.conf;
+
+# Settings for a TLS enabled server.
+#
+#    server {
+#        listen       443 ssl http2 default_server;
+#        listen       [::]:443 ssl http2 default_server;
+#        server_name  _;
+#        root         /usr/share/nginx/html;
+#
+#        ssl_certificate "/etc/pki/nginx/server.crt";
+#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
+#        ssl_session_cache shared:SSL:1m;
+#        ssl_session_timeout  10m;
+#        ssl_ciphers HIGH:!aNULL:!MD5;
+#        ssl_prefer_server_ciphers on;
+#
+#        # Load configuration files for the default server block.
+#        include /etc/nginx/default.d/*.conf;
+#
+#        location / {
+#        }
+#
+#        error_page 404 /404.html;
+#            location = /40x.html {
+#        }
+#
+#        error_page 500 502 503 504 /50x.html;
+#            location = /50x.html {
+#        }
+#    }
+
+}
\ No newline at end of file
diff --git a/docker/nginx_ops.conf b/docker/nginx_ops.conf
new file mode 100644
index 0000000..a0ac403
--- /dev/null
+++ b/docker/nginx_ops.conf
@@ -0,0 +1,18 @@
+upstream  codo-cron-nginx{
+    server  127.0.0.1:9900;
+}
+
+server {
+        listen 80;
+        server_name cron.opendevops.cn;
+        access_log /var/log/nginx/codo-cron-access.log;
+        error_log  /var/log/nginx/codo-cron-error.log;
+        location / {
+                proxy_set_header Host $http_host;
+                proxy_redirect off;
+                proxy_set_header X-Real-IP $remote_addr;
+                proxy_set_header X-Scheme $scheme;
+                proxy_pass http://codo-cron-nginx;
+
+        }
+}
\ No newline at end of file
diff --git a/docker/prestart.sh b/docker/prestart.sh
new file mode 100644
index 0000000..2b3b6dd
--- /dev/null
+++ b/docker/prestart.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+cd /var/www/codo-cron/
+
+#定时任务-cron
+CRON_DB_DBNAME='codo_cron'
+sed -i "s#cookie_secret = .*#cookie_secret = '${cookie_secret}'#g" settings.py
+sed -i "s#DEFAULT_DB_DBHOST = .*#DEFAULT_DB_DBHOST = os.getenv('DEFAULT_DB_DBHOST', '${DEFAULT_DB_DBHOST}')#g" settings.py  && \
+sed -i "s#DEFAULT_DB_DBPORT = .*#DEFAULT_DB_DBPORT = os.getenv('DEFAULT_DB_DBPORT', '${DEFAULT_DB_DBPORT}')#g" settings.py  && \
+sed -i "s#DEFAULT_DB_DBUSER = .*#DEFAULT_DB_DBUSER = os.getenv('DEFAULT_DB_DBUSER', '${DEFAULT_DB_DBUSER}')#g" settings.py  && \
+sed -i "s#DEFAULT_DB_DBPWD = .*#DEFAULT_DB_DBPWD = os.getenv('DEFAULT_DB_DBPWD', '${DEFAULT_DB_DBPWD}')#g" settings.py  && \
+sed -i "s#DEFAULT_DB_DBNAME = .*#DEFAULT_DB_DBNAME = os.getenv('DEFAULT_DB_DBNAME', '${CRON_DB_DBNAME}')#g" settings.py  && \
+sed -i "s#READONLY_DB_DBHOST = .*#READONLY_DB_DBHOST = os.getenv('READONLY_DB_DBHOST', '${READONLY_DB_DBHOST}')#g" settings.py  && \
+sed -i "s#READONLY_DB_DBPORT = .*#READONLY_DB_DBPORT = os.getenv('READONLY_DB_DBPORT', '${READONLY_DB_DBPORT}')#g" settings.py  && \
+sed -i "s#READONLY_DB_DBUSER = .*#READONLY_DB_DBUSER = os.getenv('READONLY_DB_DBUSER', '${READONLY_DB_DBUSER}')#g" settings.py  && \
+sed -i "s#READONLY_DB_DBPWD = .*#READONLY_DB_DBPWD = os.getenv('READONLY_DB_DBPWD', '${READONLY_DB_DBPWD}')#g" settings.py  && \
+sed -i "s#READONLY_DB_DBNAME = .*#READONLY_DB_DBNAME = os.getenv('READONLY_DB_DBNAME', '${CRON_DB_DBNAME}')#g" settings.py
+
+try_num=0
+
+while [[ $try_num -le 100 ]];
+do
+     if $(curl  -s ${DEFAULT_DB_DBHOST}:${DEFAULT_DB_DBPORT}  > /dev/null);then
+          python3 db_sync.py
+          exit 0
+     else
+          echo 'wait mysql start to do db_sync.py'
+     fi
+     let try_num+=1
+     sleep 6
+done
\ No newline at end of file
diff --git a/docker/supervisor_ops.conf b/docker/supervisor_ops.conf
new file mode 100644
index 0000000..a7b1fd3
--- /dev/null
+++ b/docker/supervisor_ops.conf
@@ -0,0 +1,36 @@
+[supervisord]
+nodaemon=true
+
+[group:codo_cron]
+programs=cron,cron-prestart
+
+[program:cron-prestart]
+command=sh docker/prestart.sh
+directory=/var/www/codo-cron
+user=root
+autostart = true
+autorestart=false
+redirect_stderr=true
+stdout_logfile=/var/log/supervisor/codo-cron-prestart.log
+loglevel=info
+logfile_maxbytes=10MB
+logfile_backups=1
+
+[program:cron]
+command=python3 startup.py --service=cron --port=99%(process_num)02d
+process_name=%(program_name)s_%(process_num)02d
+numprocs=1
+directory=/var/www/codo-cron/
+user=root
+autostart = true
+autorestart=true
+redirect_stderr=true
+stdout_logfile=/var/log/supervisor/cron.log
+loglevel=info
+logfile_maxbytes=100MB
+logfile_backups=3
+
+[program:nginx]
+command=/usr/sbin/nginx -g "daemon off;"
+autostart=true
+autorestart=true
\ No newline at end of file
diff --git a/doc/requirements.txt b/requirements.txt
similarity index 100%
rename from doc/requirements.txt
rename to requirements.txt