1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#!/usr/bin/python # conding:utf8 from optparse import OptionParser
import re
import pycurl
import StringIO
import sys
import urllib2
import subprocess
import threading
import os
from optparse import OptionParser
def parse_url(url):
c = pycurl.Curl()
b = StringIO.StringIO()
c.setopt(c.URL, url)
c.setopt(pycurl.FOLLOWLOCATION, 1 )
c.setopt(c.WRITEFUNCTION, b.write)
c.perform()
c.setopt(c.CONNECTTIMEOUT, 5 )
c.setopt(c.TIMEOUT, 5 )
status = c.getinfo(pycurl.HTTP_CODE)
if status = = 200 :
content = b.getvalue()
return content
c.close()
b.close()
def nginx_download(version):
nginx_package = "nginx-%s.tar.gz" % (version)
url = "http://nginx.org/download/%s" % (nginx_package)
if not os.path.exists( "/root/%s" % (nginx_package)):
print "download %s ..." % (nginx_package)
f = urllib2.urlopen(url)
data = f.read()
with open ( "/root/%s" % (nginx_package), "wb" ) as nginx:
nginx.write(data)
print "download success"
return 0
def list_nginx():
url = "http://nginx.org/download/"
content = parse_url(url)
version = []
p = re. compile (r '>(nginx-(.*?).tar.gz)<' )
for m in p.finditer(content):
version.append(m.group( 2 ))
return version[: - 21 : - 1 ]
def install_nginx(version):
nginx_package = "nginx-%s.tar.gz" % (version)
lua_module = "https://github.com/openresty/lua-nginx-module.git"
echo_module = "https://github.com/openresty/echo-nginx-module.git"
check_module = "https://github.com/yaoweibin/nginx_upstream_check_module"
packages = [ 'openssl-devel' , 'zlib-devel' , 'pcre-devel' , 'lua-devel' , 'gcc' ]
for package in packages:
p = subprocess.call( "rpm -qa | grep '%s'" % (package), shell = True )
print p
if p ! = 0 :
p1 = subprocess.call( "yum install %s -y" % (package), shell = True )
if p1 = = 0 :
print "yum install %s success" % (package)
if os.path.exists( "/root" ):
subprocess.call( "cd /root" , shell = True )
if not os.path.exists( "/root/lua-nginx-module" ):
print "git lua-nginx-module ..."
p = subprocess.call( "git clone %s" % (lua_module), shell = True )
if p = = 0 :
print "git lua-nginx-module success"
if not os.path.exists( "/root/echo-nginx-module" ):
print "git echo-nginx-module ..."
p = subprocess.call( "git clone %s" % (echo_module), shell = True )
if p = = 0 :
print "git echo-nginx-module success"
if not os.path.exists( "/root/nginx_upstream_check_module" ):
print "git nginx_upstream_check_module ..."
p = subprocess.call( "git clone %s" % (check_module), shell = True )
if p = = 0 :
print "git nginx_upstream_check_module success"
if not os.path.exists( "/root/nginx-%s" % (version)):
print "tar %s ..." % (nginx_package)
subprocess.call( "tar -xzvf %s" % (nginx_package), shell = True )
print "tar success"
print "install %s ..." % (nginx_package)
p = subprocess.call( "cd /root/nginx-%s;./configure --prefix=/usr/local/nginx-%s --with-http_ssl_module --with-http_gzip_static_module --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --add-module=/root/lua-nginx-module --add-module=/root/echo-nginx-module" % (version, version), shell = True )
if p = = 0 :
p = subprocess.call( "cd /root/nginx-%s;make" % (version), shell = True )
if p = = 0 :
subprocess.call( "cd /root/nginx-%s;make install" % (version), shell = True )
print "install %s success" % (nginx_package)
def main():
parser = OptionParser()
parser.add_option( "-l" , "--list" , action = "store_true" , dest = "list" , help = "list nginx version" )
parser.add_option( "-i" , "--version" , action = "store" , dest = "version" , help = "install nginx version" )
(options, args) = parser.parse_args()
if options. list :
for version in list_nginx():
print version
elif options.version:
if options.version in list_nginx():
if nginx_download(options.version) = = 0 :
install_nginx(options.version)
if __name__ = = '__main__' :
main()
|
本文转自 Art_Hero 51CTO博客,原文链接:http://blog.51cto.com/curran/1624960,如需转载请自行联系原作者