def sendSocket(host, port, params): try: s = connectServer(host, port) if s == None: return None s.send(params) print(s) sysout.log(TAG, "send params: " + str(params)) resp = s.recv(1024).decode('utf-8') if resp != None: # if type(resp) == type({}): # status = resp['status'] # result = resp['result'] # s.close() # if (status == '200'): # listener.onResponse(result) # else: # listener.onFail(result) sysout.log(TAG, "response: " + str(resp)) s.close() return json.loads(resp) except Exception as e: # listener.onException(s) sysout.err(TAG, e) return e
def resetPortJupyter(cname, index, action='start'): sysout.log(TAG, 'resetPortJupyter...') host = config.vms_host port = config.vms_port p_body = {"action": action, "cname": cname, "index": index} params = ("new_task_ssh@:" + json.dumps(p_body)).encode('utf-8') result = socketUtils.sendSocket(host, port, params) if result != None: sysout.log(TAG, result) if type(result) == type({}): if result['status'] == '200': # start vm success return {'status': 1, 'result': 'reset port success!'} else: # start fail return { 'status': 0, 'result': 'reset port failed! cause: ' + result['result'] } else: return { 'status': 0, 'result': 'reset port failed! cause: Server response form error.' } else: return { 'status': 0, 'result': 'reset port failed! cause: Server have no response.' }
def createDir(dir): sysout.log(TAG, 'dir= ' + dir) exist = os.path.exists(dir) if not exist: os.makedirs(dir) exist = os.path.exists(dir) return exist
def runWithVm(userId, projectId, projectName, version, vmId, passwd, isoName, isoRemarks, gpu, cpu, memory, action='start'): res = None if action != None and action == 'stop': # shutdown vm res = vmManager.startVm(userId, isoName, vmId, passwd, gpu, cpu, memory, isoRemarks, action) else: # run vm res = vmManager.startVm(userId, isoName, vmId, passwd, gpu, cpu, memory, isoRemarks) if res['status'] == 1: # start vm success # { # 'status': 1, # 'result': { # 'jupyeter': 'http://'], # 'message': (result['result'])['message'] # } # } path = res['result'].get( 'jupyter', '') + '/notebooks/system/' + str(projectId) + '/' + str(version) parentPath = config.dir_home + '/' + str(userId) + '/system/' + str( projectId) + '/' + str(version) notebook = path + '/' + fileManager.getOneNbFileName( parentPath, '.ipynb') html = path + '/' + fileManager.getOneNbFileName(parentPath, '.html') py = path + '/' + fileManager.getOneNbFileName(parentPath, '.py') result = { 'status': 1, 'result': { 'projectId': projectId, 'projectName': projectName, 'version': version, 'notebook': notebook, 'html': html, 'py': py } } # sysout.log(TAG, json.loads(result)) return result else: # 'status' == 0 sysout.log(TAG, res) return res
def connectServer(host, port): try: addr = (host, port) sysout.log(TAG, "connect socket server: " + str(addr)) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(8) s.connect(addr) return s except Exception as e: sysout.err(TAG, e) return {'status': 0, 'result': 'connect time out!'}
def createPreProject(userId, projectId, projectName, projectType): pjHome = '' if config.dir_home_user != "": pjHome = config.dir_home + "/" + config.dir_home_user + '/' + str( userId) + '/system' else: pjHome = config.dir_home + '/' + str(userId) + '/system' if not os.path.exists(pjHome): os.makedirs(pjHome) shell.execute('cp ' + config.file_system_readme + ' ' + pjHome + '/') path = config.dir_home_user + '/' + str(userId) + '/system/' + str( projectId) + '/1' # 1--version of pj, vesionInit=1 dir = config.dir_home + path if (fileManager.createDir(dir)): notebook = fileManager.createProject(dir, projectId, projectName, projectType, path) sysout.log(TAG, "first project = " + str(notebook)) return notebook return 'System can not create user dir of -- ' + dir
def execute(cmd): sysout.log(TAG, "executed: ["+ cmd +"]") return os.system(cmd)
def startVm(userId, isoName, vmId, passwd, gpu, cpu, memory, isoRemarks, action='start'): sysout.log(TAG, 'startVm...') host = config.vms_host port = config.vms_port p_body = { "iname": isoName, "cname": vmId, "user": userId, "action": action, "password": passwd, "nname": isoRemarks, "gpu": gpu, "cpu": cpu, "mem": memory } p_body = json.dumps(p_body) params = ("new_task_publish@" + p_body).encode('utf-8') # params = json.dumps().encode('utf-8') result = socketUtils.sendSocket(host, port, params) if result != None: sysout.log(TAG, result) if type(result) == type({}): if result['status'] == 200 or (result['status'] == 211 and action == 'start'): # start vm success # resp example # { # "status": 200, # "result": { # "jupyter": "http://xxx:000/xxx/notebooks/" # "message": "xxx" # } # } return {'status': 1, 'result': result['result']} elif result['status'] == 223: # start fail return { 'status': 0, 'result': "It's too short time for last request, please try again 1 minute later!" } else: # start fail return { 'status': 0, 'result': 'start vm failed! cause: ' + str(result['result']) } else: return { 'status': 0, 'result': 'start vm failed! cause: Server response form error.' } else: return { 'status': 0, 'result': 'start vm failed! cause: Server have no response.' }