python在Linux叢集運維時需要統一python版本,簡單的編寫python安裝腳本,以便統一安裝,需要機器有安裝包源環境(網絡源/本地源)python源碼包(下載下傳到本地或線上),源碼包國内推薦sohu python源包。
此腳本無任何技術可言,用os.syetem 調用Linux指令,根據傳回值判斷是否成功執行 僅input_timeout_python 問題 在stackoverflow上尋求的答案。
判斷系統版本用platform (不同版本的依賴和安裝方式不同)
if "redhat" in os_platform:
print color_green("正在安裝依賴包...")
res = os.system(
"yum -y install wget sqlite-devel xz gcc automake zlib-devel openssl-devel epel-release git > /dev/null 2>&1")
elif "Ubuntu" in os_platform:
res = os.system(
" apt-get -y install wget libkrb5-dev libsqlite3-dev gcc make automake libssl-dev zlib1g-dev libmysqlclient-dev libffi-dev git > /dev/null 2>&1")
else:
print color_red("此腳本暫不支援此系統,{}".format(os_platform))
sys.exit(1)
input_timeout_on_python 問題(這是一個自己給自己挖的坑,應該安裝python的同仁都要安裝ssl支援吧)
自己修改代碼如下(引用忽略):
action = "yes" #預設值
timeout = 5
print color_green("請選擇是否支援ssl,預設支援(yes/no)"),
rlist, _, _ = select([sys.stdin], [], [], timeout)
if rlist:
action = sys.stdin.readline() #擷取新的輸入值(如果有)
os.system("sed -i '205s/^#//g' ./Python-3.6.2/Modules/Setup")
os.system("sed -i '210s/^#//g' ./Python-3.6.2/Modules/Setup")
os.system("sed -i '211s/^#//g' ./Python-3.6.2/Modules/Setup")
os.system("sed -i '212s/^#//g' ./Python-3.6.2/Modules/Setup")
def color_red(context):
return '\033[1;31;40m{}\033[0m'.format(context)
def color_green(context):
return '\033[1;32;40m{}\033[0m'.format(context)