Fix AgentOSCMD - create encapsulated windows in no capture logs

dev-linux
Ivan Maslov 4 years ago
parent e8a7f5ed5a
commit 5063b97320

@ -1,4 +1,4 @@
import threading, socket, getpass, sys, uuid, subprocess, base64, psutil, getpass
import threading, socket, getpass, sys, uuid, subprocess, base64, psutil, getpass, time
from . import O2A, A2O # Data flow Orchestrator To Agent
from . import Processor # Processor Queue
from subprocess import CREATE_NEW_CONSOLE # Flag to create new process in another CMD
@ -33,6 +33,7 @@ def OSCMD(inCMDStr, inRunAsyncBool=True, inGSettings = None, inSendOutputToOrche
:param inRunAsyncBool: True - Agent processor don't wait execution; False - Agent processor wait cmd execution
:param inGSettings: Agent global settings dict
:param inSendOutputToOrchestratorLogsBool: True - catch cmd execution output and send it to the Orchestrator logs; Flase - else case; Default True
!ATTENTION! If you need to start absolutely encapsulated app - set this flag as False. If you set True - the app output will come to Agent
:param inCMDEncodingStr: Set the encoding of the DOS window on the Agent server session. Windows is beautiful :) . Default is "cp1251" early was "cp866" - need test
:return:
"""
@ -42,7 +43,12 @@ def OSCMD(inCMDStr, inRunAsyncBool=True, inGSettings = None, inSendOutputToOrche
lL = inGSettings.get("Logger",None) if type(inGSettings) is dict else None
lResultStr = ""
lOSCMDKeyStr = str(uuid.uuid4())[0:4].upper()
lCMDProcess = subprocess.Popen(f'cmd /c {inCMDStr}', stdout=subprocess.PIPE, stderr=subprocess.STDOUT, creationflags=CREATE_NEW_CONSOLE)
lCMDProcess = None
if inSendOutputToOrchestratorLogsBool == True:
lCMDProcess = subprocess.Popen(f'cmd /c {inCMDStr}', stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
else:
lCMDProcess = subprocess.Popen(f'cmd /c {inCMDStr}', stdout=None, stderr=None,
creationflags=CREATE_NEW_CONSOLE)
lListenBool = True
lMessageStr = f"{lOSCMDKeyStr}: # # # # AGENT CMD Process has been STARTED # # # # "
if lL: lL.info(lMessageStr)
@ -51,15 +57,22 @@ def OSCMD(inCMDStr, inRunAsyncBool=True, inGSettings = None, inSendOutputToOrche
if lL: lL.info(lMessageStr)
A2O.LogListSend(inGSettings=inGSettings, inLogList=[lMessageStr])
while lListenBool:
lOutputLineBytes = lCMDProcess.stdout.readline()
if lOutputLineBytes == b"":
lListenBool = False
lStr = lOutputLineBytes.decode(inCMDEncodingStr) # was cp866, on win server don't work properly - set cp1251
if lStr.endswith("\n"): lStr = lStr[:-1]
lMessageStr = f"{lOSCMDKeyStr}: {lStr}"
if lL: lL.info(lMessageStr)
if inSendOutputToOrchestratorLogsBool: A2O.LogListSend(inGSettings=inGSettings, inLogList=[lMessageStr])
lResultStr+=lStr
if inSendOutputToOrchestratorLogsBool == True: # Capturing can be turned on!
lOutputLineBytes = lCMDProcess.stdout.readline()
if lOutputLineBytes == b"":
lListenBool = False
lStr = lOutputLineBytes.decode(inCMDEncodingStr) # was cp866, on win server don't work properly - set cp1251
if lStr.endswith("\n"): lStr = lStr[:-1]
lMessageStr = f"{lOSCMDKeyStr}: {lStr}"
if lL: lL.info(lMessageStr)
A2O.LogListSend(inGSettings=inGSettings, inLogList=[lMessageStr])
lResultStr+=lStr
else: #Capturing is not turned on - wait until process will be closed
lCMDProcessPoll = lCMDProcess.poll()
if lCMDProcessPoll is None: # Process is alive - wait
time.sleep(2)
else:
lListenBool = False
lMessageStr = f"{lOSCMDKeyStr}: # # # # AGENT CMD Process has been FINISHED # # # # "
if lL: lL.info(lMessageStr)
A2O.LogListSend(inGSettings=inGSettings, inLogList=[lMessageStr])

@ -218,13 +218,12 @@
<li><p><strong>inRunAsyncBool</strong> True - Agent processor dont wait execution; False - Agent processor wait cmd execution</p></li>
<li><p><strong>inGSettings</strong> Agent global settings dict</p></li>
<li><p><strong>inSendOutputToOrchestratorLogsBool</strong> True - catch cmd execution output and send it to the Orchestrator logs; Flase - else case; Default True</p></li>
<li><p><strong>inCMDEncodingStr</strong> Set the encoding of the DOS window on the Agent server session. Windows is beautiful :) . Default is “cp1251” early was “cp866” - need test</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p></p>
</dd>
</dl>
<p>!ATTENTION! If you need to start absolutely encapsulated app - set this flag as False. If you set True - the app output will come to Agent
:param inCMDEncodingStr: Set the encoding of the DOS window on the Agent server session. Windows is beautiful :) . Default is “cp1251” early was “cp866” - need test
:return:</p>
</dd></dl>
<dl class="py function">

@ -179,7 +179,7 @@
<div itemprop="articleBody">
<h1>Source code for pyOpenRPA.Agent.__Agent__</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">threading</span><span class="o">,</span> <span class="nn">socket</span><span class="o">,</span> <span class="nn">getpass</span><span class="o">,</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">uuid</span><span class="o">,</span> <span class="nn">subprocess</span><span class="o">,</span> <span class="nn">base64</span><span class="o">,</span> <span class="nn">psutil</span><span class="o">,</span> <span class="nn">getpass</span>
<span></span><span class="kn">import</span> <span class="nn">threading</span><span class="o">,</span> <span class="nn">socket</span><span class="o">,</span> <span class="nn">getpass</span><span class="o">,</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">uuid</span><span class="o">,</span> <span class="nn">subprocess</span><span class="o">,</span> <span class="nn">base64</span><span class="o">,</span> <span class="nn">psutil</span><span class="o">,</span> <span class="nn">getpass</span><span class="o">,</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">O2A</span><span class="p">,</span> <span class="n">A2O</span> <span class="c1"># Data flow Orchestrator To Agent</span>
<span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">Processor</span> <span class="c1"># Processor Queue</span>
<span class="kn">from</span> <span class="nn">subprocess</span> <span class="kn">import</span> <span class="n">CREATE_NEW_CONSOLE</span> <span class="c1"># Flag to create new process in another CMD</span>
@ -214,6 +214,7 @@
<span class="sd"> :param inRunAsyncBool: True - Agent processor don&#39;t wait execution; False - Agent processor wait cmd execution</span>
<span class="sd"> :param inGSettings: Agent global settings dict</span>
<span class="sd"> :param inSendOutputToOrchestratorLogsBool: True - catch cmd execution output and send it to the Orchestrator logs; Flase - else case; Default True</span>
<span class="sd"> !ATTENTION! If you need to start absolutely encapsulated app - set this flag as False. If you set True - the app output will come to Agent</span>
<span class="sd"> :param inCMDEncodingStr: Set the encoding of the DOS window on the Agent server session. Windows is beautiful :) . Default is &quot;cp1251&quot; early was &quot;cp866&quot; - need test</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
@ -223,7 +224,12 @@
<span class="n">lL</span> <span class="o">=</span> <span class="n">inGSettings</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;Logger&quot;</span><span class="p">,</span><span class="kc">None</span><span class="p">)</span> <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">inGSettings</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">dict</span> <span class="k">else</span> <span class="kc">None</span>
<span class="n">lResultStr</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
<span class="n">lOSCMDKeyStr</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">uuid</span><span class="o">.</span><span class="n">uuid4</span><span class="p">())[</span><span class="mi">0</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
<span class="n">lCMDProcess</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;cmd /c </span><span class="si">{</span><span class="n">inCMDStr</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stderr</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">STDOUT</span><span class="p">,</span> <span class="n">creationflags</span><span class="o">=</span><span class="n">CREATE_NEW_CONSOLE</span><span class="p">)</span>
<span class="n">lCMDProcess</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">if</span> <span class="n">inSendOutputToOrchestratorLogsBool</span> <span class="o">==</span> <span class="kc">True</span><span class="p">:</span>
<span class="n">lCMDProcess</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;cmd /c </span><span class="si">{</span><span class="n">inCMDStr</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stderr</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">STDOUT</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">lCMDProcess</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;cmd /c </span><span class="si">{</span><span class="n">inCMDStr</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stderr</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
<span class="n">creationflags</span><span class="o">=</span><span class="n">CREATE_NEW_CONSOLE</span><span class="p">)</span>
<span class="n">lListenBool</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">lMessageStr</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">lOSCMDKeyStr</span><span class="si">}</span><span class="s2">: # # # # AGENT CMD Process has been STARTED # # # # &quot;</span>
<span class="k">if</span> <span class="n">lL</span><span class="p">:</span> <span class="n">lL</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">lMessageStr</span><span class="p">)</span>
@ -232,15 +238,22 @@
<span class="k">if</span> <span class="n">lL</span><span class="p">:</span> <span class="n">lL</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">lMessageStr</span><span class="p">)</span>
<span class="n">A2O</span><span class="o">.</span><span class="n">LogListSend</span><span class="p">(</span><span class="n">inGSettings</span><span class="o">=</span><span class="n">inGSettings</span><span class="p">,</span> <span class="n">inLogList</span><span class="o">=</span><span class="p">[</span><span class="n">lMessageStr</span><span class="p">])</span>
<span class="k">while</span> <span class="n">lListenBool</span><span class="p">:</span>
<span class="n">lOutputLineBytes</span> <span class="o">=</span> <span class="n">lCMDProcess</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
<span class="k">if</span> <span class="n">lOutputLineBytes</span> <span class="o">==</span> <span class="sa">b</span><span class="s2">&quot;&quot;</span><span class="p">:</span>
<span class="n">lListenBool</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">lStr</span> <span class="o">=</span> <span class="n">lOutputLineBytes</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">inCMDEncodingStr</span><span class="p">)</span> <span class="c1"># was cp866, on win server don&#39;t work properly - set cp1251</span>
<span class="k">if</span> <span class="n">lStr</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">):</span> <span class="n">lStr</span> <span class="o">=</span> <span class="n">lStr</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">lMessageStr</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">lOSCMDKeyStr</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">lStr</span><span class="si">}</span><span class="s2">&quot;</span>
<span class="k">if</span> <span class="n">lL</span><span class="p">:</span> <span class="n">lL</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">lMessageStr</span><span class="p">)</span>
<span class="k">if</span> <span class="n">inSendOutputToOrchestratorLogsBool</span><span class="p">:</span> <span class="n">A2O</span><span class="o">.</span><span class="n">LogListSend</span><span class="p">(</span><span class="n">inGSettings</span><span class="o">=</span><span class="n">inGSettings</span><span class="p">,</span> <span class="n">inLogList</span><span class="o">=</span><span class="p">[</span><span class="n">lMessageStr</span><span class="p">])</span>
<span class="n">lResultStr</span><span class="o">+=</span><span class="n">lStr</span>
<span class="k">if</span> <span class="n">inSendOutputToOrchestratorLogsBool</span> <span class="o">==</span> <span class="kc">True</span><span class="p">:</span> <span class="c1"># Capturing can be turned on!</span>
<span class="n">lOutputLineBytes</span> <span class="o">=</span> <span class="n">lCMDProcess</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
<span class="k">if</span> <span class="n">lOutputLineBytes</span> <span class="o">==</span> <span class="sa">b</span><span class="s2">&quot;&quot;</span><span class="p">:</span>
<span class="n">lListenBool</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">lStr</span> <span class="o">=</span> <span class="n">lOutputLineBytes</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">inCMDEncodingStr</span><span class="p">)</span> <span class="c1"># was cp866, on win server don&#39;t work properly - set cp1251</span>
<span class="k">if</span> <span class="n">lStr</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">):</span> <span class="n">lStr</span> <span class="o">=</span> <span class="n">lStr</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">lMessageStr</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">lOSCMDKeyStr</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">lStr</span><span class="si">}</span><span class="s2">&quot;</span>
<span class="k">if</span> <span class="n">lL</span><span class="p">:</span> <span class="n">lL</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">lMessageStr</span><span class="p">)</span>
<span class="n">A2O</span><span class="o">.</span><span class="n">LogListSend</span><span class="p">(</span><span class="n">inGSettings</span><span class="o">=</span><span class="n">inGSettings</span><span class="p">,</span> <span class="n">inLogList</span><span class="o">=</span><span class="p">[</span><span class="n">lMessageStr</span><span class="p">])</span>
<span class="n">lResultStr</span><span class="o">+=</span><span class="n">lStr</span>
<span class="k">else</span><span class="p">:</span> <span class="c1">#Capturing is not turned on - wait until process will be closed</span>
<span class="n">lCMDProcessPoll</span> <span class="o">=</span> <span class="n">lCMDProcess</span><span class="o">.</span><span class="n">poll</span><span class="p">()</span>
<span class="k">if</span> <span class="n">lCMDProcessPoll</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="c1"># Process is alive - wait</span>
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">lListenBool</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">lMessageStr</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">lOSCMDKeyStr</span><span class="si">}</span><span class="s2">: # # # # AGENT CMD Process has been FINISHED # # # # &quot;</span>
<span class="k">if</span> <span class="n">lL</span><span class="p">:</span> <span class="n">lL</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">lMessageStr</span><span class="p">)</span>
<span class="n">A2O</span><span class="o">.</span><span class="n">LogListSend</span><span class="p">(</span><span class="n">inGSettings</span><span class="o">=</span><span class="n">inGSettings</span><span class="p">,</span> <span class="n">inLogList</span><span class="o">=</span><span class="p">[</span><span class="n">lMessageStr</span><span class="p">])</span>

File diff suppressed because one or more lines are too long

@ -39,13 +39,9 @@ Execute CMD on the Agent daemonic process
* **inSendOutputToOrchestratorLogsBool** True - catch cmd execution output and send it to the Orchestrator logs; Flase - else case; Default True
* **inCMDEncodingStr** Set the encoding of the DOS window on the Agent server session. Windows is beautiful :) . Default is “cp1251” early was “cp866” - need test
* **Returns**
!ATTENTION! If you need to start absolutely encapsulated app - set this flag as False. If you set True - the app output will come to Agent
:param inCMDEncodingStr: Set the encoding of the DOS window on the Agent server session. Windows is beautiful :) . Default is “cp1251” early was “cp866” - need test
:return:
### pyOpenRPA.Agent.__Agent__.OSFileBinaryDataBase64StrCreate(inFilePathStr, inFileDataBase64Str, inGSettings=None)

Loading…
Cancel
Save