~tanawts/+junk/brimstone

« back to all changes in this revision

Viewing changes to brimstone.py

  • Committer: Jr Aquino
  • Date: 2011-04-07 20:08:33 UTC
  • Revision ID: jr.aquino@citrixonline.com-20110407200833-l4p2du6w7zdt3lel
Added correction to number of parallel jobs

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
version = '0.9.9'
34
34
import getpass, os, time, commands, sys, signal, re, optparse, platform
35
35
import urllib2, threading, socket, cStringIO
 
36
import Queue
36
37
from threading import Thread
37
38
from time import sleep
38
39
 
108
109
class hyperSsh(Thread):
109
110
    "Going to HyperActive"
110
111
    
111
 
    def __init__ (self, scp, pw, cmd, host, timeout):
 
112
    def __init__ (self, scp, pw, cmd, q, timeout):
112
113
        Thread.__init__(self)
113
114
        
114
 
        self.host = host
 
115
        self.q = q
115
116
        self.cmd = cmd
116
117
        self.pw = pw
117
118
        self.scp_file = scp
122
123
                   
123
124
    def run(self):
124
125
        "Run ssh command(s) via threading"
125
 
        
126
 
        if self.scp_file == None:
127
 
            self.ssh()
128
 
        else:
129
 
            for filename in (self.scp_file.split(',')):
130
 
                self.scp(filename)
131
 
            if options.command:
132
 
                self.ssh()
 
126
 
 
127
        global done        
 
128
        while not done:
 
129
            try:
 
130
                self.host = self.q.get(0)
 
131
                if self.scp_file == None:
 
132
                    self.ssh()
 
133
                else:
 
134
                    for filename in (self.scp_file.split(',')):
 
135
                        self.scp(filename)
 
136
                    if options.command:
 
137
                        self.ssh()
 
138
            except Queue.Empty:
 
139
                done = True
 
140
                break
133
141
 
134
142
    def exitMsg(self, hostname, command, data, start_time, statusMsg):
135
143
        "Print thread exit message"
177
185
            error = 'Timeout'
178
186
            self.exitMsg(hostname, command, error, start_time, 'Failed: ')
179
187
            return
 
188
        except socket.error:
 
189
            error = 'Timeout'
 
190
            self.exitMsg(hostname, command, error, start_time, 'Failed: ')
 
191
            return
180
192
        except paramiko.SSHException, e:
181
193
            error = 'SSH Failure: %s' % (e)
182
194
            self.exitMsg(hostname, command, error, start_time, 'Failed: ')
264
276
        self.alive = []
265
277
        self.dead = []
266
278
        self.password = ''
267
 
        self.cmdoutput = []
268
279
        self.default_realm = ''
269
280
 
270
281
    def krbCheck(self):
435
446
        "The real workload"
436
447
        
437
448
        status = ('Succeeded: ', 'Failed: ')
438
 
        self.cmdoutput = []
 
449
        self.threads = []
439
450
        logname = 'coal.log'
440
451
        logfile = open(logname, 'w')
441
452
        global done
442
453
        done = False        
443
454
        global alive
444
455
        global headcount
 
456
        global q
445
457
        alive = len(self.alive)
446
458
        headcount = len(self.alive)
 
459
        jobs = options.jobs
 
460
        q = Queue.Queue()
447
461
        
448
462
        print (bcolors.OKBLUE + 'Welcome to Brimstone' + bcolors.ENDC)
 
463
        # Add hosts to the queue
449
464
        for host in self.alive:
450
 
            output = (hyperSsh(options.scp, self.password, options.command, 
451
 
                        host, options.timeout))
452
 
            self.cmdoutput.append(output)
453
 
            output.start()
 
465
            q.put(host)
454
466
        
455
 
        # Loop Needed to catch Interupts, Children don't listen.
456
 
        while not done and len(threading.enumerate()) > 1:
457
 
            sleep(1)
458
 
        return
 
467
        if jobs > 0:    
 
468
            for n in range(jobs):
 
469
                thread = (hyperSsh(options.scp, self.password, options.command, 
 
470
                            q, options.timeout))
 
471
                self.threads.append(thread)
 
472
                thread.setDaemon(True)
 
473
                thread.start()
 
474
            
 
475
            for t in self.threads:
 
476
                t.join()
 
477
        else:
 
478
            print 'You must specify a valid number of jobs to run in paralell'
 
479
            sys.exit(1)
459
480
 
460
481
def myhandler(signal, frame):
461
482
    "Kill Child Threads and Spawn"
466
487
    print escape_message
467
488
    for cmd in threading.enumerate():
468
489
        if cmd.getName() != 'MainThread':
 
490
            print dir(cmd)
469
491
            logfile.write(escape_message)
470
492
            cmd._Thread__stop()
471
493
            sys.exit(1)
503
525
        
504
526
    # If testing, just print out the host list and exit
505
527
    if options.test:
506
 
        for line in brimstone.hostnames:
507
 
            print line
 
528
        for host in brimstone.hostnames:
 
529
            print host
508
530
        sys.exit(1)
509
 
        
 
531
    
 
532
    if not options.timeout:
 
533
        print "Warning: No timeout set.\nYour jobs will be dependant on default TCP timeouts."
510
534
    # Check to see if Hosts are alive
511
535
    brimstone.stickPoke()
512
536
    
532
556
                    default=None, type="string",
533
557
                    help="Command(s) to execute on each target host.")
534
558
 
 
559
    parser.add_option("-j", "--jobs", dest="jobs",
 
560
                    default=2, type="int", 
 
561
                    help="Set number of parallel jobs for ssh.")
 
562
 
535
563
    parser.add_option("-p", "--puppet-foreman", dest="puppet",
536
564
                    default=None, type="string", 
537
565
                    help="Puppet Foreman Server.")
554
582
                    help="Enable hostlist testing.")
555
583
 
556
584
    parser.add_option("-t", "--timeout", dest="timeout",
557
 
                    default=None, type="int", 
 
585
                    default=0, type="int", 
558
586
                    help="Set timeout for ssh.")
559
587
 
560
588
    parser.add_option("-v", "--verbose", dest="verbose",