|
| 1 | +import sys |
| 2 | +import time |
| 3 | +import random |
| 4 | +import _thread |
| 5 | +import subprocess |
| 6 | +import os |
| 7 | +import requests |
| 8 | + |
| 9 | +from utils.controller import VMHostPuppeteer |
| 10 | +from utils.temps import StashTempData |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +workers_dict = { |
| 17 | + |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +class Worker: |
| 23 | + status = 'sleep' |
| 24 | + thread = None |
| 25 | + process = None |
| 26 | + script = None |
| 27 | + last_msg = '' |
| 28 | + def __init__(self, data: dict) -> None: |
| 29 | + unique_id, remote_ip, script = data['unique_id'], data['remote_ip'], data['script'] |
| 30 | + self.unique_id = unique_id |
| 31 | + self.remote_ip = remote_ip |
| 32 | + self.script = data['script'] |
| 33 | + |
| 34 | + def start(self): |
| 35 | + ''' |
| 36 | + main thread |
| 37 | + ''' |
| 38 | + while self.script != 'sleep' and self.status != 'sleep': |
| 39 | + current_dir = os.path.dirname(os.path.realpath(__file__)) |
| 40 | + cmd_command = f"python {current_dir}\\{self.script}.py {self.remote_ip} {self.unique_id}" #TODO params |
| 41 | + print(f"{self.unique_id} executing command {cmd_command}") |
| 42 | + process = subprocess.Popen(cmd_command.split(), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 43 | + while True: |
| 44 | + if self.status == 'sleep': |
| 45 | + print(f'{self.unique_id} self.status changed to "sleep"') |
| 46 | + break |
| 47 | + time.sleep(0.01) |
| 48 | + line = process.stdout.readline().decode('utf-8') |
| 49 | + # (stdout, stderr) = process.communicate() |
| 50 | + if line != '': |
| 51 | + self.last_msg = line |
| 52 | + print(f'{self.unique_id} Line: {line[:-2]}') # :-2 to remove \n or \t or whatever is there |
| 53 | + # continue # TODO need? |
| 54 | + #check if process is still running |
| 55 | + process_is_running = process.poll() is None |
| 56 | + print(f"{self.unique_id} process_is_running {process_is_running}") |
| 57 | + if process_is_running == False: |
| 58 | + print(f"{self.unique_id} rerunning the process") |
| 59 | + break |
| 60 | + return |
| 61 | + |
| 62 | + def stop(self): |
| 63 | + pass |
| 64 | + |
| 65 | + def do(self,action = None): |
| 66 | + if self.script == 'sleep': |
| 67 | + # stop script if possible |
| 68 | + self.status = 'sleep' |
| 69 | + self.thread = None |
| 70 | + return |
| 71 | + elif self.script != 'sleep' and self.thread is None: |
| 72 | + self.status = 'running' |
| 73 | + print(f'{self.unique_id} starting new thread') |
| 74 | + self.thread = _thread.start_new_thread(self.start, ()) |
| 75 | + print(f'{self.unique_id} started new thread') |
| 76 | + return |
| 77 | + |
| 78 | + |
| 79 | +def getListOfThisMachineGuestsMacAddresses(): |
| 80 | + ''' |
| 81 | + returns [mac_address] under this host |
| 82 | + ''' |
| 83 | + mac_adresses = [ |
| 84 | + "00E04CF42DB6", |
| 85 | + "00E04C1BCB8E", |
| 86 | + "00E04C000001", |
| 87 | + "00E04C000002", |
| 88 | + "00E04C000003", |
| 89 | + ] |
| 90 | + |
| 91 | + # TODO cmd_command = 'powershell -command "Get-VM | Get-VMNetworkAdapter | ft MacAddress" ' |
| 92 | + |
| 93 | + |
| 94 | + return mac_adresses |
| 95 | + |
| 96 | + |
| 97 | +def getDataFromCoordinator(): |
| 98 | + ''' |
| 99 | + get currenct status of jobs, related workers |
| 100 | + ''' |
| 101 | + coordinator_response = [ |
| 102 | + { |
| 103 | + "unique_id": "5XBYN26LML123123PJ6V2J", |
| 104 | + "remote_ip": "192.168.0.104", |
| 105 | + "assigned_script" : "dummy_script", |
| 106 | + "status" : "sleep" |
| 107 | + }, |
| 108 | + { |
| 109 | + "unique_id": "1924934893288fdsau", |
| 110 | + "remote_ip": "192.168.0.105", |
| 111 | + "assigned_script" : "dummy_script", |
| 112 | + "status" : "sleep" |
| 113 | + } |
| 114 | + ] |
| 115 | + |
| 116 | + # TODO |
| 117 | + # r = requests.get(coordinator_url) |
| 118 | + # data = r.json() |
| 119 | + # TODO sort jobs, get assigned jobs for this machine, so it may be mac adresses under this machine |
| 120 | + # and also some physical workers assigned for this machine |
| 121 | + |
| 122 | + |
| 123 | + return coordinator_response |
| 124 | + |
| 125 | +MAC_ADDRESSES = getListOfThisMachineGuestsMacAddresses() |
| 126 | + |
| 127 | +while True: |
| 128 | + |
| 129 | + jobs = getDataFromCoordinator() |
| 130 | + for job in jobs: |
| 131 | + # check if the worker was initialized |
| 132 | + try: |
| 133 | + workers_dict[job['unique_id']] |
| 134 | + except: |
| 135 | + workers_dict[job['unique_id']] = Worker(job) |
| 136 | + |
| 137 | + worker:Worker = workers_dict[job['unique_id']] |
| 138 | + #update job |
| 139 | + worker.script = job['script'] |
| 140 | + worker.do(job['script']) |
| 141 | + |
| 142 | + |
| 143 | + time.sleep(5) |
0 commit comments