Minor updates:
[monitor.git] / monitor / reboot.py
1 #!/usr/bin/python
2 #
3 # Reboot specified nodes
4 #
5
6 import getpass, getopt
7 import os, sys
8 import xml, xmlrpclib
9 import errno, time, traceback
10 import urllib2
11 import urllib
12 import threading
13 import array, struct
14 import base64
15 from subprocess import PIPE, Popen
16 import pcucontrol.transports.ssh.pxssh as pxssh
17 import pcucontrol.transports.ssh.pexpect as pexpect
18 import socket
19
20 # Use our versions of telnetlib and pyssh
21 sys.path.insert(0, os.path.dirname(sys.argv[0]))
22 import pcucontrol.transports.telnetlib as telnetlib
23 sys.path.insert(0, os.path.dirname(sys.argv[0]) + "/pyssh")    
24 import pcucontrol.transports.pyssh as pyssh
25
26 from monitor import config
27 from monitor.wrapper import plc
28
29 from pcucontrol.util import command
30 from pcucontrol.reboot import pcu_name, model_to_object, reboot_api, convert_oldmodelname_to_newmodelname, reboot_test_new
31
32
33 # Event class ID from pcu events
34 #NODE_POWER_CONTROL = 3
35
36 # Monitor user ID
37 #MONITOR_USER_ID = 11142
38
39 import logging
40 logger = logging.getLogger("monitor")
41 verbose = 1
42 #dryrun = 0;
43
44 def get_pcu_values(pcu_id):
45         from monitor.database.info.model import FindbadPCURecord
46         #print "pcuid: %s" % pcu_id
47         try:
48                 pcurec = FindbadPCURecord.get_latest_by(plc_pcuid=pcu_id)
49                 if pcurec:
50                         values = pcurec.to_dict()
51                 else:
52                         values = None
53         except:
54                 values = None
55
56         return values
57
58 def reboot(nodename):
59         return reboot_policy(nodename, True, False)
60
61 def reboot_str(nodename):
62         global verbose
63         continue_probe = True
64         dryrun=False
65
66         pcu = plc.getpcu(nodename)
67         if not pcu:
68                 logger.debug("no pcu for %s" % nodename)
69                 print "no pcu for %s" % nodename
70                 return "%s has no pcu" % nodename
71
72         values = get_pcu_values(pcu['pcu_id'])
73         if values == None:
74                 logger.debug("No values for pcu probe %s" % nodename)
75                 print "No values for pcu probe %s" % nodename
76                 return "no info for pcu_id %s" % pcu['pcu_id']
77         
78         # Try the PCU first
79         logger.debug("Trying PCU %s %s" % (pcu['hostname'], pcu['model']))
80
81         ret = reboot_test_new(nodename, values, verbose, dryrun)
82         return ret
83         
84 def reboot_policy(nodename, continue_probe, dryrun):
85         global verbose
86
87         pcu = plc.getpcu(nodename)
88         if not pcu:
89                 logger.debug("no pcu for %s" % nodename)
90                 print "no pcu for %s" % nodename
91                 return False # "%s has no pcu" % nodename
92
93         values = get_pcu_values(pcu['pcu_id'])
94         if values == None:
95                 logger.debug("No values for pcu probe %s" % nodename)
96                 print "No values for pcu probe %s" % nodename
97                 return False #"no info for pcu_id %s" % pcu['pcu_id']
98         
99         # Try the PCU first
100         logger.debug("Trying PCU %s %s" % (pcu['hostname'], pcu['model']))
101
102         ret = reboot_test_new(nodename, values, verbose, dryrun)
103
104         if ret != 0:
105                 print ret
106                 return False
107         else:
108                 print "return true"
109                 return True
110
111 if __name__ == "__main__":
112         print "ERROR: Can not execute module as a command! Please use commands/%s.py" % os.path.splitext(__file__)[0]
113