5 def runcmd(command, args, username, password, timeout = None):
8 result_ready = threading.Condition()
12 result_ready.acquire()
17 result_ready.release()
19 def do_command(command, username, password):
22 # Popen4 is a popen-type class that combines stdout and stderr
23 p = popen2.Popen4(command)
25 # read all output data
26 p.tochild.write("%s\n" % username)
27 p.tochild.write("%s\n" % password)
29 data = p.fromchild.read()
32 # might get interrupted by a signal in poll() or waitpid()
35 set_result((retval, data))
38 if ex.errno == errno.EINTR:
45 command = " ".join([command] + args)
47 worker = threading.Thread(target = do_command, args = (command, username, password, ))
48 worker.setDaemon(True)
49 result_ready.acquire()
51 result_ready.wait(timeout)
54 raise Exception, "command timed-out: '%s'" % command
56 result_ready.release()
59 if isinstance(result, Exception):
62 (retval, data) = result
63 if os.WIFEXITED(retval) and os.WEXITSTATUS(retval) == 0:
66 out = "system command ('%s') " % command
67 if os.WIFEXITED(retval):
68 out += "failed, rc = %d" % os.WEXITSTATUS(retval)
70 out += "killed by signal %d" % os.WTERMSIG(retval)
72 out += "; output follows:\n" + data
75 def racadm_reboot(host, username, password, dryrun):
77 ip = socket.gethostbyname(host)
79 cmd = "/usr/sbin/racadm"
82 output = runcmd(cmd, ["-r %s -i serveraction powercycle" % ip],
85 output = runcmd(cmd, ["-r %s -i getsysinfo" % ip],
88 print "RUNCMD: %s" % output
91 except Exception, err:
92 logger.debug("runcmd raised exception %s" % err)
96 from optparse import OptionParser
97 parser = OptionParser()
98 parser.set_defaults(ip="", user="", password="")
99 parser.add_option("-r", "", dest="ip", metavar="nodename.edu",
100 help="A single node name to add to the nodegroup")
101 parser.add_option("-u", "", dest="user", metavar="username",
103 parser.add_option("-p", "", dest="password", metavar="password",
105 (options, args) = parser.parse_args()
107 if __name__ == '__main__':
109 if options.ip is not "" and \
110 options.user is not "" and \
111 options.password is not "":
113 racadm_reboot(options.ip, options.user, options.password, False)