def remove_fe(self): """ Removing Linux driver from the Linux_Host using rpm -e """ try: vm=Passh(host=vmHost, username=vmUser, password=vmPass, verbose=VerboseLevel, doReturn=True) cmd = 'rpm -qa|grep fedriver' stdoutData, stderrData = vm.run_ssh(cmd=cmd) if stdoutData and not stderrData: ToRemove = stdoutData[0].strip() print "rpm -ev %s %s" %(rpmOptions, ToRemove) cmd = "rpm -ev %s %s" %(rpmOptions, ToRemove) stdoutData, stderrData = vm.run_ssh(cmd=cmd) for line in stdoutData: print line.strip('\n') for line in stderrData: print line.strip('\n') cmd = 'rpm -qa|grep %s' %ToRemove stdoutData, stderrData = vm.run_ssh(cmd=cmd) if not stdoutData: print "Removed %s" %ToRemove else: print "Error while trying to remove %s" %ToRemove else: print "Warning: did not find a Linux driver to remove." for line in stdoutData: print line.strip('\n') for line in stderrData: print line.strip('\n') except Exception, e: print "Error: %s" %e
def get_uname_a(self, verbose=False): """ Run uname -a and return the result """ if verbose: print "Determining Linux Machine OS type and version ..." UnameA = "" try: cmd = "uname -a" if verbose: print "\n %s" %cmd vm=Passh(host=vmHost, username=vmUser, password=vmPass, verbose=VerboseLevel, doReturn=True) stdoutData, stderrData = vm.run_ssh(cmd=cmd) if verbose: for line in stdoutData: print " (reply)", line.strip('\n') for line in stderrData: print " (error)", line.strip('\n') if len(stdoutData) == 1 and not stderrData: UnameA = stdoutData[0].strip() except Exception, e: print "Error: %s" %e
def get_os_update_version(self, verbose=None): """ Get the OS update version. For example, from 'uname -r' we get 2.6.18-164.el5 The update version is the '164' part. We will split by '-' and take the second half (164.el5), then we split again by '.' and take the first part (164). """ # get the output of uname -r # XXX TODO: pass these in, instead of using global vm=Passh(host=vmHost, username=vmUser, password=vmPass, verbose=VerboseLevel, doReturn=True) cmd = "uname -r" if verbose: print "\n %s" %cmd stdoutData, stderrData = vm.run_ssh(cmd=cmd) if verbose: print " (reply) %s" %stdoutData[0] words = stdoutData[0].strip("\n").split("-",1) if verbose: print "Split ", stdoutData[0].strip(), " by '-' into", words # in some cases, uname returns a string with more than one "-", for example 2.6.27.19-5-default # we will treat the second or more "-" as a "." updatePart = words[1].replace("-",".").split(".") if verbose: print "Split ", words[1], " by '.' into", updatePart # remove the last element from the list, which is usually the "el5" or "smp" part of the update string if len(updatePart) > 1 : updatePart.pop() if verbose: print "Update part, with last element removed", updatePart update = "".join(updatePart).strip() if verbose: print "the update string is", update return update
def query_fe(self): """ query Linux_Host for the Linux driver installed, if any """ try: vm=Passh(host=vmHost, username=vmUser, password=vmPass, verbose=VerboseLevel) cmd = "rpm -qa|grep fedrivers" vm.run_ssh(cmd=cmd) except Exception, e: print "Error: %s" %e
def get_os_release(self, verbose=False): """ run cat /etc/redhat-release or /etc/SuSE-release, and return the result Such as "Red Hat Linux release 5.4 (final)" or SUSE Linux Enterprise Server 11 (x86_64) VERSION = 11 PATCHLEVEL = 0 """ if verbose: print "reading /etc/*release ..." vm=Passh(host=vmHost, username=vmUser, password=vmPass, verbose=VerboseLevel, doReturn=True) OSTypeList = dict(redhat='/etc/redhat-release', \ sles='/etc/SuSE-release') #OSTypeList = {'redhat': '/etc/redhat-release', 'sles': '/etc/SuSE-release'} OsType = None OSFile = None # check where is the /etc/redhat-release, or its equivilant file for key, value in OSTypeList.items(): #print "key is", key, ". value is", value if verbose: print "\n check if '%s' exists on Linux Machine " % value cmd = "[[ -f %s ]] && echo yes || echo no" % value stdoutData, stderrData = vm.run_ssh(cmd=cmd) if verbose: for line in stdoutData: print " (reply)", line.strip('\n') for line in stderrData: print " (error)", line.strip('\n') if "yes\n" in stdoutData: if verbose: print "\n==> This os is '%s' family" %key OSType = key OSFile = value break cmd = "cat %s" %OSFile if verbose: print "\n %s" % cmd stdoutData, stderrData = vm.run_ssh(cmd=cmd) return stdoutData, stderrData
def show_fe(self, buildType="official", buildNum=None): """ show what drivers are available for updating """ try: if buildNum == 'latest': bld = Passh(host=buildHost, username=buildName, password=buildPass, verbose=VerboseLevel, doReturn=True) matchString = "???" cmd = "cd /import/automation/builds/server1 && /bin/ls -1drt %s-%s 2>/dev/null" \ %(buildType, matchString) stdoutData, stderrData = bld.run_ssh(cmd=cmd) buildNum = stdoutData[-1].strip().split('-')[1] print "The latest build is '%s'" %buildNum if buildNum == None: regEx = "*" print "Available driver builds:" bld = Passh(host=buildHost, username=buildName, password=buildPass, verbose=VerboseLevel, doReturn=True) cmd = "cd /import/automation/builds/server1 && /bin/ls -1drt %s-%s" \ %(buildType, regEx) stdoutData, stderrData = bld.run_ssh(cmd=cmd) if stderrData and "No such file or directory" in stderrData[0].strip(): print "Warning: Did not find any builds." else: for line in stdoutData: # we only want to show the symlinks such as "official-123", not the long form if not re.search(r'\.', line): print line.strip() for line in stderrData: print line.strip() else: # a build num is passed in bld = Passh(host=buildHost, username=buildName, password=buildPass, verbose=VerboseLevel, doReturn=True) cmd = "cd /import/automation/builds/server1/%s-%s/packages && /bin/ls -1rtL *fedriver*" %(buildType, buildNum) stdoutData, stderrData = bld.run_ssh(cmd=cmd) if stderrData and "No such file or directory" in stderrData[0].strip(): print "Error: No such build '%s'" %buildNum else: print "Builds %s Linux driver files:" %buildNum for line in stdoutData: print line.strip() for line in stderrData: print line.strip() except Exception, e: print "Error: %s" %e
def update_fe(self, buildType="official", buildNum=None, simulate=None): """ update the fe driver on Linux_Host. """ #print "executing updateFE" # if user does not pass in options, try getting it from self object if simulate is None: simulate = self.simulate if buildNum is None: print "Error: Unable to update driver because 'build' not specified" sys.exit(-1) # XXX todo: raise exception instead if buildNum == 'latest': bld = Passh(host=buildHost, username=buildName, password=buildPass, verbose=VerboseLevel, doReturn=True) matchString = "???" cmd = "cd /import/automation/builds/server1 && /bin/ls -1drt %s-%s 2>/dev/null" \ %(buildType, matchString) stdoutData, stderrData = bld.run_ssh(cmd=cmd) buildNum = stdoutData[-1].strip().split('-')[1] print "\nThe latest build is '%s'" %buildNum if simulate: print " \n*** simulation mode. Not actually running the commands ***\n" try: # determine what version we need vmos=OSUtil() stdoutData, stderrData = vmos.get_os_release(verbose=verbose) # if we get bad data, just substitute a string for OSRelease if stdoutData and not stderrData: OSRelease = stdoutData[0].strip() else: OSRelease = "Unknown OS" print "Determine the Linux driver for Linux_Host %s '%s'" %(vmHost, OSRelease) OSData = vmos.get_os_info_str() OSString = OSData # get the correct build from build machine bld = Passh(host=buildHost, username=buildName, password=buildPass, verbose=VerboseLevel, doReturn=True) # if update string is 0, we pretend it doesnt exist. otherwise we put "u" in front of the update string. OSUpdate = vmos.get_os_update_version(verbose=verbose) if OSUpdate == '0': OSUpdate = "" else: OSUpdate ="u" + OSUpdate OSString = OSData + OSUpdate cmd = "cd /import/automation/builds/server1/%s-%s/packages && /bin/ls -1rtL *fedriver*%s-*|grep -v installer" \ %(buildType, buildNum, OSString) stdoutData, stderrData = bld.run_ssh(cmd=cmd) if verbose: #print "stdoutData" for line in stdoutData: print line.strip('\n') #print "stderr" for line in stderrData: print line.strip('\n') # if we got only one return and no error, then we found our driver and it is an exact match if len(stdoutData) == 1 and not stderrData and "fedrivers" in stdoutData[0]: Driver = stdoutData[0].strip() print "\n FOUND exact match. Driver rpm is: \n\n %s" % Driver # if we got an error, try again without the OS version string elif (stderrData and "No such file" in stderrData[0]): if verbose: print "WARNING: exact match not not found. Trying without update string." OSString = OSData cmd = "cd /import/automation/builds/server1/%s-%s/packages && /bin/ls -1rtL *fedriver*%s-*|grep -v installer" \ %(buildType, buildNum, OSString) stdoutData, stderrData = bld.run_ssh(cmd=cmd) if len(stdoutData) == 1 and not stderrData and "fedrivers" in stdoutData[0]: # found an close match Driver = stdoutData[0].strip() print "\n FOUND a close match. Driver rpm is: \n\n %s" % Driver else: print "ERROR: Did NOT find a driver for this build and Linux_Host OS." sys.exit(ERROR) #XXX # if we got more than one matches, we need to ask the user to specify which they want elif (len(stdoutData) > 1 and not stderrData and "fedrivers" in stdoutData[0]): print "Warning: more than one matching drivers found:" for line in stdoutData: print line.strip('\n') print "Please indicate which driver to use by specifying (to be determined) options" sys.exit(ERROR) #XXX TODO: add a cli option to specify which of the drivers to use # build numbers such as "official-123" are symlinked to a real directory. # So we get the real dir instead of symlink, which we need for http get later cmd = "ls -l /import/automation/builds/server1/%s-%s" %(buildType, buildNum) stdoutData, stderrData = bld.run_ssh(cmd=cmd) if not stderrData and len(stdoutData) == 1: RealDir = stdoutData[0].split("->")[1] RealDir = os.path.basename(RealDir).strip() if verbose: print "%s-%s is symlinked to %s" %(buildType, buildNum, RealDir) else: print "Error: Unable to determine the actual directory for %s-%s. Aborting." (buildType, buildNum) sys.exit(ERROR) # update the build on the Linux_Host, or simulate that if the user requested # in simulation, we just run uname on the Linux_Host, and if that succeeds, we assume other command should succeed. vm=Passh(host=vmHost, username=vmUser, password=vmPass, verbose=VerboseLevel, doReturn=True) if simulate is True: if verbose: print "\n *** simulation mode. Not actually running the commands ***\n" print "\nContacting Linux_Host..." cmd = "uname" stdoutData, stderrData = vm.run_ssh(cmd=cmd) if verbose: print stdoutData[0] #XXX if len(stdoutData) == 1 and not stderrData and stdoutData[0].strip() == "Linux": print "\nSuccessfully logged into Linux_Host %s as %s/%s" %(vmHost, vmUser, vmPass) print "\nWould try to download http://myserver1/builds/server1/%s/packages/%s" %(RealDir, Driver) print "\nWould try to run 'cd /tmp && rpm -Uvh %s %s'" %(rpmOptions, Driver) else: print "ERROR: Would FAIL to log in to the Linux_Host %s as %s/%s" %(vmHost, vmUser, vmPass) print "ERROR: Would abort here." print "\n *** simulation completed ***" else: print "\n +++ UPDATING THE Linux_Host with this driver in 10 seconds. +++" print "\n +++ Press control-C now if you want to abort. +++\n" time.sleep(10) cmd = "cd /tmp && curl -O http://myserver1/builds/server1/%s/packages/%s" %(RealDir, Driver) stdoutData, stderrData = vm.run_ssh(cmd=cmd) if verbose: print "\nExecuting: %s\n" %cmd for line in stdoutData: print line.strip('\n') for line in stderrData: print line.strip('\n') ErrorFound = False print "------------------------------------------------------------------------" cmd = "cd /tmp && rpm -Uvh %s %s" %(rpmOptions, Driver) print "rpm -Uvh %s %s" %(rpmOptions, Driver) stdoutData, stderrData = vm.run_ssh(cmd=cmd) for line in stdoutData: if re.search('WARNING|FATAL|nvalid', line): ErrorFound = True print line.strip('\n') for line in stderrData: if re.search('WARNING|FATAL|nvalid', line): ErrorFound = True print line.strip('\n') print "------------------------------------------------------------------------" if ErrorFound: print "\nWARNING: Errors or Warnings encountered while updating driver. Please check." print "\nThe following driver is installed on the Linux_Host:\n" cmd = "rpm -qa|grep fedrivers" stdoutData, stderrData = vm.run_ssh(cmd=cmd) for line in stdoutData: print "\t",line.strip('\n') for line in stderrData: print "\t",line.strip('\n') print "\nDone" except Exception, e: print "Error: %s" %e