-import reboot
-from reboot import pcu_name
-
-import soltesz
-import plc
-import comon
-import threadpool
-import syncplcdb
-
-def nmap_portstatus(status):
- ps = {}
- l_nmap = status.split()
- ports = l_nmap[4:]
-
- continue_probe = False
- for port in ports:
- results = port.split('/')
- ps[results[0]] = results[1]
- if results[1] == "open":
- continue_probe = True
- return (ps, continue_probe)
-
-def get_pcu(pcuname):
- plc_lock.acquire()
- try:
- print "GetPCU from PLC %s" % pcuname
- l_pcu = plc.GetPCUs({'pcu_id' : pcuname})
- print l_pcu
- if len(l_pcu) > 0:
- l_pcu = l_pcu[0]
- except:
- try:
- print "GetPCU from file %s" % pcuname
- l_pcus = soltesz.dbLoad("pculist")
- for i in l_pcus:
- if i['pcu_id'] == pcuname:
- l_pcu = i
- except:
- import traceback
- traceback.print_exc()
- l_pcu = None
-
- plc_lock.release()
- return l_pcu
-
-def get_nodes(node_ids):
- plc_lock.acquire()
- l_node = []
- try:
- l_node = plc.getNodes(node_ids, ['hostname', 'last_contact', 'node_id', 'ports'])
- except:
- try:
- plc_nodes = soltesz.dbLoad("l_plcnodes")
- for n in plc_nodes:
- if n['node_id'] in node_ids:
- l_node.append(n)
- except:
- import traceback
- traceback.print_exc()
- l_node = None
-
- plc_lock.release()
- if l_node == []:
- l_node = None
- return l_node
-
-
-def get_plc_pcu_values(pcuname):
- """
- Try to contact PLC to get the PCU info.
- If that fails, try a backup copy from the last run.
- If that fails, return None
- """
- values = {}
-
- l_pcu = get_pcu(pcuname)
-
- if l_pcu is not None:
- site_id = l_pcu['site_id']
- node_ids = l_pcu['node_ids']
- l_node = get_nodes(node_ids)
-
- if l_node is not None:
- for node in l_node:
- values[node['hostname']] = node['ports'][0]
-
- values['nodenames'] = [node['hostname'] for node in l_node]
-
- # NOTE: this is for a dry run later. It doesn't matter which node.
- values['node_id'] = l_node[0]['node_id']
-
- values.update(l_pcu)
- else:
- values = None
-
- return values
-
-def get_plc_site_values(site_id):
- ### GET PLC SITE ######################
- plc_lock.acquire()
- values = {}
- d_site = None
-
- try:
- d_site = plc.getSites({'site_id': site_id}, ['max_slices', 'slice_ids', 'node_ids', 'login_base'])
- if len(d_site) > 0:
- d_site = d_site[0]
- except:
- try:
- plc_sites = soltesz.dbLoad("l_plcsites")
- for site in plc_sites:
- if site['site_id'] == site_id:
- d_site = site
- break
- except:
- import traceback
- traceback.print_exc()
- values = None
-
- plc_lock.release()
-
- if d_site is not None:
- max_slices = d_site['max_slices']
- num_slices = len(d_site['slice_ids'])
- num_nodes = len(d_site['node_ids'])
- loginbase = d_site['login_base']
- values['plcsite'] = {'num_nodes' : num_nodes,
- 'max_slices' : max_slices,
- 'num_slices' : num_slices,
- 'login_base' : loginbase,
- 'status' : 'SUCCESS'}
- else:
- values = None
-
-
- return values
-
-
-def collectPingAndSSH(pcuname, cohash):
-
- continue_probe = True
- errors = None
- values = {}
- ### GET PCU ######################
- try:
- b_except = False
- try:
- v = get_plc_pcu_values(pcuname)
- if v is not None:
- values.update(v)
- else:
- continue_probe = False
- except:
- b_except = True
- import traceback
- traceback.print_exc()
- continue_probe = False
-
- if b_except or not continue_probe: return (None, None, None)
-
- if values['hostname'] is not None:
- values['hostname'] = values['hostname'].strip()
-
- if values['ip'] is not None:
- values['ip'] = values['ip'].strip()
-
- #### COMPLETE ENTRY #######################
-
- values['complete_entry'] = []
- #if values['protocol'] is None or values['protocol'] is "":
- # values['complete_entry'] += ["protocol"]
- if values['model'] is None or values['model'] is "":
- values['complete_entry'] += ["model"]
- # Cannot continue due to this condition
- continue_probe = False
-
- if values['password'] is None or values['password'] is "":
- values['complete_entry'] += ["password"]
- # Cannot continue due to this condition
- continue_probe = False
-
- if len(values['complete_entry']) > 0:
- continue_probe = False
-
- if values['hostname'] is None or values['hostname'] is "":
- values['complete_entry'] += ["hostname"]
- if values['ip'] is None or values['ip'] is "":
- values['complete_entry'] += ["ip"]
-
- # If there are no nodes associated with this PCU, then we cannot continue.
- if len(values['node_ids']) == 0:
- continue_probe = False
- values['complete_entry'] += ['NoNodeIds']
-
- #### DNS and IP MATCH #######################
- if values['hostname'] is not None and values['hostname'] is not "" and \
- values['ip'] is not None and values['ip'] is not "":
- #print "Calling socket.gethostbyname(%s)" % values['hostname']
- try:
- ipaddr = socket.gethostbyname(values['hostname'])
- if ipaddr == values['ip']:
- values['dnsmatch'] = "DNS-OK"
- else:
- values['dnsmatch'] = "DNS-MISMATCH"
- continue_probe = False
-
- except Exception, err:
- values['dnsmatch'] = "DNS-NOENTRY"
- values['hostname'] = values['ip']
- #print err
- else:
- if values['ip'] is not None and values['ip'] is not "":
- values['dnsmatch'] = "NOHOSTNAME"
- values['hostname'] = values['ip']
- else:
- values['dnsmatch'] = "NO-DNS-OR-IP"
- values['hostname'] = "No_entry_in_DB"
- continue_probe = False
-
- #### RUN NMAP ###############################
- if continue_probe:
- nmap = soltesz.CMD()
- (oval,eval) = nmap.run_noexcept("nmap -oG - -P0 -p22,23,80,443,5869,16992 %s | grep Host:" % pcu_name(values))
- # NOTE: an empty / error value for oval, will still work.
- (values['portstatus'], continue_probe) = nmap_portstatus(oval)
- else:
- values['portstatus'] = None
-
-
- ###### DRY RUN ############################
- if 'node_ids' in values and len(values['node_ids']) > 0:
- rb_ret = reboot.reboot_test(values['nodenames'][0], values, continue_probe, 1, True)
- else:
- rb_ret = "Not_Run" # No nodes to test"
-
- values['reboot'] = rb_ret
-
- ### GET PLC SITE ######################
- v = get_plc_site_values(values['site_id'])
- if v is not None:
- values.update(v)
- else:
- values['plcsite'] = {'status' : "GS_FAILED"}
-
- except:
- print "____________________________________"
- print values
- errors = values
- print "____________________________________"
- import traceback
- errors['traceback'] = traceback.format_exc()
- print errors['traceback']
-
- values['checked'] = time.time()
- return (pcuname, values, errors)
-
-def recordPingAndSSH(request, result):
- global errorState
- global externalState
- global count
- (nodename, values, errors) = result
-
- if values is not None:
- global_round = externalState['round']
- pcu_id = "id_%s" % nodename
- externalState['nodes'][pcu_id]['values'] = values
- externalState['nodes'][pcu_id]['round'] = global_round
-
- count += 1
- print "%d %s %s" % (count, nodename, externalState['nodes'][pcu_id]['values'])
- soltesz.dbDump(config.dbname, externalState)
-
- if errors is not None:
- pcu_id = "id_%s" % nodename
- errorState[pcu_id] = errors
- soltesz.dbDump("findbadpcu_errors", errorState)
-