去年有出一个python整的LAMP自动安装,不过比较傻,直接调用的yum 去安装了XXX...不过这次一样有用shell..我也想如何不调用shell 来弄一个LAMP自动安装部署啥啥的..不过尼玛智商有限,没搞定,暂且分享一下
先说说目前的缺陷
这个脚本总体来说是调用一个字典,组成这个字典是最花费时间的,实际代码到是没几行,本来想把Nginx 的部署也加进去,什么memcached phpmyadmin apc 这样的常用组件作为功能加进去,尼玛,时间不够,只能打打酱油,包括最后我也只能用一行行if来让脚本能run起来,好吧,我想时间充足了一定要改进下,如果你看到此代码,如果仔细看看啊,对...日志记录这块,因为我没有判断异常,函数执行失败,脚本不会写日志,有时间改一下,不过近期不太会改。
注:
废话也不多说,这个脚本没有多少实用性,仅作分享学习之意吧,注(这类脚本还是用shell简单粗暴),有时间我也会改进它,也欢迎各位大牛来一起XXX
软件版本:
###怎么是这么奇葩的组合###
##等Nginx 加入的时候再改好了,只所以这么用是公司内部有套系统PHP版本要求高..所以直接用这个测试了..跑的也还行###
[root@ipython ~]# ls source/*.bz2 | sort
source/apr-1.5.1.tar.bz2
source/apr-util-1.5.3.tar.bz2
source/curl-7.36.0.tar.bz2
source/freetype-2.5.2.tar.bz2
source/httpd-2.4.9.tar.bz2
source/jpegsrc.v9a.tar.bz2
source/libmcrypt-2.5.8.tar.bz2
source/libpng-1.6.8.tar.bz2
source/libxml2-2.8.0.tar.bz2
source/mysql-5.1.58.tar.bz2
source/ncurses-5.9.tar.bz2
source/pcre-8.35.tar.bz2
source/php-5.5.12.tar.bz2
source/xcache-3.1.0.tar.bz2
source/zlib-1.2.8.tar.bz2
部分代码如下(具体看附件吧):
#!/usr/bin/python
##coding:utf-8##
#-------------------------------------------------------------------------------
# Name: LAMP-Auto-Install.py
#
# Author: LiuSha
#
# Created: 9/07/2014
# Copyright: (c) http://www.ipython.me/ 2014
#-------------------------------------------------------------------------------
import os
import sys
import time
import getopt
import logging
import commands
from Dict import Global
from platform import machine
#Define Log Output#
logging.basicConfig(filename = os.path.join(os.getcwd(), 'log.txt'),
filemode = 'w',
level = logging.DEBUG,
format = '%(asctime)s %(filename)s %(levelname)s %(message)s',
datefmt = '%a,%d %b %Y %H:%M',)
#Define Main Funtion Class#
class funcTion():
def main(self):
"""define global main config. get global build param"""
try:
longargs = ["prefix=","debug=","webservice=","jobdir=","add-vhost=","add-func=","howto="]
opts,args = getopt.getopt(sys.argv[1:],"h",longargs)
Global.Mainargs = dict(opts)
except getopt.GetoptError,err:
print str(err)
sys.exit(2)
def echo(self,output,color = '32'):
"""define calor Output funtion"""
return '\033[1;{1};40m{0}\033[0m'.format(output,color)
def exec_commands(self,cmd,cmdAlias,istName = ''):
"""define commands exec function"""
status,output = commands.getstatusoutput("{0}".format(cmd))
if int(status) == 0:
logging.info('{0} execution successful -->\n ####---{1}---####'.format(cmdAlias,istName))
else:
logging.debug('{0} execution exception -->\n ####---{1}---####\n\n{2}\n\n'.format(cmdAlias,istName,output))
def Unpack(self,istName,jobdir,tarname,tardir):
"""define unpack function"""
print self.echo("%s Starting Install".ljust(80)%istName)
if tarname[-3:] == 'bz2':
os.chdir("%s"%jobdir)
os.system("tar jxf %s"%tarname)
os.chdir("%s"%tardir)
else:
print self.echo('[Error1]:Unpark format Error','31')
def command_Check(self,command,istCommand):
"""define Command check via Yum"""
if os.path.exists("/usr/bin/%s"%command) == False:
self.exec_commands("yum -y install {0} {1}".format(istCommand,Global.workMode['OutPut']),"yum install {0}".format(command),"command_Check[function]")
else:
pass
如何使用:
##解包##
[root@ipython ~]# unzip LAMP-Auto-Install.zip
Archive: LAMP-Auto-Install.zip
creating: source/
inflating: source/apr-1.5.1.tar.bz2
inflating: source/apr-util-1.5.3.tar.bz2
creating: source/conf/
inflating: source/conf/httpd
inflating: source/conf/httpd.conf
inflating: source/conf/iptables
inflating: source/conf/limits.conf
inflating: source/conf/my.cnf
inflating: source/conf/mysqld
inflating: source/conf/php.ini
inflating: source/conf/sysctl.conf
inflating: source/curl-7.36.0.tar.bz2
inflating: source/freetype-2.5.2.tar.bz2
inflating: source/httpd-2.4.9.tar.bz2
inflating: source/jpegsrc.v9a.tar.bz2
inflating: source/libmcrypt-2.5.8.tar.bz2
inflating: source/libpng-1.6.8.tar.bz2
inflating: source/libxml2-2.8.0.tar.bz2
inflating: source/mysql-5.1.58.tar.bz2
inflating: source/ncurses-5.9.tar.bz2
inflating: source/pcre-8.35.tar.bz2
inflating: source/php-5.5.12.tar.bz2
inflating: source/xcache-3.1.0.tar.bz2
inflating: source/zlib-1.2.8.tar.bz2
inflating: Dict.py
inflating: Main.py
我也象征性的整了个Help:
[root@ipython ~]# python Main.py --howto=enable
Usage:
--prefix [=PATH]:
install path
--jobdir [=PATH]:
source code path
--debug [enable|disable]:
whether friendly output
--add-vhost [=Domain(www.ipython.com)]:
add virtual host
--add-func [=xcache]:
add function
--webservice [apache|nginx]:
nginx or apache
example:
python Main.py --prefix=/software --jobdir=`pwd`/source --webservice=apache --add-func=xcache
python Main.py --prefix=/software --jobdir=`pwd`/source --webservice=apache --debug=disable
run起来:
有定义logging,会记录日志,为当前目录中的log.txt
服务必须可以启动
[root@ipython ~]# service mysqld start
Starting MySQL SUCCESS!
[root@ipython ~]# service httpd start
Starting Apache web server: