日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python快速部署一个服务器_Python加Shell快速部署集群

發布時間:2025/3/20 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python快速部署一个服务器_Python加Shell快速部署集群 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近痛感在集群里逐臺部署ganglia, cacti這些監控的東西很麻煩,就寫了個小程序去批量自動部署。原理是通過Python的pexpect用ssh去復制文件和執行系統命令,我用它來部署ganglia等,但是其他的東西也可以通過這個腳本來批量部署,只要自己編寫部署腳本就可以了。主要是提出一個解決思路,看對大家是否有所幫助。

先約定一個概念,我們把放置python和腳本的服務器叫做主控節點或者server,把需要安裝的節點叫做受控節點或者client。以下均以server和client代稱。

首先是配置文件,我需要先定義一個配置文件,來約定server的一些路徑,和client具體信息。

#-*-coding:UTF-8 -*-

log_dir = './logs/'#定義日志路徑,不過我還沒寫,打算用log4py來做

client_tmp_dir = '/tmp/'#定義client端存放腳本路徑

ssh_port = '22'#SSH端口

script_dir = './shells/'#server端腳本存放路徑

node_list = [

{'ip':'192.168.1.1', 'user':'root', 'passwd':'123456', 'cmd':'sh /tmp/dpkg_client_Ubuntu_x.x86_64.sh'},

#cmd為在client端執行的命令,別的不解釋

{'ip':'192.168.1.2', 'user':'root', 'passwd':'123456', 'cmd':'sh /tmp/dpkg_client_ubuntu_x.x86_64.sh'},

]

接下來是主程序

#!/usr/bin/env python

#-*-coding:UTF-8 -*-

import os

import sys

import platform

#用import方式引入conf.py

import conf

import subprocess

'''

判斷server(本機)操作系統類型和版本的類

'''

class System:

def GetBranch(self):

Branch = platform.dist()[0]

return Branch

def GetRelease(self):

Release = platform.dist()[1]

return Release

def GetInstaller(self):

if self.GetBranch() in ['Ubuntu', 'debian']:

installer = 'apt-get'

elif self.GetBranch() in ['RedHat', 'Fedora', 'CentOS']:

installer = 'yum'

elif self.GetBranch() in ['SUSE']:

installer = 'zypper'

else:

installer = 'unknown'

return installer

'''

以操作系統版本為依據獲取相應的pexpect包并嘗試引入,因pexpect非默認操作系統安裝,這部分支持RH,Ubuntu,Debian,SuSE

'''

try:

import pexpect

except ImportError:

installer = System()

inst = install.GetInstaller()

if (inst == 'apt-get') or (inst == 'zypper'):

cmd = '%s install python-pexpect' % (inst)

elif inst == 'yum':

cmd = '$s install pexpect' % (inst)

else:

cmd = 'echo "Not support yet:)"';

try:

fd = subprocess.Popen( cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE )

out = fd.stdout.readlines()

err = fd.stderr.readlines()

all = out+err

all = "".join(all)

except OSError,e:

all = "Cannot run command, Exception:"+e+os.linesep

import pexpect

#print all

'''

pexpect執行類,分兩個方法,ssh和scp,自動判斷是否首次連接,并自動完成yes或輸入密碼的應答。

'''

class Expect:

#定義ssh方法,入口變量包括ip, port,username,password,執行命令

def ssh(self, ip, port, user, passwd, cmd):

#創建連接子進程對象

ssh = pexpect.spawn('ssh -p %s %s@%s "%s"' % (port, user, ip, cmd))

r = ''

try:

#判斷是否首次連接,如果是首次,則回答yes并輸入密碼,否則直接輸入密碼

i = ssh.expect(['password:', 'continue connecting (yes/no)?'], timeout=5)

if i == 0 :

ssh.sendline(passwd)

elif i == 1:

ssh.sendline('yes')

ssh.expect('password:')

ssh.sendline(passwd)

except pexpect.EOF:

ssh.close()

else:

r = ssh.read()

ssh.expect(pexpect.EOF)

ssh.close()

return r

#定義scp方法,入口變量包括ip,port,username,password,需要復制到client的文件名,復制到client的路徑

def scp(self, ip, port, user, passwd, srcfile = "index.html", distpath):

#創建連接子進程對象

ssh = pexpect.spawn('scp -P %s %s %s@%s:%s ' % (port, file, user, ip, distpath))

r= ''

try:

#判斷是否首次連接,如果是首次,則回答yes并輸入密碼,否則直接輸入密碼

i = ssh.expect(['password:', 'continue connecting (yes/no)?'], timeout=5)

if i == 0:

ssh.sendline(passwd)

elif i == 1:

ssh.senline('yes')

ssh.expect('password:')

ssh.sendline(passwd)

except pexpect.EOF:

ssh.close()

else:

r = ssh.read()

ssh.expect(pexpect.EOF)

ssh.close()

return r

#創建conf中的對象,只是為了寫起來方便。不創建直接用也行

packages = conf.package_dir

logs = conf.log_dir

c_tmp = conf.client_tmp_dir

port = conf.ssh_port

scripts = conf.script_dir

nodes = conf.node_list

expect = Expect()

#在本機執行server端腳本。該腳本會安裝Ganglia gmetad,gmond,cacti,nagios,gangliaweb,mysql,apache等等

os.system("sh " + scripts + "dpkg_server_ubuntu_x.x86_64.sh")

#循環列出conf的列表中配置的主機,用戶名,密碼,執行命令

for i in range(len(nodes)):

ip = nodes[i]['ip']

user = nodes[i]['user']

passwd = nodes[i]['passwd']

cmd = nodes[i]['cmd']

#將本機的client執行腳本復制到client端的conf.py中定義的路徑client_tmp_dir

r = expect.scp(ip, port, user, passwd, scripts+'dpkg_client_ubuntu_x.x86_64.sh', c_tmp)

print r

#在client端執行剛復制過去的腳本,腳本中包含gmond,nagios-client,snmpd等等

r = expect.ssh(ip, port, user, passwd, cmd)

print r

我還沒寫按自動判斷client端操作系統安裝不同腳本的邏輯,有興趣的可以自己改改,其實本身腳本并不難,核心都是在pexpect,我主要是不想用puppet,不想用puppet的主要原因是不想學ruby和他那復雜的配置文件。不光是部署監控,自己寫shell腳本,隨便部署什么都可以。nginx,php,反正能用腳本完成的事都可以干。

總結

以上是生活随笔為你收集整理的python快速部署一个服务器_Python加Shell快速部署集群的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。