应用在Windows系统中的自动化部署实践(转)

上一篇 / 下一篇  2014-05-19 15:30:07 / 个人分类:自动化测试

因为公司的产品有linux 和windows两套部署环境,领导安排我先来做windows的自动化部署。由于本人对windows 的dos命令基本没啥概念,所以在最终完成之前,走了很多弯路,在这里记载下来,希望能够对看到这篇文章的人,有所帮助。

好了,废话少说,直接上步骤。

Background :

开发给过来的就是一个server.jar,双击server.jar,可以选择安装路径,选择licence,并最终安装完成。安装完成之后还需要做三个配置:

配置1):配置数据库信息 [在Dos窗口打开安装目录下的DBconnection.bat,输入相应数据库信息,输入完毕以后会自动测试是否链接成功]

配置2):升级数据库 [如果数据库和产品的version不一致,则需要执行dbupgrade.bat]

配置3):修改Run.bat中的某些参数

部署架构图:[从Host 部署到 各个 slave中去]

我的想法是这样的:

首先在Host 机器上手工执行server.jar并安装到某个目录,比如 C:\programs\baseDir\,然后把baseDir复制到 各个slave机器【怎么复制过去让我吃了不少苦头】上去,当然复制之前会做好各种配置,最后通过host调用slave上的某个东东去执行已经复制过去的run.bat.【这里的 “某个东东”让我破费周折,呵呵,这是后话,暂且按下不表

本地配置这里就不详细描述了,主要是用VBS调用Bat,如果bat需要交互的话,就用Wscript.SendKeys。

这里主要说一下我碰到的两个难题。

难题一:把文件夹复制到Slave机器上去:

经过摸索,我发现dos有一个命令,Xcopy,是一个非常强大的拷贝命令,支持本地和远程拷贝,当然拷贝的时候还包含子目录和子文件夹,比如我现在想把 C:\programs\baseDir 这个文件夹拷贝到远程机器上去,可以用如下命令:

 1xcopy c:\programs\baseDir \\RemoteIp\remotefolder\ 

但是这样做,有一个前提,是 RemoteIp所对应的机器需要把 remotefolder设置为共享,并且允许可写,如果每次都复制到这个目录还好,如果我临时想换一个目录,那么又要新建一个目录,并设置共享,这 “显然不美” (最近在看《测试之美》,呵呵,只有美的东西才容易被别人喜欢,比如美女),所以我换了一种方式,直接xcopy 到Remote机器上的根目录,比如如下所示:

 1xcopy c:\programs\baseDir \\RemoteIp\c$\dirA\dirB\ 

这样只需要知道对方的IP,至少一个盘符(比如C盘,就用C$),那么就可以复制到任意文件夹下面,xcopy如果检测到在相应盘符下面没有文件夹,会自动创建,但是如果直接执行这个命令的话,很有可能会碰到如下提示:

 1Access Is Dennied 

除了这个提示以外,没有任何其它提示,在此表示微软的bug report封装的太厉害了,经过苦苦搜索,终于解决了这个问题,Xcopy如果直接拷贝到对方绝对路径,需要先授权,在这里授权 用 net use 命令:(如果 不熟悉此命令,可以在Dos窗口 输入 net use /? 来看说明)

 1Net use \\RemoteDir\Admin$password /user:username 

注意这里的Admin$,这个应该是windows的默认共享,选中computer--右键 manage--System Tools --Share Folders--Shares 可以看得到,

如果这里不加Admin$的话,可能会提示:

1Invalid drive specification20File(s) copied

执行net use之后,dos窗口应该有提示:

 1The command completed successfully. 

此时再执行xcopy应该就没有问题了,如果不是successfully,而仍然是 Access Is Dennied,那么可能和 “简单文件共享” 有关,试试下面这个神器吧:

MicrosoftFixit50053.msi

下载链接 : http://support.microsoft.com/kb/307874/zh-cn

把这个文件在相关机器上都执行一遍,此时在  net use,再 xcopy ,OK了

!!!!!收工!!!!!!!

难题二 :现在把所有的文件都拷贝到远程机器上去了,那么怎么启动呢?

策略1)因为启动文件是一个Bat文件,我最先想到的是利用telnet命令,并暗自窃喜,原来一切都这么简单。。,但 是很快发现 telnet消耗的是本地资源,我启动一个应用都至少消耗1.5G的内存,我要同时启动那么多机器,用telnet的方式全部消耗本地资源,显然不现实。

策略2)放弃telent,转而把目光投向远程服务,我在host机器上向slave机器发起一个命令,在slave机器上创建一个服务,用这个服务去调用对应的 bat文件,想法很好,但是很快被一个错误打击了,没找到解决方案,具体是什么错误忘记了。。。

策略3)再研究,发现 schtasks 这玩意是个好东西,可以利用host发送schtasks命令在slave机器上创建服务,然后还可以随时启动服务和停止服务,为了尽可能的不要让任务自 己运行,而是在收到我指令的时候才运行,我把这个任务设置成每12个月运行一次。。。

 1schtasks /create /S RemoteIp /u admin /p password /sc monthly /mo 12 /tn TaskName /tr C:\dirA\dirB\Slave1Run.bat /F 

此时会显示创建成功,然后去触发这个任务

 1schtasks /Run /S RemoteIp /u username /p password /tn taskname 

然后在远程win7机器上也运行了,可以看到界面,然而,当Win7 向 Win Xp 发起请求的时候,Access is dennied 又出来了。。。搜遍中外网站 (国外网站请用goagent,你懂的),最后在微硬的社区发现这样一段话:

I suspect you are running from/To Win7/Vista to XP

SchTasks.exe are not the same between these systems and it won't work. It took me a while to figure it out.

