def executeScript(self, service, scriptname): """ execute a script from remote location""" scriptpath = None try: # parse the body if (not request.body or request.body == ""): LOG.error('invalid body found in post command') return errorResult(request, response, Errors.INVALID_REQUEST, 'No body found in post command', controller=self) body = json.loads(request.body) paramobj = body['params'] if 'params' in body else [] params = paramobj if type(paramobj) == list else paramobj.split() LOG.info('%s' % (params)) scriptpath = None for package in manifestutil.packagesInManifest(service): scriptpathtmp = os.path.join( manifestutil.packagePath(service, 'active', package), 'cronus', 'scripts', scriptname) if os.path.exists(scriptpathtmp): scriptpath = scriptpathtmp break if not scriptpath: return errorResult(request, response, Errors.INVALID_REQUEST, 'script %s not found' % scriptname, controller=self) cmd = ['sudo', '-u', 'cronusapp', scriptpath] for param in params: param = param.encode('ascii', 'ignore') cmd.append(param) LOG.info('cmd = %s' % cmd) appGlobal = config['pylons.app_globals'] execThread = ExecThread(appGlobal.threadMgr, cmd) execThread.setLogLevel('info') execThread.start() execThread.threadMgrEvent.wait() return statusResult(request, response, execThread, controller=self) except Exception as excp: return errorResult( request, response, error=Errors.UNKNOWN_ERROR, errorMsg='Unknown error when executing cmd %s, %s - %s' % (scriptpath, str(excp), traceback.format_exc(2)), controller=self)
def executeCmd(self): """ execute a command synchronously """ try: # parse the body if (not request.body or request.body == ""): LOG.error('invalid body found in post command') return errorResult(request, response, 10001, 'No body found', controller=self) body = json.loads(request.body) cmd0 = body['cmd'] if 'cmd' in body else None needsudo = asbool( body['need-sudo']) if 'need-sudo' in body else False sudotgt = body['sudo-target'] if 'sudo-target' in body else None paramobj = body['params'] if 'params' in body else [] params = paramobj if type(paramobj) == list else paramobj.split() LOG.info('%s %s %s %s' % (cmd0, needsudo, sudotgt, params)) if cmd0 is None or cmd0 == '': return errorResult(request, response, 10002, 'No command found', controller=self) cmd = [cmd0.encode('ascii', 'ignore')] if needsudo: cmd.insert(0, 'sudo') if sudotgt is not None: sudotgt = sudotgt.encode('ascii', 'ignore') cmd.insert(1, sudotgt) cmd.insert(1, '-u') for param in params: param = param.encode('ascii', 'ignore') cmd.append(param) appGlobal = config['pylons.app_globals'] execThread = ExecThread(appGlobal.threadMgr, cmd) execThread.setLogLevel('info') execThread.start() return statusResult(request, response, execThread, controller=self) except Exception as excp: return errorResult( request, response, error=Errors.UNKNOWN_ERROR, errorMsg='Unknown error when executing cmd %s, %s - %s' % (cmd, str(excp), traceback.format_exc(2)), controller=self)
def _executeCommand(self, cmd, timeout = 2, service = None): ''' execute command ''' execThread = ExecThread(None, cmd) execThread.setLogLevel('debug') execThread.run() # now wait for the threads to complete and update progress status = execThread.getStatus() if (status['error'] != None): return None # raise AgentException(status['error'], status['errorMsg']) return status['result']
def executeScript(self, service, scriptname): """ execute a script from remote location""" scriptpath = None try: # parse the body if (not request.body or request.body == ""): LOG.error('invalid body found in post command') return errorResult(request, response, Errors.INVALID_REQUEST, 'No body found in post command', controller = self) body = json.loads(request.body) paramobj = body['params'] if 'params' in body else [] params = paramobj if type(paramobj) == list else paramobj.split() LOG.info('%s' % (params)) scriptpath = None for package in manifestutil.packagesInManifest(service): scriptpathtmp = os.path.join(manifestutil.packagePath(service, 'active', package), 'cronus', 'scripts', scriptname) if os.path.exists(scriptpathtmp): scriptpath = scriptpathtmp break if not scriptpath: return errorResult(request, response, Errors.INVALID_REQUEST, 'script %s not found' % scriptname, controller = self) cmd = ['sudo', '-u', 'cronusapp', scriptpath] for param in params: param = param.encode('ascii', 'ignore') cmd.append(param) LOG.info('cmd = %s' % cmd) appGlobal = config['pylons.app_globals'] execThread = ExecThread(appGlobal.threadMgr, cmd) execThread.setLogLevel('info') execThread.start() execThread.threadMgrEvent.wait() return statusResult(request, response, execThread, controller = self) except Exception as excp: return errorResult(request, response, error = Errors.UNKNOWN_ERROR, errorMsg = 'Unknown error when executing cmd %s, %s - %s' % (scriptpath, str(excp), traceback.format_exc(2)), controller = self)
def executeCmd(self): """ execute a command synchronously """ try: # parse the body if not request.body: LOG.error('invalid body found in post command') return errorResult(request, response, Errors.INVALID_REQUEST, 'No body found', controller = self) body = json.loads(request.body.encode('ascii', 'ignore')) if 'cmd' not in body: return errorResult(request, response, Errors.INVALID_REQUEST, 'No cmd found', controller = self) cmd0 = body['cmd'] hasSudo = ('sudoUser' in body and body['sudoUser']) sudoUser = body['sudoUser'] if ('sudoUser' in body and body['sudoUser'] != 'root') else None LOG.info('%s %s %s' % (cmd0, hasSudo, sudoUser)) cmd = cmd0.split() if hasSudo: cmd.insert(0, 'sudo') if sudoUser is not None: cmd.insert(1, sudoUser) cmd.insert(1, '-u') appGlobal = config['pylons.app_globals'] execThread = ExecThread(appGlobal.threadMgr, cmd) execThread.setLogLevel('info') contextutils.copyJobContexts(self, execThread) execThread.start() return statusResult(request, response, execThread, controller = self) except Exception as excp: return errorResult(request, response, error = Errors.UNKNOWN_ERROR, errorMsg = 'Unknown error when executing cmd %s, %s - %s' % (cmd, str(excp), traceback.format_exc(2)), controller = self)
def executeScript(self): """ execute a script from remote location""" scriptpath = None try: # parse the body if (not request.body or request.body == ""): LOG.error('invalid body found in post command') return errorResult(request, response, 10001, 'No body found in post command', controller = self) body = json.loads(request.body.encode('ascii', 'ignore')) scriptloc = body['scriptLocation'] if 'scriptLocation' in body else None scriptname = body['scriptName'] if 'scriptName' in body else None hasSudo = ('sudoUser' in body and body['sudoUser']) sudoUser = body['sudoUser'] if ('sudoUser' in body and body['sudoUser'] != 'root') else None paramobj = body['params'] if 'params' in body else [] params = paramobj if type(paramobj) == list else paramobj.split() LOG.info('%s %s %s %s %s' % (scriptloc, scriptname, hasSudo, sudoUser, params)) if not scriptloc: return errorResult(request, response, Errors.INVALID_REQUEST, 'Script location not found', controller = self) if not scriptname: return errorResult(request, response, Errors.INVALID_REQUEST, 'Script name not found', controller = self) scriptpath = os.path.join(self.dataPath(), scriptname) LOG.info('scriptpath = %s' % scriptpath) utils.runsyscmd('wget %s -O %s' % (scriptloc, scriptpath)) if not os.path.exists(scriptpath): return errorResult(request, response, Errors.FILE_NOT_FOUND_ERROR, 'Failed to get script %s' % scriptpath, controller = self) utils.rchmod(scriptpath, '+rx') cmd = [scriptpath] if hasSudo: cmd.insert(0, 'sudo') if sudoUser: cmd.insert(1, sudoUser) cmd.insert(1, '-u') for param in params: cmd.append(param) LOG.info('cmd = %s' % cmd) appGlobal = config['pylons.app_globals'] execThread = ExecThread(appGlobal.threadMgr, cmd) execThread.setLogLevel('info') contextutils.copyJobContexts(self, execThread) execThread.start() execThread.threadMgrEvent.wait() return statusResult(request, response, execThread, controller = self) except Exception as excp: return errorResult(request, response, error = Errors.UNKNOWN_ERROR, errorMsg = 'Unknown error when executing cmd %s, %s - %s' % (scriptpath, str(excp), traceback.format_exc(2)), controller = self)
def executeScript(self): """ execute a script from remote location""" scriptpath = None try: # parse the body if (not request.body or request.body == ""): LOG.error('invalid body found in post command') return errorResult(request, response, 10001, 'No body found in post command', controller=self) body = json.loads(request.body) scriptloc = body['script-location'].encode( 'ascii', 'ignore') if 'script-location' in body else None scriptname = body['script-name'].encode( 'ascii', 'ignore') if 'script-name' in body else None needsudo = asbool( body['need-sudo']) if 'need-sudo' in body else False sudotgt = body['sudo-target'].encode( 'ascii', 'ignore') if 'sudo-target' in body else None paramobj = body['params'] if 'params' in body else [] params = paramobj if type(paramobj) == list else paramobj.split() LOG.info('%s %s %s %s %s' % (scriptloc, scriptname, needsudo, sudotgt, params)) if scriptloc is None or scriptloc == '': return errorResult(request, response, 10003, 'Script location not found', controller=self) if scriptname is None or scriptname == '': return errorResult(request, response, 10003, 'Script name not found', controller=self) scriptpath = os.path.join(self.dataPath(), scriptname) LOG.info('scriptpath = %s' % scriptpath) os.system('wget %s -O %s' % (scriptloc, scriptpath)) if scriptpath is None or not os.path.exists(scriptpath): return errorResult(request, response, 10003, 'Failed to get script %s' % scriptpath, controller=self) rchmod(scriptpath, '+rx') cmd = [scriptpath] if needsudo: cmd.insert(0, 'sudo') if sudotgt is not None: cmd.insert(1, sudotgt) cmd.insert(1, '-u') for param in params: param = param.encode('ascii', 'ignore') cmd.append(param) LOG.info('cmd = %s' % cmd) appGlobal = config['pylons.app_globals'] execThread = ExecThread(appGlobal.threadMgr, cmd) execThread.setLogLevel('info') execThread.start() execThread.threadMgrEvent.wait() return statusResult(request, response, execThread, controller=self) except Exception as excp: return errorResult( request, response, error=Errors.UNKNOWN_ERROR, errorMsg='Unknown error when executing cmd %s, %s - %s' % (scriptpath, str(excp), traceback.format_exc(2)), controller=self)