Search code examples
pythonjenkinspexpect

process run by pexpect.run() method terminates abruptly


When i run below code i see the process terminates even before completion. I validated command by running it manually command just works file. cmssso-util produces output which are about 1200 lines.Can this be a buffer issue. I validated script by assigning 'ls -ltr' to variable command works fine. Referred Documentation from below link: https://pexpect.readthedocs.io/en/stable/_modules/pexpect/run.html

  • I tried prefixing command by 'bash -c' which did not fix this issue.
  • I tried to find out how pexpect determines to terminate a process , still could not get any clear documentation. Please help me.
import pexpect
command = "cmsso-util domain-repoint -m execute  --src-emb-admin " + 'sourceVcAdmin' + " --replication-partner-fqdn " + 'destVc' + " --replication-partner-admin " + 'destVcAdmin' + " --dest-domain-name " +    'destDomain'

print("Running command  : " + command) 

(command_output, exitstatus) =   pexpect.run(command ,withexitstatus=1, events={'Enter Source embedded vCenter Server Admin Password :' : '\r\n','Enter Replication partner Platform Services Controller Admin Password :' : '\r\n','All Repoint configuration settings are correct; proceed?(.*)' :  'Y\r\n'})

print("----Command output------------")
print(command_output)
print("-----------------------------")

assert exitstatus is 0 , "Execution Failed"
print("Successfully Completed Embedded Cross Domain Re-pointing ")

Solution

  • I could resolve this issue by using the following code:

        import pexpect
    try :
            command = "cmsso-util domain-repoint -m execute  --src-emb-admin " + 'sourceVcAdmin' + " --replication-partner-fqdn " + 'destVc' + " --replication-partner-admin " + 'destVcAdmin' + " --dest-domain-name " +    'destDomain'
            print("Running command  : " + command) 
            child = pexpect.spawn(command, timeout=3000,maxread=12000)
            child.expect (['Enter Source embedded vCenter Server Admin Password :'],timeout=40000)
            child.sendline(<password>)
            child.expect (['Enter Replication partner Platform Services Controller Admin Password :'],timeout=40000)
            child.sendline(<password>)
            child.expect (['All Repoint configuration settings are correct; proceed?(.*)'],timeout=40000)
            child.sendline('Y')
            child.expect(pexpect.EOF)
            print(child.before)   
            assert(child.status == 0 , "Operation Failed!", "Successfully Completed Embedded Cross Domain Re-pointing")
    except:
        print("Exception was thrown")
        print("debug information:")
        print(str(child))
        child.close()
        exit(1)
    

    This is done by increasing default child = pexpect.spawn(command, timeout=600,maxread=8000)value and maxread parameters