def receive_stream(command): """Receive a stream of data""" command.respond(message="ready", follow_up=False) log.debug("Receving data stream ....") if command['kind'] == 'console': console_dir = os.path.join(os.path.expanduser("~"), ".cstar_perf", "console_out") try: os.makedirs(console_dir) except OSError: pass console = open(os.path.join(console_dir, command['test_id']), "w") tmp = cStringIO.StringIO() sha = hashlib.sha256() try: def frame_callback(frame, binary): if not binary: frame = frame.encode("utf-8") if command['kind'] == 'console': console.write(frame) console_publish(context['cluster'], { 'job_id': command['test_id'], 'msg': frame }) console.flush() else: console_publish(context['cluster'], { 'job_id': command['test_id'], 'ctl': 'IN_PROGRESS' }) sha.update(frame) tmp.write(frame) socket_comms.receive_stream(ws, command, frame_callback) if command['kind'] == 'console': console.close() # TODO: confirm with the client that the sha is correct # before storing finally: # In the event of a socket error, we always want to commit # what we have of the artifact to the database. Better to # have something than nothing. It's the client's # responsibility to resend artifacts that failed. db.update_test_artifact(command['test_id'], command['kind'], tmp, command['name']) command.respond(message='stream_received', done=True, sha256=sha.hexdigest())
def receive_artifact_chunk_object(command): command.respond(message="ready", follow_up=False, done=False) tmp = cStringIO.StringIO() chunk_sha = hashlib.sha256() def frame_callback(frame, binary): if not binary: frame = frame.encode("utf-8") chunk_sha.update(frame) tmp.write(frame) socket_comms.receive_stream(ws, command, frame_callback) # save chunk to db db.insert_artifact_chunk(command['object_id'], command['chunk_id'], command['chunk_size'], chunk_sha.hexdigest(), tmp, command['num_of_chunks'], command['file_size'], command['object_sha']) # respond with current sha command.respond(message='chunk_received', done=True, chunk_id=command['chunk_id'], chunk_sha=chunk_sha.hexdigest())
def receive_stream(command): """Receive a stream of data""" command.respond(message="ready", follow_up=False) log.debug("Receving data stream ....") if command['kind'] == 'console': console_dir = os.path.join(os.path.expanduser("~"), ".cstar_perf", "console_out") try: os.makedirs(console_dir) except OSError: pass console = open(os.path.join(console_dir, command['test_id']), "w") tmp = cStringIO.StringIO() sha = hashlib.sha256() try: def frame_callback(frame, binary): if not binary: frame = frame.encode("utf-8") if command['kind'] == 'console': console.write(frame) console_publish(context['cluster'], {'job_id':command['test_id'], 'msg':frame}) console.flush() else: console_publish(context['cluster'], {'job_id':command['test_id'], 'ctl':'IN_PROGRESS'}) sha.update(frame) tmp.write(frame) socket_comms.receive_stream(ws, command, frame_callback) if command['kind'] == 'console': console.close() # TODO: confirm with the client that the sha is correct # before storing finally: # In the event of a socket error, we always want to commit # what we have of the artifact to the database. Better to # have something than nothing. It's the client's # responsibility to resend artifacts that failed. db.update_test_artifact(command['test_id'], command['kind'], tmp, command['name']) command.respond(message='stream_received', done=True, sha256=sha.hexdigest())