好吧,Schtasks 你好,Schtasks 再见。

策略4)继续搜。。,最后搜到一个神器 psexec

下载地址:http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

Utilities like Telnet and remote control programs like Symantec's PC Anywhere let you execute programs on remote systems, but they can be a pain to set up and require that you install client software on the remote systems that you wish to access. PsExec is a light-weight telnet-replacement that lets you execute processes on other systems, complete with full interactivity for console applications, without having to manually install client software. PsExec's most powerful uses include launching interactive command-prompts on remote systems and remote-enabling tools like IpConfig that otherwise do not have the ability to show information about remote systems.

  好吧,死马当活马医,立即尝试!,把psexec配置好以后,执行以下命令:

 1psexec \\RemoteIp -u username -p password -i -d c:\dirA\Run.bat 

发现在有的机器上成功,有的机器上显示Access is dennied。。。看来微硬对这个log很喜欢啊。。,此时再次搬出之前的神器MicrosoftFixit50053.msi

参看URL :

http://forum.sysinternals.com/topic15919.html

http://davidchuprogramming.blogspot.com/2009/12/psexecexe-access-denied.html

执行完以后,启动成功!

!!!!!收工!!!!!!!

最后,附加几个VBS方法,May be helpful or not。

获取系统中正在运行的cmd.exe的个数:

'==============================================================
'Function Name : CountNumberOfCMD
'Summary :
'    Count All cmd processes in system
'==============================================================
Function CountNumberOfCMD()
    Dim objWMIService, processItems, processName
    processName = "cmd.exe"
    Set bjWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set processItems = objWMIService.ExecQuery("Select * from Win32_Process where Name='"  & processName & "'")
    CountNumberOfCMD = processItems.Count
End Function
获取当前位置:
'===================================================================
'Function Name: GetTestDataFileAbsolutePath
'Summary: Get The Config Excel's Absolute Path
'===================================================================
Function GetTestDataFileAbsolutePath()
Dim ws
set ws=CreateObject("Scripting.FileSystemObject")
GetTestDataFileAbsolutePath = ws.GetFile(Wscript.ScriptFullName).ParentFolder.Path & "\Config\Config.xls"
'GetTestDataFileAbsolutePath = "C:\AD\Config\Config.xls"
Set ws = Nothing
End Function

打开一个excel:
'===================================================================
'Function Name: openExcel
'Summary: Open Excel File
'===================================================================
Function openExcel()
Set xlsApp = CreateObject("Excel.Application") '创建Excel对象
xlsApp.Visible = False 'true 为显示excel对象,false为不显示
Set xlsWorkBook = xlsApp.Workbooks.Open (GetTestDataFileAbsolutePath()) '打开指定路径的Excel表格
End Function

关闭excel:

Function closeExcel()
  xlsWorkBook.Close
  xlsApp.Quit
  Set RowCount = Nothing
  Set xlsSheet = Nothing
  Set xlsWorkBook = Nothing '释放内存
  Set xlsApp = Nothing  '释放Excel对象   
End Function

VBS 调用Dos命令:

'============================================================
'Name: NetUseToSlave
'Summary: Net use to slave to get the cotrol privilege
'
'============================================================
Function NetUseToSlave(ObjSlave)
    Dim oShell
    Set Shell = CreateObject("WScript.Shell")
    oShell.Run ("%comspec% /c " & "NET USE \\" & ObjSlave.SlaveIp &"\Admin$ /delete /yes"),1,True
    WScript.Sleep(2000)
    oShell.Run ("%comspec% /c " & "NET USE \\" & ObjSlave.SlaveIp & "\Admin$ " & Passowrd &" /user:" & Username),1,True
    WScript.Sleep(1000)
    Set Shell = Nothing
End Function

vbs判断文件是否存在:

'========================================================
'Function Name:CheckFileExists
'Summary: Check file exists or not
'FolderPath: Absolute path, as D:\server\bin\db.log
'========================================================
Function CheckFileExists(FilePath)
    Dim fso
    set fso=createobject("scripting.filesystemobject")
    if fso.FileExists(FilePath) then
       CheckFileExists = True
    else
       CheckFileExists = False
    end If
    Set fso =Nothing
End Function


vbs判断文件夹是否存在:

'======================================================
'Function Name:CheckFolderExists
'Summary: Check folder exists or not
'FolderPath: Absolute path, as c:\dir
'======================================================
Function CheckFolderExists(FolderPath)
  Dim fso
  set fso=createobject("scripting.filesystemobject")
  if fso.FolderExists(FolderPath) then
     CheckFolderExists = True
  else
    CheckFolderExists = False
  end If
  Set fso =Nothing
End Function


vbs正则修改文件内容:

Function ModifyLaunch(slave)
  Dim FileName,FS, FileStream,OutStream,FileContents,RFileContensts
  FileName = slave.CopyFromDir & "\bin\launch.bat"
  Set FS = CreateObject("Scripting.FileSystemObject")  
  on error resume Next  
  Set FileStream = FS.OpenTextFile(FileName)  
  FileContents = FileStream.ReadAll

  'Sub1 : replace Ip
  Dim regEx
  Set regEx = New RegExp
 
  regEx.Pattern = "1.7.7.\d+"
  RFileContensts = regEx.Replace(FileContents,"228.7.7." & Split(slave.SlaveIp,".")(3))
 
  on error resume Next
  Set utStream = FS.OpenTextFile(FileName, 2, True)  
  OutStream.Write RFileContensts
 
  Set FS = Nothing
  Set FileStream = Nothing
  Set utStream = Nothing
End Function




TAG:

 

评分:0

我来说两句

Open Toolbar