def wait_gxpd_to_finish(self, O, code): g = self.wait_gxpd_to_bring_up(O, code) if g is not None: # say # "Brought up on GUPID ACCESS_PORT TARGET_LABEL HOSTNAME\n" # e.g., # "Brought up on hongo100-tau-2008-07-06-14-40-00-3878 None hongo hongo100\n" # ioman.LOG("Brought up on %s %s\n" % (g, O.seq)) self.Wm("Brought up on %s %s\n" % (g, O.seq)) else: if dbg >= 2: ioman.LOG( "gxpd did not bring up, killing the process with SIGINT\n") self.kill_x(signal.SIGINT) try: time.sleep(2.0) except KeyboardInterrupt: pass if dbg >= 2: ioman.LOG( "gxpd did not bring up, killing the process with SIGKILL\n" ) self.kill_x(signal.SIGKILL) # self.Wm("WAIT suruyo---------n\n") self.wait(1) self.flush_outs()
def remote_installed(self): """ true if this process is running the automatically installed gxpd """ gxp_dir = os.environ["GXP_DIR"] flag = os.path.join(gxp_dir, "REMOTE_INSTALLED") if dbg >= 2: ioman.LOG("checking remote flag %s/%s\n" % (gxp_dir, flag)) if os.path.exists(flag): if dbg >= 2: ioman.LOG("exists, remotely installed\n") return 1 else: if dbg >= 2: ioman.LOG("does not exit, locally installed\n") return 0
def spawn_gxpd(self, O): """ run ssh, sh, qsub_wrap or whatever to eventually spawn off new gxpd.py (locally or remotely). """ code = "INSTALL%09d" % random.randint(0, 999999999) if dbg >= 2: ioman.LOG("code = %s\n" % code) stub_sz, prog = self.mk_program(O, code) python_cmd = self.mk_python_cmdline(O.python, stub_sz) sub = self.subst_cmd(python_cmd, O.rsh) if dbg >= 2: ioman.LOG("cmd to exec:\n%s\n" % sub) self.spawn(sub) self.send(prog) return code
def expect_hello(self, hello, timeout, forward_err): OK = ioman.ch_event.OK begin_mark = "BEGIN_%s " % hello end_mark = " END_%s" % hello if dbg >= 2: # self.Em ioman.LOG("expect %s %s\n" % (begin_mark, timeout)) s = self.expect([begin_mark, ("TIMEOUT", timeout)], forward_err) if s != OK: return (s, None) if dbg >= 2: # self.Em ioman.LOG("expect %s %s\n" % (end_mark, 2.0)) s = self.expect([end_mark, ("TIMEOUT", 2.0)], forward_err) if s != OK: return (s, None) return (OK, self.ev.data[:-len(end_mark)])
def mk_program(self, O, code): """ Return a string of python program which, when invoked without argument, installs all inst_files under a randomly created directory under target_prefix. For example, say target_prefix='target', src_files=[ 'abc', 'def' ], it will create target/RANDOM_DIR/abc and target/RANDOM_DIR/def. When successful, the program will write <code> into standard out. The actual logic is taken from inst_remote_file. """ # append main function to inst_remote_file (normally inst_remote.py) if self.remote_installed(): inst_data, inst_data_LOG = None, None # perhaps we need no install else: inst_data, inst_data_LOG = self.mk_installed_data(O.src_file) # if dbg>=2: ioman.LOG("inst_data:\n%r\n" % inst_data) first_script = self.expand(O.first_script, None) first_args = self.expands(O.first_args_template, O.__dict__) second_script = self.expand(O.second_script, None) second_args = self.expands(O.second_args_template, O.__dict__) gxp_top = os.environ["GXP_TOP"] if dbg >= 2: main_LOG = (""" check_install_exec(python=%r, first_script=%r, first_args=%r, second_script=%r, second_args=%r, target_prefix=%r, gxp_top=%r, inst_data=%r, code=%r) """ % (O.python, first_script, first_args, second_script, second_args, O.target_prefix, gxp_top, inst_data_LOG, code)) main = ("check_install_exec(%r, %r, %r, %r, %r, %r, %r, %r, %r)" % (O.python, first_script, first_args, second_script, second_args, O.target_prefix, gxp_top, inst_data, code)) inst_remote_stub = self.read_file( self.expand(O.inst_remote_stub_file, None)) inst_remote = self.read_file(self.expand(O.inst_remote_file, None)) inst_remote_and_main = ("%s\n%s\n" % (inst_remote, main)) if dbg >= 2: inst_remote_and_main_LOG = ("%s\n%s\n" % (inst_remote, main_LOG)) prog = ("%s%10d%s" % (inst_remote_stub, len(inst_remote_and_main), inst_remote_and_main)) if dbg >= 2: prog_LOG = ("%s%10d%s" % (inst_remote_stub, len(inst_remote_and_main), inst_remote_and_main_LOG)) ioman.LOG(("string to feed cmd:\n-----\n%s\n-----\n" % prog_LOG)) # wp = open("progprog", "wb") # wp.write(prog) # wp.close() return len(inst_remote_stub), prog
def wait_gxpd_to_bring_up(self, O, code): """ wait for gxpd to send a msg saying it has brought up return None if it appears to have failed. """ OK = ioman.ch_event.OK if dbg >= 2: ioman.LOG("Wait for gxpd to send code [%s]\n" % code) if self.expect([code, ("TIMEOUT", O.hello_timeout)], 1) != OK: if dbg >= 1: ioman.LOG("Install NG\n") self.Em("Install NG\n") return None if dbg >= 2: ioman.LOG("Got code [%s]\n" % self.ev.data) ioman.LOG("Wait for gxpd to send OK or WD\n") if self.expect([" OK\n", " WD\n", ("TIMEOUT", O.hello_timeout)], 1) != OK: if dbg >= 1: self.Em("Install NG\n") ioman.LOG("Install NG\n") return None if self.ev.data[-4:] == " WD\n": if dbg >= 2: ioman.LOG("Got WD, send install data\n") inst_data, inst_data_LOG = self.mk_installed_data(O.src_file) inst_data_str = "%r" % inst_data inst_data_msg = "%10d%s" % (len(inst_data_str), inst_data_str) if dbg >= 2: ioman.LOG(("inst_data_msg:-----\n%s-----\n" % inst_data_msg)) self.send(inst_data_msg) elif self.ev.data[-4:] == " OK\n": if dbg >= 2: ioman.LOG("Got OK, no need to send install data\n") if dbg >= 2: ioman.LOG("Wait for gxpd to send hello\n") s, g = self.expect_hello(O.hello, O.hello_timeout, 1) if s == OK: return g if dbg >= 1: ioman.LOG("Bring up NG\n") self.Em("Bring up NG\n") return None
def set_inst_environment(self): if dbg >= 2: ioman.LOG("setting environment\n") env = os.environ if "GXP_DIR" not in env or "GXP_TOP" not in env: if dbg >= 2: ioman.LOG("GXP_DIR or GXP_TOP not in environment, find them\n") gxp_dir, err = this_file.get_this_dir() if gxp_dir is None: self.Em("%s\n" % err) return -1 prefix, gxp_top = os.path.split(gxp_dir) env["GXP_DIR"] = gxp_dir env["GXP_TOP"] = gxp_top if "GXP_GUPID" not in env: if dbg >= 2: ioman.LOG("GXP_GUPID not in environment, set it to default\n") env["GXP_GUPID"] = "gupid" if dbg >= 2: for v in ["GXP_DIR", "GXP_TOP", "GXP_GUPID"]: ioman.LOG("%s : %s\n" % (v, env[v]))
def expect_(self, expected_dict, forward_dict, return_on_any_event): """ expected_dict : channel -> pattern forwar_dict : channel -> (channel_to_forward,forward_eof) return_on_any_event : 0 or 1 wait for the spawned process to send something via its stdout/stderr, or something to come from this process's stdin. For channels in expected_dict, we wait for a particular pattern to come and quit when it comes. For channels in forward_dict, we simply wait for anything to come and forward everything to the channel_to_forward. forward also EOF when forward_eof flag is 1. """ for ch, ex in expected_dict.items(): # set pattern to expect ch.set_expected(ex) for ch in forward_dict.keys(): # say we accept everything ch.set_expected([("*", )]) event_kind = None while 1: self.ch, self.ev = self.process_an_event() ch, ev = self.ch, self.ev assert ch is not None if expected_dict.has_key(ch): event_kind = ev.kind elif forward_dict.has_key(ch): fch, forward_eof = forward_dict[ch] if len(ev.data) > 0: # foward any data fch.write_stream(ev.data) if forward_eof and ev.kind == ioman.ch_event.EOF: # foward eof if so specified fch.write_eof() elif ev.kind == ioman.ch_event.IO_ERROR: # got error. log it. ioman.LOG("expected error: [%s]\n" % ev.err_msg) if return_on_any_event or event_kind is not None: # quit if we got the expected pattern or # told to quit always break for ch in expected_dict.keys(): ch.set_expected([]) for ch in forward_dict.keys(): ch.set_expected([]) return event_kind