9 def runcmd(command, args, username, password, timeout = None):
12 result_ready = threading.Condition()
16 result_ready.acquire()
21 result_ready.release()
23 def do_command(command, username, password):
26 # Popen4 is a popen-type class that combines stdout and stderr
27 p = popen2.Popen4(command)
29 # read all output data
30 p.tochild.write("%s\n" % username)
31 p.tochild.write("%s\n" % password)
33 data = p.fromchild.read()
36 # might get interrupted by a signal in poll() or waitpid()
39 set_result((retval, data))
42 if ex.errno == errno.EINTR:
49 command = " ".join([command] + args)
51 worker = threading.Thread(target = do_command, args = (command, username, password, ))
52 worker.setDaemon(True)
53 result_ready.acquire()
55 result_ready.wait(timeout)
58 raise Exception, "command timed-out: '%s'" % command
60 result_ready.release()
63 if isinstance(result, Exception):
66 (retval, data) = result
67 if os.WIFEXITED(retval) and os.WEXITSTATUS(retval) == 0:
70 out = "system command ('%s') " % command
71 if os.WIFEXITED(retval):
72 out += "failed, rc = %d" % os.WEXITSTATUS(retval)
74 out += "killed by signal %d" % os.WTERMSIG(retval)
76 out += "; output follows:\n" + data
79 def racadm_reboot(host, username, password, dryrun, state="powercycle"):
81 ip = socket.gethostbyname(host)
83 cmd = "/usr/sbin/racadm"
86 output = runcmd(cmd, ["-r %s -i serveraction %s" % (ip, state)],
89 output = runcmd(cmd, ["-r %s -i getsysinfo" % ip],
92 print "RUNCMD: %s" % output
95 except Exception, err:
96 #logger.debug("runcmd raised exception %s" % err)
97 print "runcmd raised exception %s" % err
101 from optparse import OptionParser
102 parser = OptionParser()
103 parser.set_defaults(ip="", user="", password="", state="powercycle")
104 parser.add_option("-r", "", dest="ip", metavar="nodename.edu",
105 help="A single node name to add to the nodegroup")
106 parser.add_option("-u", "", dest="user", metavar="username",
108 parser.add_option("-s", "", dest="state", metavar="powercycle",
110 parser.add_option("-p", "", dest="password", metavar="password",
112 (options, args) = parser.parse_args()
114 if __name__ == '__main__':
116 if options.ip is not "" and \
117 options.user is not "" and \
118 options.password is not "":
120 racadm_reboot(options.ip, options.user, options.password, False, options.state)