def execRemoteCommand(self, command, asynch=False, recoverFromFailure=True): """ Executes the command on the remove machine. @param command: The command to execute on the remote host. Must not be None. @param asynch: A boolean flag, whether to run the command asynchrounously or not. @param recoverFromFailure: A boolean flag, whether to try to recover if the connection had staled or failed. @return the output of the command (a list of text lines), if it was run synchrounously. @raise SSHException: if a connection could not be established, or the command gave an error. """ assert command is not None, "Command is None" assert asynch is not None, "Asynch is None" assert recoverFromFailure is not None, "recoverFromFailure is None" try: _, stdout, stderr = self.getSSHClient().exec_command(command) output = [] if not asynch: output = stdout.readlines() errors = stderr.readlines() if errors: log.warning( "Error messages encountered when connecting to %s, messages: %s", self.address, formatOutput(errors)) return output except (SSHException, socket.error) as e: if recoverFromFailure: # Mark the connection for re-instantiation and try again self.client = None return self.execRemoteCommand(command, asynch, False) else: raise e
def _launchVM(self, hardwareId, numVMs=1): startVMCommand = ("java -jar %s provision " + \ "-providerId %s " + \ "-accessKeyId %s " + \ "-secretKey %s " + \ "-imageOwnerId %s " + \ "-locationId %s " + \ "-imageId %s " + \ "-hardwareId %s " + \ "-secGroup %s " + \ "-keyPair %s " + \ "-groupName %s " + \ "-endPoint %s " + \ "-numVMs %s") \ %(self.vmManagerJar, self.providerId, self.accesskeyid, self.secretkey, self.imageOwnerId, self.locationId, self.imageId, hardwareId, self.securityGroupName, \ self.keyPairName, self.groupName, self.endPoint, numVMs) log.debug("VMManager command " + startVMCommand) out = execLocal(startVMCommand) assert numVMs == len( out), "Output should be just %s addresses, but it is:%s" % ( numVMs, format(formatOutput(out))) return out
def _reload(self): rrBalanceParam = self._balanceParam() command = ". ~/functions.sh; resetLoadBalancer " + rrBalanceParam + "; sudo service haproxy reload" log.info( "Resetting the load balancer with the following servers and ratios: " + rrBalanceParam) output = self.execRemoteCommand(command) log.info("Load balancer output: " + formatOutput(output))
def runWorkload(self, loadScale = 50, rampUp = 90, steadyState = 600, rampDown = 10): """ Runs the CloudStone Faban Client asynchrounously. Modifies the Run script. @param loadScale: the CloudStone load scale in seconds. Must not be None, 0 or negative. @param rampUp: the CloudStone ramp up in seconds. Must not be None, 0 or negative. @param steadyState: the CloudStone steady state in seconds. Must not be None, 0 or negative. @param rampDown: the CloudStone ramp down in seconds. Must not be None, 0 or negative. """ assert loadScale is not None and loadScale > 0, "loadScale %s is invalid" % loadScale assert rampUp is not None and rampUp > 0, "rampUp %s is invalid" % rampUp assert steadyState is not None and steadyState > 0, "steadyState %s is invalid" % steadyState assert rampDown is not None and rampDown > 0, "rampDown %s is invalid" % rampDown log.info("Prepare run config file:%s", self.runConfig) # Read the file line by line and modify the appropriate sections inputFile = None try: inputFile = fileinput.input(self.runConfig, inplace=True) for line in inputFile: newLine = line faScaleRegEx = r'(\s*<fa:scale>\s*)\d+(\s*</fa:scale>\s*)' rampUpRegEx = r'(\s*<fa:rampUp>\s*)\d+(\s*</fa:rampUp>\s*)' steadyStateRegEx = r'(\s*<fa:steadyState>\s*)\d+(\s*</fa:steadyState>\s*)' rampDownRegEx = r'(\s*<fa:rampDown>\s*)\d+(\s*</fa:rampDown>\s*)' xmlRegExToValues = {faScaleRegEx : loadScale, rampUpRegEx:rampUp, steadyStateRegEx:steadyState, rampDownRegEx:rampDown} for htmlRegEx in xmlRegExToValues: match = re.match(htmlRegEx, line) if match is not None: newLine = match.group(1) + str(xmlRegExToValues[htmlRegEx]) + match.group(2) break sys.stdout.write(newLine) finally: if inputFile is not None: inputFile.close() # Start the remote monitoring remotePath = os.path.join("/cloudstone/faban", os.path.os.path.basename(self.runConfig)) log.info("Copying Test/Run Config File: %s to %s:%s", self.runConfig, self.readableName, remotePath) sftp = self.getSSHClient().open_sftp() sftp.put(self.runConfig, remotePath) sftp.close() # Start the CloudStone client command = "cd /cloudstone/faban ; ./bin/fabancli submit OlioDriver test " + remotePath log.info("Starting workload with loadscale=%d rampUp=%d steadyState=%d rampDown=%d", loadScale, rampUp, steadyState, rampDown) output = self.execRemoteCommand(command) log.info("Workload initiated, output:%s", formatOutput(output))
def _launchVM(self, hardwareId, numVMs=1): startVMCommand = ("java -jar %s provision " + \ "-providerId %s " + \ "-accessKeyId %s " + \ "-secretKey %s " + \ "-imageOwnerId %s " + \ "-locationId %s " + \ "-imageId %s " + \ "-hardwareId %s " + \ "-secGroup %s " + \ "-keyPair %s " + \ "-groupName %s " + \ "-endPoint %s " + \ "-numVMs %s") \ %(self.vmManagerJar, self.providerId, self.accesskeyid, self.secretkey, self.imageOwnerId, self.locationId, self.imageId, hardwareId, self.securityGroupName, \ self.keyPairName, self.groupName, self.endPoint, numVMs) log.debug("VMManager command " + startVMCommand) out = execLocal(startVMCommand) assert numVMs == len(out), "Output should be just %s addresses, but it is:%s" % (numVMs, format(formatOutput(out))) return out
def _reload(self): rrBalanceParam = self._balanceParam() command = ". ~/functions.sh; resetLoadBalancer " + rrBalanceParam + "; sudo service haproxy reload" log.info("Resetting the load balancer with the following servers and ratios: " + rrBalanceParam) output = self.execRemoteCommand(command) log.info( "Load balancer output: " + formatOutput(output))
def execRemoteCommand(self, command, asynch=False, recoverFromFailure=True): """ Executes the command on the remove machine. @param command: The command to execute on the remote host. Must not be None. @param asynch: A boolean flag, whether to run the command asynchrounously or not. @param recoverFromFailure: A boolean flag, whether to try to recover if the connection had staled or failed. @return the output of the command (a list of text lines), if it was run synchrounously. @raise SSHException: if a connection could not be established, or the command gave an error. """ assert command is not None, "Command is None" assert asynch is not None, "Asynch is None" assert recoverFromFailure is not None, "recoverFromFailure is None" try: _, stdout, stderr = self.getSSHClient().exec_command(command) output = [] if not asynch: output = stdout.readlines() errors = stderr.readlines() if errors: log.warning("Error messages encountered when connecting to %s, messages: %s", self.address, formatOutput(errors)) return output except (SSHException, socket.error) as e: if recoverFromFailure: # Mark the connection for re-instantiation and try again self.client = None return self.execRemoteCommand(command, asynch, False) else : raise e