def __init__(self, threadMgr, service, manifest, name = 'agent_thread', parentId = None): """ Constructor """ AgentThread.__init__(self, threadMgr, cat = [manifestutil.serviceCat(service)], name = name, parentId = parentId) self.__manifest = manifest self.__service = service contextutils.injectcontext(self, {'service':service}) self._LOG = manifestutil.getServiceLogger(self, logging.getLogger(__name__))
def __init__(self, threadMgr, service, manifest): """ Constructor """ ManifestControl.__init__(self, threadMgr, service, manifest) self.setName(ActivateManifest.THREAD_NAME) # stage of activation, used for cleanup self._activationStage = 0 self.__LOG = manifestutil.getServiceLogger(self, logging.getLogger(__name__))
def __init__(self, threadMgr, service, manifest, packages, skipProp = True, skipActivation = False): """ Constructor """ ManifestControl.__init__(self, threadMgr, service, manifest) self.setName(DeployService.THREAD_NAME) self.__packages = packages self.__skipProp = skipProp self.__skipActivation = skipActivation self.__LOG = manifestutil.getServiceLogger(self, logging.getLogger(__name__))
def __init__(self, threadMgr, service): """ Constructor """ ManifestControl.__init__(self, threadMgr, service, manifest=None, name='reset_service') self.setName(ResetService.THREAD_NAME) self.__LOG = manifestutil.getServiceLogger(self, logging.getLogger(__name__))
def __init__(self, threadMgr, cmd, cat = None): """ Constructor. @param threadMgr - the thread Mgr object @param cmd - cmd list @param timeout - amount of time we give to this thread """ if threadMgr is None: threadMgr = NullThreadMgr() AgentThread.__init__(self, threadMgr, cat, name = 'exec_thread') self.__cmd = cmd # default timeouts configuration self._timeout = int(config['exec_thread_timeout']) self._progressTimeout = int(config['exec_thread_progress_timeout']) self.__cmdProcess = None self.__response = {} self.__partial_response = None self.__partial = '' self.__logLevel = 'info' service = 'agent' self.__LOG = manifestutil.getServiceLogger(self, logging.getLogger(__name__), service)
def doRun(self): """ run - exec the thread and parse the results """ display_cmd = '' outputLogged = False try: serviceFromPath = manifestutil.serviceFromPath(self.__cmd, 'agent') self.__service = contextutils.getcontext(self, 'service', serviceFromPath) self.__LOG = manifestutil.getServiceLogger(self, logging.getLogger(__name__), self.__service) self.log("Exec Thread running, cmd=%s" % self.__cmd) closeFds = readall = True # context injection via environment variables if self.__injectctx: cronusapp_home = str(manifestutil.servicePath(self.__service)) correlation_id = str(contextutils.getcontext(self, 'guid', '')) authz_token = str(contextutils.getcontext(self, 'authztoken', '')) env_variables = 'SERVICE_NAME=%s CRONUSAPP_HOME=%s CORRELATIONID=%s AUTHZTOKEN=%s' % ( self.__service, cronusapp_home, correlation_id, authz_token) if self.__service: lcmMetas = serviceutil.getLcmMetas(self.__service) if lcmMetas: for key, value in lcmMetas.items(): env_variables += (' %s=%s' % (key.upper(), value)) 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 # execute self.__cmdProcess = subprocess.Popen(self.__cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE, close_fds = closeFds) self.logExecOutput('%s uuid start ' % (self.getUuid())) outputLogged = True self.logExecOutput('cmd: %s' % (self.__cmd if isinstance(self.__cmd, basestring) else (' '.join(self.__cmd)))) # in case we don't get any results self.__response = {'progress': 0.0, 'result': None} self.log('cmd(%s) output:' % display_cmd) 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 self.__outputlines.extend(lines) for line in lines: self.processExecResponse(line) self.log(line) if (not lines): time.sleep(float(config['exec_thread_sleep_time'])) if self.__outputlines: self.logExecOutput('\n'.join(self.__outputlines)) # 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'])) if self.__partial: self.processExecResponse(self.__partial) self.logExecOutput(self.__partial) self.log(self.__partial) self.log('cmd(%s) output done' % display_cmd) returnCode = self.__cmdProcess.poll() self.logExecOutput('exitcode: %s' % returnCode) # 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) if lines: self.logExecOutput('stderr: %s' % '\n'.join(lines)) self.log('cmd(%s) stderr: %s' % (display_cmd, '\n'.join(lines))) 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']) 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: self._updateStatus(progress = 100) 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) if outputLogged: self.logExecOutput('%s uuid end' % (self.getUuid())) self.log("Exec Thread done, cmd=%s" % self.__cmd)
def __init__(self, threadMgr, service): """ Constructor """ ManifestControl.__init__(self, threadMgr, service, manifest = None, name = 'reset_service') self.setName(ResetService.THREAD_NAME) self.__LOG = manifestutil.getServiceLogger(self, logging.getLogger(__name__))
def __init__(self, threadMgr, service, action): """ Constructor """ ManifestControl.__init__(self, threadMgr, service, manifest = None) self.setName(StartStopService.THREAD_NAME) self.__action = action self.__LOG = manifestutil.getServiceLogger(self, logging.getLogger(__name__))