使用STAF進行自動化安裝測試

首先安裝STAF

在實體機和目標機器中均需要安裝,STAF擁有對Python支持的庫函數,可以直接調用,比較方便,所以測試腳本決定用Python來寫。

需要注意的是,在/staf/bin中有一個STAF.cfg文件需要修改server和client端的trustlevel = 5.注意不同的command的trust level的要求不一樣,所以在我的測試環境裏面直接設定成爲5比較方便。


安裝之後,需要將STAF在實驗機中設定爲開機自啓動。以便可以直接進行連接操作,不過我是使用虛擬機進行測試,只要直接載入一個開機的snapshot就可以了。

具體可以參照這個網址的開機自啓動設置。

點擊打開鏈接


這次測試環境爲windows,方法是目標機要從server上面檢查並且下載一個安裝文件,並且執行安裝。所以,windows 必須執行靜默安裝,並且通過STAF執行ftp命令時候需要通過載入一個conf文件來執行下載任務。

同時在Python中需要操作exsi,這個API可以直接Google到。Python真的很方便額。。。


需要注意的是在操作時候,安裝和check的代碼之間需要有一個sleep等待,以保證client端執行成功。


#-------------------------------------------------------------------------------
# Name:        STAF_test
# Purpose:     Used for staf automation test. rollback a snapshot, download and
#              install a program from server
#
# Author:      Levin
#
# Requirement: You need to install the Staf on your local machine and add your
#              machine's ip to the */staf/bin/STAF.cfg. And add the */staf/bin
#              to your python path.
#
# Created:     25/08/2014
# Copyright:   (c) Levin 2014
# Licence:     <your licence>
#-------------------------------------------------------------------------------

from PySTAF import *
import pysphere
from xml.etree import ElementTree as ET
import sys
from time import sleep

##-----------Prepare the variables need to use-----------------##
nodeall = ET.parse('config.xml')
IPnode = nodeall.find('SETTING')
EXSIIP = IPnode.getchildren()[0].text
UserName = IPnode.getchildren()[1].text
PassWord = IPnode.getchildren()[2].text
ClientIP = IPnode.getchildren()[3].text
VirtualMachine = IPnode.getchildren()[4].text
Snapshot = IPnode.getchildren()[5].text

Pathnode = nodeall.find('WORKINGPATH')
ClientPath = Pathnode.getchildren()[0].text
LocalPath = Pathnode.getchildren()[1].text


##------------------installation process------------##
def staf_install():
    try:
        handle = STAFHandle("test")
    except STAFException, e:
        print "Error registering with STAF, RC: %d" % e.rc
        sys.exit(e.rc)

    while (True):
        result = handle.submit(ClientIP, "ping", "ping")
        if (result.rc == 0):
            break
        print "Error submitting request, RC: %d, Result: %s" % (result.rc, result.result)
    print "connected"

    result = handle.submit("local", "FS", "COPY File "+LocalPath+r"\ftpSample.conf TODIRECTORY "+ClientPath+" TOMACHINE "+ClientIP)
    if (result.rc == 0):
        print "Upload config file succeed!"
    else :
        print "Upload config file failed!"
        print "%s" % result.result
        sys.exit(1)
    result = handle.submit(ClientIP, "process", "start shell command" +  "\"ftp -s:ftpSample.conf\"" + "workdir " + ClientPath)
    if (result.rc == 0):
        print "Download install file succeed!"
    else :
        print "Download install file failed!"
        print "%s" % result.result
        sys.exit(2)
    print "Now wait 6 seconds"
    sleep(6)
    result = handle.submit(ClientIP,"process", "start shell command" + "\"ActivePerl-5.16.3.1604-MSWin32-x86-298023.msi/qn\""+ " workdir " + ClientPath)
    print "Now wait 23 seconds for installation complete"
    sleep(23)
    result = handle.submit(ClientIP,"fs", "list directory " + r"C:\Perl\bin")
    if ("perl.exe" in result.resultObj):
        print "Installation succeed!"
    else :
        print "Installation failed!"
        sys.exit(3)

##--------------reset the exsi to clean the environment-------##
def Reset_Virtualmachine():
    server = pysphere.VIServer()
    try :
        server.connect(EXSIIP, UserName, PassWord)
    except pysphere.VIException, e :
        print "Can not connect to exsi. Exit now!"
        sys.exit(e.rc)
    print "Connected to EXSI"
    vm = server.get_vm_by_name("win2k3-R2-32bits")
    vm.revert_to_named_snapshot(Snapshot)

if __name__ == '__main__':
    Reset_Virtualmachine()
    print "Enter to Staf install."
    staf_install()


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章