def beforeRun(self): """ set external timeout values if any """ # set timeout if contextutils.existcontext(self, 'thread_timeout'): self._timeout = contextutils.getcontext(self, 'thread_timeout', self._timeout) if contextutils.existcontext(self, 'thread_progress_timeout'): self._progressTimeout = contextutils.getcontext(self, 'thread_progress_timeout', self._progressTimeout)
def testContextUtils(self): sc = SomeClass() contextutils.injectcontext(sc, {'guid':'test-guid'}) assert contextutils.existcontext(sc, 'guid') assert not contextutils.existcontext(sc, 'guid1') assert contextutils.getcontext(sc, 'guid1', 'test-guid1') == 'test-guid1' assert contextutils.getcontext(sc, 'guid') == 'test-guid' contextutils.injectcontext(sc, {'guid':'new-guid'}) assert contextutils.getcontext(sc, 'guid') == 'new-guid' assert contextutils.popcontext(sc, 'guid') == 'new-guid' assert not contextutils.existcontext(sc, 'guid')
def testContextUtils(self): sc = SomeClass() contextutils.injectcontext(sc, {'guid': 'test-guid'}) assert contextutils.existcontext(sc, 'guid') assert not contextutils.existcontext(sc, 'guid1') assert contextutils.getcontext(sc, 'guid1', 'test-guid1') == 'test-guid1' assert contextutils.getcontext(sc, 'guid') == 'test-guid' contextutils.injectcontext(sc, {'guid': 'new-guid'}) assert contextutils.getcontext(sc, 'guid') == 'new-guid' assert contextutils.popcontext(sc, 'guid') == 'new-guid' assert not contextutils.existcontext(sc, 'guid')
def doRun(self): """ run - exec the thread and parse the results """ display_cmd = '' try: self.__LOG.debug("Exec Thread running %s" % self.__cmd) closeFds = readall = True service = manifestutil.serviceFromPath(self.__cmd) cronusapp_home = '' if (service != None and len(service) > 0): cronusapp_home = str(manifestutil.servicePath(service)) elif (contextutils.existcontext(self, 'service')): service = str(contextutils.getcontext(self, 'service')) cronusapp_home = str(manifestutil.servicePath(service)) correlation_id = str(contextutils.getcontext(self, 'guid', '')) env_variables = 'CRONUSAPP_HOME=%s LCM_CORRELATIONID=%s' % (cronusapp_home, correlation_id) if isinstance(self.__cmd, basestring): cmd_0 = str(self.__cmd) if (cmd_0.find('sudo -u') >= 0): # as we have problem setting environment variables for sudo as root cmd_0 = cmd_0.replace("sudo", ("sudo %s" % env_variables), 1) self.__cmd = cmd_0 display_cmd = cmd_0.split(os.path.sep)[-1] else: cmd_0 = self.__cmd[0] if (cmd_0 == 'sudo' and '-u' in self.__cmd): # as we have problem setting environment variables for sudo as root for env_variable in env_variables.split(' ')[::-1]: self.__cmd.insert(1, env_variable) display_cmd = self.__cmd[-1].split(os.path.sep)[-1] threadName = 'exec_thread(%s)' % display_cmd self.__cmdProcess = subprocess.Popen(self.__cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE, close_fds = closeFds) # in case we don't get any results self.__response = {'progress': 100, 'result': None} if 'info' == self.__logLevel: self.__LOG.info("started Exec Thread..., cmd=%s" % self.__cmd) outputlines = [] while True: self._checkStop(threadName = threadName) stopped = (self.__cmdProcess.poll() != None) # read from stdout, if process completes, read all from stdout lines = self.readStream(self.__cmdProcess.stdout, readall = readall) if (not lines and stopped): break outputlines.extend(lines) if 'info' == self.__logLevel and lines: self.__LOG.info('cmd(%s) output:' % display_cmd) for line in lines: self.__LOG.info(line) else: self.__LOG.debug('cmd(%s) output:' % display_cmd) for line in lines: self.__LOG.debug(line) for line in lines: self.processExecResponse(line) if (not lines): time.sleep(float(config['exec_thread_sleep_time'])) if 'info' == self.__logLevel and outputlines: OUTPUTLOG.info('%s output start %s %s output end' % (self.getUuid(), '\\n'.join(outputlines), self.getUuid())) # maybe the script just closed stdout # wait until the script finishes while (self.__cmdProcess.poll() == None): self._checkStop(threadName = threadName) time.sleep(float(config['exec_thread_sleep_time'])) returnCode = self.__cmdProcess.poll() # the error condition if (returnCode != 0): self._updateStatus(httpStatus = 500) # read from stderr and log if self.__cmdProcess.stderr is not None: lines = self.readStream(self.__cmdProcess.stderr) for line in lines: self.processExecResponse(line) self.__LOG.info('cmd(%s) stderr: %s' % (display_cmd, line)) self.__LOG.warning(self.__response) errorCode = int(self.__response['error']) if 'error' in self.__response else returnCode msg = 'Application script error (%s) error code (%d) error msg (%s)' % (self.__cmd, errorCode, (self.__response['errorMsg'] if 'errorMsg' in self.__response else '')) self.__LOG.warning(msg) # now add 16000 to the error code to indicate this is a client error clientErrorCode = Errors.CLIENT_SCRIPT_ERROR + abs(errorCode) self._updateStatus(error = clientErrorCode, errorMsg = msg) else: self._updateStatus(httpStatus = 200) if 'result' in self.__response: self._updateStatus(result = self.__response['result']) self._updateStatus(progress = 100) except SystemExit as excep: # status already set in agent thread msg = "System Exception for cmd %s - %s" % (self.__cmd, self.getStatus()['errorMsg']) self.__LOG.warning("%s - %s" % (msg, str(excep))) raise excep except OSError as excep: if self.__cmdProcess is None: msg = 'Cannot create subprocess cmd(%s) %s - %s' % (self.__cmd, str(excep), traceback.format_exc(2)) else: msg = 'Unknown OSError cmd(%s) %s - %s' % (self.__cmd, str(excep), traceback.format_exc(2)) self.__LOG.warning(msg) self._updateStatus(httpStatus = 500, error = Errors.UNKNOWN_ERROR, errorMsg = msg) except Exception as excep: msg = 'Unknown error cmd(%s) %s - %s' % (self.__cmd, str(excep), traceback.format_exc(2)) self.__LOG.warning(msg) self._updateStatus(httpStatus = 500, error = Errors.UNKNOWN_ERROR, errorMsg = msg) finally: if self.__cmdProcess: if (self.__cmdProcess.stdout != None): self.__cmdProcess.stdout.close() if (self.__cmdProcess.stdin != None): self.__cmdProcess.stdin.close() if (self.__cmdProcess.stderr != None): self.__cmdProcess.stderr.close() self.kill(self.__cmdProcess, self.__cmd) self.__LOG.debug("Exec Thread %s done" % self.__cmd)