Last active
August 29, 2015 14:23
-
-
Save tawateer/a876b582e4bee86fb35f to your computer and use it in GitHub Desktop.
Nginx 配置文件发布脚本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" Nginx 配置文件发布. | |
支持使用 lvs http check 发布和 reload 发布两种方式. | |
""" | |
import os | |
import sys | |
import json | |
import logging | |
import optparse | |
import urllib | |
import urllib2 | |
import subprocess | |
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout, | |
format='%(message)s') | |
NGINX_CONF_URL = 'http://access.hy01.nosa.me/nginx/conf/' | |
NGINX_CONF_TMP_DIR = "/home/work/nginx_conf_deploy/" | |
HEALTHY_FILE = '/home/work/nginx/conf/http_check/index.html' | |
HEALTHY_FILE_BAK = "/home/work/nginx/index.html" | |
def shell(cmd): | |
process = subprocess.Popen(args=cmd, stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, shell=True) | |
std_out, std_err = process.communicate() | |
return_code = process.poll() | |
return return_code, std_out, std_err | |
def _shell(cmd, _exit=1): | |
rc , so, se = shell(cmd) | |
if rc == 0: | |
message = "cmd:%s" % cmd | |
logging.info(message) | |
return so.strip() | |
else: | |
message = "cmd:%s, error:%s" % (cmd, se) | |
logging.error(message) | |
if _exit == 1: | |
sys.exit(1) | |
else: | |
return False | |
def get_nginx_conf_url(nginx, branch): | |
data_dict = { | |
"nginx": nginx, | |
"branch": branch | |
} | |
data = urllib.urlencode(data_dict) | |
response = urllib2.urlopen(NGINX_CONF_URL + "?" + data) | |
response = response.read() | |
ret_dict = json.loads(response) | |
# return ret_dict | |
if ret_dict["status"] == "success": | |
message = "Get nginx conf success, url:%s" % ret_dict["message"] | |
logging.info(message) | |
return ret_dict["message"] | |
else: | |
message = "Get nginx conf failed, message:%s" % ret_dict["message"] | |
logging.error(message) | |
sys.exit(1) | |
def download_nginx_conf(url): | |
if not os.path.isdir(NGINX_CONF_TMP_DIR): | |
os.makedirs(NGINX_CONF_TMP_DIR) | |
dst_file = os.path.basename(url) | |
dst_path = "%s/%s" % (NGINX_CONF_TMP_DIR, dst_file) | |
try: | |
urllib.urlretrieve(url, dst_path) | |
message = "Download %s to %s" % (url, dst_path) | |
logging.error(message) | |
return dst_path | |
except Exception, e: | |
message = "Download %s to %s failed, error:%s" % (url, dst_path, e) | |
logging.error(message) | |
sys.exit(1) | |
def main(): | |
""" | |
1. 获取最新配置; | |
2. 备份用于 LVS 检测的标志文件; | |
3. 备份旧配置; | |
4. 解压新配置到 Nginx 配置目录; | |
5. 检查配置文件有无语法错误; | |
6. 如果有语法错误, 删除新配置, 恢复旧配置, 退出; | |
此时如果reload为True, 那么执行以下步骤: | |
7. reload Nginx; | |
8. 等待 6s; | |
9. 退出. | |
否则执行下面步骤: | |
7. 删除用于 LVS 检测的标志文件; | |
8. 等待 15s; | |
9. 停止 Nginx; | |
10. 启动 Nginx; | |
11. 等待 15s(等待 Nginx 检测应用程序存活); | |
12. 恢复用于 LVS 检测的标志文件; | |
13. 等待 20s; | |
14. 退出. | |
""" | |
parser = optparse.OptionParser() | |
parser.add_option("-b", "--branch", dest="branch", default="master", help="which branch to deploy.") | |
parser.add_option("-r", "--reload", dest="reload", default=False, help="reload or not.") | |
(options, args) = parser.parse_args() | |
logging.info("branch:%s" % options.branch) | |
logging.info("reload:%s" % options.reload) | |
nginx = _shell("hostname") | |
nginx_conf_url = get_nginx_conf_url(nginx, options.branch) | |
nginx_conf_path = download_nginx_conf(nginx_conf_url) | |
cmds = { | |
"bak_conf_del": "/bin/rm -rf /home/work/nginx/conf_bak", | |
"conf_bak": "/bin/cp -a /home/work/nginx/conf /home/work/nginx/conf_bak", | |
"conf_restore": "/bin/cp -rf /home/work/nginx/conf_bak/* /home/work/nginx/conf/", | |
"conf_del": "/bin/rm -rf /home/work/nginx/conf/*", | |
"conf_tar": "tar xzf %s -C /home/work/nginx/conf" % nginx_conf_path, | |
"conf_check": "sudo /home/work/nginx/sbin/nginx -t", | |
"healthy_bak": "/bin/cp -f %s %s" % (HEALTHY_FILE, HEALTHY_FILE_BAK), | |
"healthy_restore": "/bin/cp -f %s %s" % (HEALTHY_FILE_BAK, HEALTHY_FILE), | |
"healthy_del": "/bin/rm -f %s" % HEALTHY_FILE, | |
"sleep15": "sleep 15", | |
"sleep20": "sleep 20", | |
"sleep6": "sleep 6", | |
"nginx_stop": "sudo /etc/init.d/nginx stop", | |
"nginx_start": "sudo /etc/init.d/nginx start", | |
"nginx_reload": "sudo /etc/init.d/nginx reload" | |
} | |
_shell(cmds["bak_conf_del"]) | |
_shell(cmds["healthy_bak"]) | |
_shell(cmds["conf_bak"]) | |
_shell(cmds["conf_del"]) | |
_shell(cmds["conf_tar"]) | |
check_status = _shell(cmds["conf_check"], _exit=0) | |
if check_status == False: | |
_shell(cmds["conf_del"]) | |
_shell(cmds["conf_restore"]) | |
sys.exit(1) | |
if options.reload in [True, "True"]: | |
_shell(cmds["nginx_reload"]) | |
_shell(cmds["sleep6"]) | |
sys.exit(0) | |
_shell(cmds["healthy_del"]) | |
_shell(cmds["sleep15"]) | |
_shell(cmds["nginx_stop"]) | |
_shell(cmds["nginx_start"]) | |
_shell(cmds["sleep15"]) | |
_shell(cmds["healthy_restore"]) | |
_shell(cmds["sleep20"]) | |
sys.exit(0) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment