from re import sub import sys import os import subprocess import time DEVICE_PASSWORD = b'tkadbrdhmc1!\n' SOURCE_DIR = '~/sources/' STORAGE_DIR = '/hdd/' LOG_REBOOT = '~/sources/cfg/log_reboot.txt' REC_COUNT = '/rec_count.txt' LOG_EXTRACT = '/log_extract.txt' CONTAINER_EXT_1 = ['docker', 'run', '--name', 'ext_01', '--gpus', ''"device=0"'', '--rm'] CONTAINER_EXT_2 = ['docker', 'run', '--name', 'ext_02', '--gpus', ''"device=1"'', '--rm'] CONTAINER_IMAGE_1 = ['ellishuntingmoon/mailsys:0.5','python3',os.path.join(os.path.abspath(os.path.expanduser(SOURCE_DIR)),'mailextract_01.py')] CONTAINER_IMAGE_2 = ['ellishuntingmoon/mailsys:0.5','python3',os.path.join(os.path.abspath(os.path.expanduser(SOURCE_DIR)),'mailextract_02.py')] def container_status(): clist = [] docker_process = subprocess.Popen(('docker','ps','-a'), stdout=subprocess.PIPE) try: clist = subprocess.check_output(('grep','ext_'), stdin=docker_process.stdout).decode() clist = clist.split() clist= [i for i in clist if 'ext_' in i] return clist except subprocess.CalledProcessError as e: return clist def request_extract(): nowtime = time.localtime() nowdate = "%04d/%02d/%02d" % (nowtime.tm_year, nowtime.tm_mon, nowtime.tm_mday) hdd_list = os.listdir(os.path.abspath(STORAGE_DIR)) reboot_list = [] container_list = container_status() #print(container_list) with open (os.path.abspath(os.path.expanduser(LOG_REBOOT)), "r") as log_reboot: reboot_list = log_reboot.readlines() #NOTE: nowtime.tm_wday == 6 is sunday if (nowtime.tm_wday == 6) and (nowdate not in reboot_list[-1]) and (len(hdd_list) == 1): with open (os.path.abspath(os.path.expanduser(LOG_REBOOT)), "a+") as log_reboot: log_reboot.write("\n[REBOOT] Device reboot executed at %04d/%02d/%02d %02d:%02d:%02d" % (nowtime.tm_year, nowtime.tm_mon, nowtime.tm_mday, nowtime.tm_hour, nowtime.tm_min, nowtime.tm_sec)) #FIXME: MUST ERASE COMMENT WHEN YOU COMPLETE #subprocess.call(('echo','rebooting...')) #subprocess.Popen(('sudo','-S','shutdown','-r','now'), stdin=subprocess.PIPE, stderr=subprocess.PIPE).communicate(input=PASSWORD) elif len(container_list) == 0 : hdd_list = [] hdd_list = os.listdir(os.path.abspath(STORAGE_DIR)) hdd_list.remove('lost+found') #roundrobin surgeons for surgeon in hdd_list: if len(container_list) != 0: break svocount = 0 surgeondatalist = os.listdir(os.path.abspath(STORAGE_DIR+surgeon)) #rec_count 내용 확인 with open (os.path.abspath(STORAGE_DIR+surgeon+REC_COUNT), "r") as log_reboot: svocount = int(log_reboot.readline()) svofulllist = [file for file in surgeondatalist if file.endswith('.svo')] #svo 개수가 rec_count -2일때 and 파일 개수가 rec_count와 동일 하면 분석 시작 if (len(svofulllist) == (svocount-2)) and len(surgeondatalist) == svocount: exttime = time.localtime() with open (os.path.abspath(STORAGE_DIR+surgeon+LOG_EXTRACT), "w+") as log_extract: log_extract.write("\n[EXTRACT] EXTRACT %d svo files executed at %04d/%02d/%02d %02d:%02d:%02d" % (len(svofulllist),exttime.tm_year, exttime.tm_mon, exttime.tm_mday, exttime.tm_hour, exttime.tm_min, exttime.tm_sec)) containermiddle = [\ '-v', os.path.abspath(STORAGE_DIR+surgeon)+':'+ os.path.abspath(STORAGE_DIR+surgeon), \ '-v', os.path.abspath(os.path.expanduser(SOURCE_DIR))+':'+os.path.abspath(os.path.expanduser(SOURCE_DIR)), \ '-w', os.path.abspath(STORAGE_DIR+surgeon) ] #docker run --name ext_01 --gpus '"device=0"' --rm -v /hdd/{surgeon}:/hdd/{surgeon} -v /home/mc365/sources/:/home/mc365/sources/ -w /hdd/{surgeon} ellishuntingmoon/mailsys:0.5 python3 /home/mc365/sources/mailextract_01.py #print(CONTAINER_EXT_1 + containermiddle + CONTAINER_IMAGE_1) subprocess.Popen(CONTAINER_EXT_1 + containermiddle + CONTAINER_IMAGE_1) subprocess.Popen(CONTAINER_EXT_2 + containermiddle + CONTAINER_IMAGE_2) #svo1list = [file for file in svofulllist if file.endswith('1.svo')] #svo2list = [file for file in svofulllist if file.endswith('2.svo')] #print('ext start') time.sleep(15) container_list = container_status() else: #print('already finished') pass #print(svo1list, svo2list) else: #print('extract is in progress... wait until it finish...') pass def main(): while True: request_extract() time.sleep(15) if __name__== '__main__': main()