def connect(self): """ """ accessParams = self.accessParamsGet() if not accessParams.accessMethodValue == 'ssh': # EH_problem() pass try: icm.TM_here( format('ssh -l %s %s' % (accessParams.userNameValue, accessParams.targetFqdnValue))) connection = pexpect.spawn( 'ssh -l %s %s' % (accessParams.userNameValue, accessParams.targetFqdnValue)) except Exception as e: icm.TM_here('EXCEPTION: ' + str(e)) raise try: icm.TM_here('pexpect -- waiting for COMMAND_PROMPT=' + COMMAND_PROMPT) i = connection.expect( [pexpect.TIMEOUT, SSH_NEWKEY, COMMAND_PROMPT, '(?i)password']) except Exception as e: icm.TM_here('EXCEPTION: ' + str(e)) raise #icm.TM_here() if i == 0: # Timeout icm.TM_here() print('ERROR! could not login with SSH. Here is what SSH said:') print(connection.before, connection.after) print(str(connection)) sys.exit(1) if i == 1: # In this case SSH does not have the public key cached. icm.TM_here() connection.sendline('yes') connection.expect('(?i)password') if i == 2: icm.TM_here("Auto Login -- Public Key") # This may happen if a public key was setup to automatically login. # But beware, the COMMAND_PROMPT at this point is very trivial and # could be fooled by some output in the MOTD or login message. #pass # 20170428 -- Mohsen Fix connection.sendline(' ') return connection if i == 3: icm.TM_here( format('Received: ' + connection.before + connection.after)) icm.TM_here('Sending: Passwd') #icm.TM_here('Sending: ' + accessParams.passwordValue ) connection.sendline(accessParams.passwordValue) # Now we are either at the command prompt or # the login process is asking for our terminal type. icm.LOG_here("Connected ....") return connection
def cmnd( self, interactive=False, # Can also be called non-interactively svcSpec=None, # or Cmnd-Input perfSap=None, # or Cmnd-Input headers=None, # or Cmnd-Input argsList=[], # or Args-Input ): cmndOutcome = self.getOpOutcome() if interactive: if not self.cmndLineValidate(outcome=cmndOutcome): return cmndOutcome effectiveArgsList = G.icmRunArgsGet().cmndArgs else: effectiveArgsList = argsList callParamsDict = { 'svcSpec': svcSpec, 'perfSap': perfSap, 'headers': headers, } if not icm.cmndCallParamsValidate( callParamsDict, interactive, outcome=cmndOutcome): return cmndOutcome svcSpec = callParamsDict['svcSpec'] perfSap = callParamsDict['perfSap'] headers = callParamsDict['headers'] cmndArgsSpecDict = self.cmndArgsSpec() if not self.cmndArgsValidate( effectiveArgsList, cmndArgsSpecDict, outcome=cmndOutcome): return cmndOutcome ####+END: icm.TM_here("svcSpec={svcSpec} -- perfSap={perfSap}".format( svcSpec=svcSpec, perfSap=perfSap)) try: loadedSvcSpec, origin_url = loadSvcSpec(svcSpec, perfSap) except Exception as e: icm.EH_problem_usageError( "wsInvokerIcm.svcOpsList Failed -- svcSpec={svcSpec}".format( svcSpec=svcSpec, )) icm.EH_critical_exception(e) return pp = pprint.PrettyPrinter(indent=4) icm.TM_here("{}".format(pp.pformat(loadedSvcSpec))) processSvcSpec(loadedSvcSpec, origin_url, perfSap, headers, svcSpec)
def cmnd( self, interactive=False, # Can also be called non-interactively ): cmndOutcome = self.getOpOutcome() if interactive: if not self.cmndLineValidate(outcome=cmndOutcome): return cmndOutcome callParamsDict = {} if not icm.cmndCallParamsValidate( callParamsDict, interactive, outcome=cmndOutcome): return cmndOutcome ####+END: logger = icm.LOG_Control().loggerGet() logger.debug('Raw Logging' + ucf.stackFrameInfoGet(1)) icm.TM_here('Here' + ' Tracking') icm.TM_note('UnHere Tracking') #for thisArg in icm.icmRunArgs.cmndArgs: #print ('cmndExample() received cmndArg=' + thisArg) icm.do(intrusiveFunc, ' With Some Parameter')
def bash( command, **kwargs ): ####+END: """Based on verbosity level, add appropriate kwargs. Based on --verbosity set hide=False Based on --callTracking set echo=True """ #icm.TM_here("Args: {}".format(args)) for key in kwargs: icm.TM_here("keyword arg: %s: %s" % (key, kwargs[key])) specifiedArg_hide = kwargs.pop('hide', None) specifiedArg_echo = kwargs.pop('echo', None) specifiedArg_warn = kwargs.pop('warn', None) verbosityLevel = icm.icmRunArgs_verbosityLevel() if specifiedArg_hide is not None: kwargs['hide'] = specifiedArg_hide else: if verbosityLevel >= 30: #kwargs['hide'] = True # True is different from 'both' in that it overrides echo=True kwargs['hide'] = 'both' else: kwargs['hide'] = False if specifiedArg_echo is not None: kwargs['echo'] = specifiedArg_echo else: if icm.icmRunArgs_isCallTrackingMonitorOn(): kwargs['echo'] = True else: kwargs['echo'] = False #result = run(command, hide=True, warn=True, echo=True) #print(kwargs) result = run(command, **kwargs) return result
def configParamsGet(self): """Unused and incomplete -- mirrors accessParamsGet """ class ConfigParams(object): """ """ def __init__(self, paramsDict=None): '''Constructor''' self.accessMethod = paramsDict.get('accessMethod') self.accessMethodValue = self.accessMethod.parValueGetLines( )[0] self.targetFqdn = paramsDict.get('targetFqdn') self.targetFqdnValue = self.targetFqdn.parValueGetLines()[0] paramsDict = self.readFileParDictFrom("params/config/cur") icm.TM_here(paramsDict) theseParams = ConfigParams(paramsDict=paramsDict) return theseParams
def paramProc(thisParam): """At this point, we have the target and the parameter, ready for processing. - From thisParam's fileParams, get the agent and parName, - Then remoteExec the agent on target and get the results. - Record the obtained results with local invokation of the agent. """ icm.TM_here( 'targetPath=' + thisPathTarget) # Target Access Information icm.TM_here(format('ticmoBase=' + thisTicmoBase)) # TICMO Base paramBase = thisParam.base() icm.TM_here('paramBase=' + paramBase) agent = icm.FILE_ParamValueReadFrom(parRoot=paramBase, parName='agent') if not agent: return (icm.EH_problem_unassignedError()) parName = icm.FILE_ParamValueReadFrom( parRoot=paramBase, parName='parName') if not parName: return (icm.EH_problem_unassignedError()) commandLine = format(agent + ' -p mode=agent -i ' + parName) icm.LOG_here('RemoteExec: ' + commandLine) resultLines = linuxTarget.runCommand( connection, commandLine) pipeableResultLines = "" for thisResultLine in resultLines: pipeableResultLines = pipeableResultLines + thisResultLine + '\n' icm.LOG_here('ResultLines: ' + str(resultLines)) # We can now dateVer and empnaPkg write the resultLines for parName in TICMO #fileParWriteBase = os.path.join(thisTicmoBase, empnaPkg, dateVer) # updated = icm.FILE_ParamWriteTo(parRoot=fileParWriteBase, # parName=parName, # parValue=resultLines[0]) # # We ask the agent to capture the resultLines in ticmo # commandLine = format( agent + ' ' + '-n showRun -p mode=capture -p ticmoBase=' + ' -i ' + parName) commandArgs = shlex.split(commandLine) icm.LOG_here('SubProc: ' + commandLine) p = subprocess.Popen(commandArgs, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate( input=format(pipeableResultLines.encode())) if out: icm.ANN_note("Stdout: " + out) if err: icm.ANN_note("Stderr: " + err) return
def ro_opInvokeCapture(roOp, ): ####+END: pp = pprint.PrettyPrinter(indent=4) #icm.TM_here("svcSpec={svcSpec} -- perfSap={perfSap}".format(svcSpec=roOp.svcSpec, perfSap=roOp.perfSap)) loadedSvcSpec, origin_url = loadSvcSpec(roOp.svcSpec, roOp.perfSap) if roOp.perfSap: #origin_url = "http://localhost:8080" origin_url = roOp.perfSap # # NOTYET LOG level changes here # #icm.TM_here("{}".format(pp.pformat(loadedSvcSpec))) opBravadoObj = getOperationWithResourceAndOpName( loadedSvcSpec, origin_url, roOp.resource, roOp.opName, ) if not opBravadoObj: icm.EH_problem_usageError( """getOperationWithResourceAndOpName Failed: resource={resource} opName={opName}""" .format( resource=roOp.resource, opName=roOp.opName, )) return None requestOptions = dict() params = roOp.roParams headerParams = params.headerParams if headerParams: requestOptions["headers"] = headerParams urlParams = params.urlParams if urlParams == None: urlParams = dict() bodyParams = params.bodyParams icm.TM_here("{}".format(pp.pformat(bodyParams))) if bodyParams: # # With ** we achieve kwargs # # func(**{'type':'Event'}) is equivalent to func(type='Event') # request = construct_request(opBravadoObj, requestOptions, body=bodyParams, **urlParams) else: request = construct_request(opBravadoObj, requestOptions, **urlParams) icm.LOG_here("request={request}".format(request=pp.pformat(request))) c = RequestsClient() # # This is where the invoke request goes out # future = c.request(request) # # This where the invoke response comes in # opResults = {} try: result = future.result() except Exception as e: #icm.EH_critical_exception(e) opResults = None roResults = ro.Ro_Results( httpResultCode=500, # type=int httpResultText="Internal Server Error", # type=str opResults=opResults, opResultHeaders=None, ) if opResults != None: # # result # # 2018-10-01 -- https://github.com/Yelp/bravado/blob/master/bravado/requests_client.py # class RequestsResponseAdapter(IncomingResponse): # # type(result._delegate.text) = unicode # type(result._delegate.content) = str # opResults = None if result._delegate.content: try: opResults = result.json() except Exception as e: icm.EH_critical_exception(e) opResults = None roResults = ro.Ro_Results( httpResultCode=result._delegate.status_code, # type=int httpResultText=result._delegate.reason, # type=str opResults=opResults, opResultHeaders=result._delegate.headers, ) icm.LOG_here( "RESPONSE: status_code={status_code} -- reason={reason} -- text={text}" .format( status_code=result._delegate.status_code, reason=result._delegate.reason, text=result._delegate.text, )) icm.LOG_here("RESPONSE: responseHeaders: {headers}".format( headers=pp.pformat(result._delegate.headers))) roOp.roResults = roResults return roOp
def bodyArgToDict( bodyAny, bodyFile, bodyStr, bodyFunc, ): """ Returns None if all Args were None, and returns "" if one of the args was "", Otherwize a dict or {}.""" def bodyStrAsDict(bodyStr): return ast.literal_eval(bodyStr) def bodyFileAsDict(bodyFile): with open(bodyFile, 'r') as myfile: data = myfile.read().replace('\n', '') return ast.literal_eval(data) def bodyFuncAsDict(bodyFunc): resDict = eval(bodyFunc) # NOTYET, verify that we have a dict return resDict if bodyAny != None: icm.TM_here("bodyAny={}".format(pp.pformat(bodyAny))) if bodyAny == "": return "" # Be it file, function or string if os.path.isfile(bodyAny): return bodyFileAsDict(bodyAny) elif bodyAny == "NOTYET-valid func": return bodyFuncAsDict(bodyAny) else: # We then take bodyAny to be a string return bodyStrAsDict(bodyAny) elif bodyFile != None: icm.TM_here("bodyFile={}".format(pp.pformat(bodyFile))) if bodyFile == "": return "" if os.path.isfile(bodyAny): return bodyFileAsDict(bodyFile) else: return {} elif bodyFunc != None: icm.TM_here("bodyFunc={}".format(pp.pformat(bodyFunc))) if bodyFunc == "": return "" if bodyFunc == "NOTYET-valid func": return bodyFuncAsDict(bodyFunc) else: return {} elif bodyStr != None: icm.TM_here("bodyStr={}".format(pp.pformat(bodyStr))) if bodyStr == "": return "" bodyValue = bodyStrAsDict(bodyStr) return bodyValue else: # So they were all None, meaning that no form of "body" was specified. return None
def opInvoke(headers, op, *args, **kwargs): ####+END: """ NOTYET, Important, opInvoke should be layered on top of opInvokeCapture """ pp = pprint.PrettyPrinter(indent=4) headerLines = list() if headers: with open(headers, 'rb') as file: headerLines = file.readlines() # else: # print("Has No Headers") headerLinesAsDict = dict() for each in headerLines: headerLineAsList = each.split(":") headerLineAsListLen = len(headerLineAsList) if headerLineAsListLen == 2: headerLineTag = headerLineAsList[0] headerLineValue = headerLineAsList[1] else: icm.EH_problem_usageError( "Expected 2: {}".format(headerLineAsListLen)) continue headerLinesAsDict[headerLineTag] = headerLineValue.lstrip(' ').rstrip() requestOptions = dict() if headerLinesAsDict: requestOptions["headers"] = headerLinesAsDict def bodyArgToDict( bodyAny, bodyFile, bodyStr, bodyFunc, ): """ Returns None if all Args were None, and returns "" if one of the args was "", Otherwize a dict or {}.""" def bodyStrAsDict(bodyStr): return ast.literal_eval(bodyStr) def bodyFileAsDict(bodyFile): with open(bodyFile, 'r') as myfile: data = myfile.read().replace('\n', '') return ast.literal_eval(data) def bodyFuncAsDict(bodyFunc): resDict = eval(bodyFunc) # NOTYET, verify that we have a dict return resDict if bodyAny != None: icm.TM_here("bodyAny={}".format(pp.pformat(bodyAny))) if bodyAny == "": return "" # Be it file, function or string if os.path.isfile(bodyAny): return bodyFileAsDict(bodyAny) elif bodyAny == "NOTYET-valid func": return bodyFuncAsDict(bodyAny) else: # We then take bodyAny to be a string return bodyStrAsDict(bodyAny) elif bodyFile != None: icm.TM_here("bodyFile={}".format(pp.pformat(bodyFile))) if bodyFile == "": return "" if os.path.isfile(bodyAny): return bodyFileAsDict(bodyFile) else: return {} elif bodyFunc != None: icm.TM_here("bodyFunc={}".format(pp.pformat(bodyFunc))) if bodyFunc == "": return "" if bodyFunc == "NOTYET-valid func": return bodyFuncAsDict(bodyFunc) else: return {} elif bodyStr != None: icm.TM_here("bodyStr={}".format(pp.pformat(bodyStr))) if bodyStr == "": return "" bodyValue = bodyStrAsDict(bodyStr) return bodyValue else: # So they were all None, meaning that no form of "body" was specified. return None # icm.TM_here("Args: {}".format(args)) # for key in kwargs: # icm.TM_here("another keyword arg: %s: %s" % (key, kwargs[key])) bodyAny = kwargs.pop('body', None) bodyFile = kwargs.pop('bodyFile', None) bodyStr = kwargs.pop('bodyStr', None) bodyFunc = kwargs.pop('bodyFunc', None) bodyValue = bodyArgToDict(bodyAny, bodyFile, bodyStr, bodyFunc) icm.TM_here(pp.pformat(requestOptions)) if bodyValue == None: request = construct_request(op, requestOptions, **kwargs) elif bodyValue == "": # Causes An Exception That Describes Expected Dictionary request = construct_request(op, requestOptions, body=None, **kwargs) else: request = construct_request(op, requestOptions, body=bodyValue, **kwargs) icm.LOG_here("request={request}".format(request=pp.pformat(request))) c = RequestsClient() future = c.request(request) try: result = future.result() except Exception as e: #icm.EH_critical_exception(e) result = None if result: icm.LOG_here("responseHeaders: {headers}".format( headers=pp.pformat(result._delegate.headers))) icm.ANN_write("Operation Status: {result}".format(result=result)) icm.ANN_write("Operation Result: {result}".format( result=pp.pformat(result.json())))
def cmnd( self, interactive=False, # Can also be called non-interactively svcSpec=None, # or Cmnd-Input resource=None, # or Cmnd-Input opName=None, # or Cmnd-Input perfSap=None, # or Cmnd-Input headers=None, # or Cmnd-Input argsList=[], # or Args-Input ): cmndOutcome = self.getOpOutcome() if interactive: if not self.cmndLineValidate(outcome=cmndOutcome): return cmndOutcome effectiveArgsList = G.icmRunArgsGet().cmndArgs else: effectiveArgsList = argsList callParamsDict = { 'svcSpec': svcSpec, 'resource': resource, 'opName': opName, 'perfSap': perfSap, 'headers': headers, } if not icm.cmndCallParamsValidate( callParamsDict, interactive, outcome=cmndOutcome): return cmndOutcome svcSpec = callParamsDict['svcSpec'] resource = callParamsDict['resource'] opName = callParamsDict['opName'] perfSap = callParamsDict['perfSap'] headers = callParamsDict['headers'] cmndArgsSpecDict = self.cmndArgsSpec() if not self.cmndArgsValidate( effectiveArgsList, cmndArgsSpecDict, outcome=cmndOutcome): return cmndOutcome ####+END: opParsList = self.cmndArgsGet("0&-1", cmndArgsSpecDict, effectiveArgsList) icm.TM_here("svcSpec={svcSpec} -- perfSap={perfSap}".format( svcSpec=svcSpec, perfSap=perfSap)) #generateSvcInfo("http://localhost:8080/swagger.json") loadedSvcSpec, origin_url = loadSvcSpec(svcSpec, perfSap) if perfSap: #origin_url = "http://localhost:8080" origin_url = perfSap pp = pprint.PrettyPrinter(indent=4) icm.TM_here("{}".format(pp.pformat(loadedSvcSpec))) op = getOperationWithResourceAndOpName(loadedSvcSpec, origin_url, resource, opName) opInvokeEvalStr = "opInvoke(headers, op, " for each in opParsList: parVal = each.split("=") parValLen = len(parVal) if parValLen == 2: parName = parVal[0] parValue = parVal[1] else: icm.EH_problem_usageError( "Expected 2: {parValLen}".format(parValLen=parValLen)) continue opInvokeEvalStr = opInvokeEvalStr + """{parName}="{parValue}", """.format( parName=parName, parValue=parValue) opInvokeEvalStr = opInvokeEvalStr + ")" icm.TM_here("Invoking With Eval: str={opInvokeEvalStr}".format( opInvokeEvalStr=opInvokeEvalStr, )) eval(opInvokeEvalStr) return
def staticsVerify(self): """ """ icm.TM_here('NOTYET')
def staticsPull(self): """ """ icm.TM_here('NOTYET')
def staticsSet(self): """ """ icm.TM_here('NOTYET')
def connectInteractive(self): """ """ icm.TM_here('NOTYET')
def targetsAccessListGet(interactive=False, targetFqdn=None, accessMethod=None, userName=None, password=None): """ Returns a list of path to targetSpecifiers. If interactive args have been specified, an ephemera targetPathSpecifier is added to list of path to be returned. Loaded TARGET_list is appended to returnedList. """ try: icm.callableEntryEnhancer(type='cmnd') except StopIteration: return G = icm.IcmGlobalContext() icmRunArgs = G.icmRunArgsGet() #icmParamDict = G.icmParamDictGet() #thisFunc = icm.FUNC_currentGet() #cmndThis= thisFunc.__name__ #cmndMandatoryParams=[] #cmndOptionalParams=[] cmndPathTargets = [] if interactive == True: if not len(icmRunArgs.cmndArgs) == 0: try: icm.EH_runTime('Bad Number Of cmndArgs') except RuntimeError: return if icmRunArgs.targetFqdn: targetFqdn = icmRunArgs.targetFqdn if icmRunArgs.accessMethod: accessMethod = icmRunArgs.accessMethod if icmRunArgs.userName: userName = icmRunArgs.userName if icmRunArgs.password: password = icmRunArgs.password if targetFqdn != None: ephemeraTargetBase = format("/tmp/ephemera-target/" + targetFqdn) try: os.makedirs(ephemeraTargetBase, 0o777) except OSError: pass linuxTarget = TARGET_Proxy_Linux(basePath=ephemeraTargetBase) linuxTarget.accessParamsSet( accessMethod=accessMethod, targetFqdn=targetFqdn, userName=userName, password=password, ) cmndPathTargets.append(ephemeraTargetBase) # Check For cmndArgs and stdin and Add Them To cmndTargets for thisCmndArg in icmRunArgs.cmndArgs: icm.TM_here(thisCmndArg) cmndPathTargets.append(thisCmndArg) # NOTYET: Check For TargetParams and Add Them To cmndTargets tl = TARGET_List() targetList = tl.targetListGet() #icm.icm.TM_here(targetList) for thisTarget in targetList: targetType = thisTarget.targetType() if targetType != 'bxp': icm.EH_problem_usageError(targetType) continue dnType = thisTarget.dnType() #dnQualifier = thisTarget.dnQualifier() dnBase = thisTarget.base() if dnType == 'path': cmndPathTargets.append(dnBase) else: icm.EH_problem_usageError(dnType) # for thisPathTarget in cmndPathTargets: # print thisPathTarget return cmndPathTargets