From 3585cb3946910bf5a1a06db67952524ba3dbca9e Mon Sep 17 00:00:00 2001 From: Ivan Maslov Date: Wed, 16 Dec 2020 18:21:20 +0300 Subject: [PATCH] Orchestrator executable with auto run with admin rights --- Orchestrator/pyOpenRPA.Orchestrator_x64.cmd | 3 +- ...OpenRPA.Orchestrator_x64_administrator.lnk | Bin 1749 -> 0 bytes ...Orchestrator_x64_administrator_startup.cmd | 2 - .../Orchestrator/__Orchestrator__.py | 348 +++++++++--------- 4 files changed, 180 insertions(+), 173 deletions(-) delete mode 100644 Orchestrator/pyOpenRPA.Orchestrator_x64_administrator.lnk delete mode 100644 Orchestrator/pyOpenRPA.Orchestrator_x64_administrator_startup.cmd diff --git a/Orchestrator/pyOpenRPA.Orchestrator_x64.cmd b/Orchestrator/pyOpenRPA.Orchestrator_x64.cmd index ddb14a81..b6773cdf 100644 --- a/Orchestrator/pyOpenRPA.Orchestrator_x64.cmd +++ b/Orchestrator/pyOpenRPA.Orchestrator_x64.cmd @@ -1,4 +1,3 @@ cd %~dp0 copy /Y ..\Resources\WPy64-3720\python-3.7.2.amd64\python.exe ..\Resources\WPy64-3720\python-3.7.2.amd64\pyOpenRPA_Orchestrator.exe -.\..\Resources\WPy64-3720\python-3.7.2.amd64\pyOpenRPA_Orchestrator.exe "OrchestratorSettings.py" -pause >nul \ No newline at end of file +.\..\Resources\WPy64-3720\python-3.7.2.amd64\pyOpenRPA_Orchestrator.exe "OrchestratorSettings.py" \ No newline at end of file diff --git a/Orchestrator/pyOpenRPA.Orchestrator_x64_administrator.lnk b/Orchestrator/pyOpenRPA.Orchestrator_x64_administrator.lnk deleted file mode 100644 index 403e763593b23ab98681194aa7c70d9a81c1c161..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1749 zcmbtTT}%{L6#f>$;7{wKsGt-#q&2P$n=^KvcV)vxe@R!%XL;XEI9{xdqDuwQKPMbj4(p=5SR2lIMlRN= z_kH88RAtzC>0UP8^>~K!vB_Jh&0KovG@!)XY*D*Ea-y~~fh|~sOibNsY|b5&sKWCb z4}{#Jm!Fwl>S7AD=tP3M<=AT@8O{6MM2ZJO;ocx>Y&4@e*DWW4wr9mc&Q`f5kU1?F zT1K=u0tg|DUIZCkg#$L~kQ#s63BT8;?(m-=<()IQtKD)YxXK%|;hsMJkPHLR5hJGG z%+Q7=-p`xiJgin4PWWQMXscSA#(DilIT7q5*@B%(OCOy@-SC;XQM5uOf8pF|a~@Vp z1+~>%Z*gw0IY)fmXXVVq9n0c3`RSna|N8tYajjki$v#Q{AZMMc@cs&=5CPPw*Na?G zKVVmyPn0O$-P?hvo#kU+*)}oBQn*YKESD*5JS#gu?tQORpv;-M9R?~;4u#!}po>Ta zwBz?mO~!|`9NGd}%GmeV$rThKHKy6>&=LVX7WBshZB3fpe9cGU1QW^l!K5D7 zVtsa4Ne{Oh73E4GLIN&w(dJcy0xv3wVd+@bbOma&Do1;zW3r$9tp+J_{xuf{SbB+H z^-p?k{^7-!b(f!vuNX-jD(Yig`iOP3a+)QgV(h>k%BdJ0K6E>FVK<*YfgTE=mql-| zNn5uy%Gi%a-kXbthWBukKUEKtCE%8X9FsI5<(Yox+Em>d=je1sNB`H4erREQv0M;R zV)N4ou#y<20Bh)=`dnYj(+P(Jm&#row}^+MmX;}wq+LzhN7hlY)^Uu?U94ND^djtr zL|v>qz}AOJF)5#!U~h_3L%Xt8|L5+%6DIaA+)J_F9tmuy8u&G5>^I!@{hl{I#&{vk z%qW#xa(KPYYX9r~<|kO;Dq|~63i@ePWc!b&V)d8nYm|{HqyH_&Lvo>>W^|M-Zu{Ho zz1i, , , ) - lDaemonLastDateTime=datetime.datetime.now() - gSettingsDict["ServerDict"]["WorkingDirectoryPathStr"] = os.getcwd() # Set working directory in g settings - - #Инициализация сервера - lThreadServer = Server.RobotDaemonServer("ServerThread", gSettingsDict) - lThreadServer.start() - if lL: lL.info("Web server has been started") #Logging - - # Init the RobotScreenActive in another thread - lRobotScreenActiveThread = threading.Thread(target= Monitor.CheckScreen) - lRobotScreenActiveThread.daemon = True # Run the thread in daemon mode. - lRobotScreenActiveThread.start() # Start the thread execution. - if lL: lL.info("Robot Screen active has been started") #Logging - - # Init the RobotRDPActive in another thread - lRobotRDPActiveThread = threading.Thread(target= RobotRDPActive.RobotRDPActive, kwargs={"inGSettings":gSettingsDict}) - lRobotRDPActiveThread.daemon = True # Run the thread in daemon mode. - lRobotRDPActiveThread.start() # Start the thread execution. - if lL: lL.info("Robot RDP active has been started") #Logging - - # Init autocleaner in another thread - lAutocleanerThread = threading.Thread(target= GSettingsAutocleaner, kwargs={"inGSettings":gSettingsDict}) - lAutocleanerThread.daemon = True # Run the thread in daemon mode. - lAutocleanerThread.start() # Start the thread execution. - if lL: lL.info("Autocleaner thread has been started") #Logging - - # Orchestrator start activity - if lL: lL.info("Orchestrator start activity run") #Logging - for lActivityItem in gSettingsDict["OrchestratorStart"]["ActivityList"]: - Processor.ActivityListOrDict(lActivityItem) - - # Processor thread - lProcessorThread = threading.Thread(target= Processor.ProcessorRunSync, kwargs={"inGSettings":gSettingsDict}) - lProcessorThread.daemon = True # Run the thread in daemon mode. - lProcessorThread.start() # Start the thread execution. - if lL: lL.info("Processor has been started (ProcessorDict)") #Logging - - - if lL: lL.info("Scheduler loop start") #Logging - gDaemonActivityLogDictRefreshSecInt = 10 # The second period for clear lDaemonActivityLogDict from old items - gDaemonActivityLogDictLastTime = time.time() # The second perioad for clean lDaemonActivityLogDict from old items - while True: + # https://stackoverflow.com/questions/130763/request-uac-elevation-from-within-a-python-script + def is_admin(): try: - lCurrentDateTime = datetime.datetime.now() - #Циклический обход правил - lFlagSearchActivityType=True - # Periodically clear the lDaemonActivityLogDict - if time.time()-gDaemonActivityLogDictLastTime>=gDaemonActivityLogDictRefreshSecInt: - gDaemonActivityLogDictLastTime = time.time() # Update the time - for lIndex, lItem in enumerate(lDaemonActivityLogDict): - if lItem["ActivityEndDateTime"] and lCurrentDateTime<=lItem["ActivityEndDateTime"]: - pass - # Activity is actual - do not delete now - else: - # remove the activity - not actual - lDaemonActivityLogDict.pop(lIndex,None) - lIterationLastDateTime = lDaemonLastDateTime # Get current datetime before iterator (need for iterate all activities in loop) - # Iterate throught the activity list - for lIndex, lItem in enumerate(gSettingsDict["SchedulerDict"]["ActivityTimeList"]): - try: - # Prepare GUID of the activity - lGUID = None - if "GUID" in lItem and lItem["GUID"]: - lGUID = lItem["GUID"] - else: - lGUID = str(uuid.uuid4()) - lItem["GUID"]=lGUID - - #Проверка дней недели, в рамках которых можно запускать активность - lItemWeekdayList=lItem.get("WeekdayList", [0, 1, 2, 3, 4, 5, 6]) - if lCurrentDateTime.weekday() in lItemWeekdayList: - if lFlagSearchActivityType: - ####################################################################### - #Branch 1 - if has TimeHH:MM - ####################################################################### - if "TimeHH:MM" in lItem: - #Вид активности - запуск процесса - #Сформировать временной штамп, относительно которого надо будет проверять время - #часовой пояс пока не учитываем - lActivityDateTime=datetime.datetime.strptime(lItem["TimeHH:MM"],"%H:%M") - lActivityDateTime=lActivityDateTime.replace(year=lCurrentDateTime.year,month=lCurrentDateTime.month,day=lCurrentDateTime.day) - #Убедиться в том, что время наступило - if ( - lActivityDateTime>=lDaemonLastDateTime and - lCurrentDateTime>=lActivityDateTime): - # Log info about activity - if lL: lL.info(f"Scheduler:: Activity list is started in new thread. Scheduler item: {lItem}") #Logging - # Do the activity - lThread = threading.Thread(target=Processor.ActivityListExecute, kwargs={"inGSettings": inGSettings, "inActivityList":lItem["ActivityList"]}) - lThread.start() - lIterationLastDateTime = datetime.datetime.now() # Set the new datetime for the new processor activity - except Exception as e: - if lL: lL.exception(f"Scheduler: Exception has been catched in Scheduler module when activity time item was initialising. ActivityTimeItem is {lItem}") - lDaemonLastDateTime = lIterationLastDateTime # Set the new datetime for the new processor activity - #Уснуть до следующего прогона - time.sleep(lDaemonLoopSeconds) - except Exception as e: - if lL: lL.exception(f"Scheduler: Exception has been catched in Scheduler module. Global error") + return ctypes.windll.shell32.IsUserAnAdmin() + except: + return False + if not is_admin(): + # Re-run the program with admin rights + ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1) + else: + # Code of your program here + #mGlobalDict = Settings.Settings(sys.argv[1]) + gSettingsDict = inGSettings # Alias for old name in alg + inGSettings["VersionStr"] = __version__ + #Logger alias + lL = gSettingsDict["Logger"] + if lL: lL.info("Link the gSettings in submodules") #Logging + Processor.gSettingsDict = gSettingsDict + Timer.gSettingsDict = gSettingsDict + Timer.Processor.gSettingsDict = gSettingsDict + Server.gSettingsDict = gSettingsDict + Server.ProcessorOld.gSettingsDict = gSettingsDict # Backward compatibility + + # Check _SessionLast_RDPList.json in working directory. if exist - load into gsettings + # GSettings + #"RobotRDPActive": { + # "RDPList": { + if os.path.exists("_SessionLast_RDPList.json"): + lFile = open("_SessionLast_RDPList.json", "r", encoding="utf-8") + lSessionLastRDPList = json.loads(lFile.read()) + lFile.close() # Close the file + os.remove("_SessionLast_RDPList.json") # remove the temp file + gSettingsDict["RobotRDPActive"]["RDPList"]=lSessionLastRDPList # Set the last session dict + if lL: lL.warning(f"RDP Session List was restored from previous Orchestrator session") + + # Init SettingsUpdate defs from file list (after RDP restore) + lSettingsUpdateFilePathList = gSettingsDict.get("OrchestratorStart", {}).get("DefSettingsUpdatePathList",[]) + lSubmoduleFunctionName = "SettingsUpdate" + lSettingsPath = "\\".join(os.path.join(os.getcwd(), __file__).split("\\")[:-1]) + for lModuleFilePathItem in lSettingsUpdateFilePathList: # Import defs with try catch + try: # Try to init - go next if error and log in logger + lModuleName = lModuleFilePathItem[0:-3] + lFileFullPath = os.path.join(lSettingsPath, lModuleFilePathItem) + lTechSpecification = importlib.util.spec_from_file_location(lModuleName, lFileFullPath) + lTechModuleFromSpec = importlib.util.module_from_spec(lTechSpecification) + lTechSpecificationModuleLoader = lTechSpecification.loader.exec_module(lTechModuleFromSpec) + if lSubmoduleFunctionName in dir(lTechModuleFromSpec): + # Run SettingUpdate function in submodule + getattr(lTechModuleFromSpec, lSubmoduleFunctionName)(gSettingsDict) + except Exception as e: + if lL: lL.exception(f"Error when init .py file in orchestrator '{lModuleFilePathItem}'. Exception is below:") + + # Turn on backward compatibility + BackwardCompatibility.Update(inGSettings= gSettingsDict) + + # Init the log dump to WEB + #import pdb; pdb.set_trace() + ############################################ + if len(lL.handlers)== 0: + mRobotLoggerFormatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') + else: + mRobotLoggerFormatter = lL.handlers[0].formatter + mHandlerDumpLogList = LoggerHandlerDumpLogList.LoggerHandlerDumpLogList(inDict=inGSettings["Client"], + inKeyStr="DumpLogList", + inHashKeyStr="DumpLogListHashStr", + inRowCountInt=inGSettings["Client"]["DumpLogListCountInt"]) + mHandlerDumpLogList.setFormatter(mRobotLoggerFormatter) + lL.addHandler(mHandlerDumpLogList) + + # Append Orchestrator def to ProcessorDictAlias + lModule = sys.modules[__name__] + lModuleDefList = dir(lModule) + for lItemDefNameStr in lModuleDefList: + # Dont append alias for defs Orchestrator and ___deprecated_orchestrator_start__ + if lItemDefNameStr not in ["Orchestrator", "___deprecated_orchestrator_start__"]: + lItemDef = getattr(lModule,lItemDefNameStr) + if callable(lItemDef): inGSettings["ProcessorDict"]["AliasDefDict"][lItemDefNameStr]=lItemDef + + #Инициализация настроечных параметров + lDaemonLoopSeconds=gSettingsDict["SchedulerDict"]["CheckIntervalSecFloat"] + lDaemonActivityLogDict={} #Словарь отработанных активностей, ключ - кортеж (, , , ) + lDaemonLastDateTime=datetime.datetime.now() + gSettingsDict["ServerDict"]["WorkingDirectoryPathStr"] = os.getcwd() # Set working directory in g settings + + #Инициализация сервера + lThreadServer = Server.RobotDaemonServer("ServerThread", gSettingsDict) + lThreadServer.start() + if lL: lL.info("Web server has been started") #Logging + + # Init the RobotScreenActive in another thread + lRobotScreenActiveThread = threading.Thread(target= Monitor.CheckScreen) + lRobotScreenActiveThread.daemon = True # Run the thread in daemon mode. + lRobotScreenActiveThread.start() # Start the thread execution. + if lL: lL.info("Robot Screen active has been started") #Logging + + # Init the RobotRDPActive in another thread + lRobotRDPActiveThread = threading.Thread(target= RobotRDPActive.RobotRDPActive, kwargs={"inGSettings":gSettingsDict}) + lRobotRDPActiveThread.daemon = True # Run the thread in daemon mode. + lRobotRDPActiveThread.start() # Start the thread execution. + if lL: lL.info("Robot RDP active has been started") #Logging + + # Init autocleaner in another thread + lAutocleanerThread = threading.Thread(target= GSettingsAutocleaner, kwargs={"inGSettings":gSettingsDict}) + lAutocleanerThread.daemon = True # Run the thread in daemon mode. + lAutocleanerThread.start() # Start the thread execution. + if lL: lL.info("Autocleaner thread has been started") #Logging + + # Orchestrator start activity + if lL: lL.info("Orchestrator start activity run") #Logging + for lActivityItem in gSettingsDict["OrchestratorStart"]["ActivityList"]: + Processor.ActivityListOrDict(lActivityItem) + + # Processor thread + lProcessorThread = threading.Thread(target= Processor.ProcessorRunSync, kwargs={"inGSettings":gSettingsDict}) + lProcessorThread.daemon = True # Run the thread in daemon mode. + lProcessorThread.start() # Start the thread execution. + if lL: lL.info("Processor has been started (ProcessorDict)") #Logging + + + if lL: lL.info("Scheduler loop start") #Logging + gDaemonActivityLogDictRefreshSecInt = 10 # The second period for clear lDaemonActivityLogDict from old items + gDaemonActivityLogDictLastTime = time.time() # The second perioad for clean lDaemonActivityLogDict from old items + while True: + try: + lCurrentDateTime = datetime.datetime.now() + #Циклический обход правил + lFlagSearchActivityType=True + # Periodically clear the lDaemonActivityLogDict + if time.time()-gDaemonActivityLogDictLastTime>=gDaemonActivityLogDictRefreshSecInt: + gDaemonActivityLogDictLastTime = time.time() # Update the time + for lIndex, lItem in enumerate(lDaemonActivityLogDict): + if lItem["ActivityEndDateTime"] and lCurrentDateTime<=lItem["ActivityEndDateTime"]: + pass + # Activity is actual - do not delete now + else: + # remove the activity - not actual + lDaemonActivityLogDict.pop(lIndex,None) + lIterationLastDateTime = lDaemonLastDateTime # Get current datetime before iterator (need for iterate all activities in loop) + # Iterate throught the activity list + for lIndex, lItem in enumerate(gSettingsDict["SchedulerDict"]["ActivityTimeList"]): + try: + # Prepare GUID of the activity + lGUID = None + if "GUID" in lItem and lItem["GUID"]: + lGUID = lItem["GUID"] + else: + lGUID = str(uuid.uuid4()) + lItem["GUID"]=lGUID + + #Проверка дней недели, в рамках которых можно запускать активность + lItemWeekdayList=lItem.get("WeekdayList", [0, 1, 2, 3, 4, 5, 6]) + if lCurrentDateTime.weekday() in lItemWeekdayList: + if lFlagSearchActivityType: + ####################################################################### + #Branch 1 - if has TimeHH:MM + ####################################################################### + if "TimeHH:MM" in lItem: + #Вид активности - запуск процесса + #Сформировать временной штамп, относительно которого надо будет проверять время + #часовой пояс пока не учитываем + lActivityDateTime=datetime.datetime.strptime(lItem["TimeHH:MM"],"%H:%M") + lActivityDateTime=lActivityDateTime.replace(year=lCurrentDateTime.year,month=lCurrentDateTime.month,day=lCurrentDateTime.day) + #Убедиться в том, что время наступило + if ( + lActivityDateTime>=lDaemonLastDateTime and + lCurrentDateTime>=lActivityDateTime): + # Log info about activity + if lL: lL.info(f"Scheduler:: Activity list is started in new thread. Scheduler item: {lItem}") #Logging + # Do the activity + lThread = threading.Thread(target=Processor.ActivityListExecute, kwargs={"inGSettings": inGSettings, "inActivityList":lItem["ActivityList"]}) + lThread.start() + lIterationLastDateTime = datetime.datetime.now() # Set the new datetime for the new processor activity + except Exception as e: + if lL: lL.exception(f"Scheduler: Exception has been catched in Scheduler module when activity time item was initialising. ActivityTimeItem is {lItem}") + lDaemonLastDateTime = lIterationLastDateTime # Set the new datetime for the new processor activity + #Уснуть до следующего прогона + time.sleep(lDaemonLoopSeconds) + except Exception as e: + if lL: lL.exception(f"Scheduler: Exception has been catched in Scheduler module. Global error") # Backward compatibility below to 1.2.0 def __deprecated_orchestrator_start__():