#!/usr/bin/env python

log = open('/home/panstarrs/ippm/operations/who_is_using_the_cluster/update_all_nodes.log', 'w')

import sys
sys.path.append('/home/panstarrs/ippm/common/lib/python2.7/site-packages')

import threading
import socket
import subprocess

nodes = [  "ipp004", "ipp005", "ipp006", "ipp007", "ipp008", "ipp009", 
           "ipp010", "ipp011", "ipp012", "ipp013", "ipp014", "ipp015", "ipp016", "ipp017", "ipp018", "ipp019", 
           "ipp020", "ipp021", "ipp023", "ipp024", "ipp025", "ipp026", "ipp027", "ipp028", "ipp029", 
           "ipp030", "ipp031", "ipp032", "ipp033", "ipp034", "ipp035", "ipp036", "ipp037", "ipp038", "ipp039", 
           "ipp040", "ipp041", "ipp042", "ipp043", "ipp044", "ipp045", "ipp046", "ipp047", "ipp048", "ipp049", 
           "ipp050", "ipp051", "ipp052", "ipp053", "ipp054", "ipp055", "ipp056", "ipp057", "ipp058", "ipp059", 
           "ipp060", "ipp061", "ipp062", "ipp063", "ipp064", "ipp065", "ipp066", 
           "ippc01", "ippc02", "ippc03", "ippc04", "ippc05", "ippc06", "ippc07", "ippc08", "ippc09", 
           "ippc10", "ippc11", "ippc12", "ippc13", "ippc14", "ippc15", "ippc16", "ippc17", "ippc18", "ippc19", 
           "ippc20", "ippc21", "ippc22", "ippc23", "ippc24", "ippc25", "ippc26", "ippc27", "ippc28", "ippc29", 
           "ippc30", "ippc31", "ippc32", "ippc33", "ippc34", "ippc35", "ippc36", "ippc37", "ippc38", "ippc39", 
           "ippc40", "ippc41", "ippc42", "ippc43", "ippc44", "ippc45", "ippc46", "ippc47", "ippc48", "ippc49", 
           "ippc50", "ippc51", "ippc52", "ippc53", "ippc54", "ippc55", "ippc56", "ippc57", "ippc58", "ippc59", 
           "ippc60", "ippc61", "ippc62", "ippc63", 
           "stare00", "stare01", "stare02", "stare03", "stare04", 
           "ippdb00", "ippdb01", "ippdb02", "ippdb03", 
           "stsci00", "stsci01", "stsci02", "stsci03", "stsci04", 
           "stsci05", "stsci06", "stsci07", "stsci08", "stsci09",
           ]

class SshThread(threading.Thread):
    def __init__(self, node):
        threading.Thread.__init__(self)
        self.node = node
    def run(self):
        subprocess.Popen( [ 'ssh', 
                            self.node, 
                            '/home/panstarrs/ippm/operations/who_is_using_the_cluster/get_processes_information.py'] )

if __name__=='__main__':
    ssh_threads = []
    for node in nodes:
        try:
            ssh_threads.append(SshThread(node))
        except socket.error, e:
            print 'Exception caught for node %s' % node
            print '  %s' % e
    for t in ssh_threads:
        try:
            t.start()
        except Exception, e:
            print t.node, e
	    ssh_threads.remove(t)
    for t in ssh_threads:
        try:
            t.join()
        except RuntimeError, e:
            print t.node, ": ", e

